Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "internal.h"
  58. int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) {
  59. int i, nw, lb, rb;
  60. BN_ULONG *t, *f;
  61. BN_ULONG l;
  62. r->neg = a->neg;
  63. nw = n / BN_BITS2;
  64. if (bn_wexpand(r, a->top + nw + 1) == NULL) {
  65. return 0;
  66. }
  67. lb = n % BN_BITS2;
  68. rb = BN_BITS2 - lb;
  69. f = a->d;
  70. t = r->d;
  71. t[a->top + nw] = 0;
  72. if (lb == 0) {
  73. for (i = a->top - 1; i >= 0; i--) {
  74. t[nw + i] = f[i];
  75. }
  76. } else {
  77. for (i = a->top - 1; i >= 0; i--) {
  78. l = f[i];
  79. t[nw + i + 1] |= (l >> rb) & BN_MASK2;
  80. t[nw + i] = (l << lb) & BN_MASK2;
  81. }
  82. }
  83. memset(t, 0, nw * sizeof(t[0]));
  84. r->top = a->top + nw + 1;
  85. bn_correct_top(r);
  86. return 1;
  87. }
  88. int BN_lshift1(BIGNUM *r, const BIGNUM *a) {
  89. BN_ULONG *ap, *rp, t, c;
  90. int i;
  91. if (r != a) {
  92. r->neg = a->neg;
  93. if (bn_wexpand(r, a->top + 1) == NULL) {
  94. return 0;
  95. }
  96. r->top = a->top;
  97. } else {
  98. if (bn_wexpand(r, a->top + 1) == NULL) {
  99. return 0;
  100. }
  101. }
  102. ap = a->d;
  103. rp = r->d;
  104. c = 0;
  105. for (i = 0; i < a->top; i++) {
  106. t = *(ap++);
  107. *(rp++) = ((t << 1) | c) & BN_MASK2;
  108. c = (t & BN_TBIT) ? 1 : 0;
  109. }
  110. if (c) {
  111. *rp = 1;
  112. r->top++;
  113. }
  114. return 1;
  115. }
  116. int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) {
  117. int i, j, nw, lb, rb;
  118. BN_ULONG *t, *f;
  119. BN_ULONG l, tmp;
  120. nw = n / BN_BITS2;
  121. rb = n % BN_BITS2;
  122. lb = BN_BITS2 - rb;
  123. if (nw >= a->top || a->top == 0) {
  124. BN_zero(r);
  125. return 1;
  126. }
  127. i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;
  128. if (r != a) {
  129. r->neg = a->neg;
  130. if (bn_wexpand(r, i) == NULL) {
  131. return 0;
  132. }
  133. } else {
  134. if (n == 0) {
  135. return 1; /* or the copying loop will go berserk */
  136. }
  137. }
  138. f = &(a->d[nw]);
  139. t = r->d;
  140. j = a->top - nw;
  141. r->top = i;
  142. if (rb == 0) {
  143. for (i = j; i != 0; i--) {
  144. *(t++) = *(f++);
  145. }
  146. } else {
  147. l = *(f++);
  148. for (i = j - 1; i != 0; i--) {
  149. tmp = (l >> rb) & BN_MASK2;
  150. l = *(f++);
  151. *(t++) = (tmp | (l << lb)) & BN_MASK2;
  152. }
  153. if ((l = (l >> rb) & BN_MASK2)) {
  154. *(t) = l;
  155. }
  156. }
  157. return 1;
  158. }
  159. int BN_rshift1(BIGNUM *r, const BIGNUM *a) {
  160. BN_ULONG *ap, *rp, t, c;
  161. int i, j;
  162. if (BN_is_zero(a)) {
  163. BN_zero(r);
  164. return 1;
  165. }
  166. i = a->top;
  167. ap = a->d;
  168. j = i - (ap[i - 1] == 1);
  169. if (a != r) {
  170. if (bn_wexpand(r, j) == NULL) {
  171. return 0;
  172. }
  173. r->neg = a->neg;
  174. }
  175. rp = r->d;
  176. t = ap[--i];
  177. c = (t & 1) ? BN_TBIT : 0;
  178. if (t >>= 1) {
  179. rp[i] = t;
  180. }
  181. while (i > 0) {
  182. t = ap[--i];
  183. rp[i] = ((t >> 1) & BN_MASK2) | c;
  184. c = (t & 1) ? BN_TBIT : 0;
  185. }
  186. r->top = j;
  187. return 1;
  188. }
  189. int BN_set_bit(BIGNUM *a, int n) {
  190. int i, j, k;
  191. if (n < 0) {
  192. return 0;
  193. }
  194. i = n / BN_BITS2;
  195. j = n % BN_BITS2;
  196. if (a->top <= i) {
  197. if (bn_wexpand(a, i + 1) == NULL) {
  198. return 0;
  199. }
  200. for (k = a->top; k < i + 1; k++) {
  201. a->d[k] = 0;
  202. }
  203. a->top = i + 1;
  204. }
  205. a->d[i] |= (((BN_ULONG)1) << j);
  206. return 1;
  207. }
  208. int BN_clear_bit(BIGNUM *a, int n) {
  209. int i, j;
  210. if (n < 0) {
  211. return 0;
  212. }
  213. i = n / BN_BITS2;
  214. j = n % BN_BITS2;
  215. if (a->top <= i) {
  216. return 0;
  217. }
  218. a->d[i] &= (~(((BN_ULONG)1) << j));
  219. bn_correct_top(a);
  220. return 1;
  221. }
  222. int BN_is_bit_set(const BIGNUM *a, int n) {
  223. int i, j;
  224. if (n < 0) {
  225. return 0;
  226. }
  227. i = n / BN_BITS2;
  228. j = n % BN_BITS2;
  229. if (a->top <= i) {
  230. return 0;
  231. }
  232. return (a->d[i]>>j)&1;
  233. }
  234. int BN_mask_bits(BIGNUM *a, int n) {
  235. int b, w;
  236. if (n < 0) {
  237. return 0;
  238. }
  239. w = n / BN_BITS2;
  240. b = n % BN_BITS2;
  241. if (w >= a->top) {
  242. return 0;
  243. }
  244. if (b == 0) {
  245. a->top = w;
  246. } else {
  247. a->top = w + 1;
  248. a->d[w] &= ~(BN_MASK2 << b);
  249. }
  250. bn_correct_top(a);
  251. return 1;
  252. }