fips_rngvs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Crude test driver for processing the VST and MCT testvector files
  3. * generated by the CMVP RNGVS product.
  4. *
  5. * Note the input files are assumed to have a _very_ specific format
  6. * as described in the NIST document "The Random Number Generator
  7. * Validation System (RNGVS)", May 25, 2004.
  8. *
  9. */
  10. #define OPENSSL_FIPSAPI
  11. #include <openssl/opensslconf.h>
  12. #ifndef OPENSSL_FIPS
  13. #include <stdio.h>
  14. int main(int argc, char **argv)
  15. {
  16. printf("No FIPS RNG support\n");
  17. return 0;
  18. }
  19. #else
  20. #include <openssl/bn.h>
  21. #include <openssl/dsa.h>
  22. #include <openssl/fips.h>
  23. #include <openssl/err.h>
  24. #include <openssl/rand.h>
  25. #include <openssl/fips_rand.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include "fips_utl.h"
  29. static void vst(FILE *in, FILE *out)
  30. {
  31. unsigned char *key = NULL;
  32. unsigned char *v = NULL;
  33. unsigned char *dt = NULL;
  34. unsigned char ret[16];
  35. char buf[1024];
  36. char lbuf[1024];
  37. char *keyword, *value;
  38. long i, keylen;
  39. keylen = 0;
  40. while(fgets(buf,sizeof buf,in) != NULL)
  41. {
  42. fputs(buf,out);
  43. if(!strncmp(buf,"[AES 128-Key]", 13))
  44. keylen = 16;
  45. else if(!strncmp(buf,"[AES 192-Key]", 13))
  46. keylen = 24;
  47. else if(!strncmp(buf,"[AES 256-Key]", 13))
  48. keylen = 32;
  49. if (!parse_line(&keyword, &value, lbuf, buf))
  50. continue;
  51. if(!strcmp(keyword,"Key"))
  52. {
  53. key=hex2bin_m(value,&i);
  54. if (i != keylen)
  55. {
  56. fprintf(stderr, "Invalid key length, expecting %ld\n", keylen);
  57. return;
  58. }
  59. }
  60. else if(!strcmp(keyword,"DT"))
  61. {
  62. dt=hex2bin_m(value,&i);
  63. if (i != 16)
  64. {
  65. fprintf(stderr, "Invalid DT length\n");
  66. return;
  67. }
  68. }
  69. else if(!strcmp(keyword,"V"))
  70. {
  71. v=hex2bin_m(value,&i);
  72. if (i != 16)
  73. {
  74. fprintf(stderr, "Invalid V length\n");
  75. return;
  76. }
  77. if (!key || !dt)
  78. {
  79. fprintf(stderr, "Missing key or DT\n");
  80. return;
  81. }
  82. FIPS_x931_set_key(key, keylen);
  83. FIPS_x931_seed(v,16);
  84. FIPS_x931_set_dt(dt);
  85. if (FIPS_x931_bytes(ret,16) <= 0)
  86. {
  87. fprintf(stderr, "Error getting PRNG value\n");
  88. return;
  89. }
  90. OutputValue("R", ret, 16, out, 0);
  91. OPENSSL_free(key);
  92. key = NULL;
  93. OPENSSL_free(dt);
  94. dt = NULL;
  95. OPENSSL_free(v);
  96. v = NULL;
  97. }
  98. }
  99. }
  100. static void mct(FILE *in, FILE *out)
  101. {
  102. unsigned char *key = NULL;
  103. unsigned char *v = NULL;
  104. unsigned char *dt = NULL;
  105. unsigned char ret[16];
  106. char buf[1024];
  107. char lbuf[1024];
  108. char *keyword, *value;
  109. long i, keylen;
  110. int j;
  111. keylen = 0;
  112. while(fgets(buf,sizeof buf,in) != NULL)
  113. {
  114. fputs(buf,out);
  115. if(!strncmp(buf,"[AES 128-Key]", 13))
  116. keylen = 16;
  117. else if(!strncmp(buf,"[AES 192-Key]", 13))
  118. keylen = 24;
  119. else if(!strncmp(buf,"[AES 256-Key]", 13))
  120. keylen = 32;
  121. if (!parse_line(&keyword, &value, lbuf, buf))
  122. continue;
  123. if(!strcmp(keyword,"Key"))
  124. {
  125. key=hex2bin_m(value,&i);
  126. if (i != keylen)
  127. {
  128. fprintf(stderr, "Invalid key length, expecting %ld\n", keylen);
  129. return;
  130. }
  131. }
  132. else if(!strcmp(keyword,"DT"))
  133. {
  134. dt=hex2bin_m(value,&i);
  135. if (i != 16)
  136. {
  137. fprintf(stderr, "Invalid DT length\n");
  138. return;
  139. }
  140. }
  141. else if(!strcmp(keyword,"V"))
  142. {
  143. v=hex2bin_m(value,&i);
  144. if (i != 16)
  145. {
  146. fprintf(stderr, "Invalid V length\n");
  147. return;
  148. }
  149. if (!key || !dt)
  150. {
  151. fprintf(stderr, "Missing key or DT\n");
  152. return;
  153. }
  154. FIPS_x931_set_key(key, keylen);
  155. FIPS_x931_seed(v,16);
  156. for (i = 0; i < 10000; i++)
  157. {
  158. FIPS_x931_set_dt(dt);
  159. if (FIPS_x931_bytes(ret,16) <= 0)
  160. {
  161. fprintf(stderr, "Error getting PRNG value\n");
  162. return;
  163. }
  164. /* Increment DT */
  165. for (j = 15; j >= 0; j--)
  166. {
  167. dt[j]++;
  168. if (dt[j])
  169. break;
  170. }
  171. }
  172. OutputValue("R", ret, 16, out, 0);
  173. OPENSSL_free(key);
  174. key = NULL;
  175. OPENSSL_free(dt);
  176. dt = NULL;
  177. OPENSSL_free(v);
  178. v = NULL;
  179. }
  180. }
  181. }
  182. int main(int argc,char **argv)
  183. {
  184. FILE *in, *out;
  185. if (argc == 4)
  186. {
  187. in = fopen(argv[2], "r");
  188. if (!in)
  189. {
  190. fprintf(stderr, "Error opening input file\n");
  191. exit(1);
  192. }
  193. out = fopen(argv[3], "w");
  194. if (!out)
  195. {
  196. fprintf(stderr, "Error opening output file\n");
  197. exit(1);
  198. }
  199. }
  200. else if (argc == 2)
  201. {
  202. in = stdin;
  203. out = stdout;
  204. }
  205. else
  206. {
  207. fprintf(stderr,"%s [mct|vst]\n",argv[0]);
  208. exit(1);
  209. }
  210. fips_algtest_init();
  211. FIPS_x931_reset();
  212. if (!FIPS_x931_test_mode())
  213. {
  214. fprintf(stderr, "Error setting PRNG test mode\n");
  215. exit(1);
  216. }
  217. if(!strcmp(argv[1],"mct"))
  218. mct(in, out);
  219. else if(!strcmp(argv[1],"vst"))
  220. vst(in, out);
  221. else
  222. {
  223. fprintf(stderr,"Don't know how to %s.\n",argv[1]);
  224. exit(1);
  225. }
  226. if (argc == 4)
  227. {
  228. fclose(in);
  229. fclose(out);
  230. }
  231. return 0;
  232. }
  233. #endif