add.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  94. // Widths are public, so we normalize to make |a| the larger one.
  95. if (a->width < b->width) {
  96. const BIGNUM *tmp = a;
  97. a = b;
  98. b = tmp;
  99. }
  100. int max = a->width;
  101. int min = b->width;
  102. if (!bn_wexpand(r, max + 1)) {
  103. return 0;
  104. }
  105. r->width = max + 1;
  106. BN_ULONG carry = bn_add_words(r->d, a->d, b->d, min);
  107. for (int i = min; i < max; i++) {
  108. // |r| and |a| may alias, so use a temporary.
  109. BN_ULONG tmp = carry + a->d[i];
  110. carry = tmp < a->d[i];
  111. r->d[i] = tmp;
  112. }
  113. r->d[max] = carry;
  114. return 1;
  115. }
  116. int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  117. if (!bn_uadd_consttime(r, a, b)) {
  118. return 0;
  119. }
  120. bn_set_minimal_width(r);
  121. return 1;
  122. }
  123. int BN_add_word(BIGNUM *a, BN_ULONG w) {
  124. BN_ULONG l;
  125. int i;
  126. // degenerate case: w is zero
  127. if (!w) {
  128. return 1;
  129. }
  130. // degenerate case: a is zero
  131. if (BN_is_zero(a)) {
  132. return BN_set_word(a, w);
  133. }
  134. // handle 'a' when negative
  135. if (a->neg) {
  136. a->neg = 0;
  137. i = BN_sub_word(a, w);
  138. if (!BN_is_zero(a)) {
  139. a->neg = !(a->neg);
  140. }
  141. return i;
  142. }
  143. for (i = 0; w != 0 && i < a->width; i++) {
  144. a->d[i] = l = a->d[i] + w;
  145. w = (w > l) ? 1 : 0;
  146. }
  147. if (w && i == a->width) {
  148. if (!bn_wexpand(a, a->width + 1)) {
  149. return 0;
  150. }
  151. a->width++;
  152. a->d[i] = w;
  153. }
  154. return 1;
  155. }
  156. int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  157. int add = 0, neg = 0;
  158. const BIGNUM *tmp;
  159. // a - b a-b
  160. // a - -b a+b
  161. // -a - b -(a+b)
  162. // -a - -b b-a
  163. if (a->neg) {
  164. if (b->neg) {
  165. tmp = a;
  166. a = b;
  167. b = tmp;
  168. } else {
  169. add = 1;
  170. neg = 1;
  171. }
  172. } else {
  173. if (b->neg) {
  174. add = 1;
  175. neg = 0;
  176. }
  177. }
  178. if (add) {
  179. if (!BN_uadd(r, a, b)) {
  180. return 0;
  181. }
  182. r->neg = neg;
  183. return 1;
  184. }
  185. if (BN_ucmp(a, b) < 0) {
  186. if (!BN_usub(r, b, a)) {
  187. return 0;
  188. }
  189. r->neg = 1;
  190. } else {
  191. if (!BN_usub(r, a, b)) {
  192. return 0;
  193. }
  194. r->neg = 0;
  195. }
  196. return 1;
  197. }
  198. int bn_usub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  199. // |b| may have more words than |a| given non-minimal inputs, but all words
  200. // beyond |a->width| must then be zero.
  201. int b_width = b->width;
  202. if (b_width > a->width) {
  203. if (!bn_fits_in_words(b, a->width)) {
  204. OPENSSL_PUT_ERROR(BN, BN_R_ARG2_LT_ARG3);
  205. return 0;
  206. }
  207. b_width = a->width;
  208. }
  209. if (!bn_wexpand(r, a->width)) {
  210. return 0;
  211. }
  212. BN_ULONG borrow = bn_sub_words(r->d, a->d, b->d, b_width);
  213. for (int i = b_width; i < a->width; i++) {
  214. // |r| and |a| may alias, so use a temporary.
  215. BN_ULONG tmp = a->d[i];
  216. r->d[i] = a->d[i] - borrow;
  217. borrow = tmp < r->d[i];
  218. }
  219. if (borrow) {
  220. OPENSSL_PUT_ERROR(BN, BN_R_ARG2_LT_ARG3);
  221. return 0;
  222. }
  223. r->width = a->width;
  224. r->neg = 0;
  225. return 1;
  226. }
  227. int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) {
  228. if (!bn_usub_consttime(r, a, b)) {
  229. return 0;
  230. }
  231. bn_set_minimal_width(r);
  232. return 1;
  233. }
  234. int BN_sub_word(BIGNUM *a, BN_ULONG w) {
  235. int i;
  236. // degenerate case: w is zero
  237. if (!w) {
  238. return 1;
  239. }
  240. // degenerate case: a is zero
  241. if (BN_is_zero(a)) {
  242. i = BN_set_word(a, w);
  243. if (i != 0) {
  244. BN_set_negative(a, 1);
  245. }
  246. return i;
  247. }
  248. // handle 'a' when negative
  249. if (a->neg) {
  250. a->neg = 0;
  251. i = BN_add_word(a, w);
  252. a->neg = 1;
  253. return i;
  254. }
  255. if ((bn_minimal_width(a) == 1) && (a->d[0] < w)) {
  256. a->d[0] = w - a->d[0];
  257. a->neg = 1;
  258. return 1;
  259. }
  260. i = 0;
  261. for (;;) {
  262. if (a->d[i] >= w) {
  263. a->d[i] -= w;
  264. break;
  265. } else {
  266. a->d[i] -= w;
  267. i++;
  268. w = 1;
  269. }
  270. }
  271. if ((a->d[i] == 0) && (i == (a->width - 1))) {
  272. a->width--;
  273. }
  274. return 1;
  275. }