bn.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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->top)) {
  128. return NULL;
  129. }
  130. OPENSSL_memcpy(dest->d, src->d, sizeof(src->d[0]) * src->top);
  131. dest->top = src->top;
  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->top = 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->top = 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. static const unsigned char bits[256] = {
  154. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  155. 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  156. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  157. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  158. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  159. 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  160. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  161. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  162. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  163. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  164. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
  165. #if defined(OPENSSL_64_BIT)
  166. if (l & 0xffffffff00000000L) {
  167. if (l & 0xffff000000000000L) {
  168. if (l & 0xff00000000000000L) {
  169. return (bits[(int)(l >> 56)] + 56);
  170. } else {
  171. return (bits[(int)(l >> 48)] + 48);
  172. }
  173. } else {
  174. if (l & 0x0000ff0000000000L) {
  175. return (bits[(int)(l >> 40)] + 40);
  176. } else {
  177. return (bits[(int)(l >> 32)] + 32);
  178. }
  179. }
  180. } else
  181. #endif
  182. {
  183. if (l & 0xffff0000L) {
  184. if (l & 0xff000000L) {
  185. return (bits[(int)(l >> 24L)] + 24);
  186. } else {
  187. return (bits[(int)(l >> 16L)] + 16);
  188. }
  189. } else {
  190. if (l & 0xff00L) {
  191. return (bits[(int)(l >> 8)] + 8);
  192. } else {
  193. return (bits[(int)(l)]);
  194. }
  195. }
  196. }
  197. }
  198. unsigned BN_num_bits(const BIGNUM *bn) {
  199. const int max = bn->top - 1;
  200. if (BN_is_zero(bn)) {
  201. return 0;
  202. }
  203. return max*BN_BITS2 + BN_num_bits_word(bn->d[max]);
  204. }
  205. unsigned BN_num_bytes(const BIGNUM *bn) {
  206. return (BN_num_bits(bn) + 7) / 8;
  207. }
  208. void BN_zero(BIGNUM *bn) {
  209. bn->top = bn->neg = 0;
  210. }
  211. int BN_one(BIGNUM *bn) {
  212. return BN_set_word(bn, 1);
  213. }
  214. int BN_set_word(BIGNUM *bn, BN_ULONG value) {
  215. if (value == 0) {
  216. BN_zero(bn);
  217. return 1;
  218. }
  219. if (!bn_wexpand(bn, 1)) {
  220. return 0;
  221. }
  222. bn->neg = 0;
  223. bn->d[0] = value;
  224. bn->top = 1;
  225. return 1;
  226. }
  227. int BN_set_u64(BIGNUM *bn, uint64_t value) {
  228. #if BN_BITS2 == 64
  229. return BN_set_word(bn, value);
  230. #elif BN_BITS2 == 32
  231. if (value <= BN_MASK2) {
  232. return BN_set_word(bn, (BN_ULONG)value);
  233. }
  234. if (!bn_wexpand(bn, 2)) {
  235. return 0;
  236. }
  237. bn->neg = 0;
  238. bn->d[0] = (BN_ULONG)value;
  239. bn->d[1] = (BN_ULONG)(value >> 32);
  240. bn->top = 2;
  241. return 1;
  242. #else
  243. #error "BN_BITS2 must be 32 or 64."
  244. #endif
  245. }
  246. int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
  247. if (!bn_wexpand(bn, num)) {
  248. return 0;
  249. }
  250. OPENSSL_memmove(bn->d, words, num * sizeof(BN_ULONG));
  251. // |bn_wexpand| verified that |num| isn't too large.
  252. bn->top = (int)num;
  253. bn_correct_top(bn);
  254. bn->neg = 0;
  255. return 1;
  256. }
  257. int BN_is_negative(const BIGNUM *bn) {
  258. return bn->neg != 0;
  259. }
  260. void BN_set_negative(BIGNUM *bn, int sign) {
  261. if (sign && !BN_is_zero(bn)) {
  262. bn->neg = 1;
  263. } else {
  264. bn->neg = 0;
  265. }
  266. }
  267. int bn_wexpand(BIGNUM *bn, size_t words) {
  268. BN_ULONG *a;
  269. if (words <= (size_t)bn->dmax) {
  270. return 1;
  271. }
  272. if (words > (INT_MAX / (4 * BN_BITS2))) {
  273. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  274. return 0;
  275. }
  276. if (bn->flags & BN_FLG_STATIC_DATA) {
  277. OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  278. return 0;
  279. }
  280. a = OPENSSL_malloc(sizeof(BN_ULONG) * words);
  281. if (a == NULL) {
  282. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  283. return 0;
  284. }
  285. OPENSSL_memcpy(a, bn->d, sizeof(BN_ULONG) * bn->top);
  286. OPENSSL_free(bn->d);
  287. bn->d = a;
  288. bn->dmax = (int)words;
  289. return 1;
  290. }
  291. int bn_expand(BIGNUM *bn, size_t bits) {
  292. if (bits + BN_BITS2 - 1 < bits) {
  293. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  294. return 0;
  295. }
  296. return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2);
  297. }
  298. void bn_correct_top(BIGNUM *bn) {
  299. BN_ULONG *ftl;
  300. int tmp_top = bn->top;
  301. if (tmp_top > 0) {
  302. for (ftl = &(bn->d[tmp_top - 1]); tmp_top > 0; tmp_top--) {
  303. if (*(ftl--)) {
  304. break;
  305. }
  306. }
  307. bn->top = tmp_top;
  308. }
  309. if (bn->top == 0) {
  310. bn->neg = 0;
  311. }
  312. }