| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bn.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 |
| 20 | #include "internal.h" |
| 21 |
| 22 | void bn_big_endian_to_words(BN_ULONG *out, size_t out_len, const uint8_t *in, |
| 23 | size_t in_len) { |
| 24 | // The caller should have sized |out| to fit |in| without truncating. This |
| 25 | // condition ensures we do not overflow |out|, so use a runtime check. |
| 26 | BSSL_CHECK(in_len <= out_len * sizeof(BN_ULONG)); |
| 27 |
| 28 | // Load whole words. |
| 29 | while (in_len >= sizeof(BN_ULONG)) { |
| 30 | in_len -= sizeof(BN_ULONG); |
| 31 | out[0] = CRYPTO_load_word_be(in + in_len); |
| 32 | out++; |
| 33 | out_len--; |
| 34 | } |
| 35 |
| 36 | // Load the last partial word. |
| 37 | if (in_len != 0) { |
| 38 | BN_ULONG word = 0; |
| 39 | for (size_t i = 0; i < in_len; i++) { |
| 40 | word = (word << 8) | in[i]; |
| 41 | } |
| 42 | out[0] = word; |
| 43 | out++; |
| 44 | out_len--; |
| 45 | } |
| 46 |
| 47 | // Fill the remainder with zeros. |
| 48 | OPENSSL_memset(out, 0, out_len * sizeof(BN_ULONG)); |
| 49 | } |
| 50 |
| 51 | BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) { |
| 52 | BIGNUM *bn = NULL; |
| 53 | if (ret == NULL) { |
| 54 | bn = BN_new(); |
| 55 | if (bn == NULL) { |
| 56 | return NULL; |
| 57 | } |
| 58 | ret = bn; |
| 59 | } |
| 60 |
| 61 | if (len == 0) { |
| 62 | ret->width = 0; |
| 63 | return ret; |
| 64 | } |
| 65 |
| 66 | size_t num_words = ((len - 1) / BN_BYTES) + 1; |
| 67 | if (!bn_wexpand(ret, num_words)) { |
| 68 | BN_free(bn); |
| 69 | return NULL; |
| 70 | } |
| 71 |
| 72 | // |bn_wexpand| must check bounds on |num_words| to write it into |
| 73 | // |ret->dmax|. |
| 74 | assert(num_words <= INT_MAX); |
| 75 | ret->width = (int)num_words; |
| 76 | ret->neg = 0; |
| 77 |
| 78 | bn_big_endian_to_words(ret->d, ret->width, in, len); |
| 79 | return ret; |
| 80 | } |
| 81 |
| 82 | BIGNUM *BN_lebin2bn(const uint8_t *in, size_t len, BIGNUM *ret) { |
| 83 | BIGNUM *bn = NULL; |
| 84 | if (ret == NULL) { |
| 85 | bn = BN_new(); |
| 86 | if (bn == NULL) { |
| 87 | return NULL; |
| 88 | } |
| 89 | ret = bn; |
| 90 | } |
| 91 |
| 92 | if (len == 0) { |
| 93 | ret->width = 0; |
| 94 | ret->neg = 0; |
| 95 | return ret; |
| 96 | } |
| 97 |
| 98 | // Reserve enough space in |ret|. |
| 99 | size_t num_words = ((len - 1) / BN_BYTES) + 1; |
| 100 | if (!bn_wexpand(ret, num_words)) { |
| 101 | BN_free(bn); |
| 102 | return NULL; |
| 103 | } |
| 104 | ret->width = (int)num_words; |
| 105 |
| 106 | // Make sure the top bytes will be zeroed. |
| 107 | ret->d[num_words - 1] = 0; |
| 108 |
| 109 | // We only support little-endian platforms, so we can simply memcpy the |
| 110 | // internal representation. |
| 111 | OPENSSL_memcpy(ret->d, in, len); |
| 112 | return ret; |
| 113 | } |
| 114 |
| 115 | BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) { |
| 116 | return BN_lebin2bn(in, len, ret); |
| 117 | } |
| 118 |
| 119 | // fits_in_bytes returns one if the |num_words| words in |words| can be |
| 120 | // represented in |num_bytes| bytes. |
| 121 | static int fits_in_bytes(const BN_ULONG *words, size_t num_words, |
| 122 | size_t num_bytes) { |
| 123 | const uint8_t *bytes = (const uint8_t *)words; |
| 124 | size_t tot_bytes = num_words * sizeof(BN_ULONG); |
| 125 | uint8_t mask = 0; |
| 126 | for (size_t i = num_bytes; i < tot_bytes; i++) { |
| 127 | mask |= bytes[i]; |
| 128 | } |
| 129 | return mask == 0; |
| 130 | } |
| 131 |
| 132 | void bn_assert_fits_in_bytes(const BIGNUM *bn, size_t num) { |
| 133 | const uint8_t *bytes = (const uint8_t *)bn->d; |
| 134 | size_t tot_bytes = bn->width * sizeof(BN_ULONG); |
| 135 | if (tot_bytes > num) { |
| 136 | CONSTTIME_DECLASSIFY(bytes + num, tot_bytes - num); |
| 137 | for (size_t i = num; i < tot_bytes; i++) { |
| 138 | assert(bytes[i] == 0); |
| 139 | } |
| 140 | (void)bytes; |
| 141 | } |
| 142 | } |
| 143 |
| 144 | void bn_words_to_big_endian(uint8_t *out, size_t out_len, const BN_ULONG *in, |
| 145 | size_t in_len) { |
| 146 | // The caller should have selected an output length without truncation. |
| 147 | declassify_assert(fits_in_bytes(in, in_len, out_len)); |
| 148 |
| 149 | // We only support little-endian platforms, so the internal representation is |
| 150 | // also little-endian as bytes. We can simply copy it in reverse. |
| 151 | const uint8_t *bytes = (const uint8_t *)in; |
| 152 | size_t num_bytes = in_len * sizeof(BN_ULONG); |
| 153 | if (out_len < num_bytes) { |
| 154 | num_bytes = out_len; |
| 155 | } |
| 156 |
| 157 | for (size_t i = 0; i < num_bytes; i++) { |
| 158 | out[out_len - i - 1] = bytes[i]; |
| 159 | } |
| 160 | // Pad out the rest of the buffer with zeroes. |
| 161 | OPENSSL_memset(out, 0, out_len - num_bytes); |
| 162 | } |
| 163 |
| 164 | size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) { |
| 165 | size_t n = BN_num_bytes(in); |
| 166 | bn_words_to_big_endian(out, n, in->d, in->width); |
| 167 | return n; |
| 168 | } |
| 169 |
| 170 | int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) { |
| 171 | if (!fits_in_bytes(in->d, in->width, len)) { |
| 172 | return 0; |
| 173 | } |
| 174 |
| 175 | // We only support little-endian platforms, so we can simply memcpy into the |
| 176 | // internal representation. |
| 177 | const uint8_t *bytes = (const uint8_t *)in->d; |
| 178 | size_t num_bytes = in->width * BN_BYTES; |
| 179 | if (len < num_bytes) { |
| 180 | num_bytes = len; |
| 181 | } |
| 182 |
| 183 | OPENSSL_memcpy(out, bytes, num_bytes); |
| 184 | // Pad out the rest of the buffer with zeroes. |
| 185 | OPENSSL_memset(out + num_bytes, 0, len - num_bytes); |
| 186 | return 1; |
| 187 | } |
| 188 |
| 189 | int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) { |
| 190 | if (!fits_in_bytes(in->d, in->width, len)) { |
| 191 | return 0; |
| 192 | } |
| 193 |
| 194 | bn_words_to_big_endian(out, len, in->d, in->width); |
| 195 | return 1; |
| 196 | } |
| 197 |
| 198 | BN_ULONG BN_get_word(const BIGNUM *bn) { |
| 199 | switch (bn_minimal_width(bn)) { |
| 200 | case 0: |
| 201 | return 0; |
| 202 | case 1: |
| 203 | return bn->d[0]; |
| 204 | default: |
| 205 | return BN_MASK2; |
| 206 | } |
| 207 | } |
| 208 |
| 209 | int BN_get_u64(const BIGNUM *bn, uint64_t *out) { |
| 210 | switch (bn_minimal_width(bn)) { |
| 211 | case 0: |
| 212 | *out = 0; |
| 213 | return 1; |
| 214 | case 1: |
| 215 | *out = bn->d[0]; |
| 216 | return 1; |
| 217 | #if defined(OPENSSL_32_BIT) |
| 218 | case 2: |
| 219 | *out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32); |
| 220 | return 1; |
| 221 | #endif |
| 222 | default: |
| 223 | return 0; |
| 224 | } |
| 225 | } |
| 226 |