fips_shatest.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* fips_shatest.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2005.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <ctype.h>
  61. #include <string.h>
  62. #include <openssl/bio.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/err.h>
  65. #include <openssl/bn.h>
  66. #include <openssl/x509v3.h>
  67. #ifndef OPENSSL_FIPS
  68. int main(int argc, char *argv[])
  69. {
  70. printf("No FIPS SHAXXX support\n");
  71. return (0);
  72. }
  73. #else
  74. # include "fips_utl.h"
  75. static int dgst_test(FILE *out, FILE *in);
  76. static int print_dgst(const EVP_MD *md, FILE *out,
  77. unsigned char *Msg, int Msglen);
  78. static int print_monte(const EVP_MD *md, FILE *out,
  79. unsigned char *Seed, int SeedLen);
  80. int main(int argc, char **argv)
  81. {
  82. FILE *in = NULL, *out = NULL;
  83. int ret = 1;
  84. if (!FIPS_mode_set(1)) {
  85. do_print_errors();
  86. goto end;
  87. }
  88. if (argc == 1)
  89. in = stdin;
  90. else
  91. in = fopen(argv[1], "r");
  92. if (argc < 2)
  93. out = stdout;
  94. else
  95. out = fopen(argv[2], "w");
  96. if (!in) {
  97. fprintf(stderr, "FATAL input initialization error\n");
  98. goto end;
  99. }
  100. if (!out) {
  101. fprintf(stderr, "FATAL output initialization error\n");
  102. goto end;
  103. }
  104. if (!dgst_test(out, in)) {
  105. fprintf(stderr, "FATAL digest file processing error\n");
  106. goto end;
  107. } else
  108. ret = 0;
  109. end:
  110. if (ret)
  111. do_print_errors();
  112. if (in && (in != stdin))
  113. fclose(in);
  114. if (out && (out != stdout))
  115. fclose(out);
  116. return ret;
  117. }
  118. # define SHA_TEST_MAX_BITS 102400
  119. # define SHA_TEST_MAXLINELEN (((SHA_TEST_MAX_BITS >> 3) * 2) + 100)
  120. int dgst_test(FILE *out, FILE *in)
  121. {
  122. const EVP_MD *md = NULL;
  123. char *linebuf, *olinebuf, *p, *q;
  124. char *keyword, *value;
  125. unsigned char *Msg = NULL, *Seed = NULL;
  126. long MsgLen = -1, Len = -1, SeedLen = -1;
  127. int ret = 0;
  128. int lnum = 0;
  129. olinebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
  130. linebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
  131. if (!linebuf || !olinebuf)
  132. goto error;
  133. while (fgets(olinebuf, SHA_TEST_MAXLINELEN, in)) {
  134. lnum++;
  135. strcpy(linebuf, olinebuf);
  136. keyword = linebuf;
  137. /* Skip leading space */
  138. while (isspace((unsigned char)*keyword))
  139. keyword++;
  140. /* Look for = sign */
  141. p = strchr(linebuf, '=');
  142. /* If no = or starts with [ (for [L=20] line) just copy */
  143. if (!p) {
  144. fputs(olinebuf, out);
  145. continue;
  146. }
  147. q = p - 1;
  148. /* Remove trailing space */
  149. while (isspace((unsigned char)*q))
  150. *q-- = 0;
  151. *p = 0;
  152. value = p + 1;
  153. /* Remove leading space from value */
  154. while (isspace((unsigned char)*value))
  155. value++;
  156. /* Remove trailing space from value */
  157. p = value + strlen(value) - 1;
  158. while (*p == '\n' || isspace((unsigned char)*p))
  159. *p-- = 0;
  160. if (!strcmp(keyword, "[L") && *p == ']') {
  161. switch (atoi(value)) {
  162. case 20:
  163. md = EVP_sha1();
  164. break;
  165. case 28:
  166. md = EVP_sha224();
  167. break;
  168. case 32:
  169. md = EVP_sha256();
  170. break;
  171. case 48:
  172. md = EVP_sha384();
  173. break;
  174. case 64:
  175. md = EVP_sha512();
  176. break;
  177. default:
  178. goto parse_error;
  179. }
  180. } else if (!strcmp(keyword, "Len")) {
  181. if (Len != -1)
  182. goto parse_error;
  183. Len = atoi(value);
  184. if (Len < 0)
  185. goto parse_error;
  186. /* Only handle multiples of 8 bits */
  187. if (Len & 0x7)
  188. goto parse_error;
  189. if (Len > SHA_TEST_MAX_BITS)
  190. goto parse_error;
  191. MsgLen = Len >> 3;
  192. }
  193. else if (!strcmp(keyword, "Msg")) {
  194. long tmplen;
  195. if (strlen(value) & 1)
  196. *(--value) = '0';
  197. if (Msg)
  198. goto parse_error;
  199. Msg = hex2bin_m(value, &tmplen);
  200. if (!Msg)
  201. goto parse_error;
  202. } else if (!strcmp(keyword, "Seed")) {
  203. if (strlen(value) & 1)
  204. *(--value) = '0';
  205. if (Seed)
  206. goto parse_error;
  207. Seed = hex2bin_m(value, &SeedLen);
  208. if (!Seed)
  209. goto parse_error;
  210. } else if (!strcmp(keyword, "MD"))
  211. continue;
  212. else
  213. goto parse_error;
  214. fputs(olinebuf, out);
  215. if (md && Msg && (MsgLen >= 0)) {
  216. if (!print_dgst(md, out, Msg, MsgLen))
  217. goto error;
  218. OPENSSL_free(Msg);
  219. Msg = NULL;
  220. MsgLen = -1;
  221. Len = -1;
  222. } else if (md && Seed && (SeedLen > 0)) {
  223. if (!print_monte(md, out, Seed, SeedLen))
  224. goto error;
  225. OPENSSL_free(Seed);
  226. Seed = NULL;
  227. SeedLen = -1;
  228. }
  229. }
  230. ret = 1;
  231. error:
  232. if (olinebuf)
  233. OPENSSL_free(olinebuf);
  234. if (linebuf)
  235. OPENSSL_free(linebuf);
  236. if (Msg)
  237. OPENSSL_free(Msg);
  238. if (Seed)
  239. OPENSSL_free(Seed);
  240. return ret;
  241. parse_error:
  242. fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
  243. goto error;
  244. }
  245. static int print_dgst(const EVP_MD *emd, FILE *out,
  246. unsigned char *Msg, int Msglen)
  247. {
  248. int i, mdlen;
  249. unsigned char md[EVP_MAX_MD_SIZE];
  250. if (!EVP_Digest(Msg, Msglen, md, (unsigned int *)&mdlen, emd, NULL)) {
  251. fputs("Error calculating HASH\n", stderr);
  252. return 0;
  253. }
  254. fputs("MD = ", out);
  255. for (i = 0; i < mdlen; i++)
  256. fprintf(out, "%02x", md[i]);
  257. fputs("\n", out);
  258. return 1;
  259. }
  260. static int print_monte(const EVP_MD *md, FILE *out,
  261. unsigned char *Seed, int SeedLen)
  262. {
  263. unsigned int i, j, k;
  264. int ret = 0;
  265. EVP_MD_CTX ctx;
  266. unsigned char *m1, *m2, *m3, *p;
  267. unsigned int mlen, m1len, m2len, m3len;
  268. EVP_MD_CTX_init(&ctx);
  269. if (SeedLen > EVP_MAX_MD_SIZE)
  270. mlen = SeedLen;
  271. else
  272. mlen = EVP_MAX_MD_SIZE;
  273. m1 = OPENSSL_malloc(mlen);
  274. m2 = OPENSSL_malloc(mlen);
  275. m3 = OPENSSL_malloc(mlen);
  276. if (!m1 || !m2 || !m3)
  277. goto mc_error;
  278. m1len = m2len = m3len = SeedLen;
  279. memcpy(m1, Seed, SeedLen);
  280. memcpy(m2, Seed, SeedLen);
  281. memcpy(m3, Seed, SeedLen);
  282. fputs("\n", out);
  283. for (j = 0; j < 100; j++) {
  284. for (i = 0; i < 1000; i++) {
  285. EVP_DigestInit_ex(&ctx, md, NULL);
  286. EVP_DigestUpdate(&ctx, m1, m1len);
  287. EVP_DigestUpdate(&ctx, m2, m2len);
  288. EVP_DigestUpdate(&ctx, m3, m3len);
  289. p = m1;
  290. m1 = m2;
  291. m1len = m2len;
  292. m2 = m3;
  293. m2len = m3len;
  294. m3 = p;
  295. EVP_DigestFinal_ex(&ctx, m3, &m3len);
  296. }
  297. fprintf(out, "COUNT = %d\n", j);
  298. fputs("MD = ", out);
  299. for (k = 0; k < m3len; k++)
  300. fprintf(out, "%02x", m3[k]);
  301. fputs("\n\n", out);
  302. memcpy(m1, m3, m3len);
  303. memcpy(m2, m3, m3len);
  304. m1len = m2len = m3len;
  305. }
  306. ret = 1;
  307. mc_error:
  308. if (m1)
  309. OPENSSL_free(m1);
  310. if (m2)
  311. OPENSSL_free(m2);
  312. if (m3)
  313. OPENSSL_free(m3);
  314. EVP_MD_CTX_cleanup(&ctx);
  315. return ret;
  316. }
  317. #endif