convert.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/bn.h>
  57. #include <assert.h>
  58. #include <ctype.h>
  59. #include <limits.h>
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include <openssl/bio.h>
  63. #include <openssl/bytestring.h>
  64. #include <openssl/err.h>
  65. #include <openssl/mem.h>
  66. #include "internal.h"
  67. BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
  68. size_t num_words;
  69. unsigned m;
  70. BN_ULONG word = 0;
  71. BIGNUM *bn = NULL;
  72. if (ret == NULL) {
  73. ret = bn = BN_new();
  74. }
  75. if (ret == NULL) {
  76. return NULL;
  77. }
  78. if (len == 0) {
  79. ret->top = 0;
  80. return ret;
  81. }
  82. num_words = ((len - 1) / BN_BYTES) + 1;
  83. m = (len - 1) % BN_BYTES;
  84. if (bn_wexpand(ret, num_words) == NULL) {
  85. if (bn) {
  86. BN_free(bn);
  87. }
  88. return NULL;
  89. }
  90. /* |bn_wexpand| must check bounds on |num_words| to write it into
  91. * |ret->dmax|. */
  92. assert(num_words <= INT_MAX);
  93. ret->top = (int)num_words;
  94. ret->neg = 0;
  95. while (len--) {
  96. word = (word << 8) | *(in++);
  97. if (m-- == 0) {
  98. ret->d[--num_words] = word;
  99. word = 0;
  100. m = BN_BYTES - 1;
  101. }
  102. }
  103. /* need to call this due to clear byte at top if avoiding having the top bit
  104. * set (-ve number) */
  105. bn_correct_top(ret);
  106. return ret;
  107. }
  108. BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
  109. BIGNUM *bn = NULL;
  110. if (ret == NULL) {
  111. bn = BN_new();
  112. ret = bn;
  113. }
  114. if (ret == NULL) {
  115. return NULL;
  116. }
  117. if (len == 0) {
  118. ret->top = 0;
  119. ret->neg = 0;
  120. return ret;
  121. }
  122. /* Reserve enough space in |ret|. */
  123. size_t num_words = ((len - 1) / BN_BYTES) + 1;
  124. if (!bn_wexpand(ret, num_words)) {
  125. BN_free(bn);
  126. return NULL;
  127. }
  128. ret->top = num_words;
  129. /* Make sure the top bytes will be zeroed. */
  130. ret->d[num_words - 1] = 0;
  131. /* We only support little-endian platforms, so we can simply memcpy the
  132. * internal representation. */
  133. OPENSSL_memcpy(ret->d, in, len);
  134. bn_correct_top(ret);
  135. return ret;
  136. }
  137. size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
  138. size_t n, i;
  139. BN_ULONG l;
  140. n = i = BN_num_bytes(in);
  141. while (i--) {
  142. l = in->d[i / BN_BYTES];
  143. *(out++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
  144. }
  145. return n;
  146. }
  147. int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
  148. /* If we don't have enough space, fail out. */
  149. size_t num_bytes = BN_num_bytes(in);
  150. if (len < num_bytes) {
  151. return 0;
  152. }
  153. /* We only support little-endian platforms, so we can simply memcpy into the
  154. * internal representation. */
  155. OPENSSL_memcpy(out, in->d, num_bytes);
  156. /* Pad out the rest of the buffer with zeroes. */
  157. OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
  158. return 1;
  159. }
  160. /* constant_time_select_ulong returns |x| if |v| is 1 and |y| if |v| is 0. Its
  161. * behavior is undefined if |v| takes any other value. */
  162. static BN_ULONG constant_time_select_ulong(int v, BN_ULONG x, BN_ULONG y) {
  163. BN_ULONG mask = v;
  164. mask--;
  165. return (~mask & x) | (mask & y);
  166. }
  167. /* constant_time_le_size_t returns 1 if |x| <= |y| and 0 otherwise. |x| and |y|
  168. * must not have their MSBs set. */
  169. static int constant_time_le_size_t(size_t x, size_t y) {
  170. return ((x - y - 1) >> (sizeof(size_t) * 8 - 1)) & 1;
  171. }
  172. /* read_word_padded returns the |i|'th word of |in|, if it is not out of
  173. * bounds. Otherwise, it returns 0. It does so without branches on the size of
  174. * |in|, however it necessarily does not have the same memory access pattern. If
  175. * the access would be out of bounds, it reads the last word of |in|. |in| must
  176. * not be zero. */
  177. static BN_ULONG read_word_padded(const BIGNUM *in, size_t i) {
  178. /* Read |in->d[i]| if valid. Otherwise, read the last word. */
  179. BN_ULONG l = in->d[constant_time_select_ulong(
  180. constant_time_le_size_t(in->dmax, i), in->dmax - 1, i)];
  181. /* Clamp to zero if above |d->top|. */
  182. return constant_time_select_ulong(constant_time_le_size_t(in->top, i), 0, l);
  183. }
  184. int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
  185. /* Special case for |in| = 0. Just branch as the probability is negligible. */
  186. if (BN_is_zero(in)) {
  187. OPENSSL_memset(out, 0, len);
  188. return 1;
  189. }
  190. /* Check if the integer is too big. This case can exit early in non-constant
  191. * time. */
  192. if ((size_t)in->top > (len + (BN_BYTES - 1)) / BN_BYTES) {
  193. return 0;
  194. }
  195. if ((len % BN_BYTES) != 0) {
  196. BN_ULONG l = read_word_padded(in, len / BN_BYTES);
  197. if (l >> (8 * (len % BN_BYTES)) != 0) {
  198. return 0;
  199. }
  200. }
  201. /* Write the bytes out one by one. Serialization is done without branching on
  202. * the bits of |in| or on |in->top|, but if the routine would otherwise read
  203. * out of bounds, the memory access pattern can't be fixed. However, for an
  204. * RSA key of size a multiple of the word size, the probability of BN_BYTES
  205. * leading zero octets is low.
  206. *
  207. * See Falko Stenzke, "Manger's Attack revisited", ICICS 2010. */
  208. size_t i = len;
  209. while (i--) {
  210. BN_ULONG l = read_word_padded(in, i / BN_BYTES);
  211. *(out++) = (uint8_t)(l >> (8 * (i % BN_BYTES))) & 0xff;
  212. }
  213. return 1;
  214. }
  215. int BN_bn2cbb_padded(CBB *out, size_t len, const BIGNUM *in) {
  216. uint8_t *ptr;
  217. return CBB_add_space(out, &ptr, len) && BN_bn2bin_padded(ptr, len, in);
  218. }
  219. static const char hextable[] = "0123456789abcdef";
  220. char *BN_bn2hex(const BIGNUM *bn) {
  221. char *buf = OPENSSL_malloc(1 /* leading '-' */ + 1 /* zero is non-empty */ +
  222. bn->top * BN_BYTES * 2 + 1 /* trailing NUL */);
  223. if (buf == NULL) {
  224. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  225. return NULL;
  226. }
  227. char *p = buf;
  228. if (bn->neg) {
  229. *(p++) = '-';
  230. }
  231. if (BN_is_zero(bn)) {
  232. *(p++) = '0';
  233. }
  234. int z = 0;
  235. for (int i = bn->top - 1; i >= 0; i--) {
  236. for (int j = BN_BITS2 - 8; j >= 0; j -= 8) {
  237. /* strip leading zeros */
  238. int v = ((int)(bn->d[i] >> (long)j)) & 0xff;
  239. if (z || v != 0) {
  240. *(p++) = hextable[v >> 4];
  241. *(p++) = hextable[v & 0x0f];
  242. z = 1;
  243. }
  244. }
  245. }
  246. *p = '\0';
  247. return buf;
  248. }
  249. /* decode_hex decodes |in_len| bytes of hex data from |in| and updates |bn|. */
  250. static int decode_hex(BIGNUM *bn, const char *in, int in_len) {
  251. if (in_len > INT_MAX/4) {
  252. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  253. return 0;
  254. }
  255. /* |in_len| is the number of hex digits. */
  256. if (bn_expand(bn, in_len * 4) == NULL) {
  257. return 0;
  258. }
  259. int i = 0;
  260. while (in_len > 0) {
  261. /* Decode one |BN_ULONG| at a time. */
  262. int todo = BN_BYTES * 2;
  263. if (todo > in_len) {
  264. todo = in_len;
  265. }
  266. BN_ULONG word = 0;
  267. int j;
  268. for (j = todo; j > 0; j--) {
  269. char c = in[in_len - j];
  270. BN_ULONG hex;
  271. if (c >= '0' && c <= '9') {
  272. hex = c - '0';
  273. } else if (c >= 'a' && c <= 'f') {
  274. hex = c - 'a' + 10;
  275. } else if (c >= 'A' && c <= 'F') {
  276. hex = c - 'A' + 10;
  277. } else {
  278. hex = 0;
  279. /* This shouldn't happen. The caller checks |isxdigit|. */
  280. assert(0);
  281. }
  282. word = (word << 4) | hex;
  283. }
  284. bn->d[i++] = word;
  285. in_len -= todo;
  286. }
  287. assert(i <= bn->dmax);
  288. bn->top = i;
  289. return 1;
  290. }
  291. /* decode_dec decodes |in_len| bytes of decimal data from |in| and updates |bn|. */
  292. static int decode_dec(BIGNUM *bn, const char *in, int in_len) {
  293. int i, j;
  294. BN_ULONG l = 0;
  295. /* Decode |BN_DEC_NUM| digits at a time. */
  296. j = BN_DEC_NUM - (in_len % BN_DEC_NUM);
  297. if (j == BN_DEC_NUM) {
  298. j = 0;
  299. }
  300. l = 0;
  301. for (i = 0; i < in_len; i++) {
  302. l *= 10;
  303. l += in[i] - '0';
  304. if (++j == BN_DEC_NUM) {
  305. if (!BN_mul_word(bn, BN_DEC_CONV) ||
  306. !BN_add_word(bn, l)) {
  307. return 0;
  308. }
  309. l = 0;
  310. j = 0;
  311. }
  312. }
  313. return 1;
  314. }
  315. typedef int (*decode_func) (BIGNUM *bn, const char *in, int in_len);
  316. typedef int (*char_test_func) (int c);
  317. static int bn_x2bn(BIGNUM **outp, const char *in, decode_func decode, char_test_func want_char) {
  318. BIGNUM *ret = NULL;
  319. int neg = 0, i;
  320. int num;
  321. if (in == NULL || *in == 0) {
  322. return 0;
  323. }
  324. if (*in == '-') {
  325. neg = 1;
  326. in++;
  327. }
  328. for (i = 0; want_char((unsigned char)in[i]) && i + neg < INT_MAX; i++) {}
  329. num = i + neg;
  330. if (outp == NULL) {
  331. return num;
  332. }
  333. /* in is the start of the hex digits, and it is 'i' long */
  334. if (*outp == NULL) {
  335. ret = BN_new();
  336. if (ret == NULL) {
  337. return 0;
  338. }
  339. } else {
  340. ret = *outp;
  341. BN_zero(ret);
  342. }
  343. if (!decode(ret, in, i)) {
  344. goto err;
  345. }
  346. bn_correct_top(ret);
  347. if (!BN_is_zero(ret)) {
  348. ret->neg = neg;
  349. }
  350. *outp = ret;
  351. return num;
  352. err:
  353. if (*outp == NULL) {
  354. BN_free(ret);
  355. }
  356. return 0;
  357. }
  358. int BN_hex2bn(BIGNUM **outp, const char *in) {
  359. return bn_x2bn(outp, in, decode_hex, isxdigit);
  360. }
  361. char *BN_bn2dec(const BIGNUM *a) {
  362. /* It is easier to print strings little-endian, so we assemble it in reverse
  363. * and fix at the end. */
  364. BIGNUM *copy = NULL;
  365. CBB cbb;
  366. if (!CBB_init(&cbb, 16) ||
  367. !CBB_add_u8(&cbb, 0 /* trailing NUL */)) {
  368. goto cbb_err;
  369. }
  370. if (BN_is_zero(a)) {
  371. if (!CBB_add_u8(&cbb, '0')) {
  372. goto cbb_err;
  373. }
  374. } else {
  375. copy = BN_dup(a);
  376. if (copy == NULL) {
  377. goto err;
  378. }
  379. while (!BN_is_zero(copy)) {
  380. BN_ULONG word = BN_div_word(copy, BN_DEC_CONV);
  381. if (word == (BN_ULONG)-1) {
  382. goto err;
  383. }
  384. const int add_leading_zeros = !BN_is_zero(copy);
  385. for (int i = 0; i < BN_DEC_NUM && (add_leading_zeros || word != 0); i++) {
  386. if (!CBB_add_u8(&cbb, '0' + word % 10)) {
  387. goto cbb_err;
  388. }
  389. word /= 10;
  390. }
  391. assert(word == 0);
  392. }
  393. }
  394. if (BN_is_negative(a) &&
  395. !CBB_add_u8(&cbb, '-')) {
  396. goto cbb_err;
  397. }
  398. uint8_t *data;
  399. size_t len;
  400. if (!CBB_finish(&cbb, &data, &len)) {
  401. goto cbb_err;
  402. }
  403. /* Reverse the buffer. */
  404. for (size_t i = 0; i < len/2; i++) {
  405. uint8_t tmp = data[i];
  406. data[i] = data[len - 1 - i];
  407. data[len - 1 - i] = tmp;
  408. }
  409. BN_free(copy);
  410. return (char *)data;
  411. cbb_err:
  412. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  413. err:
  414. BN_free(copy);
  415. CBB_cleanup(&cbb);
  416. return NULL;
  417. }
  418. int BN_dec2bn(BIGNUM **outp, const char *in) {
  419. return bn_x2bn(outp, in, decode_dec, isdigit);
  420. }
  421. int BN_asc2bn(BIGNUM **outp, const char *in) {
  422. const char *const orig_in = in;
  423. if (*in == '-') {
  424. in++;
  425. }
  426. if (in[0] == '0' && (in[1] == 'X' || in[1] == 'x')) {
  427. if (!BN_hex2bn(outp, in+2)) {
  428. return 0;
  429. }
  430. } else {
  431. if (!BN_dec2bn(outp, in)) {
  432. return 0;
  433. }
  434. }
  435. if (*orig_in == '-' && !BN_is_zero(*outp)) {
  436. (*outp)->neg = 1;
  437. }
  438. return 1;
  439. }
  440. int BN_print(BIO *bp, const BIGNUM *a) {
  441. int i, j, v, z = 0;
  442. int ret = 0;
  443. if (a->neg && BIO_write(bp, "-", 1) != 1) {
  444. goto end;
  445. }
  446. if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1) {
  447. goto end;
  448. }
  449. for (i = a->top - 1; i >= 0; i--) {
  450. for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
  451. /* strip leading zeros */
  452. v = ((int)(a->d[i] >> (long)j)) & 0x0f;
  453. if (z || v != 0) {
  454. if (BIO_write(bp, &hextable[v], 1) != 1) {
  455. goto end;
  456. }
  457. z = 1;
  458. }
  459. }
  460. }
  461. ret = 1;
  462. end:
  463. return ret;
  464. }
  465. int BN_print_fp(FILE *fp, const BIGNUM *a) {
  466. BIO *b;
  467. int ret;
  468. b = BIO_new(BIO_s_file());
  469. if (b == NULL) {
  470. return 0;
  471. }
  472. BIO_set_fp(b, fp, BIO_NOCLOSE);
  473. ret = BN_print(b, a);
  474. BIO_free(b);
  475. return ret;
  476. }
  477. BN_ULONG BN_get_word(const BIGNUM *bn) {
  478. switch (bn->top) {
  479. case 0:
  480. return 0;
  481. case 1:
  482. return bn->d[0];
  483. default:
  484. return BN_MASK2;
  485. }
  486. }
  487. int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
  488. switch (bn->top) {
  489. case 0:
  490. *out = 0;
  491. return 1;
  492. case 1:
  493. *out = bn->d[0];
  494. return 1;
  495. #if defined(OPENSSL_32_BIT)
  496. case 2:
  497. *out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32);
  498. return 1;
  499. #endif
  500. default:
  501. return 0;
  502. }
  503. }
  504. size_t BN_bn2mpi(const BIGNUM *in, uint8_t *out) {
  505. const size_t bits = BN_num_bits(in);
  506. const size_t bytes = (bits + 7) / 8;
  507. /* If the number of bits is a multiple of 8, i.e. if the MSB is set,
  508. * prefix with a zero byte. */
  509. int extend = 0;
  510. if (bytes != 0 && (bits & 0x07) == 0) {
  511. extend = 1;
  512. }
  513. const size_t len = bytes + extend;
  514. if (len < bytes ||
  515. 4 + len < len ||
  516. (len & 0xffffffff) != len) {
  517. /* If we cannot represent the number then we emit zero as the interface
  518. * doesn't allow an error to be signalled. */
  519. if (out) {
  520. OPENSSL_memset(out, 0, 4);
  521. }
  522. return 4;
  523. }
  524. if (out == NULL) {
  525. return 4 + len;
  526. }
  527. out[0] = len >> 24;
  528. out[1] = len >> 16;
  529. out[2] = len >> 8;
  530. out[3] = len;
  531. if (extend) {
  532. out[4] = 0;
  533. }
  534. BN_bn2bin(in, out + 4 + extend);
  535. if (in->neg && len > 0) {
  536. out[4] |= 0x80;
  537. }
  538. return len + 4;
  539. }
  540. BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out) {
  541. if (len < 4) {
  542. OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
  543. return NULL;
  544. }
  545. const size_t in_len = ((size_t)in[0] << 24) |
  546. ((size_t)in[1] << 16) |
  547. ((size_t)in[2] << 8) |
  548. ((size_t)in[3]);
  549. if (in_len != len - 4) {
  550. OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
  551. return NULL;
  552. }
  553. int out_is_alloced = 0;
  554. if (out == NULL) {
  555. out = BN_new();
  556. if (out == NULL) {
  557. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  558. return NULL;
  559. }
  560. out_is_alloced = 1;
  561. }
  562. if (in_len == 0) {
  563. BN_zero(out);
  564. return out;
  565. }
  566. in += 4;
  567. if (BN_bin2bn(in, in_len, out) == NULL) {
  568. if (out_is_alloced) {
  569. BN_free(out);
  570. }
  571. return NULL;
  572. }
  573. out->neg = ((*in) & 0x80) != 0;
  574. if (out->neg) {
  575. BN_clear_bit(out, BN_num_bits(out) - 1);
  576. }
  577. return out;
  578. }