pkcs_1_test.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, 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. if (hash_idx == -1) {
  19. fprintf(stderr, "pkcs_1 tests require sha1");
  20. return 1;
  21. }
  22. srand(LTC_TEST_RAND_SEED);
  23. /* do many tests */
  24. for (x = 0; x < 100; x++) {
  25. zeromem(buf, sizeof(buf));
  26. /* make a dummy message (of random length) */
  27. l3 = (rand() & 31) + 8;
  28. for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
  29. /* pick a random lparam len [0..16] */
  30. lparamlen = abs(rand()) % 17;
  31. /* pick a random saltlen 0..16 */
  32. saltlen = abs(rand()) % 17;
  33. /* PKCS #1 v2.0 supports modlens not multiple of 8 */
  34. modlen = 800 + (abs(rand()) % 224);
  35. /* encode it */
  36. l1 = sizeof(buf[1]);
  37. DO(pkcs_1_oaep_encode(buf[0], l3, lparam, lparamlen, modlen, &yarrow_prng, hash_idx, -1, buf[1], &l1));
  38. /* decode it */
  39. l2 = sizeof(buf[2]);
  40. DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, -1, buf[2], &l2, &res1));
  41. if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
  42. fprintf(stderr, "Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
  43. fprintf(stderr, "ORIGINAL:\n");
  44. for (x = 0; x < l3; x++) {
  45. fprintf(stderr, "%02x ", buf[0][x]);
  46. }
  47. fprintf(stderr, "\nRESULT:\n");
  48. for (x = 0; x < l2; x++) {
  49. fprintf(stderr, "%02x ", buf[2][x]);
  50. }
  51. fprintf(stderr, "\n\n");
  52. return 1;
  53. }
  54. /* test PSS */
  55. l1 = sizeof(buf[1]);
  56. DO(pkcs_1_pss_encode(buf[0], l3, saltlen, &yarrow_prng, hash_idx, modlen, buf[1], &l1));
  57. DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res1));
  58. buf[0][i1 = abs(rand()) % l3] ^= 1;
  59. DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res2));
  60. buf[0][i1] ^= 1;
  61. buf[1][abs(rand()) % (l1 - 1)] ^= 1;
  62. pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3);
  63. if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
  64. fprintf(stderr, "PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
  65. return 1;
  66. }
  67. }
  68. return 0;
  69. }
  70. #else
  71. int pkcs_1_test(void)
  72. {
  73. return CRYPT_NOP;
  74. }
  75. #endif