• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
bn_big_endian_to_words(BN_ULONG * out,size_t out_len,const uint8_t * in,size_t in_len)64 void bn_big_endian_to_words(BN_ULONG *out, size_t out_len, const uint8_t *in,
65                             size_t in_len) {
66   for (size_t i = 0; i < out_len; i++) {
67     if (in_len < sizeof(BN_ULONG)) {
68       // Load the last partial word.
69       BN_ULONG word = 0;
70       for (size_t j = 0; j < in_len; j++) {
71         word = (word << 8) | in[j];
72       }
73       in_len = 0;
74       out[i] = word;
75       // Fill the remainder with zeros.
76       OPENSSL_memset(out + i + 1, 0, (out_len - i - 1) * sizeof(BN_ULONG));
77       break;
78     }
79 
80     in_len -= sizeof(BN_ULONG);
81     out[i] = CRYPTO_load_word_be(in + in_len);
82   }
83 
84   // The caller should have sized the output to avoid truncation.
85   assert(in_len == 0);
86 }
87 
BN_bin2bn(const uint8_t * in,size_t len,BIGNUM * ret)88 BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
89   BIGNUM *bn = NULL;
90   if (ret == NULL) {
91     bn = BN_new();
92     if (bn == NULL) {
93       return NULL;
94     }
95     ret = bn;
96   }
97 
98   if (len == 0) {
99     ret->width = 0;
100     return ret;
101   }
102 
103   size_t num_words = ((len - 1) / BN_BYTES) + 1;
104   if (!bn_wexpand(ret, num_words)) {
105     BN_free(bn);
106     return NULL;
107   }
108 
109   // |bn_wexpand| must check bounds on |num_words| to write it into
110   // |ret->dmax|.
111   assert(num_words <= INT_MAX);
112   ret->width = (int)num_words;
113   ret->neg = 0;
114 
115   bn_big_endian_to_words(ret->d, ret->width, in, len);
116   return ret;
117 }
118 
BN_le2bn(const uint8_t * in,size_t len,BIGNUM * ret)119 BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
120   BIGNUM *bn = NULL;
121   if (ret == NULL) {
122     bn = BN_new();
123     if (bn == NULL) {
124       return NULL;
125     }
126     ret = bn;
127   }
128 
129   if (len == 0) {
130     ret->width = 0;
131     ret->neg = 0;
132     return ret;
133   }
134 
135   // Reserve enough space in |ret|.
136   size_t num_words = ((len - 1) / BN_BYTES) + 1;
137   if (!bn_wexpand(ret, num_words)) {
138     BN_free(bn);
139     return NULL;
140   }
141   ret->width = (int)num_words;
142 
143   // Make sure the top bytes will be zeroed.
144   ret->d[num_words - 1] = 0;
145 
146   // We only support little-endian platforms, so we can simply memcpy the
147   // internal representation.
148   OPENSSL_memcpy(ret->d, in, len);
149   return ret;
150 }
151 
152 // fits_in_bytes returns one if the |num_words| words in |words| can be
153 // represented in |num_bytes| bytes.
fits_in_bytes(const BN_ULONG * words,size_t num_words,size_t num_bytes)154 static int fits_in_bytes(const BN_ULONG *words, size_t num_words,
155                          size_t num_bytes) {
156   const uint8_t *bytes = (const uint8_t *)words;
157   size_t tot_bytes = num_words * sizeof(BN_ULONG);
158   uint8_t mask = 0;
159   for (size_t i = num_bytes; i < tot_bytes; i++) {
160     mask |= bytes[i];
161   }
162   return mask == 0;
163 }
164 
bn_assert_fits_in_bytes(const BIGNUM * bn,size_t num)165 void bn_assert_fits_in_bytes(const BIGNUM *bn, size_t num) {
166   const uint8_t *bytes = (const uint8_t *)bn->d;
167   size_t tot_bytes = bn->width * sizeof(BN_ULONG);
168   if (tot_bytes > num) {
169     CONSTTIME_DECLASSIFY(bytes + num, tot_bytes - num);
170     for (size_t i = num; i < tot_bytes; i++) {
171       assert(bytes[i] == 0);
172     }
173     (void)bytes;
174   }
175 }
176 
bn_words_to_big_endian(uint8_t * out,size_t out_len,const BN_ULONG * in,size_t in_len)177 void bn_words_to_big_endian(uint8_t *out, size_t out_len, const BN_ULONG *in,
178                             size_t in_len) {
179   // The caller should have selected an output length without truncation.
180   assert(fits_in_bytes(in, in_len, out_len));
181 
182   // We only support little-endian platforms, so the internal representation is
183   // also little-endian as bytes. We can simply copy it in reverse.
184   const uint8_t *bytes = (const uint8_t *)in;
185   size_t num_bytes = in_len * sizeof(BN_ULONG);
186   if (out_len < num_bytes) {
187     num_bytes = out_len;
188   }
189 
190   for (size_t i = 0; i < num_bytes; i++) {
191     out[out_len - i - 1] = bytes[i];
192   }
193   // Pad out the rest of the buffer with zeroes.
194   OPENSSL_memset(out, 0, out_len - num_bytes);
195 }
196 
BN_bn2bin(const BIGNUM * in,uint8_t * out)197 size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
198   size_t n = BN_num_bytes(in);
199   bn_words_to_big_endian(out, n, in->d, in->width);
200   return n;
201 }
202 
BN_bn2le_padded(uint8_t * out,size_t len,const BIGNUM * in)203 int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
204   if (!fits_in_bytes(in->d, in->width, len)) {
205     return 0;
206   }
207 
208   // We only support little-endian platforms, so we can simply memcpy into the
209   // internal representation.
210   const uint8_t *bytes = (const uint8_t *)in->d;
211   size_t num_bytes = in->width * BN_BYTES;
212   if (len < num_bytes) {
213     num_bytes = len;
214   }
215 
216   OPENSSL_memcpy(out, bytes, num_bytes);
217   // Pad out the rest of the buffer with zeroes.
218   OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
219   return 1;
220 }
221 
BN_bn2bin_padded(uint8_t * out,size_t len,const BIGNUM * in)222 int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
223   if (!fits_in_bytes(in->d, in->width, len)) {
224     return 0;
225   }
226 
227   bn_words_to_big_endian(out, len, in->d, in->width);
228   return 1;
229 }
230 
BN_get_word(const BIGNUM * bn)231 BN_ULONG BN_get_word(const BIGNUM *bn) {
232   switch (bn_minimal_width(bn)) {
233     case 0:
234       return 0;
235     case 1:
236       return bn->d[0];
237     default:
238       return BN_MASK2;
239   }
240 }
241 
BN_get_u64(const BIGNUM * bn,uint64_t * out)242 int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
243   switch (bn_minimal_width(bn)) {
244     case 0:
245       *out = 0;
246       return 1;
247     case 1:
248       *out = bn->d[0];
249       return 1;
250 #if defined(OPENSSL_32_BIT)
251     case 2:
252       *out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32);
253       return 1;
254 #endif
255     default:
256       return 0;
257   }
258 }
259