Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

337 строки
8.8 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 <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include "internal.h"
  61. BIGNUM *BN_new(void) {
  62. BIGNUM *bn = OPENSSL_malloc(sizeof(BIGNUM));
  63. if (bn == NULL) {
  64. OPENSSL_PUT_ERROR(BN, BN_new, ERR_R_MALLOC_FAILURE);
  65. return NULL;
  66. }
  67. memset(bn, 0, sizeof(BIGNUM));
  68. bn->flags = BN_FLG_MALLOCED;
  69. return bn;
  70. }
  71. void BN_init(BIGNUM *bn) {
  72. memset(bn, 0, sizeof(BIGNUM));
  73. }
  74. void BN_free(BIGNUM *bn) {
  75. if (bn == NULL) {
  76. return;
  77. }
  78. if (bn->d != NULL && (bn->flags & BN_FLG_STATIC_DATA) == 0) {
  79. OPENSSL_free(bn->d);
  80. }
  81. if (bn->flags & BN_FLG_MALLOCED) {
  82. OPENSSL_free(bn);
  83. } else {
  84. bn->d = NULL;
  85. }
  86. }
  87. void BN_clear_free(BIGNUM *bn) {
  88. char should_free;
  89. if (bn == NULL) {
  90. return;
  91. }
  92. if (bn->d != NULL) {
  93. OPENSSL_cleanse(bn->d, bn->dmax * sizeof(bn->d[0]));
  94. if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
  95. OPENSSL_free(bn->d);
  96. }
  97. }
  98. should_free = (bn->flags & BN_FLG_MALLOCED) != 0;
  99. OPENSSL_cleanse(bn, sizeof(BIGNUM));
  100. if (should_free) {
  101. OPENSSL_free(bn);
  102. }
  103. }
  104. BIGNUM *BN_dup(const BIGNUM *src) {
  105. BIGNUM *copy;
  106. if (src == NULL) {
  107. return NULL;
  108. }
  109. copy = BN_new();
  110. if (copy == NULL) {
  111. return NULL;
  112. }
  113. if (!BN_copy(copy, src)) {
  114. BN_free(copy);
  115. return NULL;
  116. }
  117. return copy;
  118. }
  119. BIGNUM *BN_copy(BIGNUM *dest, const BIGNUM *src) {
  120. if (src == dest) {
  121. return dest;
  122. }
  123. if (bn_wexpand(dest, src->top) == NULL) {
  124. return NULL;
  125. }
  126. memcpy(dest->d, src->d, sizeof(src->d[0]) * src->top);
  127. dest->top = src->top;
  128. dest->neg = src->neg;
  129. return dest;
  130. }
  131. void BN_clear(BIGNUM *bn) {
  132. if (bn->d != NULL) {
  133. memset(bn->d, 0, bn->dmax * sizeof(bn->d[0]));
  134. }
  135. bn->top = 0;
  136. bn->neg = 0;
  137. }
  138. const BIGNUM *BN_value_one(void) {
  139. static const BN_ULONG data_one = 1;
  140. static const BIGNUM const_one = {(BN_ULONG *)&data_one, 1, 1, 0,
  141. BN_FLG_STATIC_DATA};
  142. return &const_one;
  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. } else {
  172. if (l & 0x0000ff0000000000L) {
  173. return (bits[(int)(l >> 40)] + 40);
  174. } else
  175. return (bits[(int)(l >> 32)] + 32);
  176. }
  177. } else
  178. #endif
  179. {
  180. if (l & 0xffff0000L) {
  181. if (l & 0xff000000L) {
  182. return (bits[(int)(l >> 24L)] + 24);
  183. } else {
  184. return (bits[(int)(l >> 16L)] + 16);
  185. }
  186. } else {
  187. if (l & 0xff00L) {
  188. return (bits[(int)(l >> 8)] + 8);
  189. } else {
  190. return (bits[(int)(l)]);
  191. }
  192. }
  193. }
  194. }
  195. unsigned BN_num_bits(const BIGNUM *bn) {
  196. const int max = bn->top - 1;
  197. if (BN_is_zero(bn)) {
  198. return 0;
  199. }
  200. return max*BN_BITS2 + BN_num_bits_word(bn->d[max]);
  201. }
  202. unsigned BN_num_bytes(const BIGNUM *bn) {
  203. return (BN_num_bits(bn) + 7) / 8;
  204. }
  205. void BN_zero(BIGNUM *bn) {
  206. bn->top = bn->neg = 0;
  207. }
  208. int BN_one(BIGNUM *bn) {
  209. return BN_set_word(bn, 1);
  210. }
  211. int BN_set_word(BIGNUM *bn, BN_ULONG value) {
  212. if (value == 0) {
  213. BN_zero(bn);
  214. return 1;
  215. }
  216. if (bn_wexpand(bn, 1) == NULL) {
  217. return 0;
  218. }
  219. bn->neg = 0;
  220. bn->d[0] = value;
  221. bn->top = 1;
  222. return 1;
  223. }
  224. int BN_is_negative(const BIGNUM *bn) {
  225. return bn->neg != 0;
  226. }
  227. void BN_set_negative(BIGNUM *bn, int sign) {
  228. if (sign && !BN_is_zero(bn)) {
  229. bn->neg = 1;
  230. } else {
  231. bn->neg = 0;
  232. }
  233. }
  234. BIGNUM *bn_wexpand(BIGNUM *bn, unsigned words) {
  235. BN_ULONG *a;
  236. if (words <= (unsigned) bn->dmax) {
  237. return bn;
  238. }
  239. if (words > (INT_MAX / (4 * BN_BITS2))) {
  240. OPENSSL_PUT_ERROR(BN, bn_wexpand, BN_R_BIGNUM_TOO_LONG);
  241. return NULL;
  242. }
  243. if (bn->flags & BN_FLG_STATIC_DATA) {
  244. OPENSSL_PUT_ERROR(BN, bn_wexpand, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  245. return NULL;
  246. }
  247. a = (BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG) * words);
  248. if (a == NULL) {
  249. OPENSSL_PUT_ERROR(BN, bn_wexpand, ERR_R_MALLOC_FAILURE);
  250. return NULL;
  251. }
  252. memcpy(a, bn->d, sizeof(BN_ULONG) * bn->top);
  253. if (bn->d) {
  254. OPENSSL_free(bn->d);
  255. }
  256. bn->d = a;
  257. bn->dmax = words;
  258. return bn;
  259. }
  260. BIGNUM *bn_expand(BIGNUM *bn, unsigned bits) {
  261. return bn_wexpand(bn, (bits+BN_BITS2-1)/BN_BITS2);
  262. }
  263. void bn_correct_top(BIGNUM *bn) {
  264. BN_ULONG *ftl;
  265. int tmp_top = bn->top;
  266. if (tmp_top > 0) {
  267. for (ftl = &(bn->d[tmp_top - 1]); tmp_top > 0; tmp_top--) {
  268. if (*(ftl--)) {
  269. break;
  270. }
  271. }
  272. bn->top = tmp_top;
  273. }
  274. }
  275. int BN_get_flags(const BIGNUM *bn, int flags) {
  276. return bn->flags & flags;
  277. }
  278. void BN_set_flags(BIGNUM *bn, int flags) {
  279. bn->flags |= flags;
  280. }