25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

bytes.c 6.6 KiB

Add initial support for non-minimal BIGNUMs. Thanks to Andres Erbsen for extremely helpful suggestions on how finally plug this long-standing hole! OpenSSL BIGNUMs are currently minimal-width, which means they cannot be constant-time. We'll need to either excise BIGNUM from RSA and EC or somehow fix BIGNUM. EC_SCALAR and later EC_FELEM work will excise it from EC, but RSA's BIGNUMs are more transparent. Teaching BIGNUM to handle non-minimal word widths is probably simpler. The main constraint is BIGNUM's large "calculator" API surface. One could, in theory, do arbitrary math on RSA components, which means all public functions must tolerate non-minimal inputs. This is also useful for EC; https://boringssl-review.googlesource.com/c/boringssl/+/24445 is silly. As a first step, fix comparison-type functions that were assuming minimal BIGNUMs. I've also added bn_resize_words, but it is testing-only until the rest of the library is fixed. bn->top is now a loose upper bound we carry around. It does not affect numerical results, only performance and secrecy. This is a departure from the original meaning, and compiler help in auditing everything is nice, so the final change in this series will rename bn->top to bn->width. Thus these new functions are named per "width", not "top". Looking further ahead, how are output BIGNUM widths determined? There's three notions of correctness here: 1. Do I compute the right answer for all widths? 2. Do I handle secret data in constant time? 3. Does my memory usage not balloon absurdly? For (1), a BIGNUM function must give the same answer for all input widths. BN_mod_add_quick may assume |a| < |m|, but |a| may still be wider than |m| by way of leading zeres. The simplest approach is to write code in a width-agnostic way and rely on functions to accept all widths. Where functions need to look at bn->d, we'll a few helper functions to smooth over funny widths. For (2), (1) is little cumbersome. Consider constant-time modular addition. A sane type system would guarantee input widths match. But C is weak here, and bifurcating the internals is a lot of work. Thus, at least for now, I do not propose we move RSA's internal computation out of BIGNUM. (EC_SCALAR/EC_FELEM are valuable for EC because we get to stack-allocate, curves were already specialized, and EC only has two types with many operations on those types. None of these apply to RSA. We've got numbers mod n, mod p, mod q, and their corresponding exponents, each of which is used for basically one operation.) Instead, constant-time BIGNUM functions will output non-minimal widths. This is trivial for BN_bin2bn or modular arithmetic. But for BN_mul, constant-time[*] would dictate r->top = a->top + b->top. A calculator repeatedly multiplying by one would then run out of memory. Those we'll split into a private BN_mul_fixed for crypto, leaving BN_mul for calculators. BN_mul is just BN_mul_fixed followed by bn_correct_top. [*] BN_mul is not constant-time for other reasons, but that will be fixed separately. Bug: 232 Change-Id: Ide2258ae8c09a9a41bb71d6777908d1c27917069 Reviewed-on: https://boringssl-review.googlesource.com/25244 Reviewed-by: Adam Langley <agl@google.com>
6 yıl önce
Add initial support for non-minimal BIGNUMs. Thanks to Andres Erbsen for extremely helpful suggestions on how finally plug this long-standing hole! OpenSSL BIGNUMs are currently minimal-width, which means they cannot be constant-time. We'll need to either excise BIGNUM from RSA and EC or somehow fix BIGNUM. EC_SCALAR and later EC_FELEM work will excise it from EC, but RSA's BIGNUMs are more transparent. Teaching BIGNUM to handle non-minimal word widths is probably simpler. The main constraint is BIGNUM's large "calculator" API surface. One could, in theory, do arbitrary math on RSA components, which means all public functions must tolerate non-minimal inputs. This is also useful for EC; https://boringssl-review.googlesource.com/c/boringssl/+/24445 is silly. As a first step, fix comparison-type functions that were assuming minimal BIGNUMs. I've also added bn_resize_words, but it is testing-only until the rest of the library is fixed. bn->top is now a loose upper bound we carry around. It does not affect numerical results, only performance and secrecy. This is a departure from the original meaning, and compiler help in auditing everything is nice, so the final change in this series will rename bn->top to bn->width. Thus these new functions are named per "width", not "top". Looking further ahead, how are output BIGNUM widths determined? There's three notions of correctness here: 1. Do I compute the right answer for all widths? 2. Do I handle secret data in constant time? 3. Does my memory usage not balloon absurdly? For (1), a BIGNUM function must give the same answer for all input widths. BN_mod_add_quick may assume |a| < |m|, but |a| may still be wider than |m| by way of leading zeres. The simplest approach is to write code in a width-agnostic way and rely on functions to accept all widths. Where functions need to look at bn->d, we'll a few helper functions to smooth over funny widths. For (2), (1) is little cumbersome. Consider constant-time modular addition. A sane type system would guarantee input widths match. But C is weak here, and bifurcating the internals is a lot of work. Thus, at least for now, I do not propose we move RSA's internal computation out of BIGNUM. (EC_SCALAR/EC_FELEM are valuable for EC because we get to stack-allocate, curves were already specialized, and EC only has two types with many operations on those types. None of these apply to RSA. We've got numbers mod n, mod p, mod q, and their corresponding exponents, each of which is used for basically one operation.) Instead, constant-time BIGNUM functions will output non-minimal widths. This is trivial for BN_bin2bn or modular arithmetic. But for BN_mul, constant-time[*] would dictate r->top = a->top + b->top. A calculator repeatedly multiplying by one would then run out of memory. Those we'll split into a private BN_mul_fixed for crypto, leaving BN_mul for calculators. BN_mul is just BN_mul_fixed followed by bn_correct_top. [*] BN_mul is not constant-time for other reasons, but that will be fixed separately. Bug: 232 Change-Id: Ide2258ae8c09a9a41bb71d6777908d1c27917069 Reviewed-on: https://boringssl-review.googlesource.com/25244 Reviewed-by: Adam Langley <agl@google.com>
6 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <assert.h>
  58. #include <limits.h>
  59. #include "internal.h"
  60. BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
  61. size_t num_words;
  62. unsigned m;
  63. BN_ULONG word = 0;
  64. BIGNUM *bn = NULL;
  65. if (ret == NULL) {
  66. ret = bn = BN_new();
  67. }
  68. if (ret == NULL) {
  69. return NULL;
  70. }
  71. if (len == 0) {
  72. ret->width = 0;
  73. return ret;
  74. }
  75. num_words = ((len - 1) / BN_BYTES) + 1;
  76. m = (len - 1) % BN_BYTES;
  77. if (!bn_wexpand(ret, num_words)) {
  78. if (bn) {
  79. BN_free(bn);
  80. }
  81. return NULL;
  82. }
  83. // |bn_wexpand| must check bounds on |num_words| to write it into
  84. // |ret->dmax|.
  85. assert(num_words <= INT_MAX);
  86. ret->width = (int)num_words;
  87. ret->neg = 0;
  88. while (len--) {
  89. word = (word << 8) | *(in++);
  90. if (m-- == 0) {
  91. ret->d[--num_words] = word;
  92. word = 0;
  93. m = BN_BYTES - 1;
  94. }
  95. }
  96. return ret;
  97. }
  98. BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
  99. BIGNUM *bn = NULL;
  100. if (ret == NULL) {
  101. bn = BN_new();
  102. ret = bn;
  103. }
  104. if (ret == NULL) {
  105. return NULL;
  106. }
  107. if (len == 0) {
  108. ret->width = 0;
  109. ret->neg = 0;
  110. return ret;
  111. }
  112. // Reserve enough space in |ret|.
  113. size_t num_words = ((len - 1) / BN_BYTES) + 1;
  114. if (!bn_wexpand(ret, num_words)) {
  115. BN_free(bn);
  116. return NULL;
  117. }
  118. ret->width = num_words;
  119. // Make sure the top bytes will be zeroed.
  120. ret->d[num_words - 1] = 0;
  121. // We only support little-endian platforms, so we can simply memcpy the
  122. // internal representation.
  123. OPENSSL_memcpy(ret->d, in, len);
  124. return ret;
  125. }
  126. size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
  127. size_t n, i;
  128. BN_ULONG l;
  129. n = i = BN_num_bytes(in);
  130. while (i--) {
  131. l = in->d[i / BN_BYTES];
  132. *(out++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
  133. }
  134. return n;
  135. }
  136. static int fits_in_bytes(const uint8_t *bytes, size_t num_bytes, size_t len) {
  137. uint8_t mask = 0;
  138. for (size_t i = len; i < num_bytes; i++) {
  139. mask |= bytes[i];
  140. }
  141. return mask == 0;
  142. }
  143. int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
  144. const uint8_t *bytes = (const uint8_t *)in->d;
  145. size_t num_bytes = in->width * BN_BYTES;
  146. if (len < num_bytes) {
  147. if (!fits_in_bytes(bytes, num_bytes, len)) {
  148. return 0;
  149. }
  150. num_bytes = len;
  151. }
  152. // We only support little-endian platforms, so we can simply memcpy into the
  153. // internal representation.
  154. OPENSSL_memcpy(out, bytes, num_bytes);
  155. // Pad out the rest of the buffer with zeroes.
  156. OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
  157. return 1;
  158. }
  159. int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
  160. const uint8_t *bytes = (const uint8_t *)in->d;
  161. size_t num_bytes = in->width * BN_BYTES;
  162. if (len < num_bytes) {
  163. if (!fits_in_bytes(bytes, num_bytes, len)) {
  164. return 0;
  165. }
  166. num_bytes = len;
  167. }
  168. // We only support little-endian platforms, so we can simply write the buffer
  169. // in reverse.
  170. for (size_t i = 0; i < num_bytes; i++) {
  171. out[len - i - 1] = bytes[i];
  172. }
  173. // Pad out the rest of the buffer with zeroes.
  174. OPENSSL_memset(out, 0, len - num_bytes);
  175. return 1;
  176. }
  177. BN_ULONG BN_get_word(const BIGNUM *bn) {
  178. switch (bn_minimal_width(bn)) {
  179. case 0:
  180. return 0;
  181. case 1:
  182. return bn->d[0];
  183. default:
  184. return BN_MASK2;
  185. }
  186. }
  187. int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
  188. switch (bn_minimal_width(bn)) {
  189. case 0:
  190. *out = 0;
  191. return 1;
  192. case 1:
  193. *out = bn->d[0];
  194. return 1;
  195. #if defined(OPENSSL_32_BIT)
  196. case 2:
  197. *out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32);
  198. return 1;
  199. #endif
  200. default:
  201. return 0;
  202. }
  203. }