add.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <string.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include "internal.h"
  61. int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  62. const BIGNUM *tmp;
  63. int a_neg = a->neg, ret;
  64. // a + b a+b
  65. // a + -b a-b
  66. // -a + b b-a
  67. // -a + -b -(a+b)
  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)) {
  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;
  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. // degenerate case: w is zero
  146. if (!w) {
  147. return 1;
  148. }
  149. // degenerate case: a is zero
  150. if (BN_is_zero(a)) {
  151. return BN_set_word(a, w);
  152. }
  153. // handle 'a' when negative
  154. if (a->neg) {
  155. a->neg = 0;
  156. i = BN_sub_word(a, w);
  157. if (!BN_is_zero(a)) {
  158. a->neg = !(a->neg);
  159. }
  160. return i;
  161. }
  162. for (i = 0; w != 0 && i < a->top; i++) {
  163. a->d[i] = l = a->d[i] + w;
  164. w = (w > l) ? 1 : 0;
  165. }
  166. if (w && i == a->top) {
  167. if (!bn_wexpand(a, a->top + 1)) {
  168. return 0;
  169. }
  170. a->top++;
  171. a->d[i] = w;
  172. }
  173. return 1;
  174. }
  175. int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  176. int max;
  177. int add = 0, neg = 0;
  178. const BIGNUM *tmp;
  179. // a - b a-b
  180. // a - -b a+b
  181. // -a - b -(a+b)
  182. // -a - -b b-a
  183. if (a->neg) {
  184. if (b->neg) {
  185. tmp = a;
  186. a = b;
  187. b = tmp;
  188. } else {
  189. add = 1;
  190. neg = 1;
  191. }
  192. } else {
  193. if (b->neg) {
  194. add = 1;
  195. neg = 0;
  196. }
  197. }
  198. if (add) {
  199. if (!BN_uadd(r, a, b)) {
  200. return 0;
  201. }
  202. r->neg = neg;
  203. return 1;
  204. }
  205. // We are actually doing a - b :-)
  206. max = (a->top > b->top) ? a->top : b->top;
  207. if (!bn_wexpand(r, max)) {
  208. return 0;
  209. }
  210. if (BN_ucmp(a, b) < 0) {
  211. if (!BN_usub(r, b, a)) {
  212. return 0;
  213. }
  214. r->neg = 1;
  215. } else {
  216. if (!BN_usub(r, a, b)) {
  217. return 0;
  218. }
  219. r->neg = 0;
  220. }
  221. return 1;
  222. }
  223. int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  224. int max, min, dif;
  225. register BN_ULONG t1, t2, *ap, *bp, *rp;
  226. int i, carry;
  227. max = a->top;
  228. min = b->top;
  229. dif = max - min;
  230. if (dif < 0) // hmm... should not be happening
  231. {
  232. OPENSSL_PUT_ERROR(BN, BN_R_ARG2_LT_ARG3);
  233. return 0;
  234. }
  235. if (!bn_wexpand(r, max)) {
  236. return 0;
  237. }
  238. ap = a->d;
  239. bp = b->d;
  240. rp = r->d;
  241. carry = 0;
  242. for (i = min; i != 0; i--) {
  243. t1 = *(ap++);
  244. t2 = *(bp++);
  245. if (carry) {
  246. carry = (t1 <= t2);
  247. t1 -= t2 + 1;
  248. } else {
  249. carry = (t1 < t2);
  250. t1 -= t2;
  251. }
  252. *(rp++) = t1;
  253. }
  254. if (carry) // subtracted
  255. {
  256. if (!dif) {
  257. // error: a < b
  258. return 0;
  259. }
  260. while (dif) {
  261. dif--;
  262. t1 = *(ap++);
  263. t2 = t1 - 1;
  264. *(rp++) = t2;
  265. if (t1) {
  266. break;
  267. }
  268. }
  269. }
  270. if (dif > 0 && rp != ap) {
  271. OPENSSL_memcpy(rp, ap, sizeof(*rp) * dif);
  272. }
  273. r->top = max;
  274. r->neg = 0;
  275. bn_correct_top(r);
  276. return 1;
  277. }
  278. int BN_sub_word(BIGNUM *a, BN_ULONG w) {
  279. int i;
  280. // degenerate case: w is zero
  281. if (!w) {
  282. return 1;
  283. }
  284. // degenerate case: a is zero
  285. if (BN_is_zero(a)) {
  286. i = BN_set_word(a, w);
  287. if (i != 0) {
  288. BN_set_negative(a, 1);
  289. }
  290. return i;
  291. }
  292. // handle 'a' when negative
  293. if (a->neg) {
  294. a->neg = 0;
  295. i = BN_add_word(a, w);
  296. a->neg = 1;
  297. return i;
  298. }
  299. if ((a->top == 1) && (a->d[0] < w)) {
  300. a->d[0] = w - a->d[0];
  301. a->neg = 1;
  302. return 1;
  303. }
  304. i = 0;
  305. for (;;) {
  306. if (a->d[i] >= w) {
  307. a->d[i] -= w;
  308. break;
  309. } else {
  310. a->d[i] -= w;
  311. i++;
  312. w = 1;
  313. }
  314. }
  315. if ((a->d[i] == 0) && (i == (a->top - 1))) {
  316. a->top--;
  317. }
  318. return 1;
  319. }