2
0

bn_add.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include "bn_lcl.h"
  11. /* r can == a or b */
  12. int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
  13. {
  14. int a_neg = a->neg, ret;
  15. bn_check_top(a);
  16. bn_check_top(b);
  17. /*-
  18. * a + b a+b
  19. * a + -b a-b
  20. * -a + b b-a
  21. * -a + -b -(a+b)
  22. */
  23. if (a_neg ^ b->neg) {
  24. /* only one is negative */
  25. if (a_neg) {
  26. const BIGNUM *tmp;
  27. tmp = a;
  28. a = b;
  29. b = tmp;
  30. }
  31. /* we are now a - b */
  32. if (BN_ucmp(a, b) < 0) {
  33. if (!BN_usub(r, b, a))
  34. return 0;
  35. r->neg = 1;
  36. } else {
  37. if (!BN_usub(r, a, b))
  38. return 0;
  39. r->neg = 0;
  40. }
  41. return 1;
  42. }
  43. ret = BN_uadd(r, a, b);
  44. r->neg = a_neg;
  45. bn_check_top(r);
  46. return ret;
  47. }
  48. /* unsigned add of b to a */
  49. int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
  50. {
  51. int max, min, dif;
  52. const BN_ULONG *ap, *bp;
  53. BN_ULONG *rp, carry, t1, t2;
  54. bn_check_top(a);
  55. bn_check_top(b);
  56. if (a->top < b->top) {
  57. const BIGNUM *tmp;
  58. tmp = a;
  59. a = b;
  60. b = tmp;
  61. }
  62. max = a->top;
  63. min = b->top;
  64. dif = max - min;
  65. if (bn_wexpand(r, max + 1) == NULL)
  66. return 0;
  67. r->top = max;
  68. ap = a->d;
  69. bp = b->d;
  70. rp = r->d;
  71. carry = bn_add_words(rp, ap, bp, min);
  72. rp += min;
  73. ap += min;
  74. while (dif) {
  75. dif--;
  76. t1 = *(ap++);
  77. t2 = (t1 + carry) & BN_MASK2;
  78. *(rp++) = t2;
  79. carry &= (t2 == 0);
  80. }
  81. *rp = carry;
  82. r->top += carry;
  83. r->neg = 0;
  84. bn_check_top(r);
  85. return 1;
  86. }
  87. /* unsigned subtraction of b from a, a must be larger than b. */
  88. int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
  89. {
  90. int max, min, dif;
  91. BN_ULONG t1, t2, borrow, *rp;
  92. const BN_ULONG *ap, *bp;
  93. bn_check_top(a);
  94. bn_check_top(b);
  95. max = a->top;
  96. min = b->top;
  97. dif = max - min;
  98. if (dif < 0) { /* hmm... should not be happening */
  99. BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);
  100. return 0;
  101. }
  102. if (bn_wexpand(r, max) == NULL)
  103. return 0;
  104. ap = a->d;
  105. bp = b->d;
  106. rp = r->d;
  107. borrow = bn_sub_words(rp, ap, bp, min);
  108. ap += min;
  109. rp += min;
  110. while (dif) {
  111. dif--;
  112. t1 = *(ap++);
  113. t2 = (t1 - borrow) & BN_MASK2;
  114. *(rp++) = t2;
  115. borrow &= (t1 == 0);
  116. }
  117. while (max && *--rp == 0)
  118. max--;
  119. r->top = max;
  120. r->neg = 0;
  121. bn_pollute(r);
  122. return 1;
  123. }
  124. int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
  125. {
  126. int max;
  127. int add = 0, neg = 0;
  128. bn_check_top(a);
  129. bn_check_top(b);
  130. /*-
  131. * a - b a-b
  132. * a - -b a+b
  133. * -a - b -(a+b)
  134. * -a - -b b-a
  135. */
  136. if (a->neg) {
  137. if (b->neg) {
  138. const BIGNUM *tmp;
  139. tmp = a;
  140. a = b;
  141. b = tmp;
  142. } else {
  143. add = 1;
  144. neg = 1;
  145. }
  146. } else {
  147. if (b->neg) {
  148. add = 1;
  149. neg = 0;
  150. }
  151. }
  152. if (add) {
  153. if (!BN_uadd(r, a, b))
  154. return 0;
  155. r->neg = neg;
  156. return 1;
  157. }
  158. /* We are actually doing a - b :-) */
  159. max = (a->top > b->top) ? a->top : b->top;
  160. if (bn_wexpand(r, max) == NULL)
  161. return 0;
  162. if (BN_ucmp(a, b) < 0) {
  163. if (!BN_usub(r, b, a))
  164. return 0;
  165. r->neg = 1;
  166. } else {
  167. if (!BN_usub(r, a, b))
  168. return 0;
  169. r->neg = 0;
  170. }
  171. bn_check_top(r);
  172. return 1;
  173. }