bn_shift.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 1995-2018 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 <assert.h>
  10. #include "internal/cryptlib.h"
  11. #include "bn_lcl.h"
  12. int BN_lshift1(BIGNUM *r, const BIGNUM *a)
  13. {
  14. register BN_ULONG *ap, *rp, t, c;
  15. int i;
  16. bn_check_top(r);
  17. bn_check_top(a);
  18. if (r != a) {
  19. r->neg = a->neg;
  20. if (bn_wexpand(r, a->top + 1) == NULL)
  21. return 0;
  22. r->top = a->top;
  23. } else {
  24. if (bn_wexpand(r, a->top + 1) == NULL)
  25. return 0;
  26. }
  27. ap = a->d;
  28. rp = r->d;
  29. c = 0;
  30. for (i = 0; i < a->top; i++) {
  31. t = *(ap++);
  32. *(rp++) = ((t << 1) | c) & BN_MASK2;
  33. c = (t & BN_TBIT) ? 1 : 0;
  34. }
  35. if (c) {
  36. *rp = 1;
  37. r->top++;
  38. }
  39. bn_check_top(r);
  40. return 1;
  41. }
  42. int BN_rshift1(BIGNUM *r, const BIGNUM *a)
  43. {
  44. BN_ULONG *ap, *rp, t, c;
  45. int i, j;
  46. bn_check_top(r);
  47. bn_check_top(a);
  48. if (BN_is_zero(a)) {
  49. BN_zero(r);
  50. return 1;
  51. }
  52. i = a->top;
  53. ap = a->d;
  54. j = i - (ap[i - 1] == 1);
  55. if (a != r) {
  56. if (bn_wexpand(r, j) == NULL)
  57. return 0;
  58. r->neg = a->neg;
  59. }
  60. rp = r->d;
  61. t = ap[--i];
  62. c = (t & 1) ? BN_TBIT : 0;
  63. if (t >>= 1)
  64. rp[i] = t;
  65. while (i > 0) {
  66. t = ap[--i];
  67. rp[i] = ((t >> 1) & BN_MASK2) | c;
  68. c = (t & 1) ? BN_TBIT : 0;
  69. }
  70. r->top = j;
  71. if (!r->top)
  72. r->neg = 0; /* don't allow negative zero */
  73. bn_check_top(r);
  74. return 1;
  75. }
  76. int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
  77. {
  78. int ret;
  79. if (n < 0) {
  80. BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
  81. return 0;
  82. }
  83. ret = bn_lshift_fixed_top(r, a, n);
  84. bn_correct_top(r);
  85. bn_check_top(r);
  86. return ret;
  87. }
  88. /*
  89. * In respect to shift factor the execution time is invariant of
  90. * |n % BN_BITS2|, but not |n / BN_BITS2|. Or in other words pre-condition
  91. * for constant-time-ness is |n < BN_BITS2| or |n / BN_BITS2| being
  92. * non-secret.
  93. */
  94. int bn_lshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n)
  95. {
  96. int i, nw;
  97. unsigned int lb, rb;
  98. BN_ULONG *t, *f;
  99. BN_ULONG l, m, rmask = 0;
  100. assert(n >= 0);
  101. bn_check_top(r);
  102. bn_check_top(a);
  103. nw = n / BN_BITS2;
  104. if (bn_wexpand(r, a->top + nw + 1) == NULL)
  105. return 0;
  106. if (a->top != 0) {
  107. lb = (unsigned int)n % BN_BITS2;
  108. rb = BN_BITS2 - lb;
  109. rb %= BN_BITS2; /* say no to undefined behaviour */
  110. rmask = (BN_ULONG)0 - rb; /* rmask = 0 - (rb != 0) */
  111. rmask |= rmask >> 8;
  112. f = &(a->d[0]);
  113. t = &(r->d[nw]);
  114. l = f[a->top - 1];
  115. t[a->top] = (l >> rb) & rmask;
  116. for (i = a->top - 1; i > 0; i--) {
  117. m = l << lb;
  118. l = f[i - 1];
  119. t[i] = (m | ((l >> rb) & rmask)) & BN_MASK2;
  120. }
  121. t[0] = (l << lb) & BN_MASK2;
  122. } else {
  123. /* shouldn't happen, but formally required */
  124. r->d[nw] = 0;
  125. }
  126. if (nw != 0)
  127. memset(r->d, 0, sizeof(*t) * nw);
  128. r->neg = a->neg;
  129. r->top = a->top + nw + 1;
  130. r->flags |= BN_FLG_FIXED_TOP;
  131. return 1;
  132. }
  133. int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
  134. {
  135. int i, j, nw, lb, rb;
  136. BN_ULONG *t, *f;
  137. BN_ULONG l, tmp;
  138. bn_check_top(r);
  139. bn_check_top(a);
  140. if (n < 0) {
  141. BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);
  142. return 0;
  143. }
  144. nw = n / BN_BITS2;
  145. rb = n % BN_BITS2;
  146. lb = BN_BITS2 - rb;
  147. if (nw >= a->top || a->top == 0) {
  148. BN_zero(r);
  149. return 1;
  150. }
  151. i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;
  152. if (r != a) {
  153. if (bn_wexpand(r, i) == NULL)
  154. return 0;
  155. r->neg = a->neg;
  156. } else {
  157. if (n == 0)
  158. return 1; /* or the copying loop will go berserk */
  159. }
  160. f = &(a->d[nw]);
  161. t = r->d;
  162. j = a->top - nw;
  163. r->top = i;
  164. if (rb == 0) {
  165. for (i = j; i != 0; i--)
  166. *(t++) = *(f++);
  167. } else {
  168. l = *(f++);
  169. for (i = j - 1; i != 0; i--) {
  170. tmp = (l >> rb) & BN_MASK2;
  171. l = *(f++);
  172. *(t++) = (tmp | (l << lb)) & BN_MASK2;
  173. }
  174. if ((l = (l >> rb) & BN_MASK2))
  175. *(t) = l;
  176. }
  177. if (!r->top)
  178. r->neg = 0; /* don't allow negative zero */
  179. bn_check_top(r);
  180. return 1;
  181. }
  182. /*
  183. * In respect to shift factor the execution time is invariant of
  184. * |n % BN_BITS2|, but not |n / BN_BITS2|. Or in other words pre-condition
  185. * for constant-time-ness for sufficiently[!] zero-padded inputs is
  186. * |n < BN_BITS2| or |n / BN_BITS2| being non-secret.
  187. */
  188. int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n)
  189. {
  190. int i, top, nw;
  191. unsigned int lb, rb;
  192. BN_ULONG *t, *f;
  193. BN_ULONG l, m, mask;
  194. bn_check_top(r);
  195. bn_check_top(a);
  196. assert(n >= 0);
  197. nw = n / BN_BITS2;
  198. if (nw >= a->top) {
  199. /* shouldn't happen, but formally required */
  200. BN_zero(r);
  201. return 1;
  202. }
  203. rb = (unsigned int)n % BN_BITS2;
  204. lb = BN_BITS2 - rb;
  205. lb %= BN_BITS2; /* say no to undefined behaviour */
  206. mask = (BN_ULONG)0 - lb; /* mask = 0 - (lb != 0) */
  207. mask |= mask >> 8;
  208. top = a->top - nw;
  209. if (r != a && bn_wexpand(r, top) == NULL)
  210. return 0;
  211. t = &(r->d[0]);
  212. f = &(a->d[nw]);
  213. l = f[0];
  214. for (i = 0; i < top - 1; i++) {
  215. m = f[i + 1];
  216. t[i] = (l >> rb) | ((m << lb) & mask);
  217. l = m;
  218. }
  219. t[i] = l >> rb;
  220. r->neg = a->neg;
  221. r->top = top;
  222. r->flags |= BN_FLG_FIXED_TOP;
  223. return 1;
  224. }