bn.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. BIGNUM *BN_new(void) {
  63. BIGNUM *bn = OPENSSL_malloc(sizeof(BIGNUM));
  64. if (bn == NULL) {
  65. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  66. return NULL;
  67. }
  68. OPENSSL_memset(bn, 0, sizeof(BIGNUM));
  69. bn->flags = BN_FLG_MALLOCED;
  70. return bn;
  71. }
  72. void BN_init(BIGNUM *bn) {
  73. OPENSSL_memset(bn, 0, sizeof(BIGNUM));
  74. }
  75. void BN_free(BIGNUM *bn) {
  76. if (bn == NULL) {
  77. return;
  78. }
  79. if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
  80. OPENSSL_free(bn->d);
  81. }
  82. if (bn->flags & BN_FLG_MALLOCED) {
  83. OPENSSL_free(bn);
  84. } else {
  85. bn->d = NULL;
  86. }
  87. }
  88. void BN_clear_free(BIGNUM *bn) {
  89. char should_free;
  90. if (bn == NULL) {
  91. return;
  92. }
  93. if (bn->d != NULL) {
  94. OPENSSL_cleanse(bn->d, bn->dmax * sizeof(bn->d[0]));
  95. if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
  96. OPENSSL_free(bn->d);
  97. }
  98. }
  99. should_free = (bn->flags & BN_FLG_MALLOCED) != 0;
  100. OPENSSL_cleanse(bn, sizeof(BIGNUM));
  101. if (should_free) {
  102. OPENSSL_free(bn);
  103. }
  104. }
  105. BIGNUM *BN_dup(const BIGNUM *src) {
  106. BIGNUM *copy;
  107. if (src == NULL) {
  108. return NULL;
  109. }
  110. copy = BN_new();
  111. if (copy == NULL) {
  112. return NULL;
  113. }
  114. if (!BN_copy(copy, src)) {
  115. BN_free(copy);
  116. return NULL;
  117. }
  118. return copy;
  119. }
  120. BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) {
  121. if (src == dest) {
  122. return dest;
  123. }
  124. if (bn_wexpand(dest, src->top) == NULL) {
  125. return NULL;
  126. }
  127. OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->top);
  128. dest->top = src->top;
  129. dest->neg = src->neg;
  130. return dest;
  131. }
  132. void BN_clear(BIGNUM *bn) {
  133. if (bn->d != NULL) {
  134. OPENSSL_memset(bn->d, 0, bn->dmax * sizeof(bn->d[0]));
  135. }
  136. bn->top = 0;
  137. bn->neg = 0;
  138. }
  139. const BIGNUM *BN_value_one(void) {
  140. static const BN_ULONG kOneLimbs[1] = { 1 };
  141. static const BIGNUM kOne = STATIC_BIGNUM(kOneLimbs);
  142. return &kOne;
  143. }
  144. /* BN_num_bits_word returns the minimum number of bits needed to represent the
  145. * value in |l|. */
  146. unsigned BN_num_bits_word(BN_ULONG l) {
  147. static const unsigned char bits[256] = {
  148. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  149. 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  150. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  151. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  152. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  153. 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  154. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  155. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  156. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  157. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  158. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
  159. #if defined(OPENSSL_64_BIT)
  160. if (l & 0xffffffff00000000L) {
  161. if (l & 0xffff000000000000L) {
  162. if (l & 0xff00000000000000L) {
  163. return (bits[(int)(l >> 56)] + 56);
  164. } else {
  165. return (bits[(int)(l >> 48)] + 48);
  166. }
  167. } else {
  168. if (l & 0x0000ff0000000000L) {
  169. return (bits[(int)(l >> 40)] + 40);
  170. } else {
  171. return (bits[(int)(l >> 32)] + 32);
  172. }
  173. }
  174. } else
  175. #endif
  176. {
  177. if (l & 0xffff0000L) {
  178. if (l & 0xff000000L) {
  179. return (bits[(int)(l >> 24L)] + 24);
  180. } else {
  181. return (bits[(int)(l >> 16L)] + 16);
  182. }
  183. } else {
  184. if (l & 0xff00L) {
  185. return (bits[(int)(l >> 8)] + 8);
  186. } else {
  187. return (bits[(int)(l)]);
  188. }
  189. }
  190. }
  191. }
  192. unsigned BN_num_bits(const BIGNUM *bn) {
  193. const int max = bn->top - 1;
  194. if (BN_is_zero(bn)) {
  195. return 0;
  196. }
  197. return max*BN_BITS2 + BN_num_bits_word(bn->d[max]);
  198. }
  199. unsigned BN_num_bytes(const BIGNUM *bn) {
  200. return (BN_num_bits(bn) + 7) / 8;
  201. }
  202. void BN_zero(BIGNUM *bn) {
  203. bn->top = bn->neg = 0;
  204. }
  205. int BN_one(BIGNUM *bn) {
  206. return BN_set_word(bn, 1);
  207. }
  208. int BN_set_word(BIGNUM *bn, BN_ULONG value) {
  209. if (value == 0) {
  210. BN_zero(bn);
  211. return 1;
  212. }
  213. if (bn_wexpand(bn, 1) == NULL) {
  214. return 0;
  215. }
  216. bn->neg = 0;
  217. bn->d[0] = value;
  218. bn->top = 1;
  219. return 1;
  220. }
  221. int BN_set_u64(BIGNUM *bn, uint64_t value) {
  222. #if BN_BITS2 == 64
  223. return BN_set_word(bn, value);
  224. #elif BN_BITS2 == 32
  225. if (value <= BN_MASK2) {
  226. return BN_set_word(bn, (BN_ULONG)value);
  227. }
  228. if (bn_wexpand(bn, 2) == NULL) {
  229. return 0;
  230. }
  231. bn->neg = 0;
  232. bn->d[0] = (BN_ULONG)value;
  233. bn->d[1] = (BN_ULONG)(value >> 32);
  234. bn->top = 2;
  235. return 1;
  236. #else
  237. #error "BN_BITS2 must be 32 or 64."
  238. #endif
  239. }
  240. int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
  241. if (bn_wexpand(bn, num) == NULL) {
  242. return 0;
  243. }
  244. OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG));
  245. /* |bn_wexpand| verified that |num| isn't too large. */
  246. bn->top = (int)num;
  247. bn_correct_top(bn);
  248. bn->neg = 0;
  249. return 1;
  250. }
  251. int BN_is_negative(const BIGNUM *bn) {
  252. return bn->neg != 0;
  253. }
  254. void BN_set_negative(BIGNUM *bn, int sign) {
  255. if (sign && !BN_is_zero(bn)) {
  256. bn->neg = 1;
  257. } else {
  258. bn->neg = 0;
  259. }
  260. }
  261. BIGNUM *bn_wexpand(BIGNUM *bn, size_t words) {
  262. BN_ULONG *a;
  263. if (words <= (size_t)bn->dmax) {
  264. return bn;
  265. }
  266. if (words > (INT_MAX / (4 * BN_BITS2))) {
  267. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  268. return NULL;
  269. }
  270. if (bn->flags & BN_FLG_STATIC_DATA) {
  271. OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  272. return NULL;
  273. }
  274. a = OPENSSL_malloc(sizeof(BN_ULONG) * words);
  275. if (a == NULL) {
  276. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  277. return NULL;
  278. }
  279. OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->top);
  280. OPENSSL_free(bn->d);
  281. bn->d = a;
  282. bn->dmax = (int)words;
  283. return bn;
  284. }
  285. BIGNUM *bn_expand(BIGNUM *bn, size_t bits) {
  286. if (bits + BN_BITS2 - 1 < bits) {
  287. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  288. return NULL;
  289. }
  290. return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2);
  291. }
  292. void bn_correct_top(BIGNUM *bn) {
  293. BN_ULONG *ftl;
  294. int tmp_top = bn->top;
  295. if (tmp_top > 0) {
  296. for (ftl = &(bn->d[tmp_top - 1]); tmp_top > 0; tmp_top--) {
  297. if (*(ftl--)) {
  298. break;
  299. }
  300. }
  301. bn->top = tmp_top;
  302. }
  303. if (bn->top == 0) {
  304. bn->neg = 0;
  305. }
  306. }