pkcs_1_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "test.h"
  2. int pkcs_1_test(void)
  3. {
  4. unsigned char buf[3][128];
  5. int res1, res2, res3, prng_idx, hash_idx;
  6. unsigned long x, y, l1, l2, l3, i1, i2, lparamlen, saltlen, modlen;
  7. static const unsigned char lparam[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
  8. /* get hash/prng */
  9. hash_idx = find_hash("sha1");
  10. prng_idx = find_prng("yarrow");
  11. if (hash_idx == -1 || prng_idx == -1) {
  12. printf("pkcs_1 tests require sha1/yarrow");
  13. return 1;
  14. }
  15. /* do many tests */
  16. for (x = 0; x < 100; x++) {
  17. zeromem(buf, sizeof(buf));
  18. /* make a dummy message (of random length) */
  19. l3 = (rand() & 31) + 8;
  20. for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
  21. /* random modulus len (v1.5 must be multiple of 8 though arbitrary sizes seem to work) */
  22. modlen = 800 + 8 * (abs(rand()) % 28);
  23. /* PKCS v1.5 testing (encryption) */
  24. l1 = sizeof(buf[1]);
  25. DO(pkcs_1_v15_es_encode(buf[0], l3, modlen, &test_yarrow, prng_idx, buf[1], &l1));
  26. DO(pkcs_1_v15_es_decode(buf[1], l1, modlen, buf[2], l3, &res1));
  27. if (res1 != 1 || memcmp(buf[0], buf[2], l3)) {
  28. printf("pkcs v1.5 encrypt failed %d, %lu, %lu ", res1, l1, l3);
  29. return 1;
  30. }
  31. /* PKCS v1.5 testing (signatures) */
  32. l1 = sizeof(buf[1]);
  33. DO(pkcs_1_v15_sa_encode(buf[0], l3, hash_idx, modlen, buf[1], &l1));
  34. DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res1));
  35. buf[0][i1 = abs(rand()) % l3] ^= 1;
  36. DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res2));
  37. buf[0][i1] ^= 1;
  38. buf[1][i2 = abs(rand()) % l1] ^= 1;
  39. DO(pkcs_1_v15_sa_decode(buf[0], l3, buf[1], l1, hash_idx, modlen, &res3));
  40. if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
  41. printf("pkcs v1.5 sign failed %d %d %d ", res1, res2, res3);
  42. return 1;
  43. }
  44. /* pick a random lparam len [0..16] */
  45. lparamlen = abs(rand()) % 17;
  46. /* pick a random saltlen 0..16 */
  47. saltlen = abs(rand()) % 17;
  48. /* PKCS #1 v2.0 supports modlens not multiple of 8 */
  49. modlen = 800 + (abs(rand()) % 224);
  50. /* encode it */
  51. l1 = sizeof(buf[1]);
  52. DO(pkcs_1_oaep_encode(buf[0], l3, lparam, lparamlen, modlen, &test_yarrow, prng_idx, hash_idx, buf[1], &l1));
  53. /* decode it */
  54. l2 = sizeof(buf[2]);
  55. DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, buf[2], &l2, &res1));
  56. if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
  57. printf("Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
  58. printf("ORIGINAL:\n");
  59. for (x = 0; x < l3; x++) {
  60. printf("%02x ", buf[0][x]);
  61. }
  62. printf("\nRESULT:\n");
  63. for (x = 0; x < l2; x++) {
  64. printf("%02x ", buf[2][x]);
  65. }
  66. printf("\n\n");
  67. return 1;
  68. }
  69. /* test PSS */
  70. l1 = sizeof(buf[1]);
  71. DO(pkcs_1_pss_encode(buf[0], l3, saltlen, &test_yarrow, prng_idx, hash_idx, modlen, buf[1], &l1));
  72. DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res1));
  73. buf[0][i1 = abs(rand()) % l3] ^= 1;
  74. DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res2));
  75. buf[0][i1] ^= 1;
  76. buf[1][i2 = abs(rand()) % l1] ^= 1;
  77. DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3));
  78. if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
  79. printf("PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
  80. return 1;
  81. }
  82. }
  83. return 0;
  84. }