bn_conv.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 1995-2020 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. goto err;
  24. p = buf;
  25. if (a->neg)
  26. *p++ = '-';
  27. for (i = a->top - 1; i >= 0; i--) {
  28. for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
  29. /* strip leading zeros */
  30. v = (int)((a->d[i] >> j) & 0xff);
  31. if (z || v != 0) {
  32. *p++ = Hex[v >> 4];
  33. *p++ = Hex[v & 0x0f];
  34. z = 1;
  35. }
  36. }
  37. }
  38. *p = '\0';
  39. err:
  40. return buf;
  41. }
  42. #ifndef FIPS_MODULE
  43. /* No BIO_snprintf in FIPS_MODULE */
  44. /* Must 'OPENSSL_free' the returned data */
  45. char *BN_bn2dec(const BIGNUM *a)
  46. {
  47. int i = 0, num, ok = 0, n, tbytes;
  48. char *buf = NULL;
  49. char *p;
  50. BIGNUM *t = NULL;
  51. BN_ULONG *bn_data = NULL, *lp;
  52. int bn_data_num;
  53. /*-
  54. * get an upper bound for the length of the decimal integer
  55. * num <= (BN_num_bits(a) + 1) * log(2)
  56. * <= 3 * BN_num_bits(a) * 0.101 + log(2) + 1 (rounding error)
  57. * <= 3 * BN_num_bits(a) / 10 + 3 * BN_num_bits / 1000 + 1 + 1
  58. */
  59. i = BN_num_bits(a) * 3;
  60. num = (i / 10 + i / 1000 + 1) + 1;
  61. tbytes = num + 3; /* negative and terminator and one spare? */
  62. bn_data_num = num / BN_DEC_NUM + 1;
  63. bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));
  64. buf = OPENSSL_malloc(tbytes);
  65. if (buf == NULL || bn_data == NULL)
  66. goto err;
  67. if ((t = BN_dup(a)) == NULL)
  68. goto err;
  69. p = buf;
  70. lp = bn_data;
  71. if (BN_is_zero(t)) {
  72. *p++ = '0';
  73. *p++ = '\0';
  74. } else {
  75. if (BN_is_negative(t))
  76. *p++ = '-';
  77. while (!BN_is_zero(t)) {
  78. if (lp - bn_data >= bn_data_num)
  79. goto err;
  80. *lp = BN_div_word(t, BN_DEC_CONV);
  81. if (*lp == (BN_ULONG)-1)
  82. goto err;
  83. lp++;
  84. }
  85. lp--;
  86. /*
  87. * We now have a series of blocks, BN_DEC_NUM chars in length, where
  88. * the last one needs truncation. The blocks need to be reversed in
  89. * order.
  90. */
  91. n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT1, *lp);
  92. if (n < 0)
  93. goto err;
  94. p += n;
  95. while (lp != bn_data) {
  96. lp--;
  97. n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT2, *lp);
  98. if (n < 0)
  99. goto err;
  100. p += n;
  101. }
  102. }
  103. ok = 1;
  104. err:
  105. OPENSSL_free(bn_data);
  106. BN_free(t);
  107. if (ok)
  108. return buf;
  109. OPENSSL_free(buf);
  110. return NULL;
  111. }
  112. #endif
  113. int BN_hex2bn(BIGNUM **bn, const char *a)
  114. {
  115. BIGNUM *ret = NULL;
  116. BN_ULONG l = 0;
  117. int neg = 0, h, m, i, j, k, c;
  118. int num;
  119. if (a == NULL || *a == '\0')
  120. return 0;
  121. if (*a == '-') {
  122. neg = 1;
  123. a++;
  124. }
  125. for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)
  126. continue;
  127. if (i == 0 || i > INT_MAX / 4)
  128. return 0;
  129. num = i + neg;
  130. if (bn == NULL)
  131. return num;
  132. /* a is the start of the hex digits, and it is 'i' long */
  133. if (*bn == NULL) {
  134. if ((ret = BN_new()) == NULL)
  135. return 0;
  136. } else {
  137. ret = *bn;
  138. if (BN_get_flags(ret, BN_FLG_STATIC_DATA)) {
  139. ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
  140. return 0;
  141. }
  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. }