shift.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <openssl/err.h>
  59. #include <openssl/type_check.h>
  60. #include "internal.h"
  61. int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) {
  62. int i, nw, lb, rb;
  63. BN_ULONG *t, *f;
  64. BN_ULONG l;
  65. if (n < 0) {
  66. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  67. return 0;
  68. }
  69. r->neg = a->neg;
  70. nw = n / BN_BITS2;
  71. if (!bn_wexpand(r, a->width + nw + 1)) {
  72. return 0;
  73. }
  74. lb = n % BN_BITS2;
  75. rb = BN_BITS2 - lb;
  76. f = a->d;
  77. t = r->d;
  78. t[a->width + nw] = 0;
  79. if (lb == 0) {
  80. for (i = a->width - 1; i >= 0; i--) {
  81. t[nw + i] = f[i];
  82. }
  83. } else {
  84. for (i = a->width - 1; i >= 0; i--) {
  85. l = f[i];
  86. t[nw + i + 1] |= l >> rb;
  87. t[nw + i] = l << lb;
  88. }
  89. }
  90. OPENSSL_memset(t, 0, nw * sizeof(t[0]));
  91. r->width = a->width + nw + 1;
  92. bn_set_minimal_width(r);
  93. return 1;
  94. }
  95. int BN_lshift1(BIGNUM *r, const BIGNUM *a) {
  96. BN_ULONG *ap, *rp, t, c;
  97. int i;
  98. if (r != a) {
  99. r->neg = a->neg;
  100. if (!bn_wexpand(r, a->width + 1)) {
  101. return 0;
  102. }
  103. r->width = a->width;
  104. } else {
  105. if (!bn_wexpand(r, a->width + 1)) {
  106. return 0;
  107. }
  108. }
  109. ap = a->d;
  110. rp = r->d;
  111. c = 0;
  112. for (i = 0; i < a->width; i++) {
  113. t = *(ap++);
  114. *(rp++) = (t << 1) | c;
  115. c = t >> (BN_BITS2 - 1);
  116. }
  117. if (c) {
  118. *rp = 1;
  119. r->width++;
  120. }
  121. return 1;
  122. }
  123. static void bn_rshift_words(BN_ULONG *r, const BN_ULONG *a, unsigned shift,
  124. size_t num) {
  125. unsigned shift_bits = shift % BN_BITS2;
  126. size_t shift_words = shift / BN_BITS2;
  127. if (shift_words >= num) {
  128. OPENSSL_memset(r, 0, num * sizeof(BN_ULONG));
  129. return;
  130. }
  131. if (shift_bits == 0) {
  132. OPENSSL_memmove(r, a + shift_words, (num - shift_words) * sizeof(BN_ULONG));
  133. } else {
  134. for (size_t i = shift_words; i < num - 1; i++) {
  135. r[i - shift_words] =
  136. (a[i] >> shift_bits) | (a[i + 1] << (BN_BITS2 - shift_bits));
  137. }
  138. r[num - 1 - shift_words] = a[num - 1] >> shift_bits;
  139. }
  140. OPENSSL_memset(r + num - shift_words, 0, shift_words * sizeof(BN_ULONG));
  141. }
  142. int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) {
  143. if (n < 0) {
  144. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  145. return 0;
  146. }
  147. if (!bn_wexpand(r, a->width)) {
  148. return 0;
  149. }
  150. bn_rshift_words(r->d, a->d, n, a->width);
  151. r->neg = a->neg;
  152. r->width = a->width;
  153. bn_set_minimal_width(r);
  154. return 1;
  155. }
  156. int bn_rshift_secret_shift(BIGNUM *r, const BIGNUM *a, unsigned n,
  157. BN_CTX *ctx) {
  158. int ret = 0;
  159. BN_CTX_start(ctx);
  160. BIGNUM *tmp = BN_CTX_get(ctx);
  161. if (tmp == NULL ||
  162. !BN_copy(r, a) ||
  163. !bn_wexpand(tmp, r->width)) {
  164. goto err;
  165. }
  166. // Shift conditionally by powers of two.
  167. unsigned max_bits = BN_BITS2 * r->width;
  168. for (unsigned i = 0; (max_bits >> i) != 0; i++) {
  169. BN_ULONG mask = (n >> i) & 1;
  170. mask = 0 - mask;
  171. bn_rshift_words(tmp->d, r->d, 1u << i, r->width);
  172. bn_select_words(r->d, mask, tmp->d /* apply shift */,
  173. r->d /* ignore shift */, r->width);
  174. }
  175. ret = 1;
  176. err:
  177. BN_CTX_end(ctx);
  178. return ret;
  179. }
  180. void bn_rshift1_words(BN_ULONG *r, const BN_ULONG *a, size_t num) {
  181. if (num == 0) {
  182. return;
  183. }
  184. for (size_t i = 0; i < num - 1; i++) {
  185. r[i] = (a[i] >> 1) | (a[i + 1] << (BN_BITS2 - 1));
  186. }
  187. r[num - 1] = a[num - 1] >> 1;
  188. }
  189. int BN_rshift1(BIGNUM *r, const BIGNUM *a) {
  190. if (!bn_wexpand(r, a->width)) {
  191. return 0;
  192. }
  193. bn_rshift1_words(r->d, a->d, a->width);
  194. r->width = a->width;
  195. r->neg = a->neg;
  196. bn_set_minimal_width(r);
  197. return 1;
  198. }
  199. int BN_set_bit(BIGNUM *a, int n) {
  200. if (n < 0) {
  201. return 0;
  202. }
  203. int i = n / BN_BITS2;
  204. int j = n % BN_BITS2;
  205. if (a->width <= i) {
  206. if (!bn_wexpand(a, i + 1)) {
  207. return 0;
  208. }
  209. for (int k = a->width; k < i + 1; k++) {
  210. a->d[k] = 0;
  211. }
  212. a->width = i + 1;
  213. }
  214. a->d[i] |= (((BN_ULONG)1) << j);
  215. return 1;
  216. }
  217. int BN_clear_bit(BIGNUM *a, int n) {
  218. int i, j;
  219. if (n < 0) {
  220. return 0;
  221. }
  222. i = n / BN_BITS2;
  223. j = n % BN_BITS2;
  224. if (a->width <= i) {
  225. return 0;
  226. }
  227. a->d[i] &= (~(((BN_ULONG)1) << j));
  228. bn_set_minimal_width(a);
  229. return 1;
  230. }
  231. int bn_is_bit_set_words(const BN_ULONG *a, size_t num, unsigned bit) {
  232. unsigned i = bit / BN_BITS2;
  233. unsigned j = bit % BN_BITS2;
  234. if (i >= num) {
  235. return 0;
  236. }
  237. return (a[i] >> j) & 1;
  238. }
  239. int BN_is_bit_set(const BIGNUM *a, int n) {
  240. if (n < 0) {
  241. return 0;
  242. }
  243. return bn_is_bit_set_words(a->d, a->width, n);
  244. }
  245. int BN_mask_bits(BIGNUM *a, int n) {
  246. if (n < 0) {
  247. return 0;
  248. }
  249. int w = n / BN_BITS2;
  250. int b = n % BN_BITS2;
  251. if (w >= a->width) {
  252. return 1;
  253. }
  254. if (b == 0) {
  255. a->width = w;
  256. } else {
  257. a->width = w + 1;
  258. a->d[w] &= ~(BN_MASK2 << b);
  259. }
  260. bn_set_minimal_width(a);
  261. return 1;
  262. }
  263. static int bn_count_low_zero_bits_word(BN_ULONG l) {
  264. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
  265. crypto_word_t_too_small);
  266. OPENSSL_COMPILE_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
  267. crypto_word_t_too_small_2);
  268. OPENSSL_COMPILE_ASSERT(BN_BITS2 == sizeof(BN_ULONG) * 8,
  269. bn_ulong_has_padding_bits);
  270. // C has very bizarre rules for types smaller than an int.
  271. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) >= sizeof(int),
  272. bn_ulong_is_promoted_to_int);
  273. crypto_word_t mask;
  274. int bits = 0;
  275. #if BN_BITS2 > 32
  276. // Check if the lower half of |x| are all zero.
  277. mask = constant_time_is_zero_w(l << (BN_BITS2 - 32));
  278. // If the lower half is all zeros, it is included in the bit count and we
  279. // count the upper half. Otherwise, we count the lower half.
  280. bits += 32 & mask;
  281. l = constant_time_select_w(mask, l >> 32, l);
  282. #endif
  283. // The remaining blocks are analogous iterations at lower powers of two.
  284. mask = constant_time_is_zero_w(l << (BN_BITS2 - 16));
  285. bits += 16 & mask;
  286. l = constant_time_select_w(mask, l >> 16, l);
  287. mask = constant_time_is_zero_w(l << (BN_BITS2 - 8));
  288. bits += 8 & mask;
  289. l = constant_time_select_w(mask, l >> 8, l);
  290. mask = constant_time_is_zero_w(l << (BN_BITS2 - 4));
  291. bits += 4 & mask;
  292. l = constant_time_select_w(mask, l >> 4, l);
  293. mask = constant_time_is_zero_w(l << (BN_BITS2 - 2));
  294. bits += 2 & mask;
  295. l = constant_time_select_w(mask, l >> 2, l);
  296. mask = constant_time_is_zero_w(l << (BN_BITS2 - 1));
  297. bits += 1 & mask;
  298. return bits;
  299. }
  300. int BN_count_low_zero_bits(const BIGNUM *bn) {
  301. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
  302. crypto_word_t_too_small);
  303. OPENSSL_COMPILE_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
  304. crypto_word_t_too_small_2);
  305. int ret = 0;
  306. crypto_word_t saw_nonzero = 0;
  307. for (int i = 0; i < bn->width; i++) {
  308. crypto_word_t nonzero = ~constant_time_is_zero_w(bn->d[i]);
  309. crypto_word_t first_nonzero = ~saw_nonzero & nonzero;
  310. saw_nonzero |= nonzero;
  311. int bits = bn_count_low_zero_bits_word(bn->d[i]);
  312. ret |= first_nonzero & (i * BN_BITS2 + bits);
  313. }
  314. // If got to the end of |bn| and saw no non-zero words, |bn| is zero. |ret|
  315. // will then remain zero.
  316. return ret;
  317. }