bn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 <limits.h>
  58. #include <string.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include "internal.h"
  62. #include "../delocate.h"
  63. BIGNUM *BN_new(void) {
  64. BIGNUM *bn = OPENSSL_malloc(sizeof(BIGNUM));
  65. if (bn == NULL) {
  66. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  67. return NULL;
  68. }
  69. OPENSSL_memset(bn, 0, sizeof(BIGNUM));
  70. bn->flags = BN_FLG_MALLOCED;
  71. return bn;
  72. }
  73. void BN_init(BIGNUM *bn) {
  74. OPENSSL_memset(bn, 0, sizeof(BIGNUM));
  75. }
  76. void BN_free(BIGNUM *bn) {
  77. if (bn == NULL) {
  78. return;
  79. }
  80. if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
  81. OPENSSL_free(bn->d);
  82. }
  83. if (bn->flags & BN_FLG_MALLOCED) {
  84. OPENSSL_free(bn);
  85. } else {
  86. bn->d = NULL;
  87. }
  88. }
  89. void BN_clear_free(BIGNUM *bn) {
  90. char should_free;
  91. if (bn == NULL) {
  92. return;
  93. }
  94. if (bn->d != NULL) {
  95. if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
  96. OPENSSL_free(bn->d);
  97. } else {
  98. OPENSSL_cleanse(bn->d, bn->dmax * sizeof(bn->d[0]));
  99. }
  100. }
  101. should_free = (bn->flags & BN_FLG_MALLOCED) != 0;
  102. if (should_free) {
  103. OPENSSL_free(bn);
  104. } else {
  105. OPENSSL_cleanse(bn, sizeof(BIGNUM));
  106. }
  107. }
  108. BIGNUM *BN_dup(const BIGNUM *src) {
  109. BIGNUM *copy;
  110. if (src == NULL) {
  111. return NULL;
  112. }
  113. copy = BN_new();
  114. if (copy == NULL) {
  115. return NULL;
  116. }
  117. if (!BN_copy(copy, src)) {
  118. BN_free(copy);
  119. return NULL;
  120. }
  121. return copy;
  122. }
  123. BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) {
  124. if (src == dest) {
  125. return dest;
  126. }
  127. if (!bn_wexpand(dest, src->width)) {
  128. return NULL;
  129. }
  130. OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->width);
  131. dest->width = src->width;
  132. dest->neg = src->neg;
  133. return dest;
  134. }
  135. void BN_clear(BIGNUM *bn) {
  136. if (bn->d != NULL) {
  137. OPENSSL_memset(bn->d, 0, bn->dmax * sizeof(bn->d[0]));
  138. }
  139. bn->width = 0;
  140. bn->neg = 0;
  141. }
  142. DEFINE_METHOD_FUNCTION(BIGNUM, BN_value_one) {
  143. static const BN_ULONG kOneLimbs[1] = { 1 };
  144. out->d = (BN_ULONG*) kOneLimbs;
  145. out->width = 1;
  146. out->dmax = 1;
  147. out->neg = 0;
  148. out->flags = BN_FLG_STATIC_DATA;
  149. }
  150. // BN_num_bits_word returns the minimum number of bits needed to represent the
  151. // value in |l|.
  152. unsigned BN_num_bits_word(BN_ULONG l) {
  153. // |BN_num_bits| is often called on RSA prime factors. These have public bit
  154. // lengths, but all bits beyond the high bit are secret, so count bits in
  155. // constant time.
  156. BN_ULONG x, mask;
  157. int bits = (l != 0);
  158. #if BN_BITS2 > 32
  159. // Look at the upper half of |x|. |x| is at most 64 bits long.
  160. x = l >> 32;
  161. // Set |mask| to all ones if |x| (the top 32 bits of |l|) is non-zero and all
  162. // all zeros otherwise.
  163. mask = 0u - x;
  164. mask = (0u - (mask >> (BN_BITS2 - 1)));
  165. // If |x| is non-zero, the lower half is included in the bit count in full,
  166. // and we count the upper half. Otherwise, we count the lower half.
  167. bits += 32 & mask;
  168. l ^= (x ^ l) & mask; // |l| is |x| if |mask| and remains |l| otherwise.
  169. #endif
  170. // The remaining blocks are analogous iterations at lower powers of two.
  171. x = l >> 16;
  172. mask = 0u - x;
  173. mask = (0u - (mask >> (BN_BITS2 - 1)));
  174. bits += 16 & mask;
  175. l ^= (x ^ l) & mask;
  176. x = l >> 8;
  177. mask = 0u - x;
  178. mask = (0u - (mask >> (BN_BITS2 - 1)));
  179. bits += 8 & mask;
  180. l ^= (x ^ l) & mask;
  181. x = l >> 4;
  182. mask = 0u - x;
  183. mask = (0u - (mask >> (BN_BITS2 - 1)));
  184. bits += 4 & mask;
  185. l ^= (x ^ l) & mask;
  186. x = l >> 2;
  187. mask = 0u - x;
  188. mask = (0u - (mask >> (BN_BITS2 - 1)));
  189. bits += 2 & mask;
  190. l ^= (x ^ l) & mask;
  191. x = l >> 1;
  192. mask = 0u - x;
  193. mask = (0u - (mask >> (BN_BITS2 - 1)));
  194. bits += 1 & mask;
  195. return bits;
  196. }
  197. unsigned BN_num_bits(const BIGNUM *bn) {
  198. const int width = bn_minimal_width(bn);
  199. if (width == 0) {
  200. return 0;
  201. }
  202. return (width - 1) * BN_BITS2 + BN_num_bits_word(bn->d[width - 1]);
  203. }
  204. unsigned BN_num_bytes(const BIGNUM *bn) {
  205. return (BN_num_bits(bn) + 7) / 8;
  206. }
  207. void BN_zero(BIGNUM *bn) {
  208. bn->width = bn->neg = 0;
  209. }
  210. int BN_one(BIGNUM *bn) {
  211. return BN_set_word(bn, 1);
  212. }
  213. int BN_set_word(BIGNUM *bn, BN_ULONG value) {
  214. if (value == 0) {
  215. BN_zero(bn);
  216. return 1;
  217. }
  218. if (!bn_wexpand(bn, 1)) {
  219. return 0;
  220. }
  221. bn->neg = 0;
  222. bn->d[0] = value;
  223. bn->width = 1;
  224. return 1;
  225. }
  226. int BN_set_u64(BIGNUM *bn, uint64_t value) {
  227. #if BN_BITS2 == 64
  228. return BN_set_word(bn, value);
  229. #elif BN_BITS2 == 32
  230. if (value <= BN_MASK2) {
  231. return BN_set_word(bn, (BN_ULONG)value);
  232. }
  233. if (!bn_wexpand(bn, 2)) {
  234. return 0;
  235. }
  236. bn->neg = 0;
  237. bn->d[0] = (BN_ULONG)value;
  238. bn->d[1] = (BN_ULONG)(value >> 32);
  239. bn->width = 2;
  240. return 1;
  241. #else
  242. #error "BN_BITS2 must be 32 or 64."
  243. #endif
  244. }
  245. int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
  246. if (!bn_wexpand(bn, num)) {
  247. return 0;
  248. }
  249. OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG));
  250. // |bn_wexpand| verified that |num| isn't too large.
  251. bn->width = (int)num;
  252. bn->neg = 0;
  253. return 1;
  254. }
  255. int bn_fits_in_words(const BIGNUM *bn, size_t num) {
  256. // All words beyond |num| must be zero.
  257. BN_ULONG mask = 0;
  258. for (size_t i = num; i < (size_t)bn->width; i++) {
  259. mask |= bn->d[i];
  260. }
  261. return mask == 0;
  262. }
  263. int bn_copy_words(BN_ULONG *out, size_t num, const BIGNUM *bn) {
  264. if (bn->neg) {
  265. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  266. return 0;
  267. }
  268. size_t width = (size_t)bn->width;
  269. if (width > num) {
  270. if (!bn_fits_in_words(bn, num)) {
  271. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  272. return 0;
  273. }
  274. width = num;
  275. }
  276. OPENSSL_memset(out, 0, sizeof(BN_ULONG) * num);
  277. OPENSSL_memcpy(out, bn->d, sizeof(BN_ULONG) * width);
  278. return 1;
  279. }
  280. int BN_is_negative(const BIGNUM *bn) {
  281. return bn->neg != 0;
  282. }
  283. void BN_set_negative(BIGNUM *bn, int sign) {
  284. if (sign && !BN_is_zero(bn)) {
  285. bn->neg = 1;
  286. } else {
  287. bn->neg = 0;
  288. }
  289. }
  290. int bn_wexpand(BIGNUM *bn, size_t words) {
  291. BN_ULONG *a;
  292. if (words <= (size_t)bn->dmax) {
  293. return 1;
  294. }
  295. if (words > (INT_MAX / (4 * BN_BITS2))) {
  296. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  297. return 0;
  298. }
  299. if (bn->flags & BN_FLG_STATIC_DATA) {
  300. OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  301. return 0;
  302. }
  303. a = OPENSSL_malloc(sizeof(BN_ULONG) * words);
  304. if (a == NULL) {
  305. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  306. return 0;
  307. }
  308. OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->width);
  309. OPENSSL_free(bn->d);
  310. bn->d = a;
  311. bn->dmax = (int)words;
  312. return 1;
  313. }
  314. int bn_expand(BIGNUM *bn, size_t bits) {
  315. if (bits + BN_BITS2 - 1 < bits) {
  316. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  317. return 0;
  318. }
  319. return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2);
  320. }
  321. int bn_resize_words(BIGNUM *bn, size_t words) {
  322. if ((size_t)bn->width <= words) {
  323. if (!bn_wexpand(bn, words)) {
  324. return 0;
  325. }
  326. OPENSSL_memset(bn->d + bn->width, 0,
  327. (words - bn->width) * sizeof(BN_ULONG));
  328. bn->width = words;
  329. return 1;
  330. }
  331. // All words beyond the new width must be zero.
  332. if (!bn_fits_in_words(bn, words)) {
  333. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  334. return 0;
  335. }
  336. bn->width = words;
  337. return 1;
  338. }
  339. void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a,
  340. const BN_ULONG *b, size_t num) {
  341. for (size_t i = 0; i < num; i++) {
  342. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
  343. crypto_word_t_too_small);
  344. r[i] = constant_time_select_w(mask, a[i], b[i]);
  345. }
  346. }
  347. int bn_minimal_width(const BIGNUM *bn) {
  348. int ret = bn->width;
  349. while (ret > 0 && bn->d[ret - 1] == 0) {
  350. ret--;
  351. }
  352. return ret;
  353. }
  354. void bn_set_minimal_width(BIGNUM *bn) {
  355. bn->width = bn_minimal_width(bn);
  356. if (bn->width == 0) {
  357. bn->neg = 0;
  358. }
  359. }