fips_utl.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. int hex2bin(const char *in, unsigned char *out);
  51. unsigned char *hex2bin_m(const char *in, long *plen);
  52. int do_hex2bn(BIGNUM **pr, const char *in);
  53. int do_bn_print(FILE *out, BIGNUM *bn);
  54. int do_bn_print_name(FILE *out, const char *name, BIGNUM *bn);
  55. int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf);
  56. BIGNUM *hex2bn(const char *in);
  57. int bin2hex(const unsigned char *in, int len, char *out);
  58. void pv(const char *tag, const unsigned char *val, int len);
  59. int tidy_line(char *linebuf, char *olinebuf);
  60. int bint2bin(const char *in, int len, unsigned char *out);
  61. int bin2bint(const unsigned char *in, int len, char *out);
  62. void PrintValue(char *tag, unsigned char *val, int len);
  63. void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,
  64. int bitmode);
  65. void do_print_errors(void)
  66. {
  67. const char *file, *data;
  68. int line, flags;
  69. unsigned long l;
  70. while ((l = ERR_get_error_line_data(&file, &line, &data, &flags))) {
  71. fprintf(stderr, "ERROR:%lx:lib=%d,func=%d,reason=%d"
  72. ":file=%s:line=%d:%s\n",
  73. l, ERR_GET_LIB(l), ERR_GET_FUNC(l), ERR_GET_REASON(l),
  74. file, line, flags & ERR_TXT_STRING ? data : "");
  75. }
  76. }
  77. int hex2bin(const char *in, unsigned char *out)
  78. {
  79. int n1, n2;
  80. unsigned char ch;
  81. for (n1 = 0, n2 = 0; in[n1] && in[n1] != '\n';) { /* first byte */
  82. if ((in[n1] >= '0') && (in[n1] <= '9'))
  83. ch = in[n1++] - '0';
  84. else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
  85. ch = in[n1++] - 'A' + 10;
  86. else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
  87. ch = in[n1++] - 'a' + 10;
  88. else
  89. return -1;
  90. if (!in[n1]) {
  91. out[n2++] = ch;
  92. break;
  93. }
  94. out[n2] = ch << 4;
  95. /* second byte */
  96. if ((in[n1] >= '0') && (in[n1] <= '9'))
  97. ch = in[n1++] - '0';
  98. else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
  99. ch = in[n1++] - 'A' + 10;
  100. else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
  101. ch = in[n1++] - 'a' + 10;
  102. else
  103. return -1;
  104. out[n2++] |= ch;
  105. }
  106. return n2;
  107. }
  108. unsigned char *hex2bin_m(const char *in, long *plen)
  109. {
  110. unsigned char *p;
  111. p = OPENSSL_malloc((strlen(in) + 1) / 2);
  112. *plen = hex2bin(in, p);
  113. return p;
  114. }
  115. int do_hex2bn(BIGNUM **pr, const char *in)
  116. {
  117. unsigned char *p;
  118. long plen;
  119. int r = 0;
  120. p = hex2bin_m(in, &plen);
  121. if (!p)
  122. return 0;
  123. if (!*pr)
  124. *pr = BN_new();
  125. if (!*pr)
  126. return 0;
  127. if (BN_bin2bn(p, plen, *pr))
  128. r = 1;
  129. OPENSSL_free(p);
  130. return r;
  131. }
  132. int do_bn_print(FILE *out, BIGNUM *bn)
  133. {
  134. int len, i;
  135. unsigned char *tmp;
  136. len = BN_num_bytes(bn);
  137. if (len == 0) {
  138. fputs("00", out);
  139. return 1;
  140. }
  141. tmp = OPENSSL_malloc(len);
  142. if (!tmp) {
  143. fprintf(stderr, "Memory allocation error\n");
  144. return 0;
  145. }
  146. BN_bn2bin(bn, tmp);
  147. for (i = 0; i < len; i++)
  148. fprintf(out, "%02x", tmp[i]);
  149. OPENSSL_free(tmp);
  150. return 1;
  151. }
  152. int do_bn_print_name(FILE *out, const char *name, BIGNUM *bn)
  153. {
  154. int r;
  155. fprintf(out, "%s = ", name);
  156. r = do_bn_print(out, bn);
  157. if (!r)
  158. return 0;
  159. fputs("\n", out);
  160. return 1;
  161. }
  162. int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
  163. {
  164. char *keyword, *value, *p, *q;
  165. strcpy(linebuf, olinebuf);
  166. keyword = linebuf;
  167. /* Skip leading space */
  168. while (isspace((unsigned char)*keyword))
  169. keyword++;
  170. /* Look for = sign */
  171. p = strchr(linebuf, '=');
  172. /* If no '=' exit */
  173. if (!p)
  174. return 0;
  175. q = p - 1;
  176. /* Remove trailing space */
  177. while (isspace((unsigned char)*q))
  178. *q-- = 0;
  179. *p = 0;
  180. value = p + 1;
  181. /* Remove leading space from value */
  182. while (isspace((unsigned char)*value))
  183. value++;
  184. /* Remove trailing space from value */
  185. p = value + strlen(value) - 1;
  186. while (*p == '\n' || isspace((unsigned char)*p))
  187. *p-- = 0;
  188. *pkw = keyword;
  189. *pval = value;
  190. return 1;
  191. }
  192. BIGNUM *hex2bn(const char *in)
  193. {
  194. BIGNUM *p = NULL;
  195. if (!do_hex2bn(&p, in))
  196. return NULL;
  197. return p;
  198. }
  199. int bin2hex(const unsigned char *in, int len, char *out)
  200. {
  201. int n1, n2;
  202. unsigned char ch;
  203. for (n1 = 0, n2 = 0; n1 < len; ++n1) {
  204. ch = in[n1] >> 4;
  205. if (ch <= 0x09)
  206. out[n2++] = ch + '0';
  207. else
  208. out[n2++] = ch - 10 + 'a';
  209. ch = in[n1] & 0x0f;
  210. if (ch <= 0x09)
  211. out[n2++] = ch + '0';
  212. else
  213. out[n2++] = ch - 10 + 'a';
  214. }
  215. out[n2] = '\0';
  216. return n2;
  217. }
  218. void pv(const char *tag, const unsigned char *val, int len)
  219. {
  220. char obuf[2048];
  221. bin2hex(val, len, obuf);
  222. printf("%s = %s\n", tag, obuf);
  223. }
  224. /*
  225. * To avoid extensive changes to test program at this stage just convert the
  226. * input line into an acceptable form. Keyword lines converted to form
  227. * "keyword = value\n" no matter what white space present, all other lines
  228. * just have leading and trailing space removed.
  229. */
  230. int tidy_line(char *linebuf, char *olinebuf)
  231. {
  232. char *keyword, *value, *p, *q;
  233. strcpy(linebuf, olinebuf);
  234. keyword = linebuf;
  235. /* Skip leading space */
  236. while (isspace((unsigned char)*keyword))
  237. keyword++;
  238. /* Look for = sign */
  239. p = strchr(linebuf, '=');
  240. /* If no '=' just chop leading, trailing ws */
  241. if (!p) {
  242. p = keyword + strlen(keyword) - 1;
  243. while (*p == '\n' || isspace((unsigned char)*p))
  244. *p-- = 0;
  245. strcpy(olinebuf, keyword);
  246. strcat(olinebuf, "\n");
  247. return 1;
  248. }
  249. q = p - 1;
  250. /* Remove trailing space */
  251. while (isspace((unsigned char)*q))
  252. *q-- = 0;
  253. *p = 0;
  254. value = p + 1;
  255. /* Remove leading space from value */
  256. while (isspace((unsigned char)*value))
  257. value++;
  258. /* Remove trailing space from value */
  259. p = value + strlen(value) - 1;
  260. while (*p == '\n' || isspace((unsigned char)*p))
  261. *p-- = 0;
  262. strcpy(olinebuf, keyword);
  263. strcat(olinebuf, " = ");
  264. strcat(olinebuf, value);
  265. strcat(olinebuf, "\n");
  266. return 1;
  267. }
  268. /* NB: this return the number of _bits_ read */
  269. int bint2bin(const char *in, int len, unsigned char *out)
  270. {
  271. int n;
  272. memset(out, 0, len);
  273. for (n = 0; n < len; ++n)
  274. if (in[n] == '1')
  275. out[n / 8] |= (0x80 >> (n % 8));
  276. return len;
  277. }
  278. int bin2bint(const unsigned char *in, int len, char *out)
  279. {
  280. int n;
  281. for (n = 0; n < len; ++n)
  282. out[n] = (in[n / 8] & (0x80 >> (n % 8))) ? '1' : '0';
  283. return n;
  284. }
  285. /* ---------------------------------------------*/
  286. void PrintValue(char *tag, unsigned char *val, int len)
  287. {
  288. #if VERBOSE
  289. char obuf[2048];
  290. int olen;
  291. olen = bin2hex(val, len, obuf);
  292. printf("%s = %.*s\n", tag, olen, obuf);
  293. #endif
  294. }
  295. void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,
  296. int bitmode)
  297. {
  298. char obuf[2048];
  299. int olen;
  300. if (bitmode)
  301. olen = bin2bint(val, len, obuf);
  302. else
  303. olen = bin2hex(val, len, obuf);
  304. fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
  305. #if VERBOSE
  306. printf("%s = %.*s\n", tag, olen, obuf);
  307. #endif
  308. }