Assignment 7 #4
2 changed files with 23 additions and 5 deletions
|
@ -75,7 +75,7 @@ static int pkcs1(mpz_t message, const u8 *data, const size_t length) {
|
|||
// calculate padding size (how many 0xff bytes)
|
||||
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
|
||||
// or null pointer
|
||||
return 0;
|
||||
|
@ -124,13 +124,31 @@ int rsa_sign(u8 *sig, const u8 *sha256, const rsa_key *key) {
|
|||
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
|
||||
if((sig == NULL) || (sha256 == NULL) || (pk == NULL))
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -140,4 +158,4 @@ void rsa_print(rsa_key *key) {
|
|||
gmp_printf("%Zu\n", key->n);
|
||||
gmp_printf("%Zu\n", key->e);
|
||||
gmp_printf("%Zu\n", key->d);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,6 @@ int rsa_init(rsa_key *key);
|
|||
void rsa_free(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
|
Loading…
Reference in a new issue