rsa_test.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include "rsa.h"
  6. #include "keys.h"
  7. int rsa3072_test(void){
  8. int ret;
  9. rsa_pk_t pk = {0};
  10. rsa_sk_t sk = {0};
  11. uint8_t signature[384];
  12. uint32_t signLen;
  13. // message hash to sign
  14. uint8_t hash[32] = {0x7c, 0x14, 0x67, 0x8e, 0x4d, 0x24, 0x66, 0xb2, 0xb5, 0xd1, 0xce, 0x38, 0xc7, 0x68, 0xa9, 0x38,
  15. 0x37, 0xa9, 0x09, 0x35, 0x41, 0x2f, 0x56, 0xd7, 0xec, 0xd7, 0xab, 0xea, 0x4b, 0x43, 0x19, 0x8b};
  16. uint32_t hashLen = 32;
  17. uint32_t saltLen = 20;
  18. // copy keys.h message about public key and private key to the flash RAM
  19. pk.bits = KEY_M_BITS;
  20. memcpy(&pk.modulus [RSA_MAX_MODULUS_LEN-sizeof(key_m) ], key_m, sizeof(key_m ));
  21. memcpy(&pk.exponent [RSA_MAX_MODULUS_LEN-sizeof(key_e) ], key_e, sizeof(key_e ));
  22. sk.bits = KEY_M_BITS;
  23. memcpy(&sk.modulus [RSA_MAX_MODULUS_LEN-sizeof(key_m) ], key_m, sizeof(key_m ));
  24. memcpy(&sk.public_exponet [RSA_MAX_MODULUS_LEN-sizeof(key_e) ], key_e, sizeof(key_e ));
  25. memcpy(&sk.exponent [RSA_MAX_MODULUS_LEN-sizeof(key_pe)], key_pe, sizeof(key_pe));
  26. memcpy(&sk.prime1 [RSA_MAX_PRIME_LEN - sizeof(key_p1)], key_p1, sizeof(key_p1));
  27. memcpy(&sk.prime2 [RSA_MAX_PRIME_LEN - sizeof(key_p2)], key_p2, sizeof(key_p2));
  28. memcpy(&sk.prime_exponent1 [RSA_MAX_PRIME_LEN - sizeof(key_e1)], key_e1, sizeof(key_e1));
  29. memcpy(&sk.prime_exponent2 [RSA_MAX_PRIME_LEN - sizeof(key_e2)], key_e2, sizeof(key_e2));
  30. memcpy(&sk.coefficient [RSA_MAX_PRIME_LEN - sizeof(key_c) ], key_c, sizeof(key_c ));
  31. ret = rsassaPssSign(signature, &signLen, hash, hashLen, saltLen, &sk);
  32. if (ret) {
  33. printf("rsassaPssSign fail.\n");
  34. return -1;
  35. }
  36. ret = rsassaPssVerify(signature, signLen, hash, hashLen, saltLen, &pk);
  37. if (ret) {
  38. printf("rsassaPssVerify fail.\n");
  39. return -1;
  40. } else {
  41. printf("rsassaPssVerify ok.\n");
  42. }
  43. ret = rsassaPkcs1v15Sign(signature, &signLen, hash, hashLen, &sk);
  44. if (ret) {
  45. printf("rsassaPkcs1v15Sign fail.\n");
  46. return -1;
  47. }
  48. ret = rsassaPkcs1v15Verify(signature, signLen, hash, hashLen, &pk);
  49. if (ret) {
  50. printf("rsassaPkcs1v15Verify fail.\n");
  51. return -1;
  52. } else {
  53. printf("rsassaPkcs1v15Verify ok.\n");
  54. }
  55. return 0;
  56. }