bn_print.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* crypto/bn/bn_print.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <ctype.h>
  60. #include <limits.h>
  61. #include "cryptlib.h"
  62. #include <openssl/buffer.h>
  63. #include "bn_lcl.h"
  64. static const char Hex[] = "0123456789ABCDEF";
  65. /* Must 'OPENSSL_free' the returned data */
  66. char *BN_bn2hex(const BIGNUM *a)
  67. {
  68. int i, j, v, z = 0;
  69. char *buf;
  70. char *p;
  71. if (a->neg && BN_is_zero(a)) {
  72. /* "-0" == 3 bytes including NULL terminator */
  73. buf = OPENSSL_malloc(3);
  74. } else {
  75. buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
  76. }
  77. if (buf == NULL) {
  78. BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
  79. goto err;
  80. }
  81. p = buf;
  82. if (a->neg)
  83. *(p++) = '-';
  84. if (BN_is_zero(a))
  85. *(p++) = '0';
  86. for (i = a->top - 1; i >= 0; i--) {
  87. for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
  88. /* strip leading zeros */
  89. v = ((int)(a->d[i] >> (long)j)) & 0xff;
  90. if (z || (v != 0)) {
  91. *(p++) = Hex[v >> 4];
  92. *(p++) = Hex[v & 0x0f];
  93. z = 1;
  94. }
  95. }
  96. }
  97. *p = '\0';
  98. err:
  99. return (buf);
  100. }
  101. /* Must 'OPENSSL_free' the returned data */
  102. char *BN_bn2dec(const BIGNUM *a)
  103. {
  104. int i = 0, num, ok = 0;
  105. char *buf = NULL;
  106. char *p;
  107. BIGNUM *t = NULL;
  108. BN_ULONG *bn_data = NULL, *lp;
  109. /*-
  110. * get an upper bound for the length of the decimal integer
  111. * num <= (BN_num_bits(a) + 1) * log(2)
  112. * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error)
  113. * <= BN_num_bits(a)/10 + BN_num_bits/1000 + 1 + 1
  114. */
  115. i = BN_num_bits(a) * 3;
  116. num = (i / 10 + i / 1000 + 1) + 1;
  117. bn_data =
  118. (BN_ULONG *)OPENSSL_malloc((num / BN_DEC_NUM + 1) * sizeof(BN_ULONG));
  119. buf = (char *)OPENSSL_malloc(num + 3);
  120. if ((buf == NULL) || (bn_data == NULL)) {
  121. BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
  122. goto err;
  123. }
  124. if ((t = BN_dup(a)) == NULL)
  125. goto err;
  126. #define BUF_REMAIN (num+3 - (size_t)(p - buf))
  127. p = buf;
  128. lp = bn_data;
  129. if (BN_is_zero(t)) {
  130. *(p++) = '0';
  131. *(p++) = '\0';
  132. } else {
  133. if (BN_is_negative(t))
  134. *p++ = '-';
  135. i = 0;
  136. while (!BN_is_zero(t)) {
  137. *lp = BN_div_word(t, BN_DEC_CONV);
  138. lp++;
  139. }
  140. lp--;
  141. /*
  142. * We now have a series of blocks, BN_DEC_NUM chars in length, where
  143. * the last one needs truncation. The blocks need to be reversed in
  144. * order.
  145. */
  146. BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);
  147. while (*p)
  148. p++;
  149. while (lp != bn_data) {
  150. lp--;
  151. BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);
  152. while (*p)
  153. p++;
  154. }
  155. }
  156. ok = 1;
  157. err:
  158. if (bn_data != NULL)
  159. OPENSSL_free(bn_data);
  160. if (t != NULL)
  161. BN_free(t);
  162. if (!ok && buf) {
  163. OPENSSL_free(buf);
  164. buf = NULL;
  165. }
  166. return (buf);
  167. }
  168. int BN_hex2bn(BIGNUM **bn, const char *a)
  169. {
  170. BIGNUM *ret = NULL;
  171. BN_ULONG l = 0;
  172. int neg = 0, h, m, i, j, k, c;
  173. int num;
  174. if ((a == NULL) || (*a == '\0'))
  175. return (0);
  176. if (*a == '-') {
  177. neg = 1;
  178. a++;
  179. }
  180. for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)
  181. continue;
  182. if (i > INT_MAX/4)
  183. goto err;
  184. num = i + neg;
  185. if (bn == NULL)
  186. return (num);
  187. /* a is the start of the hex digits, and it is 'i' long */
  188. if (*bn == NULL) {
  189. if ((ret = BN_new()) == NULL)
  190. return (0);
  191. } else {
  192. ret = *bn;
  193. BN_zero(ret);
  194. }
  195. /* i is the number of hex digits */
  196. if (bn_expand(ret, i * 4) == NULL)
  197. goto err;
  198. j = i; /* least significant 'hex' */
  199. m = 0;
  200. h = 0;
  201. while (j > 0) {
  202. m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;
  203. l = 0;
  204. for (;;) {
  205. c = a[j - m];
  206. if ((c >= '0') && (c <= '9'))
  207. k = c - '0';
  208. else if ((c >= 'a') && (c <= 'f'))
  209. k = c - 'a' + 10;
  210. else if ((c >= 'A') && (c <= 'F'))
  211. k = c - 'A' + 10;
  212. else
  213. k = 0; /* paranoia */
  214. l = (l << 4) | k;
  215. if (--m <= 0) {
  216. ret->d[h++] = l;
  217. break;
  218. }
  219. }
  220. j -= (BN_BYTES * 2);
  221. }
  222. ret->top = h;
  223. bn_correct_top(ret);
  224. ret->neg = neg;
  225. *bn = ret;
  226. bn_check_top(ret);
  227. return (num);
  228. err:
  229. if (*bn == NULL)
  230. BN_free(ret);
  231. return (0);
  232. }
  233. int BN_dec2bn(BIGNUM **bn, const char *a)
  234. {
  235. BIGNUM *ret = NULL;
  236. BN_ULONG l = 0;
  237. int neg = 0, i, j;
  238. int num;
  239. if ((a == NULL) || (*a == '\0'))
  240. return (0);
  241. if (*a == '-') {
  242. neg = 1;
  243. a++;
  244. }
  245. for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)
  246. continue;
  247. if (i > INT_MAX/4)
  248. goto err;
  249. num = i + neg;
  250. if (bn == NULL)
  251. return (num);
  252. /*
  253. * a is the start of the digits, and it is 'i' long. We chop it into
  254. * BN_DEC_NUM digits at a time
  255. */
  256. if (*bn == NULL) {
  257. if ((ret = BN_new()) == NULL)
  258. return (0);
  259. } else {
  260. ret = *bn;
  261. BN_zero(ret);
  262. }
  263. /* i is the number of digits, a bit of an over expand */
  264. if (bn_expand(ret, i * 4) == NULL)
  265. goto err;
  266. j = BN_DEC_NUM - (i % BN_DEC_NUM);
  267. if (j == BN_DEC_NUM)
  268. j = 0;
  269. l = 0;
  270. while (*a) {
  271. l *= 10;
  272. l += *a - '0';
  273. a++;
  274. if (++j == BN_DEC_NUM) {
  275. BN_mul_word(ret, BN_DEC_CONV);
  276. BN_add_word(ret, l);
  277. l = 0;
  278. j = 0;
  279. }
  280. }
  281. ret->neg = neg;
  282. bn_correct_top(ret);
  283. *bn = ret;
  284. bn_check_top(ret);
  285. return (num);
  286. err:
  287. if (*bn == NULL)
  288. BN_free(ret);
  289. return (0);
  290. }
  291. int BN_asc2bn(BIGNUM **bn, const char *a)
  292. {
  293. const char *p = a;
  294. if (*p == '-')
  295. p++;
  296. if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
  297. if (!BN_hex2bn(bn, p + 2))
  298. return 0;
  299. } else {
  300. if (!BN_dec2bn(bn, p))
  301. return 0;
  302. }
  303. if (*a == '-')
  304. (*bn)->neg = 1;
  305. return 1;
  306. }
  307. #ifndef OPENSSL_NO_BIO
  308. # ifndef OPENSSL_NO_FP_API
  309. int BN_print_fp(FILE *fp, const BIGNUM *a)
  310. {
  311. BIO *b;
  312. int ret;
  313. if ((b = BIO_new(BIO_s_file())) == NULL)
  314. return (0);
  315. BIO_set_fp(b, fp, BIO_NOCLOSE);
  316. ret = BN_print(b, a);
  317. BIO_free(b);
  318. return (ret);
  319. }
  320. # endif
  321. int BN_print(BIO *bp, const BIGNUM *a)
  322. {
  323. int i, j, v, z = 0;
  324. int ret = 0;
  325. if ((a->neg) && (BIO_write(bp, "-", 1) != 1))
  326. goto end;
  327. if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))
  328. goto end;
  329. for (i = a->top - 1; i >= 0; i--) {
  330. for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
  331. /* strip leading zeros */
  332. v = ((int)(a->d[i] >> (long)j)) & 0x0f;
  333. if (z || (v != 0)) {
  334. if (BIO_write(bp, &(Hex[v]), 1) != 1)
  335. goto end;
  336. z = 1;
  337. }
  338. }
  339. }
  340. ret = 1;
  341. end:
  342. return (ret);
  343. }
  344. #endif
  345. char *BN_options(void)
  346. {
  347. static int init = 0;
  348. static char data[16];
  349. if (!init) {
  350. init++;
  351. #ifdef BN_LLONG
  352. BIO_snprintf(data, sizeof data, "bn(%d,%d)",
  353. (int)sizeof(BN_ULLONG) * 8, (int)sizeof(BN_ULONG) * 8);
  354. #else
  355. BIO_snprintf(data, sizeof data, "bn(%d,%d)",
  356. (int)sizeof(BN_ULONG) * 8, (int)sizeof(BN_ULONG) * 8);
  357. #endif
  358. }
  359. return (data);
  360. }