• 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 = 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_words_to_big_endian(uint8_t * out,size_t out_len,const BN_ULONG * in,size_t in_len)165 void bn_words_to_big_endian(uint8_t *out, size_t out_len, const BN_ULONG *in,
166                             size_t in_len) {
167   // The caller should have selected an output length without truncation.
168   assert(fits_in_bytes(in, in_len, out_len));
169 
170   // We only support little-endian platforms, so the internal representation is
171   // also little-endian as bytes. We can simply copy it in reverse.
172   const uint8_t *bytes = (const uint8_t *)in;
173   size_t num_bytes = in_len * sizeof(BN_ULONG);
174   if (out_len < num_bytes) {
175     num_bytes = out_len;
176   }
177 
178   for (size_t i = 0; i < num_bytes; i++) {
179     out[out_len - i - 1] = bytes[i];
180   }
181   // Pad out the rest of the buffer with zeroes.
182   OPENSSL_memset(out, 0, out_len - num_bytes);
183 }
184 
BN_bn2bin(const BIGNUM * in,uint8_t * out)185 size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
186   size_t n = BN_num_bytes(in);
187   bn_words_to_big_endian(out, n, in->d, in->width);
188   return n;
189 }
190 
BN_bn2le_padded(uint8_t * out,size_t len,const BIGNUM * in)191 int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
192   if (!fits_in_bytes(in->d, in->width, len)) {
193     return 0;
194   }
195 
196   // We only support little-endian platforms, so we can simply memcpy into the
197   // internal representation.
198   const uint8_t *bytes = (const uint8_t *)in->d;
199   size_t num_bytes = in->width * BN_BYTES;
200   if (len < num_bytes) {
201     num_bytes = len;
202   }
203 
204   OPENSSL_memcpy(out, bytes, num_bytes);
205   // Pad out the rest of the buffer with zeroes.
206   OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
207   return 1;
208 }
209 
BN_bn2bin_padded(uint8_t * out,size_t len,const BIGNUM * in)210 int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
211   if (!fits_in_bytes(in->d, in->width, len)) {
212     return 0;
213   }
214 
215   bn_words_to_big_endian(out, len, in->d, in->width);
216   return 1;
217 }
218 
BN_get_word(const BIGNUM * bn)219 BN_ULONG BN_get_word(const BIGNUM *bn) {
220   switch (bn_minimal_width(bn)) {
221     case 0:
222       return 0;
223     case 1:
224       return bn->d[0];
225     default:
226       return BN_MASK2;
227   }
228 }
229 
BN_get_u64(const BIGNUM * bn,uint64_t * out)230 int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
231   switch (bn_minimal_width(bn)) {
232     case 0:
233       *out = 0;
234       return 1;
235     case 1:
236       *out = bn->d[0];
237       return 1;
238 #if defined(OPENSSL_32_BIT)
239     case 2:
240       *out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32);
241       return 1;
242 #endif
243     default:
244       return 0;
245   }
246 }
247