No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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