blob: 56241e3b992dd704464dffe8c7ddf1e3b97c0b77 [file] [log] [blame]
Robert Sloan8ff03552017-06-14 12:40:58 -07001/* 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
65BIGNUM *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) {
Robert Sloanab8b8882018-03-26 11:39:51 -070080 ret->width = 0;
Robert Sloan8ff03552017-06-14 12:40:58 -070081 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
Robert Sloan8f860b12017-08-28 07:37:06 -070093 // |bn_wexpand| must check bounds on |num_words| to write it into
94 // |ret->dmax|.
Robert Sloan8ff03552017-06-14 12:40:58 -070095 assert(num_words <= INT_MAX);
Robert Sloanab8b8882018-03-26 11:39:51 -070096 ret->width = (int)num_words;
Robert Sloan8ff03552017-06-14 12:40:58 -070097 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
Robert Sloan8ff03552017-06-14 12:40:58 -0700108 return ret;
109}
110
111BIGNUM *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) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700123 ret->width = 0;
Robert Sloan8ff03552017-06-14 12:40:58 -0700124 ret->neg = 0;
125 return ret;
126 }
127
Robert Sloan8f860b12017-08-28 07:37:06 -0700128 // Reserve enough space in |ret|.
Robert Sloan8ff03552017-06-14 12:40:58 -0700129 size_t num_words = ((len - 1) / BN_BYTES) + 1;
130 if (!bn_wexpand(ret, num_words)) {
131 BN_free(bn);
132 return NULL;
133 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700134 ret->width = num_words;
Robert Sloan8ff03552017-06-14 12:40:58 -0700135
Robert Sloan8f860b12017-08-28 07:37:06 -0700136 // Make sure the top bytes will be zeroed.
Robert Sloan8ff03552017-06-14 12:40:58 -0700137 ret->d[num_words - 1] = 0;
138
Robert Sloan8f860b12017-08-28 07:37:06 -0700139 // We only support little-endian platforms, so we can simply memcpy the
140 // internal representation.
Robert Sloan8ff03552017-06-14 12:40:58 -0700141 OPENSSL_memcpy(ret->d, in, len);
Robert Sloan8ff03552017-06-14 12:40:58 -0700142 return ret;
143}
144
145size_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
Robert Sloanab8b8882018-03-26 11:39:51 -0700157static 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];
Robert Sloan8542c082018-02-05 09:07:34 -0800161 }
162 return mask == 0;
163}
164
165int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700166 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;
Robert Sloan8542c082018-02-05 09:07:34 -0800173 }
174
175 // We only support little-endian platforms, so we can simply memcpy into the
176 // internal representation.
Robert Sloanab8b8882018-03-26 11:39:51 -0700177 OPENSSL_memcpy(out, bytes, num_bytes);
Robert Sloan8542c082018-02-05 09:07:34 -0800178 // Pad out the rest of the buffer with zeroes.
Robert Sloanab8b8882018-03-26 11:39:51 -0700179 OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
Robert Sloan8542c082018-02-05 09:07:34 -0800180 return 1;
181}
182
183int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700184 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;
Robert Sloan8ff03552017-06-14 12:40:58 -0700191 }
192
Robert Sloanab8b8882018-03-26 11:39:51 -0700193 // 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];
Robert Sloan8ff03552017-06-14 12:40:58 -0700197 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700198 // Pad out the rest of the buffer with zeroes.
199 OPENSSL_memset(out, 0, len - num_bytes);
Robert Sloan8ff03552017-06-14 12:40:58 -0700200 return 1;
201}
202
203BN_ULONG BN_get_word(const BIGNUM *bn) {
Robert Sloan8542c082018-02-05 09:07:34 -0800204 switch (bn_minimal_width(bn)) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700205 case 0:
206 return 0;
207 case 1:
208 return bn->d[0];
209 default:
210 return BN_MASK2;
211 }
212}
213
214int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
Robert Sloan8542c082018-02-05 09:07:34 -0800215 switch (bn_minimal_width(bn)) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700216 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}