test-rsa-sig-ver.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Testing tool for RSA PKCS #1 v1.5 signature verification
  3. * Copyright (c) 2014, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "crypto/crypto.h"
  11. #include "tls/rsa.h"
  12. #include "tls/asn1.h"
  13. #include "tls/pkcs1.h"
  14. static int cavp_rsa_sig_ver(const char *fname)
  15. {
  16. FILE *f;
  17. int ret = 0;
  18. char buf[15000], *pos, *pos2;
  19. u8 msg[200], n[512], s[512], em[512], e[512];
  20. size_t msg_len = 0, n_len = 0, s_len = 0, em_len, e_len = 0;
  21. size_t tmp_len;
  22. char sha_alg[20];
  23. int ok = 0;
  24. printf("CAVP RSA SigVer test vectors from %s\n", fname);
  25. f = fopen(fname, "r");
  26. if (f == NULL) {
  27. printf("%s does not exist - cannot validate CAVP RSA SigVer test vectors\n",
  28. fname);
  29. return 0;
  30. }
  31. while (fgets(buf, sizeof(buf), f)) {
  32. pos = os_strchr(buf, '=');
  33. if (pos == NULL)
  34. continue;
  35. pos2 = pos - 1;
  36. while (pos2 >= buf && *pos2 == ' ')
  37. *pos2-- = '\0';
  38. *pos++ = '\0';
  39. while (*pos == ' ')
  40. *pos++ = '\0';
  41. pos2 = os_strchr(pos, '\r');
  42. if (!pos2)
  43. pos2 = os_strchr(pos, '\n');
  44. if (pos2)
  45. *pos2 = '\0';
  46. else
  47. pos2 = pos + os_strlen(pos);
  48. if (os_strcmp(buf, "SHAAlg") == 0) {
  49. os_strlcpy(sha_alg, pos, sizeof(sha_alg));
  50. } else if (os_strcmp(buf, "Msg") == 0) {
  51. tmp_len = os_strlen(pos);
  52. if (tmp_len > sizeof(msg) * 2) {
  53. printf("Too long Msg\n");
  54. return -1;
  55. }
  56. msg_len = tmp_len / 2;
  57. if (hexstr2bin(pos, msg, msg_len) < 0) {
  58. printf("Invalid hex string '%s'\n", pos);
  59. ret++;
  60. break;
  61. }
  62. } else if (os_strcmp(buf, "n") == 0) {
  63. tmp_len = os_strlen(pos);
  64. if (tmp_len > sizeof(n) * 2) {
  65. printf("Too long n\n");
  66. return -1;
  67. }
  68. n_len = tmp_len / 2;
  69. if (hexstr2bin(pos, n, n_len) < 0) {
  70. printf("Invalid hex string '%s'\n", pos);
  71. ret++;
  72. break;
  73. }
  74. } else if (os_strcmp(buf, "e") == 0) {
  75. tmp_len = os_strlen(pos);
  76. if (tmp_len > sizeof(e) * 2) {
  77. printf("Too long e\n");
  78. return -1;
  79. }
  80. e_len = tmp_len / 2;
  81. if (hexstr2bin(pos, e, e_len) < 0) {
  82. printf("Invalid hex string '%s'\n", pos);
  83. ret++;
  84. break;
  85. }
  86. } else if (os_strcmp(buf, "S") == 0) {
  87. tmp_len = os_strlen(pos);
  88. if (tmp_len > sizeof(s) * 2) {
  89. printf("Too long S\n");
  90. return -1;
  91. }
  92. s_len = tmp_len / 2;
  93. if (hexstr2bin(pos, s, s_len) < 0) {
  94. printf("Invalid hex string '%s'\n", pos);
  95. ret++;
  96. break;
  97. }
  98. } else if (os_strncmp(buf, "EM", 2) == 0) {
  99. tmp_len = os_strlen(pos);
  100. if (tmp_len > sizeof(em) * 2)
  101. return -1;
  102. em_len = tmp_len / 2;
  103. if (hexstr2bin(pos, em, em_len) < 0) {
  104. printf("Invalid hex string '%s'\n", pos);
  105. ret++;
  106. break;
  107. }
  108. } else if (os_strcmp(buf, "Result") == 0) {
  109. const u8 *addr[1];
  110. size_t len[1];
  111. struct crypto_public_key *pk;
  112. int res;
  113. u8 hash[32];
  114. size_t hash_len;
  115. const struct asn1_oid *alg;
  116. addr[0] = msg;
  117. len[0] = msg_len;
  118. if (os_strcmp(sha_alg, "SHA1") == 0) {
  119. if (sha1_vector(1, addr, len, hash) < 0)
  120. return -1;
  121. hash_len = 20;
  122. alg = &asn1_sha1_oid;
  123. } else if (os_strcmp(sha_alg, "SHA256") == 0) {
  124. if (sha256_vector(1, addr, len, hash) < 0)
  125. return -1;
  126. hash_len = 32;
  127. alg = &asn1_sha256_oid;
  128. } else {
  129. continue;
  130. }
  131. printf("\nExpected result: %s\n", pos);
  132. wpa_hexdump(MSG_INFO, "Hash(Msg)", hash, hash_len);
  133. pk = crypto_public_key_import_parts(n, n_len,
  134. e, e_len);
  135. if (pk == NULL) {
  136. printf("Failed to import public key\n");
  137. ret++;
  138. continue;
  139. }
  140. res = pkcs1_v15_sig_ver(pk, s, s_len, alg,
  141. hash, hash_len);
  142. crypto_public_key_free(pk);
  143. if ((*pos == 'F' && !res) || (*pos != 'F' && res)) {
  144. printf("FAIL\n");
  145. ret++;
  146. continue;
  147. }
  148. printf("PASS\n");
  149. ok++;
  150. }
  151. }
  152. fclose(f);
  153. if (ret)
  154. printf("Test case failed\n");
  155. else
  156. printf("%d test vectors OK\n", ok);
  157. return ret;
  158. }
  159. int main(int argc, char *argv[])
  160. {
  161. int ret = 0;
  162. wpa_debug_level = 0;
  163. if (cavp_rsa_sig_ver("CAVP/SigVer15_186-3.rsp"))
  164. ret++;
  165. if (cavp_rsa_sig_ver("CAVP/SigVer15EMTest.txt"))
  166. ret++;
  167. return ret;
  168. }