Assignment 7 #4

Merged
saschato merged 75 commits from Assignment-7 into master 2024-07-08 11:19:51 +02:00
2 changed files with 23 additions and 5 deletions
Showing only changes of commit 5616ddc4e5 - Show all commits

View file

@ -75,7 +75,7 @@ static int pkcs1(mpz_t message, const u8 *data, const size_t length) {
// calculate padding size (how many 0xff bytes) // calculate padding size (how many 0xff bytes)
size_t padding_length = MODULUS_SIZE - length - 3; size_t padding_length = MODULUS_SIZE - length - 3;
if ((padding_length < 8) || (message == NULL)) { if ((padding_length < 8) || (message == NULL) || (data == NULL)) {
// message to big // message to big
// or null pointer // or null pointer
return 0; return 0;
@ -124,13 +124,31 @@ int rsa_sign(u8 *sig, const u8 *sha256, const rsa_key *key) {
return 1; return 1;
} }
// TODO
int rsa_verify(const u8 *sig, const u8 *sha256, rsa_public_key *pk) { int rsa_verify(const u8 *sig, const size_t sig_length, u8 *sha256, rsa_public_key *pk) {
// null pointer handling // null pointer handling
if((sig == NULL) || (sha256 == NULL) || (pk == NULL)) if((sig == NULL) || (sha256 == NULL) || (pk == NULL))
return 0; return 0;
// initialize bignums
mpz_t signature, message; mpz_inits(signature, message, NULL);
// import signature
mpz_import(signature, (sig_length < MODULUS_SIZE) ? sig_length : MODULUS_SIZE, 1, 1, 0, 0, sig);
// revert rsa signing process
mpz_powm(signature, signature, pk->e, pk->n);
// rebuild signed message
if(!pkcs1(message, sha256, 32))
return 0;
// compare signature with expected value
if(mpz_cmp(signature, message) != 0)
return 0;
// free bignums and return valid signature
mpz_clears(signature, message, NULL);
return 1; return 1;
} }

View file

@ -32,6 +32,6 @@ int rsa_init(rsa_key *key);
void rsa_free(rsa_key *key); void rsa_free(rsa_key *key);
int rsa_sign(u8 *sig, const u8 *sha256, const rsa_key *key); int rsa_sign(u8 *sig, const u8 *sha256, const rsa_key *key);
int rsa_verify(const u8 *sig, const u8* sha256, rsa_public_key *pk); int rsa_verify(const u8 *sig, const size_t sig_length, u8 *sha256, rsa_public_key *pk);
#endif #endif