I2C toy code
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

107 líneas
3.1 KiB

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