bn_conv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. ERR_raise(ERR_LIB_BN, 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_MODULE
  45. /* No BIO_snprintf in FIPS_MODULE */
  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. ERR_raise(ERR_LIB_BN, 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. return 0;
  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. if (BN_get_flags(ret, BN_FLG_STATIC_DATA)) {
  143. ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
  144. return 0;
  145. }
  146. BN_zero(ret);
  147. }
  148. /* i is the number of hex digits */
  149. if (bn_expand(ret, i * 4) == NULL)
  150. goto err;
  151. j = i; /* least significant 'hex' */
  152. m = 0;
  153. h = 0;
  154. while (j > 0) {
  155. m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;
  156. l = 0;
  157. for (;;) {
  158. c = a[j - m];
  159. k = OPENSSL_hexchar2int(c);
  160. if (k < 0)
  161. k = 0; /* paranoia */
  162. l = (l << 4) | k;
  163. if (--m <= 0) {
  164. ret->d[h++] = l;
  165. break;
  166. }
  167. }
  168. j -= BN_BYTES * 2;
  169. }
  170. ret->top = h;
  171. bn_correct_top(ret);
  172. *bn = ret;
  173. bn_check_top(ret);
  174. /* Don't set the negative flag if it's zero. */
  175. if (ret->top != 0)
  176. ret->neg = neg;
  177. return num;
  178. err:
  179. if (*bn == NULL)
  180. BN_free(ret);
  181. return 0;
  182. }
  183. int BN_dec2bn(BIGNUM **bn, const char *a)
  184. {
  185. BIGNUM *ret = NULL;
  186. BN_ULONG l = 0;
  187. int neg = 0, i, j;
  188. int num;
  189. if (a == NULL || *a == '\0')
  190. return 0;
  191. if (*a == '-') {
  192. neg = 1;
  193. a++;
  194. }
  195. for (i = 0; i <= INT_MAX / 4 && ossl_isdigit(a[i]); i++)
  196. continue;
  197. if (i == 0 || i > INT_MAX / 4)
  198. goto err;
  199. num = i + neg;
  200. if (bn == NULL)
  201. return num;
  202. /*
  203. * a is the start of the digits, and it is 'i' long. We chop it into
  204. * BN_DEC_NUM digits at a time
  205. */
  206. if (*bn == NULL) {
  207. if ((ret = BN_new()) == NULL)
  208. return 0;
  209. } else {
  210. ret = *bn;
  211. BN_zero(ret);
  212. }
  213. /* i is the number of digits, a bit of an over expand */
  214. if (bn_expand(ret, i * 4) == NULL)
  215. goto err;
  216. j = BN_DEC_NUM - i % BN_DEC_NUM;
  217. if (j == BN_DEC_NUM)
  218. j = 0;
  219. l = 0;
  220. while (--i >= 0) {
  221. l *= 10;
  222. l += *a - '0';
  223. a++;
  224. if (++j == BN_DEC_NUM) {
  225. if (!BN_mul_word(ret, BN_DEC_CONV)
  226. || !BN_add_word(ret, l))
  227. goto err;
  228. l = 0;
  229. j = 0;
  230. }
  231. }
  232. bn_correct_top(ret);
  233. *bn = ret;
  234. bn_check_top(ret);
  235. /* Don't set the negative flag if it's zero. */
  236. if (ret->top != 0)
  237. ret->neg = neg;
  238. return num;
  239. err:
  240. if (*bn == NULL)
  241. BN_free(ret);
  242. return 0;
  243. }
  244. int BN_asc2bn(BIGNUM **bn, const char *a)
  245. {
  246. const char *p = a;
  247. if (*p == '-')
  248. p++;
  249. if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
  250. if (!BN_hex2bn(bn, p + 2))
  251. return 0;
  252. } else {
  253. if (!BN_dec2bn(bn, p))
  254. return 0;
  255. }
  256. /* Don't set the negative flag if it's zero. */
  257. if (*a == '-' && (*bn)->top != 0)
  258. (*bn)->neg = 1;
  259. return 1;
  260. }