2
0

bn_print.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/ctype.h"
  11. #include <limits.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/buffer.h>
  14. #include "bn_lcl.h"
  15. static const char Hex[] = "0123456789ABCDEF";
  16. /* Must 'OPENSSL_free' the returned data */
  17. char *BN_bn2hex(const BIGNUM *a)
  18. {
  19. int i, j, v, z = 0;
  20. char *buf;
  21. char *p;
  22. if (BN_is_zero(a))
  23. return OPENSSL_strdup("0");
  24. buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
  25. if (buf == NULL) {
  26. BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
  27. goto err;
  28. }
  29. p = buf;
  30. if (a->neg)
  31. *p++ = '-';
  32. for (i = a->top - 1; i >= 0; i--) {
  33. for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
  34. /* strip leading zeros */
  35. v = (int)((a->d[i] >> j) & 0xff);
  36. if (z || v != 0) {
  37. *p++ = Hex[v >> 4];
  38. *p++ = Hex[v & 0x0f];
  39. z = 1;
  40. }
  41. }
  42. }
  43. *p = '\0';
  44. err:
  45. return buf;
  46. }
  47. /* Must 'OPENSSL_free' the returned data */
  48. char *BN_bn2dec(const BIGNUM *a)
  49. {
  50. int i = 0, num, ok = 0, n, tbytes;
  51. char *buf = NULL;
  52. char *p;
  53. BIGNUM *t = NULL;
  54. BN_ULONG *bn_data = NULL, *lp;
  55. int bn_data_num;
  56. /*-
  57. * get an upper bound for the length of the decimal integer
  58. * num <= (BN_num_bits(a) + 1) * log(2)
  59. * <= 3 * BN_num_bits(a) * 0.101 + log(2) + 1 (rounding error)
  60. * <= 3 * BN_num_bits(a) / 10 + 3 * BN_num_bits / 1000 + 1 + 1
  61. */
  62. i = BN_num_bits(a) * 3;
  63. num = (i / 10 + i / 1000 + 1) + 1;
  64. tbytes = num + 3; /* negative and terminator and one spare? */
  65. bn_data_num = num / BN_DEC_NUM + 1;
  66. bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));
  67. buf = OPENSSL_malloc(tbytes);
  68. if (buf == NULL || bn_data == NULL) {
  69. BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
  70. goto err;
  71. }
  72. if ((t = BN_dup(a)) == NULL)
  73. goto err;
  74. p = buf;
  75. lp = bn_data;
  76. if (BN_is_zero(t)) {
  77. *p++ = '0';
  78. *p++ = '\0';
  79. } else {
  80. if (BN_is_negative(t))
  81. *p++ = '-';
  82. while (!BN_is_zero(t)) {
  83. if (lp - bn_data >= bn_data_num)
  84. goto err;
  85. *lp = BN_div_word(t, BN_DEC_CONV);
  86. if (*lp == (BN_ULONG)-1)
  87. goto err;
  88. lp++;
  89. }
  90. lp--;
  91. /*
  92. * We now have a series of blocks, BN_DEC_NUM chars in length, where
  93. * the last one needs truncation. The blocks need to be reversed in
  94. * order.
  95. */
  96. n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT1, *lp);
  97. if (n < 0)
  98. goto err;
  99. p += n;
  100. while (lp != bn_data) {
  101. lp--;
  102. n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT2, *lp);
  103. if (n < 0)
  104. goto err;
  105. p += n;
  106. }
  107. }
  108. ok = 1;
  109. err:
  110. OPENSSL_free(bn_data);
  111. BN_free(t);
  112. if (ok)
  113. return buf;
  114. OPENSSL_free(buf);
  115. return NULL;
  116. }
  117. int BN_hex2bn(BIGNUM **bn, const char *a)
  118. {
  119. BIGNUM *ret = NULL;
  120. BN_ULONG l = 0;
  121. int neg = 0, h, m, i, j, k, c;
  122. int num;
  123. if (a == NULL || *a == '\0')
  124. return 0;
  125. if (*a == '-') {
  126. neg = 1;
  127. a++;
  128. }
  129. for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)
  130. continue;
  131. if (i == 0 || i > INT_MAX / 4)
  132. goto err;
  133. num = i + neg;
  134. if (bn == NULL)
  135. return num;
  136. /* a is the start of the hex digits, and it is 'i' long */
  137. if (*bn == NULL) {
  138. if ((ret = BN_new()) == NULL)
  139. return 0;
  140. } else {
  141. ret = *bn;
  142. BN_zero(ret);
  143. }
  144. /* i is the number of hex digits */
  145. if (bn_expand(ret, i * 4) == NULL)
  146. goto err;
  147. j = i; /* least significant 'hex' */
  148. m = 0;
  149. h = 0;
  150. while (j > 0) {
  151. m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;
  152. l = 0;
  153. for (;;) {
  154. c = a[j - m];
  155. k = OPENSSL_hexchar2int(c);
  156. if (k < 0)
  157. k = 0; /* paranoia */
  158. l = (l << 4) | k;
  159. if (--m <= 0) {
  160. ret->d[h++] = l;
  161. break;
  162. }
  163. }
  164. j -= BN_BYTES * 2;
  165. }
  166. ret->top = h;
  167. bn_correct_top(ret);
  168. *bn = ret;
  169. bn_check_top(ret);
  170. /* Don't set the negative flag if it's zero. */
  171. if (ret->top != 0)
  172. ret->neg = neg;
  173. return num;
  174. err:
  175. if (*bn == NULL)
  176. BN_free(ret);
  177. return 0;
  178. }
  179. int BN_dec2bn(BIGNUM **bn, const char *a)
  180. {
  181. BIGNUM *ret = NULL;
  182. BN_ULONG l = 0;
  183. int neg = 0, i, j;
  184. int num;
  185. if (a == NULL || *a == '\0')
  186. return 0;
  187. if (*a == '-') {
  188. neg = 1;
  189. a++;
  190. }
  191. for (i = 0; i <= INT_MAX / 4 && ossl_isdigit(a[i]); i++)
  192. continue;
  193. if (i == 0 || i > INT_MAX / 4)
  194. goto err;
  195. num = i + neg;
  196. if (bn == NULL)
  197. return num;
  198. /*
  199. * a is the start of the digits, and it is 'i' long. We chop it into
  200. * BN_DEC_NUM digits at a time
  201. */
  202. if (*bn == NULL) {
  203. if ((ret = BN_new()) == NULL)
  204. return 0;
  205. } else {
  206. ret = *bn;
  207. BN_zero(ret);
  208. }
  209. /* i is the number of digits, a bit of an over expand */
  210. if (bn_expand(ret, i * 4) == NULL)
  211. goto err;
  212. j = BN_DEC_NUM - i % BN_DEC_NUM;
  213. if (j == BN_DEC_NUM)
  214. j = 0;
  215. l = 0;
  216. while (--i >= 0) {
  217. l *= 10;
  218. l += *a - '0';
  219. a++;
  220. if (++j == BN_DEC_NUM) {
  221. if (!BN_mul_word(ret, BN_DEC_CONV)
  222. || !BN_add_word(ret, l))
  223. goto err;
  224. l = 0;
  225. j = 0;
  226. }
  227. }
  228. bn_correct_top(ret);
  229. *bn = ret;
  230. bn_check_top(ret);
  231. /* Don't set the negative flag if it's zero. */
  232. if (ret->top != 0)
  233. ret->neg = neg;
  234. return num;
  235. err:
  236. if (*bn == NULL)
  237. BN_free(ret);
  238. return 0;
  239. }
  240. int BN_asc2bn(BIGNUM **bn, const char *a)
  241. {
  242. const char *p = a;
  243. if (*p == '-')
  244. p++;
  245. if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
  246. if (!BN_hex2bn(bn, p + 2))
  247. return 0;
  248. } else {
  249. if (!BN_dec2bn(bn, p))
  250. return 0;
  251. }
  252. /* Don't set the negative flag if it's zero. */
  253. if (*a == '-' && (*bn)->top != 0)
  254. (*bn)->neg = 1;
  255. return 1;
  256. }
  257. # ifndef OPENSSL_NO_STDIO
  258. int BN_print_fp(FILE *fp, const BIGNUM *a)
  259. {
  260. BIO *b;
  261. int ret;
  262. if ((b = BIO_new(BIO_s_file())) == NULL)
  263. return 0;
  264. BIO_set_fp(b, fp, BIO_NOCLOSE);
  265. ret = BN_print(b, a);
  266. BIO_free(b);
  267. return ret;
  268. }
  269. # endif
  270. int BN_print(BIO *bp, const BIGNUM *a)
  271. {
  272. int i, j, v, z = 0;
  273. int ret = 0;
  274. if ((a->neg) && BIO_write(bp, "-", 1) != 1)
  275. goto end;
  276. if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1)
  277. goto end;
  278. for (i = a->top - 1; i >= 0; i--) {
  279. for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
  280. /* strip leading zeros */
  281. v = (int)((a->d[i] >> j) & 0x0f);
  282. if (z || v != 0) {
  283. if (BIO_write(bp, &Hex[v], 1) != 1)
  284. goto end;
  285. z = 1;
  286. }
  287. }
  288. }
  289. ret = 1;
  290. end:
  291. return ret;
  292. }
  293. char *BN_options(void)
  294. {
  295. static int init = 0;
  296. static char data[16];
  297. if (!init) {
  298. init++;
  299. #ifdef BN_LLONG
  300. BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)",
  301. sizeof(BN_ULLONG) * 8, sizeof(BN_ULONG) * 8);
  302. #else
  303. BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)",
  304. sizeof(BN_ULONG) * 8, sizeof(BN_ULONG) * 8);
  305. #endif
  306. }
  307. return data;
  308. }