pkcs_1_emsa_test.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include <tomcrypt_test.h>
  4. #if defined(LTC_PKCS_1)
  5. #include "../notes/rsa-testvectors/pkcs1v15sign-vectors.c"
  6. int pkcs_1_emsa_test(void)
  7. {
  8. int hash_idx = find_hash("sha1");
  9. unsigned int i;
  10. unsigned int j;
  11. if (ltc_mp.name == NULL) return CRYPT_NOP;
  12. DO(hash_is_valid(hash_idx));
  13. for (i = 0; i < LTC_ARRAY_SIZE(testcases_emsa); ++i) {
  14. testcase_t* t = &testcases_emsa[i];
  15. rsa_key k, *key = &k;
  16. DOX(rsa_init(key), t->name);
  17. DOX(ltc_mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name);
  18. DOX(ltc_mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name);
  19. DOX(ltc_mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name);
  20. DOX(ltc_mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name);
  21. DOX(ltc_mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name);
  22. DOX(ltc_mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name);
  23. DOX(ltc_mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name);
  24. DOX(ltc_mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name);
  25. key->type = PK_PRIVATE;
  26. for (j = 0; j < LTC_ARRAY_SIZE(t->data); ++j) {
  27. rsaData_t* s = &t->data[j];
  28. unsigned char buf[20], obuf[256];
  29. unsigned long buflen = sizeof(buf), obuflen = sizeof(obuf);
  30. int stat;
  31. DOX(hash_memory(hash_idx, s->o1, s->o1_l, buf, &buflen), s->name);
  32. DOX(rsa_sign_hash_ex(buf, buflen, obuf, &obuflen, LTC_PKCS_1_V1_5, NULL, -1, hash_idx, 0, key), s->name);
  33. COMPARE_TESTVECTOR(obuf, obuflen, s->o2, s->o2_l,s->name, j);
  34. DOX(rsa_verify_hash_ex(obuf, obuflen, buf, buflen, LTC_PKCS_1_V1_5, hash_idx, 0, &stat, key), s->name);
  35. DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name);
  36. } /* for */
  37. ltc_mp_deinit_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL);
  38. } /* for */
  39. return 0;
  40. }
  41. #else
  42. int pkcs_1_emsa_test(void)
  43. {
  44. return CRYPT_NOP;
  45. }
  46. #endif