您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

395 行
8.2 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 <openssl/err.h>
  58. #include <openssl/mem.h>
  59. #include "internal.h"
  60. int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  61. const BIGNUM *tmp;
  62. int a_neg = a->neg, ret;
  63. /* a + b a+b
  64. * a + -b a-b
  65. * -a + b b-a
  66. * -a + -b -(a+b)
  67. */
  68. if (a_neg ^ b->neg) {
  69. /* only one is negative */
  70. if (a_neg) {
  71. tmp = a;
  72. a = b;
  73. b = tmp;
  74. }
  75. /* we are now a - b */
  76. if (BN_ucmp(a, b) < 0) {
  77. if (!BN_usub(r, b, a)) {
  78. return 0;
  79. }
  80. r->neg = 1;
  81. } else {
  82. if (!BN_usub(r, a, b)) {
  83. return 0;
  84. }
  85. r->neg = 0;
  86. }
  87. return 1;
  88. }
  89. ret = BN_uadd(r, a, b);
  90. r->neg = a_neg;
  91. return ret;
  92. }
  93. int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  94. int max, min, dif;
  95. BN_ULONG *ap, *bp, *rp, carry, t1, t2;
  96. const BIGNUM *tmp;
  97. if (a->top < b->top) {
  98. tmp = a;
  99. a = b;
  100. b = tmp;
  101. }
  102. max = a->top;
  103. min = b->top;
  104. dif = max - min;
  105. if (bn_wexpand(r, max + 1) == NULL) {
  106. return 0;
  107. }
  108. r->top = max;
  109. ap = a->d;
  110. bp = b->d;
  111. rp = r->d;
  112. carry = bn_add_words(rp, ap, bp, min);
  113. rp += min;
  114. ap += min;
  115. bp += min;
  116. if (carry) {
  117. while (dif) {
  118. dif--;
  119. t1 = *(ap++);
  120. t2 = (t1 + 1) & BN_MASK2;
  121. *(rp++) = t2;
  122. if (t2) {
  123. carry = 0;
  124. break;
  125. }
  126. }
  127. if (carry) {
  128. /* carry != 0 => dif == 0 */
  129. *rp = 1;
  130. r->top++;
  131. }
  132. }
  133. if (dif && rp != ap) {
  134. while (dif--) {
  135. /* copy remaining words if ap != rp */
  136. *(rp++) = *(ap++);
  137. }
  138. }
  139. r->neg = 0;
  140. return 1;
  141. }
  142. int BN_add_word(BIGNUM *a, BN_ULONG w) {
  143. BN_ULONG l;
  144. int i;
  145. w &= BN_MASK2;
  146. /* degenerate case: w is zero */
  147. if (!w) {
  148. return 1;
  149. }
  150. /* degenerate case: a is zero */
  151. if (BN_is_zero(a)) {
  152. return BN_set_word(a, w);
  153. }
  154. /* handle 'a' when negative */
  155. if (a->neg) {
  156. a->neg = 0;
  157. i = BN_sub_word(a, w);
  158. if (!BN_is_zero(a)) {
  159. a->neg = !(a->neg);
  160. }
  161. return i;
  162. }
  163. for (i = 0; w != 0 && i < a->top; i++) {
  164. a->d[i] = l = (a->d[i] + w) & BN_MASK2;
  165. w = (w > l) ? 1 : 0;
  166. }
  167. if (w && i == a->top) {
  168. if (bn_wexpand(a, a->top + 1) == NULL) {
  169. return 0;
  170. }
  171. a->top++;
  172. a->d[i] = w;
  173. }
  174. return 1;
  175. }
  176. int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  177. int max;
  178. int add = 0, neg = 0;
  179. const BIGNUM *tmp;
  180. /* a - b a-b
  181. * a - -b a+b
  182. * -a - b -(a+b)
  183. * -a - -b b-a
  184. */
  185. if (a->neg) {
  186. if (b->neg) {
  187. tmp = a;
  188. a = b;
  189. b = tmp;
  190. } else {
  191. add = 1;
  192. neg = 1;
  193. }
  194. } else {
  195. if (b->neg) {
  196. add = 1;
  197. neg = 0;
  198. }
  199. }
  200. if (add) {
  201. if (!BN_uadd(r, a, b)) {
  202. return 0;
  203. }
  204. r->neg = neg;
  205. return 1;
  206. }
  207. /* We are actually doing a - b :-) */
  208. max = (a->top > b->top) ? a->top : b->top;
  209. if (bn_wexpand(r, max) == NULL) {
  210. return 0;
  211. }
  212. if (BN_ucmp(a, b) < 0) {
  213. if (!BN_usub(r, b, a)) {
  214. return 0;
  215. }
  216. r->neg = 1;
  217. } else {
  218. if (!BN_usub(r, a, b)) {
  219. return 0;
  220. }
  221. r->neg = 0;
  222. }
  223. return 1;
  224. }
  225. int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  226. int max, min, dif;
  227. register BN_ULONG t1, t2, *ap, *bp, *rp;
  228. int i, carry;
  229. max = a->top;
  230. min = b->top;
  231. dif = max - min;
  232. if (dif < 0) /* hmm... should not be happening */
  233. {
  234. OPENSSL_PUT_ERROR(BN, BN_usub, BN_R_ARG2_LT_ARG3);
  235. return 0;
  236. }
  237. if (bn_wexpand(r, max) == NULL) {
  238. return 0;
  239. }
  240. ap = a->d;
  241. bp = b->d;
  242. rp = r->d;
  243. carry = 0;
  244. for (i = min; i != 0; i--) {
  245. t1 = *(ap++);
  246. t2 = *(bp++);
  247. if (carry) {
  248. carry = (t1 <= t2);
  249. t1 = (t1 - t2 - 1) & BN_MASK2;
  250. } else {
  251. carry = (t1 < t2);
  252. t1 = (t1 - t2) & BN_MASK2;
  253. }
  254. *(rp++) = t1 & BN_MASK2;
  255. }
  256. if (carry) /* subtracted */
  257. {
  258. if (!dif) {
  259. /* error: a < b */
  260. return 0;
  261. }
  262. while (dif) {
  263. dif--;
  264. t1 = *(ap++);
  265. t2 = (t1 - 1) & BN_MASK2;
  266. *(rp++) = t2;
  267. if (t1) {
  268. break;
  269. }
  270. }
  271. }
  272. if (rp != ap) {
  273. for (;;) {
  274. if (!dif--) {
  275. break;
  276. }
  277. rp[0] = ap[0];
  278. if (!dif--) {
  279. break;
  280. }
  281. rp[1] = ap[1];
  282. if (!dif--) {
  283. break;
  284. }
  285. rp[2] = ap[2];
  286. if (!dif--) {
  287. break;
  288. }
  289. rp[3] = ap[3];
  290. rp += 4;
  291. ap += 4;
  292. }
  293. }
  294. r->top = max;
  295. r->neg = 0;
  296. bn_correct_top(r);
  297. return 1;
  298. }
  299. int BN_sub_word(BIGNUM *a, BN_ULONG w) {
  300. int i;
  301. w &= BN_MASK2;
  302. /* degenerate case: w is zero */
  303. if (!w) {
  304. return 1;
  305. }
  306. /* degenerate case: a is zero */
  307. if (BN_is_zero(a)) {
  308. i = BN_set_word(a, w);
  309. if (i != 0) {
  310. BN_set_negative(a, 1);
  311. }
  312. return i;
  313. }
  314. /* handle 'a' when negative */
  315. if (a->neg) {
  316. a->neg = 0;
  317. i = BN_add_word(a, w);
  318. a->neg = 1;
  319. return i;
  320. }
  321. if ((a->top == 1) && (a->d[0] < w)) {
  322. a->d[0] = w - a->d[0];
  323. a->neg = 1;
  324. return 1;
  325. }
  326. i = 0;
  327. for (;;) {
  328. if (a->d[i] >= w) {
  329. a->d[i] -= w;
  330. break;
  331. } else {
  332. a->d[i] = (a->d[i] - w) & BN_MASK2;
  333. i++;
  334. w = 1;
  335. }
  336. }
  337. if ((a->d[i] == 0) && (i == (a->top - 1))) {
  338. a->top--;
  339. }
  340. return 1;
  341. }