source: trunk/cscrypt/bn_print.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

File size: 7.1 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2/* crypto/bn/bn_print.c */
3/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * All rights reserved.
5 *
6 * This package is an SSL implementation written
7 * by Eric Young (eay@cryptsoft.com).
8 * The implementation was written so as to conform with Netscapes SSL.
9 *
10 * This library is free for commercial and non-commercial use as long as
11 * the following conditions are aheared to. The following conditions
12 * apply to all code found in this distribution, be it the RC4, RSA,
13 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
14 * included with this distribution is covered by the same copyright terms
15 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 *
17 * Copyright remains Eric Young's, and as such any Copyright notices in
18 * the code are not to be removed.
19 * If this package is used in a product, Eric Young should be given attribution
20 * as the author of the parts of the library used.
21 * This can be in the form of a textual message at program startup or
22 * in documentation (online or textual) provided with the package.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * "This product includes cryptographic software written by
35 * Eric Young (eay@cryptsoft.com)"
36 * The word 'cryptographic' can be left out if the rouines from the library
37 * being used are not cryptographic related :-).
38 * 4. If you include any Windows specific code (or a derivative thereof) from
39 * the apps directory (application code) you must include an acknowledgement:
40 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 *
42 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * The licence and distribution terms for any publically available version or
55 * derivative of this code cannot be changed. i.e. this code cannot simply be
56 * copied and put under another distribution licence
57 * [including the GNU Public Licence.]
58 */
59
60#include <stdio.h>
61#include <ctype.h>
62#include "bn_lcl.h"
63#include "buffer.h"
64#include "openssl_mods.h"
65
66static const char *Hex="0123456789ABCDEF";
67
68/* Must 'OPENSSL_free' the returned data */
69char *BN_bn2hex(const BIGNUM *a)
70 {
71 int i,j,v,z=0;
72 char *buf;
73 char *p;
74
75 buf=(char *)OPENSSL_malloc(a->top*BN_BYTES*2+2);
76 if (buf == NULL)
77 {
78 goto err;
79 }
80 p=buf;
81 if (a->neg) *(p++)='-';
82 if (a->top == 0) *(p++)='0';
83 for (i=a->top-1; i >=0; i--)
84 {
85 for (j=BN_BITS2-8; j >= 0; j-=8)
86 {
87 /* strip leading zeros */
88 v=((int)(a->d[i]>>(long)j))&0xff;
89 if (z || (v != 0))
90 {
91 *(p++)=Hex[v>>4];
92 *(p++)=Hex[v&0x0f];
93 z=1;
94 }
95 }
96 }
97 *p='\0';
98err:
99 return(buf);
100 }
101
102/* Must 'OPENSSL_free' the returned data */
103char *BN_bn2dec(const BIGNUM *a)
104 {
105 int i=0,num;
106 char *buf=NULL;
107 char *p;
108 BIGNUM *t=NULL;
109 BN_ULONG *bn_data=NULL,*lp;
110
111 i=BN_num_bits(a)*3;
112 num=(i/10+i/1000+3)+1;
113 bn_data=(BN_ULONG *)OPENSSL_malloc((num/BN_DEC_NUM+1)*sizeof(BN_ULONG));
114 buf=(char *)OPENSSL_malloc(num+3);
115 if ((buf == NULL) || (bn_data == NULL))
116 {
117 goto err;
118 }
119 if ((t=BN_dup(a)) == NULL) goto err;
120
121 p=buf;
122 lp=bn_data;
123 if (t->neg) *(p++)='-';
124 if (t->top == 0)
125 {
126 *(p++)='0';
127 *(p++)='\0';
128 }
129 else
130 {
131 i=0;
132 while (!BN_is_zero(t))
133 {
134 *lp=BN_div_word(t,BN_DEC_CONV);
135 lp++;
136 }
137 lp--;
138 /* We now have a series of blocks, BN_DEC_NUM chars
139 * in length, where the last one needs truncation.
140 * The blocks need to be reversed in order. */
141 sprintf(p,BN_DEC_FMT1,*lp);
142 while (*p) p++;
143 while (lp != bn_data)
144 {
145 lp--;
146 sprintf(p,BN_DEC_FMT2,*lp);
147 while (*p) p++;
148 }
149 }
150err:
151 if (bn_data != NULL) OPENSSL_free(bn_data);
152 if (t != NULL) BN_free(t);
153 return(buf);
154 }
155
156int BN_hex2bn(BIGNUM **bn, const char *a)
157 {
158 BIGNUM *ret=NULL;
159 BN_ULONG l=0;
160 int neg=0,h,m,i,j,k,c;
161 int num;
162
163 if ((a == NULL) || (*a == '\0')) return(0);
164
165 if (*a == '-') { neg=1; a++; }
166
167 for (i=0; isxdigit((unsigned char) a[i]); i++)
168 ;
169
170 num=i+neg;
171 if (bn == NULL) return(num);
172
173 /* a is the start of the hex digits, and it is 'i' long */
174 if (*bn == NULL)
175 {
176 if ((ret=BN_new()) == NULL) return(0);
177 }
178 else
179 {
180 ret= *bn;
181 BN_zero(ret);
182 }
183
184 /* i is the number of hex digests; */
185 if (bn_expand(ret,i*4) == NULL) goto err;
186
187 j=i; /* least significant 'hex' */
188 m=0;
189 h=0;
190 while (j > 0)
191 {
192 m=((BN_BYTES*2) <= j)?(BN_BYTES*2):j;
193 l=0;
194 for (;;)
195 {
196 c=a[j-m];
197 if ((c >= '0') && (c <= '9')) k=c-'0';
198 else if ((c >= 'a') && (c <= 'f')) k=c-'a'+10;
199 else if ((c >= 'A') && (c <= 'F')) k=c-'A'+10;
200 else k=0; /* paranoia */
201 l=(l<<4)|k;
202
203 if (--m <= 0)
204 {
205 ret->d[h++]=l;
206 break;
207 }
208 }
209 j-=(BN_BYTES*2);
210 }
211 ret->top=h;
212 bn_fix_top(ret);
213 ret->neg=neg;
214
215 *bn=ret;
216 return(num);
217err:
218 if (*bn == NULL) BN_free(ret);
219 return(0);
220 }
221
222int BN_dec2bn(BIGNUM **bn, const char *a)
223 {
224 BIGNUM *ret=NULL;
225 BN_ULONG l=0;
226 int neg=0,i,j;
227 int num;
228
229 if ((a == NULL) || (*a == '\0')) return(0);
230 if (*a == '-') { neg=1; a++; }
231
232 for (i=0; isdigit((unsigned char) a[i]); i++)
233 ;
234
235 num=i+neg;
236 if (bn == NULL) return(num);
237
238 /* a is the start of the digits, and it is 'i' long.
239 * We chop it into BN_DEC_NUM digits at a time */
240 if (*bn == NULL)
241 {
242 if ((ret=BN_new()) == NULL) return(0);
243 }
244 else
245 {
246 ret= *bn;
247 BN_zero(ret);
248 }
249
250 /* i is the number of digests, a bit of an over expand; */
251 if (bn_expand(ret,i*4) == NULL) goto err;
252
253 j=BN_DEC_NUM-(i%BN_DEC_NUM);
254 if (j == BN_DEC_NUM) j=0;
255 l=0;
256 while (*a)
257 {
258 l*=10;
259 l+= *a-'0';
260 a++;
261 if (++j == BN_DEC_NUM)
262 {
263 BN_mul_word(ret,BN_DEC_CONV);
264 BN_add_word(ret,l);
265 l=0;
266 j=0;
267 }
268 }
269 ret->neg=neg;
270
271 bn_fix_top(ret);
272 *bn=ret;
273 return(num);
274err:
275 if (*bn == NULL) BN_free(ret);
276 return(0);
277 }
278
279#ifdef BN_DEBUG
280void bn_dump1(FILE *o, const char *a, BN_ULONG *b,int n)
281 {
282 int i;
283 fprintf(o, "%s=", a);
284 for (i=n-1;i>=0;i--)
285 fprintf(o, "%08lX", b[i]); /* assumes 32-bit BN_ULONG */
286 fprintf(o, "\n");
287 }
288#endif
Note: See TracBrowser for help on using the repository browser.