pkcs_1_test.c 2.7 KB

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