fips_utl.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* ====================================================================
  2. * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. */
  49. void do_print_errors(void)
  50. {
  51. const char *file, *data;
  52. int line, flags;
  53. unsigned long l;
  54. while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)))
  55. {
  56. fprintf(stderr, "ERROR:%lx:lib=%d,func=%d,reason=%d"
  57. ":file=%s:line=%d:%s\n",
  58. l, ERR_GET_LIB(l), ERR_GET_FUNC(l), ERR_GET_REASON(l),
  59. file, line, flags & ERR_TXT_STRING ? data : "");
  60. }
  61. }
  62. int hex2bin(const char *in, unsigned char *out)
  63. {
  64. int n1, n2;
  65. unsigned char ch;
  66. for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
  67. { /* first byte */
  68. if ((in[n1] >= '0') && (in[n1] <= '9'))
  69. ch = in[n1++] - '0';
  70. else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
  71. ch = in[n1++] - 'A' + 10;
  72. else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
  73. ch = in[n1++] - 'a' + 10;
  74. else
  75. return -1;
  76. if(!in[n1])
  77. {
  78. out[n2++]=ch;
  79. break;
  80. }
  81. out[n2] = ch << 4;
  82. /* second byte */
  83. if ((in[n1] >= '0') && (in[n1] <= '9'))
  84. ch = in[n1++] - '0';
  85. else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
  86. ch = in[n1++] - 'A' + 10;
  87. else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
  88. ch = in[n1++] - 'a' + 10;
  89. else
  90. return -1;
  91. out[n2++] |= ch;
  92. }
  93. return n2;
  94. }
  95. unsigned char *hex2bin_m(const char *in, long *plen)
  96. {
  97. unsigned char *p;
  98. p = OPENSSL_malloc((strlen(in) + 1)/2);
  99. *plen = hex2bin(in, p);
  100. return p;
  101. }
  102. int do_hex2bn(BIGNUM **pr, const char *in)
  103. {
  104. unsigned char *p;
  105. long plen;
  106. int r = 0;
  107. p = hex2bin_m(in, &plen);
  108. if (!p)
  109. return 0;
  110. if (!*pr)
  111. *pr = BN_new();
  112. if (!*pr)
  113. return 0;
  114. if (BN_bin2bn(p, plen, *pr))
  115. r = 1;
  116. OPENSSL_free(p);
  117. return r;
  118. }
  119. int do_bn_print(FILE *out, BIGNUM *bn)
  120. {
  121. int len, i;
  122. unsigned char *tmp;
  123. len = BN_num_bytes(bn);
  124. if (len == 0)
  125. {
  126. fputs("00", out);
  127. return 1;
  128. }
  129. tmp = OPENSSL_malloc(len);
  130. if (!tmp)
  131. {
  132. fprintf(stderr, "Memory allocation error\n");
  133. return 0;
  134. }
  135. BN_bn2bin(bn, tmp);
  136. for (i = 0; i < len; i++)
  137. fprintf(out, "%02x", tmp[i]);
  138. OPENSSL_free(tmp);
  139. return 1;
  140. }
  141. int do_bn_print_name(FILE *out, const char *name, BIGNUM *bn)
  142. {
  143. int r;
  144. fprintf(out, "%s = ", name);
  145. r = do_bn_print(out, bn);
  146. if (!r)
  147. return 0;
  148. fputs("\n", out);
  149. return 1;
  150. }
  151. int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
  152. {
  153. char *keyword, *value, *p, *q;
  154. strcpy(linebuf, olinebuf);
  155. keyword = linebuf;
  156. /* Skip leading space */
  157. while (isspace((unsigned char)*keyword))
  158. keyword++;
  159. /* Look for = sign */
  160. p = strchr(linebuf, '=');
  161. /* If no '=' exit */
  162. if (!p)
  163. return 0;
  164. q = p - 1;
  165. /* Remove trailing space */
  166. while (isspace((unsigned char)*q))
  167. *q-- = 0;
  168. *p = 0;
  169. value = p + 1;
  170. /* Remove leading space from value */
  171. while (isspace((unsigned char)*value))
  172. value++;
  173. /* Remove trailing space from value */
  174. p = value + strlen(value) - 1;
  175. while (*p == '\n' || isspace((unsigned char)*p))
  176. *p-- = 0;
  177. *pkw = keyword;
  178. *pval = value;
  179. return 1;
  180. }
  181. BIGNUM *hex2bn(const char *in)
  182. {
  183. BIGNUM *p=NULL;
  184. if (!do_hex2bn(&p, in))
  185. return NULL;
  186. return p;
  187. }
  188. int bin2hex(const unsigned char *in,int len,char *out)
  189. {
  190. int n1, n2;
  191. unsigned char ch;
  192. for (n1=0,n2=0 ; n1 < len ; ++n1)
  193. {
  194. ch=in[n1] >> 4;
  195. if (ch <= 0x09)
  196. out[n2++]=ch+'0';
  197. else
  198. out[n2++]=ch-10+'a';
  199. ch=in[n1] & 0x0f;
  200. if(ch <= 0x09)
  201. out[n2++]=ch+'0';
  202. else
  203. out[n2++]=ch-10+'a';
  204. }
  205. out[n2]='\0';
  206. return n2;
  207. }
  208. void pv(const char *tag,const unsigned char *val,int len)
  209. {
  210. char obuf[2048];
  211. bin2hex(val,len,obuf);
  212. printf("%s = %s\n",tag,obuf);
  213. }
  214. /* To avoid extensive changes to test program at this stage just convert
  215. * the input line into an acceptable form. Keyword lines converted to form
  216. * "keyword = value\n" no matter what white space present, all other lines
  217. * just have leading and trailing space removed.
  218. */
  219. int tidy_line(char *linebuf, char *olinebuf)
  220. {
  221. char *keyword, *value, *p, *q;
  222. strcpy(linebuf, olinebuf);
  223. keyword = linebuf;
  224. /* Skip leading space */
  225. while (isspace((unsigned char)*keyword))
  226. keyword++;
  227. /* Look for = sign */
  228. p = strchr(linebuf, '=');
  229. /* If no '=' just chop leading, trailing ws */
  230. if (!p)
  231. {
  232. p = keyword + strlen(keyword) - 1;
  233. while (*p == '\n' || isspace((unsigned char)*p))
  234. *p-- = 0;
  235. strcpy(olinebuf, keyword);
  236. strcat(olinebuf, "\n");
  237. return 1;
  238. }
  239. q = p - 1;
  240. /* Remove trailing space */
  241. while (isspace((unsigned char)*q))
  242. *q-- = 0;
  243. *p = 0;
  244. value = p + 1;
  245. /* Remove leading space from value */
  246. while (isspace((unsigned char)*value))
  247. value++;
  248. /* Remove trailing space from value */
  249. p = value + strlen(value) - 1;
  250. while (*p == '\n' || isspace((unsigned char)*p))
  251. *p-- = 0;
  252. strcpy(olinebuf, keyword);
  253. strcat(olinebuf, " = ");
  254. strcat(olinebuf, value);
  255. strcat(olinebuf, "\n");
  256. return 1;
  257. }
  258. /* NB: this return the number of _bits_ read */
  259. int bint2bin(const char *in, int len, unsigned char *out)
  260. {
  261. int n;
  262. memset(out,0,len);
  263. for(n=0 ; n < len ; ++n)
  264. if(in[n] == '1')
  265. out[n/8]|=(0x80 >> (n%8));
  266. return len;
  267. }
  268. int bin2bint(const unsigned char *in,int len,char *out)
  269. {
  270. int n;
  271. for(n=0 ; n < len ; ++n)
  272. out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
  273. return n;
  274. }
  275. /*-----------------------------------------------*/
  276. void PrintValue(char *tag, unsigned char *val, int len)
  277. {
  278. #if VERBOSE
  279. char obuf[2048];
  280. int olen;
  281. olen = bin2hex(val, len, obuf);
  282. printf("%s = %.*s\n", tag, olen, obuf);
  283. #endif
  284. }
  285. void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
  286. {
  287. char obuf[2048];
  288. int olen;
  289. if(bitmode)
  290. olen=bin2bint(val,len,obuf);
  291. else
  292. olen=bin2hex(val,len,obuf);
  293. fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
  294. #if VERBOSE
  295. printf("%s = %.*s\n", tag, olen, obuf);
  296. #endif
  297. }