Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

380 Zeilen
9.5 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 <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. memset(bn, 0, sizeof(BIGNUM));
  69. bn->flags = BN_FLG_MALLOCED;
  70. return bn;
  71. }
  72. void BN_init(BIGNUM *bn) {
  73. 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. 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. 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. void BN_with_flags(BIGNUM *out, const BIGNUM *in, int flags) {
  145. memcpy(out, in, sizeof(BIGNUM));
  146. out->flags &= ~BN_FLG_MALLOCED;
  147. out->flags |= BN_FLG_STATIC_DATA | flags;
  148. }
  149. /* BN_num_bits_word returns the minimum number of bits needed to represent the
  150. * value in |l|. */
  151. unsigned BN_num_bits_word(BN_ULONG l) {
  152. static const unsigned char bits[256] = {
  153. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  154. 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  155. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  156. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 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, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  159. 8, 8, 8, 8, 8, 8, 8, 8, 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};
  164. #if defined(OPENSSL_64_BIT)
  165. if (l & 0xffffffff00000000L) {
  166. if (l & 0xffff000000000000L) {
  167. if (l & 0xff00000000000000L) {
  168. return (bits[(int)(l >> 56)] + 56);
  169. } else {
  170. return (bits[(int)(l >> 48)] + 48);
  171. }
  172. } else {
  173. if (l & 0x0000ff0000000000L) {
  174. return (bits[(int)(l >> 40)] + 40);
  175. } else {
  176. return (bits[(int)(l >> 32)] + 32);
  177. }
  178. }
  179. } else
  180. #endif
  181. {
  182. if (l & 0xffff0000L) {
  183. if (l & 0xff000000L) {
  184. return (bits[(int)(l >> 24L)] + 24);
  185. } else {
  186. return (bits[(int)(l >> 16L)] + 16);
  187. }
  188. } else {
  189. if (l & 0xff00L) {
  190. return (bits[(int)(l >> 8)] + 8);
  191. } else {
  192. return (bits[(int)(l)]);
  193. }
  194. }
  195. }
  196. }
  197. unsigned BN_num_bits(const BIGNUM *bn) {
  198. const int max = bn->top - 1;
  199. if (BN_is_zero(bn)) {
  200. return 0;
  201. }
  202. return max*BN_BITS2 + BN_num_bits_word(bn->d[max]);
  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->top = 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) == NULL) {
  219. return 0;
  220. }
  221. bn->neg = 0;
  222. bn->d[0] = value;
  223. bn->top = 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) == NULL) {
  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->top = 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) == NULL) {
  247. return 0;
  248. }
  249. memmove(bn->d, words, num * sizeof(BN_ULONG));
  250. /* |bn_wexpand| verified that |num| isn't too large. */
  251. bn->top = (int)num;
  252. bn_correct_top(bn);
  253. bn->neg = 0;
  254. return 1;
  255. }
  256. int BN_is_negative(const BIGNUM *bn) {
  257. return bn->neg != 0;
  258. }
  259. void BN_set_negative(BIGNUM *bn, int sign) {
  260. if (sign && !BN_is_zero(bn)) {
  261. bn->neg = 1;
  262. } else {
  263. bn->neg = 0;
  264. }
  265. }
  266. BIGNUM *bn_wexpand(BIGNUM *bn, size_t words) {
  267. BN_ULONG *a;
  268. if (words <= (size_t)bn->dmax) {
  269. return bn;
  270. }
  271. if (words > (INT_MAX / (4 * BN_BITS2))) {
  272. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  273. return NULL;
  274. }
  275. if (bn->flags & BN_FLG_STATIC_DATA) {
  276. OPENSSL_PUT_ERROR(BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  277. return NULL;
  278. }
  279. a = OPENSSL_malloc(sizeof(BN_ULONG) * words);
  280. if (a == NULL) {
  281. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  282. return NULL;
  283. }
  284. memcpy(a, bn->d, sizeof(BN_ULONG) * bn->top);
  285. OPENSSL_free(bn->d);
  286. bn->d = a;
  287. bn->dmax = (int)words;
  288. return bn;
  289. }
  290. BIGNUM *bn_expand(BIGNUM *bn, size_t bits) {
  291. if (bits + BN_BITS2 - 1 < bits) {
  292. OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
  293. return NULL;
  294. }
  295. return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2);
  296. }
  297. void bn_correct_top(BIGNUM *bn) {
  298. BN_ULONG *ftl;
  299. int tmp_top = bn->top;
  300. if (tmp_top > 0) {
  301. for (ftl = &(bn->d[tmp_top - 1]); tmp_top > 0; tmp_top--) {
  302. if (*(ftl--)) {
  303. break;
  304. }
  305. }
  306. bn->top = tmp_top;
  307. }
  308. if (bn->top == 0) {
  309. bn->neg = 0;
  310. }
  311. }
  312. int BN_get_flags(const BIGNUM *bn, int flags) {
  313. return bn->flags & flags;
  314. }
  315. void BN_set_flags(BIGNUM *bn, int flags) {
  316. bn->flags |= flags;
  317. }