[Assignment-7] add rsa_verify
All checks were successful
Latex Build / build-latex (Assignment 4 - Protokollsicherheit (Praxis)) (push) Successful in 1m5s
Latex Build / build-latex (Assignment 5 - Software Security - Teil 1) (push) Successful in 1m3s
Latex Build / build-latex (Assignment 6 - Software Security - Teil 2) (push) Successful in 1m0s
Latex Build / build-latex (Assignment 4 - Protokollsicherheit (Praxis)) (pull_request) Successful in 32s
Latex Build / build-latex (Assignment 5 - Software Security - Teil 1) (pull_request) Successful in 8s
Latex Build / build-latex (Assignment 6 - Software Security - Teil 2) (pull_request) Successful in 9s

This commit is contained in:
Sascha Tommasone 2024-06-29 16:46:07 +02:00
parent 4d6d39df95
commit 5616ddc4e5
Signed by: saschato
GPG key ID: 751068A86FCAA217
2 changed files with 23 additions and 5 deletions

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)
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);
}
}

View file

@ -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