2
0

bn_shift.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. int BN_lshift1(BIGNUM *r, const BIGNUM *a)
  12. {
  13. register BN_ULONG *ap, *rp, t, c;
  14. int i;
  15. bn_check_top(r);
  16. bn_check_top(a);
  17. if (r != a) {
  18. r->neg = a->neg;
  19. if (bn_wexpand(r, a->top + 1) == NULL)
  20. return (0);
  21. r->top = a->top;
  22. } else {
  23. if (bn_wexpand(r, a->top + 1) == NULL)
  24. return (0);
  25. }
  26. ap = a->d;
  27. rp = r->d;
  28. c = 0;
  29. for (i = 0; i < a->top; i++) {
  30. t = *(ap++);
  31. *(rp++) = ((t << 1) | c) & BN_MASK2;
  32. c = (t & BN_TBIT) ? 1 : 0;
  33. }
  34. if (c) {
  35. *rp = 1;
  36. r->top++;
  37. }
  38. bn_check_top(r);
  39. return (1);
  40. }
  41. int BN_rshift1(BIGNUM *r, const BIGNUM *a)
  42. {
  43. BN_ULONG *ap, *rp, t, c;
  44. int i, j;
  45. bn_check_top(r);
  46. bn_check_top(a);
  47. if (BN_is_zero(a)) {
  48. BN_zero(r);
  49. return (1);
  50. }
  51. i = a->top;
  52. ap = a->d;
  53. j = i - (ap[i - 1] == 1);
  54. if (a != r) {
  55. if (bn_wexpand(r, j) == NULL)
  56. return (0);
  57. r->neg = a->neg;
  58. }
  59. rp = r->d;
  60. t = ap[--i];
  61. c = (t & 1) ? BN_TBIT : 0;
  62. if (t >>= 1)
  63. rp[i] = t;
  64. while (i > 0) {
  65. t = ap[--i];
  66. rp[i] = ((t >> 1) & BN_MASK2) | c;
  67. c = (t & 1) ? BN_TBIT : 0;
  68. }
  69. r->top = j;
  70. if (!r->top)
  71. r->neg = 0; /* don't allow negative zero */
  72. bn_check_top(r);
  73. return (1);
  74. }
  75. int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
  76. {
  77. int i, nw, lb, rb;
  78. BN_ULONG *t, *f;
  79. BN_ULONG l;
  80. bn_check_top(r);
  81. bn_check_top(a);
  82. if (n < 0) {
  83. BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
  84. return 0;
  85. }
  86. nw = n / BN_BITS2;
  87. if (bn_wexpand(r, a->top + nw + 1) == NULL)
  88. return (0);
  89. r->neg = a->neg;
  90. lb = n % BN_BITS2;
  91. rb = BN_BITS2 - lb;
  92. f = a->d;
  93. t = r->d;
  94. t[a->top + nw] = 0;
  95. if (lb == 0)
  96. for (i = a->top - 1; i >= 0; i--)
  97. t[nw + i] = f[i];
  98. else
  99. for (i = a->top - 1; i >= 0; i--) {
  100. l = f[i];
  101. t[nw + i + 1] |= (l >> rb) & BN_MASK2;
  102. t[nw + i] = (l << lb) & BN_MASK2;
  103. }
  104. memset(t, 0, sizeof(*t) * nw);
  105. r->top = a->top + nw + 1;
  106. bn_correct_top(r);
  107. bn_check_top(r);
  108. return (1);
  109. }
  110. int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
  111. {
  112. int i, j, nw, lb, rb;
  113. BN_ULONG *t, *f;
  114. BN_ULONG l, tmp;
  115. bn_check_top(r);
  116. bn_check_top(a);
  117. if (n < 0) {
  118. BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);
  119. return 0;
  120. }
  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. if (bn_wexpand(r, i) == NULL)
  131. return (0);
  132. r->neg = a->neg;
  133. } else {
  134. if (n == 0)
  135. return 1; /* or the copying loop will go berserk */
  136. }
  137. f = &(a->d[nw]);
  138. t = r->d;
  139. j = a->top - nw;
  140. r->top = i;
  141. if (rb == 0) {
  142. for (i = j; i != 0; i--)
  143. *(t++) = *(f++);
  144. } else {
  145. l = *(f++);
  146. for (i = j - 1; i != 0; i--) {
  147. tmp = (l >> rb) & BN_MASK2;
  148. l = *(f++);
  149. *(t++) = (tmp | (l << lb)) & BN_MASK2;
  150. }
  151. if ((l = (l >> rb) & BN_MASK2))
  152. *(t) = l;
  153. }
  154. if (!r->top)
  155. r->neg = 0; /* don't allow negative zero */
  156. bn_check_top(r);
  157. return (1);
  158. }