| 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 | |
| 57 | #include <openssl/bn.h> |
| 58 | |
| 59 | #include <assert.h> |
| 60 | #include <limits.h> |
| 61 | |
| 62 | #include "internal.h" |
| 63 | |
| 64 | |
| 65 | BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) { |
| 66 | size_t num_words; |
| 67 | unsigned m; |
| 68 | BN_ULONG word = 0; |
| 69 | BIGNUM *bn = NULL; |
| 70 | |
| 71 | if (ret == NULL) { |
| 72 | ret = bn = BN_new(); |
| 73 | } |
| 74 | |
| 75 | if (ret == NULL) { |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | if (len == 0) { |
| 80 | ret->width = 0; |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | num_words = ((len - 1) / BN_BYTES) + 1; |
| 85 | m = (len - 1) % BN_BYTES; |
| 86 | if (!bn_wexpand(ret, num_words)) { |
| 87 | if (bn) { |
| 88 | BN_free(bn); |
| 89 | } |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | // |bn_wexpand| must check bounds on |num_words| to write it into |
| 94 | // |ret->dmax|. |
| 95 | assert(num_words <= INT_MAX); |
| 96 | ret->width = (int)num_words; |
| 97 | ret->neg = 0; |
| 98 | |
| 99 | while (len--) { |
| 100 | word = (word << 8) | *(in++); |
| 101 | if (m-- == 0) { |
| 102 | ret->d[--num_words] = word; |
| 103 | word = 0; |
| 104 | m = BN_BYTES - 1; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) { |
| 112 | BIGNUM *bn = NULL; |
| 113 | if (ret == NULL) { |
| 114 | bn = BN_new(); |
| 115 | ret = bn; |
| 116 | } |
| 117 | |
| 118 | if (ret == NULL) { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | if (len == 0) { |
| 123 | ret->width = 0; |
| 124 | ret->neg = 0; |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | // Reserve enough space in |ret|. |
| 129 | size_t num_words = ((len - 1) / BN_BYTES) + 1; |
| 130 | if (!bn_wexpand(ret, num_words)) { |
| 131 | BN_free(bn); |
| 132 | return NULL; |
| 133 | } |
| 134 | ret->width = num_words; |
| 135 | |
| 136 | // Make sure the top bytes will be zeroed. |
| 137 | ret->d[num_words - 1] = 0; |
| 138 | |
| 139 | // We only support little-endian platforms, so we can simply memcpy the |
| 140 | // internal representation. |
| 141 | OPENSSL_memcpy(ret->d, in, len); |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) { |
| 146 | size_t n, i; |
| 147 | BN_ULONG l; |
| 148 | |
| 149 | n = i = BN_num_bytes(in); |
| 150 | while (i--) { |
| 151 | l = in->d[i / BN_BYTES]; |
| 152 | *(out++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff; |
| 153 | } |
| 154 | return n; |
| 155 | } |
| 156 | |
| 157 | static int fits_in_bytes(const uint8_t *bytes, size_t num_bytes, size_t len) { |
| 158 | uint8_t mask = 0; |
| 159 | for (size_t i = len; i < num_bytes; i++) { |
| 160 | mask |= bytes[i]; |
| 161 | } |
| 162 | return mask == 0; |
| 163 | } |
| 164 | |
| 165 | int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) { |
| 166 | const uint8_t *bytes = (const uint8_t *)in->d; |
| 167 | size_t num_bytes = in->width * BN_BYTES; |
| 168 | if (len < num_bytes) { |
| 169 | if (!fits_in_bytes(bytes, num_bytes, len)) { |
| 170 | return 0; |
| 171 | } |
| 172 | num_bytes = len; |
| 173 | } |
| 174 | |
| 175 | // We only support little-endian platforms, so we can simply memcpy into the |
| 176 | // internal representation. |
| 177 | OPENSSL_memcpy(out, bytes, num_bytes); |
| 178 | // Pad out the rest of the buffer with zeroes. |
| 179 | OPENSSL_memset(out + num_bytes, 0, len - num_bytes); |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) { |
| 184 | const uint8_t *bytes = (const uint8_t *)in->d; |
| 185 | size_t num_bytes = in->width * BN_BYTES; |
| 186 | if (len < num_bytes) { |
| 187 | if (!fits_in_bytes(bytes, num_bytes, len)) { |
| 188 | return 0; |
| 189 | } |
| 190 | num_bytes = len; |
| 191 | } |
| 192 | |
| 193 | // We only support little-endian platforms, so we can simply write the buffer |
| 194 | // in reverse. |
| 195 | for (size_t i = 0; i < num_bytes; i++) { |
| 196 | out[len - i - 1] = bytes[i]; |
| 197 | } |
| 198 | // Pad out the rest of the buffer with zeroes. |
| 199 | OPENSSL_memset(out, 0, len - num_bytes); |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | BN_ULONG BN_get_word(const BIGNUM *bn) { |
| 204 | switch (bn_minimal_width(bn)) { |
| 205 | case 0: |
| 206 | return 0; |
| 207 | case 1: |
| 208 | return bn->d[0]; |
| 209 | default: |
| 210 | return BN_MASK2; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | int BN_get_u64(const BIGNUM *bn, uint64_t *out) { |
| 215 | switch (bn_minimal_width(bn)) { |
| 216 | case 0: |
| 217 | *out = 0; |
| 218 | return 1; |
| 219 | case 1: |
| 220 | *out = bn->d[0]; |
| 221 | return 1; |
| 222 | #if defined(OPENSSL_32_BIT) |
| 223 | case 2: |
| 224 | *out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32); |
| 225 | return 1; |
| 226 | #endif |
| 227 | default: |
| 228 | return 0; |
| 229 | } |
| 230 | } |
| 231 | |