rsa_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "test.h"
  2. #define RSA_MSGSIZE 78
  3. int rsa_test(void)
  4. {
  5. unsigned char in[1024], out[1024], tmp[1024];
  6. rsa_key key;
  7. int hash_idx, prng_idx, stat, stat2;
  8. unsigned long rsa_msgsize, len, len2;
  9. static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
  10. hash_idx = find_hash("sha1");
  11. prng_idx = find_prng("yarrow");
  12. if (hash_idx == -1 || prng_idx == -1) {
  13. printf("rsa_test requires SHA1 and yarrow");
  14. return 1;
  15. }
  16. /* make a random key */
  17. DO(rsa_make_key(&test_yarrow, prng_idx, 1024/8, 65537, &key));
  18. /* encrypt the key (without lparam) */
  19. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  20. /* make a random key/msg */
  21. yarrow_read(in, rsa_msgsize, &test_yarrow);
  22. len = sizeof(out);
  23. len2 = rsa_msgsize;
  24. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, NULL, 0, &test_yarrow, prng_idx, hash_idx, &key));
  25. /* change a byte */
  26. out[8] ^= 1;
  27. DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, &test_yarrow, prng_idx, hash_idx, &stat2, &key));
  28. /* change a byte back */
  29. out[8] ^= 1;
  30. if (len2 != rsa_msgsize) {
  31. printf("\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2);
  32. return 1;
  33. }
  34. len2 = rsa_msgsize;
  35. DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, &test_yarrow, prng_idx, hash_idx, &stat, &key));
  36. if (!(stat == 1 && stat2 == 0)) {
  37. printf("rsa_decrypt_key failed");
  38. return 1;
  39. }
  40. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  41. int x;
  42. printf("\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
  43. printf("Original contents: \n");
  44. for (x = 0; x < rsa_msgsize; ) {
  45. printf("%02x ", in[x]);
  46. if (!(++x % 16)) {
  47. printf("\n");
  48. }
  49. }
  50. printf("\n");
  51. printf("Output contents: \n");
  52. for (x = 0; x < rsa_msgsize; ) {
  53. printf("%02x ", out[x]);
  54. if (!(++x % 16)) {
  55. printf("\n");
  56. }
  57. }
  58. printf("\n");
  59. return 1;
  60. }
  61. }
  62. /* encrypt the key (with lparam) */
  63. for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
  64. len = sizeof(out);
  65. len2 = rsa_msgsize;
  66. DO(rsa_encrypt_key(in, rsa_msgsize, out, &len, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &key));
  67. /* change a byte */
  68. out[8] ^= 1;
  69. DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &stat2, &key));
  70. if (len2 != rsa_msgsize) {
  71. printf("\nrsa_decrypt_key mismatch len %lu (first decrypt)", len2);
  72. return 1;
  73. }
  74. /* change a byte back */
  75. out[8] ^= 1;
  76. len2 = rsa_msgsize;
  77. DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), &test_yarrow, prng_idx, hash_idx, &stat, &key));
  78. if (!(stat == 1 && stat2 == 0)) {
  79. printf("rsa_decrypt_key failed");
  80. return 1;
  81. }
  82. if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
  83. printf("rsa_decrypt_key mismatch len %lu", len2);
  84. return 1;
  85. }
  86. }
  87. /* sign a message (unsalted, lower cholestorol and Atkins approved) now */
  88. len = sizeof(out);
  89. DO(rsa_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, 0, &key));
  90. DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat, &key));
  91. /* change a byte */
  92. in[0] ^= 1;
  93. DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 0, &stat2, &key));
  94. if (!(stat == 1 && stat2 == 0)) {
  95. printf("rsa_verify_hash (unsalted) failed, %d, %d", stat, stat2);
  96. return 1;
  97. }
  98. /* sign a message (salted) now */
  99. len = sizeof(out);
  100. DO(rsa_sign_hash(in, 20, out, &len, &test_yarrow, prng_idx, hash_idx, 8, &key));
  101. DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 8, &stat, &key));
  102. /* change a byte */
  103. in[0] ^= 1;
  104. DO(rsa_verify_hash(out, len, in, 20, &test_yarrow, prng_idx, hash_idx, 8, &stat2, &key));
  105. if (!(stat == 1 && stat2 == 0)) {
  106. printf("rsa_verify_hash (salted) failed, %d, %d", stat, stat2);
  107. return 1;
  108. }
  109. /* free the key and return */
  110. rsa_free(&key);
  111. return 0;
  112. }