Path: blob/main/external/libecc/src/examples/sig/rsa/rsa_tests.h
2066 views
/*1* Copyright (C) 2021 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6*7* This software is licensed under a dual BSD and GPL v2 license.8* See LICENSE file at the root folder of the project.9*/10#ifndef __RSA_TESTS_H__11#define __RSA_TESTS_H__1213/* Test suite for RSA PKCS#1 algorithms */14#include "rsa.h"1516typedef enum {17RSA_PKCS1_v1_5_ENC = 0,18RSA_PKCS1_v1_5_SIG = 1,19RSA_OAEP_ENC = 2,20RSA_PSS_SIG = 3,21} rsa_alg_type;2223typedef struct {24const char *name;25rsa_alg_type type;26u32 modbits;27gen_hash_alg_type hash;28const u8 *n;29u16 nlen;30const u8 *d;31u16 dlen;32const u8 *e;33u16 elen;34const u8 *p;35u16 plen;36const u8 *q;37u16 qlen;38const u8 *dP;39u16 dPlen;40const u8 *dQ;41u16 dQlen;42const u8 *qInv;43u16 qInvlen;44const u8 *m;45u32 mlen;46const u8 *res;47u32 reslen;48const u8 *salt;49u32 saltlen;50} rsa_test;515253ATTRIBUTE_WARN_UNUSED_RET static inline int perform_rsa_tests(const rsa_test **tests, u32 num_tests)54{55int ret = 0, cmp;56unsigned int i;5758for(i = 0; i < num_tests; i++){59const rsa_test *t = tests[i];60u32 modbits = t->modbits;61rsa_pub_key pub;62rsa_priv_key priv;63rsa_priv_key priv_pq;6465/* Import the keys */66ret = rsa_import_pub_key(&pub, t->n, (u16)t->nlen, t->e, (u16)t->elen); EG(ret, err1);67if(t->dP == NULL){68const rsa_test *t_ = NULL;69MUST_HAVE((num_tests > 1) && (i < (num_tests - 1)), ret, err);70/* NOTE: we use the "next" CRT test to extract p and q */71t_ = tests[i + 1];72MUST_HAVE((t_->dP != NULL), ret, err);73/* Import the RSA_SIMPLE private key with only d and n */74ret = rsa_import_simple_priv_key(&priv, t->n, (u16)t->nlen, t->d, (u16)t->dlen, NULL, 0, NULL, 0); EG(ret, err1);75/* Import the RSA_SIMPLE_PQ with d, n, p and q */76ret = rsa_import_simple_priv_key(&priv_pq, t->n, (u16)t->nlen, t->d, (u16)t->dlen, t_->p, (u16)t_->plen, t_->q, (u16)t_->qlen); EG(ret, err1);77}78else{79/* Import the RSA_CRT CRT key */80ret = rsa_import_crt_priv_key(&priv, t->p, (u16)t->plen, t->q, (u16)t->qlen, t->dP, (u16)t->dPlen, t->dQ, (u16)t->dQlen, t->qInv, (u16)t->qInvlen, NULL, NULL, 0); EG(ret, err1);81}82#ifdef USE_SIG_BLINDING83/* We using exponent blinding, only RSA_SIMPLE_PQ are usable. We hence overwrite the key */84ret = local_memcpy(&priv, &priv_pq, sizeof(rsa_priv_key)); EG(ret, err);85#endif86/* Perform our operation */87switch(t->type){88case RSA_PKCS1_v1_5_ENC:{89u8 cipher[NN_USABLE_MAX_BYTE_LEN];90u32 clen;91if(t->salt != NULL){92clen = sizeof(cipher);93ret = rsaes_pkcs1_v1_5_encrypt(&pub, t->m, t->mlen, cipher, &clen, modbits, t->salt, t->saltlen); EG(ret, err1);94/* Check the result */95MUST_HAVE((clen == t->reslen), ret, err1);96ret = are_equal(t->res, cipher, t->reslen, &cmp); EG(ret, err1);97MUST_HAVE(cmp, ret, err1);98}99/* Try to decrypt */100clen = sizeof(cipher);101ret = rsaes_pkcs1_v1_5_decrypt(&priv, t->res, t->reslen, cipher, &clen, modbits); EG(ret, err1);102/* Check the result */103MUST_HAVE((clen == t->mlen), ret, err1);104ret = are_equal(t->m, cipher, t->mlen, &cmp); EG(ret, err1);105MUST_HAVE(cmp, ret, err1);106/* Try to decrypt with the hardened version */107clen = sizeof(cipher);108ret = rsaes_pkcs1_v1_5_decrypt_hardened(&priv, &pub, t->res, t->reslen, cipher, &clen, modbits); EG(ret, err1);109/* Check the result */110MUST_HAVE((clen == t->mlen), ret, err1);111ret = are_equal(t->m, cipher, t->mlen, &cmp); EG(ret, err1);112MUST_HAVE(cmp, ret, err1);113break;114}115case RSA_OAEP_ENC:{116u8 cipher[NN_USABLE_MAX_BYTE_LEN];117u32 clen;118if(t->salt != NULL){119clen = sizeof(cipher);120ret = rsaes_oaep_encrypt(&pub, t->m, t->mlen, cipher, &clen, modbits, NULL, 0, t->hash, t->hash, t->salt, t->saltlen); EG(ret, err1);121/* Check the result */122MUST_HAVE((clen == t->reslen), ret, err1);123ret = are_equal(t->res, cipher, t->reslen, &cmp); EG(ret, err1);124MUST_HAVE(cmp, ret, err1);125}126/* Try to decrypt */127clen = sizeof(cipher);128ret = rsaes_oaep_decrypt(&priv, t->res, t->reslen, cipher, &clen, modbits, NULL, 0, t->hash, t->hash); EG(ret, err1);129/* Check the result */130MUST_HAVE((clen == t->mlen), ret, err1);131ret = are_equal(t->m, cipher, t->mlen, &cmp); EG(ret, err1);132MUST_HAVE(cmp, ret, err1);133/* Try to decrypt with the hardened version */134clen = sizeof(cipher);135ret = rsaes_oaep_decrypt_hardened(&priv, &pub, t->res, t->reslen, cipher, &clen, modbits, NULL, 0, t->hash, t->hash); EG(ret, err1);136/* Check the result */137MUST_HAVE((clen == t->mlen), ret, err1);138ret = are_equal(t->m, cipher, t->mlen, &cmp); EG(ret, err1);139MUST_HAVE(cmp, ret, err1);140break;141}142case RSA_PKCS1_v1_5_SIG:{143u8 sig[NN_USABLE_MAX_BYTE_LEN];144u16 siglen = sizeof(sig);145MUST_HAVE((t->reslen) <= 0xffff, ret, err1);146ret = rsassa_pkcs1_v1_5_verify(&pub, t->m, t->mlen, t->res, (u16)(t->reslen), modbits, t->hash); EG(ret, err1);147/* Try to sign */148ret = rsassa_pkcs1_v1_5_sign(&priv, t->m, t->mlen, sig, &siglen, modbits, t->hash); EG(ret, err1);149/* Check the result */150MUST_HAVE((siglen == t->reslen), ret, err1);151ret = are_equal(t->res, sig, t->reslen, &cmp); EG(ret, err1);152MUST_HAVE(cmp, ret, err1);153/* Try to sign with the hardened version */154ret = rsassa_pkcs1_v1_5_sign_hardened(&priv, &pub, t->m, t->mlen, sig, &siglen, modbits, t->hash); EG(ret, err1);155/* Check the result */156MUST_HAVE((siglen == t->reslen), ret, err1);157ret = are_equal(t->res, sig, t->reslen, &cmp); EG(ret, err1);158MUST_HAVE(cmp, ret, err1);159break;160}161case RSA_PSS_SIG:{162if(t->salt == NULL){163/* In case of NULL salt, default saltlen value is the digest size */164u8 digestsize, blocksize;165ret = gen_hash_get_hash_sizes(t->hash, &digestsize, &blocksize); EG(ret, err1);166MUST_HAVE((t->reslen) <= 0xffff, ret, err1);167ret = rsassa_pss_verify(&pub, t->m, t->mlen, t->res, (u16)(t->reslen), modbits, t->hash, t->hash, digestsize); EG(ret, err1);168}169else{170MUST_HAVE((t->reslen) <= 0xffff, ret, err1);171ret = rsassa_pss_verify(&pub, t->m, t->mlen, t->res, (u16)(t->reslen), modbits, t->hash, t->hash, t->saltlen); EG(ret, err1);172}173if(t->salt != NULL){174/* Try to sign */175u8 sig[NN_USABLE_MAX_BYTE_LEN];176u16 siglen = sizeof(sig);177ret = rsassa_pss_sign(&priv, t->m, t->mlen, sig, &siglen, modbits, t->hash, t->hash, t->saltlen, t->salt); EG(ret, err1);178/* Check the result */179MUST_HAVE((t->reslen) <= 0xffff, ret, err1);180MUST_HAVE((siglen == (u16)(t->reslen)), ret, err1);181ret = are_equal(t->res, sig, t->reslen, &cmp); EG(ret, err1);182MUST_HAVE(cmp, ret, err1);183/* Try to sign with the hardened version */184ret = rsassa_pss_sign_hardened(&priv, &pub, t->m, t->mlen, sig, &siglen, modbits, t->hash, t->hash, t->saltlen, t->salt); EG(ret, err1);185/* Check the result */186MUST_HAVE((siglen == (u16)(t->reslen)), ret, err1);187ret = are_equal(t->res, sig, t->reslen, &cmp); EG(ret, err1);188MUST_HAVE(cmp, ret, err1);189}190break;191}192default:{193ret = -1;194break;195}196}197err1:198if(ret){199ext_printf("[-] Test %s failed (modbits = %" PRIu32 ")\n", t->name, t->modbits);200goto err;201}202else{203ext_printf("[+] Test %s passed (modbits = %" PRIu32 ")\n", t->name, t->modbits);204}205}206207if(!ret){208ext_printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\t=== [+] All RSA tests went OK! ===\n");209}210err:211return ret;212}213214#endif /* __RSA_TESTS_H__ */215216217