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 
BN_bin2bn(const uint8_t * in,size_t len,BIGNUM * ret)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 
BN_le2bn(const uint8_t * in,size_t len,BIGNUM * ret)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 #ifdef OPENSSL_BIGENDIAN
140   uint8_t *out = (uint8_t *)ret->d;
141   for (size_t i = 0; i < len; i++)
142     out[i ^ (BN_BYTES-1)] = in[i];
143 #else
144   OPENSSL_memcpy(ret->d, in, len);
145 #endif
146   return ret;
147 }
148 
BN_bn2bin(const BIGNUM * in,uint8_t * out)149 size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
150   size_t n, i;
151   BN_ULONG l;
152 
153   n = i = BN_num_bytes(in);
154   while (i--) {
155     l = in->d[i / BN_BYTES];
156     *(out++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
157   }
158   return n;
159 }
160 
fits_in_bytes(const uint8_t * bytes,size_t num_bytes,size_t len)161 static int fits_in_bytes(const uint8_t *bytes, size_t num_bytes, size_t len) {
162   uint8_t mask = 0;
163   for (size_t i = len; i < num_bytes; i++) {
164 #ifdef OPENSSL_BIGENDIAN
165     mask |= bytes[i ^ (BN_BYTES-1)];
166 #else
167     mask |= bytes[i];
168 #endif
169   }
170   return mask == 0;
171 }
172 
BN_bn2le_padded(uint8_t * out,size_t len,const BIGNUM * in)173 int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
174   const uint8_t *bytes = (const uint8_t *)in->d;
175   size_t num_bytes = in->width * BN_BYTES;
176   if (len < num_bytes) {
177     if (!fits_in_bytes(bytes, num_bytes, len)) {
178       return 0;
179     }
180     num_bytes = len;
181   }
182 
183 #ifdef OPENSSL_BIGENDIAN
184   for (size_t i = 0; i < num_bytes; i++) {
185     out[i] = bytes[i ^ (BN_BYTES-1)];
186   }
187 #else
188   OPENSSL_memcpy(out, bytes, num_bytes);
189 #endif
190   // Pad out the rest of the buffer with zeroes.
191   OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
192   return 1;
193 }
194 
BN_bn2bin_padded(uint8_t * out,size_t len,const BIGNUM * in)195 int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
196   const uint8_t *bytes = (const uint8_t *)in->d;
197   size_t num_bytes = in->width * BN_BYTES;
198   if (len < num_bytes) {
199     if (!fits_in_bytes(bytes, num_bytes, len)) {
200       return 0;
201     }
202     num_bytes = len;
203   }
204 
205 #ifdef OPENSSL_BIGENDIAN
206   for (size_t i = 0; i < num_bytes; i++) {
207     out[len - i - 1] = bytes[i ^ (BN_BYTES-1)];
208   }
209 #else
210   for (size_t i = 0; i < num_bytes; i++) {
211     out[len - i - 1] = bytes[i];
212   }
213 #endif
214   // Pad out the rest of the buffer with zeroes.
215   OPENSSL_memset(out, 0, len - num_bytes);
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