pkcs_1_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include <tomcrypt_test.h>
  4. #ifdef LTC_PKCS_1
  5. #ifdef LTC_TEST_REAL_RAND
  6. #define LTC_TEST_RAND_SEED time(NULL)
  7. #else
  8. #define LTC_TEST_RAND_SEED 23
  9. #endif
  10. int pkcs_1_test(void)
  11. {
  12. unsigned char buf[3][128];
  13. int res1, res2, res3, prng_idx, hash_idx;
  14. unsigned long x, y, l1, l2, l3, i1, lparamlen, saltlen, modlen;
  15. static const unsigned char lparam[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
  16. /* get hash/prng */
  17. hash_idx = find_hash("sha1");
  18. prng_idx = find_prng("yarrow");
  19. if (hash_idx == -1 || prng_idx == -1) {
  20. fprintf(stderr, "pkcs_1 tests require sha1/yarrow");
  21. return 1;
  22. }
  23. srand(LTC_TEST_RAND_SEED);
  24. /* do many tests */
  25. for (x = 0; x < 100; x++) {
  26. zeromem(buf, sizeof(buf));
  27. /* make a dummy message (of random length) */
  28. l3 = (rand() & 31) + 8;
  29. for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
  30. /* pick a random lparam len [0..16] */
  31. lparamlen = abs(rand()) % 17;
  32. /* pick a random saltlen 0..16 */
  33. saltlen = abs(rand()) % 17;
  34. /* PKCS #1 v2.0 supports modlens not multiple of 8 */
  35. modlen = 800 + (abs(rand()) % 224);
  36. /* encode it */
  37. l1 = sizeof(buf[1]);
  38. DO(pkcs_1_oaep_encode(buf[0], l3, lparam, lparamlen, modlen, &yarrow_prng, prng_idx, hash_idx, -1, buf[1], &l1));
  39. /* decode it */
  40. l2 = sizeof(buf[2]);
  41. DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, -1, buf[2], &l2, &res1));
  42. if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
  43. fprintf(stderr, "Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
  44. fprintf(stderr, "ORIGINAL:\n");
  45. for (x = 0; x < l3; x++) {
  46. fprintf(stderr, "%02x ", buf[0][x]);
  47. }
  48. fprintf(stderr, "\nRESULT:\n");
  49. for (x = 0; x < l2; x++) {
  50. fprintf(stderr, "%02x ", buf[2][x]);
  51. }
  52. fprintf(stderr, "\n\n");
  53. return 1;
  54. }
  55. /* test PSS */
  56. l1 = sizeof(buf[1]);
  57. DO(pkcs_1_pss_encode(buf[0], l3, saltlen, &yarrow_prng, prng_idx, hash_idx, modlen, buf[1], &l1));
  58. DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res1));
  59. buf[0][i1 = abs(rand()) % l3] ^= 1;
  60. DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res2));
  61. buf[0][i1] ^= 1;
  62. buf[1][abs(rand()) % (l1 - 1)] ^= 1;
  63. pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3);
  64. if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
  65. fprintf(stderr, "PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
  66. return 1;
  67. }
  68. }
  69. return 0;
  70. }
  71. #else
  72. int pkcs_1_test(void)
  73. {
  74. return CRYPT_NOP;
  75. }
  76. #endif