Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

463 rader
11 KiB

  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 <openssl/bio.h>
  62. #include <openssl/bytestring.h>
  63. #include <openssl/err.h>
  64. #include <openssl/mem.h>
  65. #include "../fipsmodule/bn/internal.h"
  66. int BN_bn2cbb_padded(CBB *out, size_t len, const BIGNUM *in) {
  67. uint8_t *ptr;
  68. return CBB_add_space(out, &ptr, len) && BN_bn2bin_padded(ptr, len, in);
  69. }
  70. static const char hextable[] = "0123456789abcdef";
  71. char *BN_bn2hex(const BIGNUM *bn) {
  72. int width = bn_minimal_width(bn);
  73. char *buf = OPENSSL_malloc(1 /* leading '-' */ + 1 /* zero is non-empty */ +
  74. width * BN_BYTES * 2 + 1 /* trailing NUL */);
  75. if (buf == NULL) {
  76. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  77. return NULL;
  78. }
  79. char *p = buf;
  80. if (bn->neg) {
  81. *(p++) = '-';
  82. }
  83. if (BN_is_zero(bn)) {
  84. *(p++) = '0';
  85. }
  86. int z = 0;
  87. for (int i = width - 1; i >= 0; i--) {
  88. for (int j = BN_BITS2 - 8; j >= 0; j -= 8) {
  89. // strip leading zeros
  90. int v = ((int)(bn->d[i] >> (long)j)) & 0xff;
  91. if (z || v != 0) {
  92. *(p++) = hextable[v >> 4];
  93. *(p++) = hextable[v & 0x0f];
  94. z = 1;
  95. }
  96. }
  97. }
  98. *p = '\0';
  99. return buf;
  100. }
  101. // decode_hex decodes |in_len| bytes of hex data from |in| and updates |bn|.
  102. static int decode_hex(BIGNUM *bn, const char *in, int in_len) {
  103. if (in_len > INT_MAX/4) {
  104. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  105. return 0;
  106. }
  107. // |in_len| is the number of hex digits.
  108. if (!bn_expand(bn, in_len * 4)) {
  109. return 0;
  110. }
  111. int i = 0;
  112. while (in_len > 0) {
  113. // Decode one |BN_ULONG| at a time.
  114. int todo = BN_BYTES * 2;
  115. if (todo > in_len) {
  116. todo = in_len;
  117. }
  118. BN_ULONG word = 0;
  119. int j;
  120. for (j = todo; j > 0; j--) {
  121. char c = in[in_len - j];
  122. BN_ULONG hex;
  123. if (c >= '0' && c <= '9') {
  124. hex = c - '0';
  125. } else if (c >= 'a' && c <= 'f') {
  126. hex = c - 'a' + 10;
  127. } else if (c >= 'A' && c <= 'F') {
  128. hex = c - 'A' + 10;
  129. } else {
  130. hex = 0;
  131. // This shouldn't happen. The caller checks |isxdigit|.
  132. assert(0);
  133. }
  134. word = (word << 4) | hex;
  135. }
  136. bn->d[i++] = word;
  137. in_len -= todo;
  138. }
  139. assert(i <= bn->dmax);
  140. bn->width = i;
  141. return 1;
  142. }
  143. // decode_dec decodes |in_len| bytes of decimal data from |in| and updates |bn|.
  144. static int decode_dec(BIGNUM *bn, const char *in, int in_len) {
  145. int i, j;
  146. BN_ULONG l = 0;
  147. // Decode |BN_DEC_NUM| digits at a time.
  148. j = BN_DEC_NUM - (in_len % BN_DEC_NUM);
  149. if (j == BN_DEC_NUM) {
  150. j = 0;
  151. }
  152. l = 0;
  153. for (i = 0; i < in_len; i++) {
  154. l *= 10;
  155. l += in[i] - '0';
  156. if (++j == BN_DEC_NUM) {
  157. if (!BN_mul_word(bn, BN_DEC_CONV) ||
  158. !BN_add_word(bn, l)) {
  159. return 0;
  160. }
  161. l = 0;
  162. j = 0;
  163. }
  164. }
  165. return 1;
  166. }
  167. typedef int (*decode_func) (BIGNUM *bn, const char *in, int in_len);
  168. typedef int (*char_test_func) (int c);
  169. static int bn_x2bn(BIGNUM **outp, const char *in, decode_func decode, char_test_func want_char) {
  170. BIGNUM *ret = NULL;
  171. int neg = 0, i;
  172. int num;
  173. if (in == NULL || *in == 0) {
  174. return 0;
  175. }
  176. if (*in == '-') {
  177. neg = 1;
  178. in++;
  179. }
  180. for (i = 0; want_char((unsigned char)in[i]) && i + neg < INT_MAX; i++) {}
  181. num = i + neg;
  182. if (outp == NULL) {
  183. return num;
  184. }
  185. // in is the start of the hex digits, and it is 'i' long
  186. if (*outp == NULL) {
  187. ret = BN_new();
  188. if (ret == NULL) {
  189. return 0;
  190. }
  191. } else {
  192. ret = *outp;
  193. BN_zero(ret);
  194. }
  195. if (!decode(ret, in, i)) {
  196. goto err;
  197. }
  198. bn_set_minimal_width(ret);
  199. if (!BN_is_zero(ret)) {
  200. ret->neg = neg;
  201. }
  202. *outp = ret;
  203. return num;
  204. err:
  205. if (*outp == NULL) {
  206. BN_free(ret);
  207. }
  208. return 0;
  209. }
  210. int BN_hex2bn(BIGNUM **outp, const char *in) {
  211. return bn_x2bn(outp, in, decode_hex, isxdigit);
  212. }
  213. char *BN_bn2dec(const BIGNUM *a) {
  214. // It is easier to print strings little-endian, so we assemble it in reverse
  215. // and fix at the end.
  216. BIGNUM *copy = NULL;
  217. CBB cbb;
  218. if (!CBB_init(&cbb, 16) ||
  219. !CBB_add_u8(&cbb, 0 /* trailing NUL */)) {
  220. goto cbb_err;
  221. }
  222. if (BN_is_zero(a)) {
  223. if (!CBB_add_u8(&cbb, '0')) {
  224. goto cbb_err;
  225. }
  226. } else {
  227. copy = BN_dup(a);
  228. if (copy == NULL) {
  229. goto err;
  230. }
  231. while (!BN_is_zero(copy)) {
  232. BN_ULONG word = BN_div_word(copy, BN_DEC_CONV);
  233. if (word == (BN_ULONG)-1) {
  234. goto err;
  235. }
  236. const int add_leading_zeros = !BN_is_zero(copy);
  237. for (int i = 0; i < BN_DEC_NUM && (add_leading_zeros || word != 0); i++) {
  238. if (!CBB_add_u8(&cbb, '0' + word % 10)) {
  239. goto cbb_err;
  240. }
  241. word /= 10;
  242. }
  243. assert(word == 0);
  244. }
  245. }
  246. if (BN_is_negative(a) &&
  247. !CBB_add_u8(&cbb, '-')) {
  248. goto cbb_err;
  249. }
  250. uint8_t *data;
  251. size_t len;
  252. if (!CBB_finish(&cbb, &data, &len)) {
  253. goto cbb_err;
  254. }
  255. // Reverse the buffer.
  256. for (size_t i = 0; i < len/2; i++) {
  257. uint8_t tmp = data[i];
  258. data[i] = data[len - 1 - i];
  259. data[len - 1 - i] = tmp;
  260. }
  261. BN_free(copy);
  262. return (char *)data;
  263. cbb_err:
  264. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  265. err:
  266. BN_free(copy);
  267. CBB_cleanup(&cbb);
  268. return NULL;
  269. }
  270. int BN_dec2bn(BIGNUM **outp, const char *in) {
  271. return bn_x2bn(outp, in, decode_dec, isdigit);
  272. }
  273. int BN_asc2bn(BIGNUM **outp, const char *in) {
  274. const char *const orig_in = in;
  275. if (*in == '-') {
  276. in++;
  277. }
  278. if (in[0] == '0' && (in[1] == 'X' || in[1] == 'x')) {
  279. if (!BN_hex2bn(outp, in+2)) {
  280. return 0;
  281. }
  282. } else {
  283. if (!BN_dec2bn(outp, in)) {
  284. return 0;
  285. }
  286. }
  287. if (*orig_in == '-' && !BN_is_zero(*outp)) {
  288. (*outp)->neg = 1;
  289. }
  290. return 1;
  291. }
  292. int BN_print(BIO *bp, const BIGNUM *a) {
  293. int i, j, v, z = 0;
  294. int ret = 0;
  295. if (a->neg && BIO_write(bp, "-", 1) != 1) {
  296. goto end;
  297. }
  298. if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1) {
  299. goto end;
  300. }
  301. for (i = bn_minimal_width(a) - 1; i >= 0; i--) {
  302. for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
  303. // strip leading zeros
  304. v = ((int)(a->d[i] >> (long)j)) & 0x0f;
  305. if (z || v != 0) {
  306. if (BIO_write(bp, &hextable[v], 1) != 1) {
  307. goto end;
  308. }
  309. z = 1;
  310. }
  311. }
  312. }
  313. ret = 1;
  314. end:
  315. return ret;
  316. }
  317. int BN_print_fp(FILE *fp, const BIGNUM *a) {
  318. BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
  319. if (b == NULL) {
  320. return 0;
  321. }
  322. int ret = BN_print(b, a);
  323. BIO_free(b);
  324. return ret;
  325. }
  326. size_t BN_bn2mpi(const BIGNUM *in, uint8_t *out) {
  327. const size_t bits = BN_num_bits(in);
  328. const size_t bytes = (bits + 7) / 8;
  329. // If the number of bits is a multiple of 8, i.e. if the MSB is set,
  330. // prefix with a zero byte.
  331. int extend = 0;
  332. if (bytes != 0 && (bits & 0x07) == 0) {
  333. extend = 1;
  334. }
  335. const size_t len = bytes + extend;
  336. if (len < bytes ||
  337. 4 + len < len ||
  338. (len & 0xffffffff) != len) {
  339. // If we cannot represent the number then we emit zero as the interface
  340. // doesn't allow an error to be signalled.
  341. if (out) {
  342. OPENSSL_memset(out, 0, 4);
  343. }
  344. return 4;
  345. }
  346. if (out == NULL) {
  347. return 4 + len;
  348. }
  349. out[0] = len >> 24;
  350. out[1] = len >> 16;
  351. out[2] = len >> 8;
  352. out[3] = len;
  353. if (extend) {
  354. out[4] = 0;
  355. }
  356. BN_bn2bin(in, out + 4 + extend);
  357. if (in->neg && len > 0) {
  358. out[4] |= 0x80;
  359. }
  360. return len + 4;
  361. }
  362. BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out) {
  363. if (len < 4) {
  364. OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
  365. return NULL;
  366. }
  367. const size_t in_len = ((size_t)in[0] << 24) |
  368. ((size_t)in[1] << 16) |
  369. ((size_t)in[2] << 8) |
  370. ((size_t)in[3]);
  371. if (in_len != len - 4) {
  372. OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
  373. return NULL;
  374. }
  375. int out_is_alloced = 0;
  376. if (out == NULL) {
  377. out = BN_new();
  378. if (out == NULL) {
  379. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  380. return NULL;
  381. }
  382. out_is_alloced = 1;
  383. }
  384. if (in_len == 0) {
  385. BN_zero(out);
  386. return out;
  387. }
  388. in += 4;
  389. if (BN_bin2bn(in, in_len, out) == NULL) {
  390. if (out_is_alloced) {
  391. BN_free(out);
  392. }
  393. return NULL;
  394. }
  395. out->neg = ((*in) & 0x80) != 0;
  396. if (out->neg) {
  397. BN_clear_bit(out, BN_num_bits(out) - 1);
  398. }
  399. return out;
  400. }