pkcs_1_test.c 2.8 KB

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