bn_conv.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <openssl/err.h>
  10. #include "crypto/ctype.h"
  11. #include "bn_local.h"
  12. static const char Hex[] = "0123456789ABCDEF";
  13. /* Must 'OPENSSL_free' the returned data */
  14. char *BN_bn2hex(const BIGNUM *a)
  15. {
  16. int i, j, v, z = 0;
  17. char *buf;
  18. char *p;
  19. if (BN_is_zero(a))
  20. return OPENSSL_strdup("0");
  21. buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
  22. if (buf == NULL) {
  23. BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
  24. goto err;
  25. }
  26. p = buf;
  27. if (a->neg)
  28. *p++ = '-';
  29. for (i = a->top - 1; i >= 0; i--) {
  30. for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
  31. /* strip leading zeros */
  32. v = (int)((a->d[i] >> j) & 0xff);
  33. if (z || v != 0) {
  34. *p++ = Hex[v >> 4];
  35. *p++ = Hex[v & 0x0f];
  36. z = 1;
  37. }
  38. }
  39. }
  40. *p = '\0';
  41. err:
  42. return buf;
  43. }
  44. #ifndef FIPS_MODE
  45. /* No BIO_snprintf in FIPS_MODE */
  46. /* Must 'OPENSSL_free' the returned data */
  47. char *BN_bn2dec(const BIGNUM *a)
  48. {
  49. int i = 0, num, ok = 0, n, tbytes;
  50. char *buf = NULL;
  51. char *p;
  52. BIGNUM *t = NULL;
  53. BN_ULONG *bn_data = NULL, *lp;
  54. int bn_data_num;
  55. /*-
  56. * get an upper bound for the length of the decimal integer
  57. * num <= (BN_num_bits(a) + 1) * log(2)
  58. * <= 3 * BN_num_bits(a) * 0.101 + log(2) + 1 (rounding error)
  59. * <= 3 * BN_num_bits(a) / 10 + 3 * BN_num_bits / 1000 + 1 + 1
  60. */
  61. i = BN_num_bits(a) * 3;
  62. num = (i / 10 + i / 1000 + 1) + 1;
  63. tbytes = num + 3; /* negative and terminator and one spare? */
  64. bn_data_num = num / BN_DEC_NUM + 1;
  65. bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));
  66. buf = OPENSSL_malloc(tbytes);
  67. if (buf == NULL || bn_data == NULL) {
  68. BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
  69. goto err;
  70. }
  71. if ((t = BN_dup(a)) == NULL)
  72. goto err;
  73. p = buf;
  74. lp = bn_data;
  75. if (BN_is_zero(t)) {
  76. *p++ = '0';
  77. *p++ = '\0';
  78. } else {
  79. if (BN_is_negative(t))
  80. *p++ = '-';
  81. while (!BN_is_zero(t)) {
  82. if (lp - bn_data >= bn_data_num)
  83. goto err;
  84. *lp = BN_div_word(t, BN_DEC_CONV);
  85. if (*lp == (BN_ULONG)-1)
  86. goto err;
  87. lp++;
  88. }
  89. lp--;
  90. /*
  91. * We now have a series of blocks, BN_DEC_NUM chars in length, where
  92. * the last one needs truncation. The blocks need to be reversed in
  93. * order.
  94. */
  95. n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT1, *lp);
  96. if (n < 0)
  97. goto err;
  98. p += n;
  99. while (lp != bn_data) {
  100. lp--;
  101. n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT2, *lp);
  102. if (n < 0)
  103. goto err;
  104. p += n;
  105. }
  106. }
  107. ok = 1;
  108. err:
  109. OPENSSL_free(bn_data);
  110. BN_free(t);
  111. if (ok)
  112. return buf;
  113. OPENSSL_free(buf);
  114. return NULL;
  115. }
  116. #endif
  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. }