10Sstevel@tonic-gate /* crypto/bn/bn_print.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <ctype.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/buffer.h>
630Sstevel@tonic-gate #include "bn_lcl.h"
640Sstevel@tonic-gate
650Sstevel@tonic-gate static const char *Hex="0123456789ABCDEF";
660Sstevel@tonic-gate
670Sstevel@tonic-gate /* Must 'OPENSSL_free' the returned data */
BN_bn2hex(const BIGNUM * a)680Sstevel@tonic-gate char *BN_bn2hex(const BIGNUM *a)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate int i,j,v,z=0;
710Sstevel@tonic-gate char *buf;
720Sstevel@tonic-gate char *p;
730Sstevel@tonic-gate
740Sstevel@tonic-gate buf=(char *)OPENSSL_malloc(a->top*BN_BYTES*2+2);
750Sstevel@tonic-gate if (buf == NULL)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate BNerr(BN_F_BN_BN2HEX,ERR_R_MALLOC_FAILURE);
780Sstevel@tonic-gate goto err;
790Sstevel@tonic-gate }
800Sstevel@tonic-gate p=buf;
810Sstevel@tonic-gate if (a->neg) *(p++)='-';
82*2139Sjp161948 if (BN_is_zero(a)) *(p++)='0';
830Sstevel@tonic-gate for (i=a->top-1; i >=0; i--)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate for (j=BN_BITS2-8; j >= 0; j-=8)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate /* strip leading zeros */
880Sstevel@tonic-gate v=((int)(a->d[i]>>(long)j))&0xff;
890Sstevel@tonic-gate if (z || (v != 0))
900Sstevel@tonic-gate {
910Sstevel@tonic-gate *(p++)=Hex[v>>4];
920Sstevel@tonic-gate *(p++)=Hex[v&0x0f];
930Sstevel@tonic-gate z=1;
940Sstevel@tonic-gate }
950Sstevel@tonic-gate }
960Sstevel@tonic-gate }
970Sstevel@tonic-gate *p='\0';
980Sstevel@tonic-gate err:
990Sstevel@tonic-gate return(buf);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /* Must 'OPENSSL_free' the returned data */
BN_bn2dec(const BIGNUM * a)1030Sstevel@tonic-gate char *BN_bn2dec(const BIGNUM *a)
1040Sstevel@tonic-gate {
105*2139Sjp161948 int i=0,num, ok = 0;
1060Sstevel@tonic-gate char *buf=NULL;
1070Sstevel@tonic-gate char *p;
1080Sstevel@tonic-gate BIGNUM *t=NULL;
1090Sstevel@tonic-gate BN_ULONG *bn_data=NULL,*lp;
1100Sstevel@tonic-gate
111*2139Sjp161948 /* get an upper bound for the length of the decimal integer
112*2139Sjp161948 * num <= (BN_num_bits(a) + 1) * log(2)
113*2139Sjp161948 * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error)
114*2139Sjp161948 * <= BN_num_bits(a)/10 + BN_num_bits/1000 + 1 + 1
115*2139Sjp161948 */
1160Sstevel@tonic-gate i=BN_num_bits(a)*3;
117*2139Sjp161948 num=(i/10+i/1000+1)+1;
1180Sstevel@tonic-gate bn_data=(BN_ULONG *)OPENSSL_malloc((num/BN_DEC_NUM+1)*sizeof(BN_ULONG));
1190Sstevel@tonic-gate buf=(char *)OPENSSL_malloc(num+3);
1200Sstevel@tonic-gate if ((buf == NULL) || (bn_data == NULL))
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate BNerr(BN_F_BN_BN2DEC,ERR_R_MALLOC_FAILURE);
1230Sstevel@tonic-gate goto err;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate if ((t=BN_dup(a)) == NULL) goto err;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate #define BUF_REMAIN (num+3 - (size_t)(p - buf))
1280Sstevel@tonic-gate p=buf;
1290Sstevel@tonic-gate lp=bn_data;
130*2139Sjp161948 if (BN_is_zero(t))
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate *(p++)='0';
1330Sstevel@tonic-gate *(p++)='\0';
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate else
1360Sstevel@tonic-gate {
137*2139Sjp161948 if (BN_is_negative(t))
138*2139Sjp161948 *p++ = '-';
139*2139Sjp161948
1400Sstevel@tonic-gate i=0;
1410Sstevel@tonic-gate while (!BN_is_zero(t))
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate *lp=BN_div_word(t,BN_DEC_CONV);
1440Sstevel@tonic-gate lp++;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate lp--;
1470Sstevel@tonic-gate /* We now have a series of blocks, BN_DEC_NUM chars
1480Sstevel@tonic-gate * in length, where the last one needs truncation.
1490Sstevel@tonic-gate * The blocks need to be reversed in order. */
1500Sstevel@tonic-gate BIO_snprintf(p,BUF_REMAIN,BN_DEC_FMT1,*lp);
1510Sstevel@tonic-gate while (*p) p++;
1520Sstevel@tonic-gate while (lp != bn_data)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate lp--;
1550Sstevel@tonic-gate BIO_snprintf(p,BUF_REMAIN,BN_DEC_FMT2,*lp);
1560Sstevel@tonic-gate while (*p) p++;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate }
159*2139Sjp161948 ok = 1;
1600Sstevel@tonic-gate err:
1610Sstevel@tonic-gate if (bn_data != NULL) OPENSSL_free(bn_data);
1620Sstevel@tonic-gate if (t != NULL) BN_free(t);
163*2139Sjp161948 if (!ok && buf)
164*2139Sjp161948 {
165*2139Sjp161948 OPENSSL_free(buf);
166*2139Sjp161948 buf = NULL;
167*2139Sjp161948 }
168*2139Sjp161948
1690Sstevel@tonic-gate return(buf);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
BN_hex2bn(BIGNUM ** bn,const char * a)1720Sstevel@tonic-gate int BN_hex2bn(BIGNUM **bn, const char *a)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate BIGNUM *ret=NULL;
1750Sstevel@tonic-gate BN_ULONG l=0;
1760Sstevel@tonic-gate int neg=0,h,m,i,j,k,c;
1770Sstevel@tonic-gate int num;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if ((a == NULL) || (*a == '\0')) return(0);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate if (*a == '-') { neg=1; a++; }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate for (i=0; isxdigit((unsigned char) a[i]); i++)
1840Sstevel@tonic-gate ;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate num=i+neg;
1870Sstevel@tonic-gate if (bn == NULL) return(num);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /* a is the start of the hex digits, and it is 'i' long */
1900Sstevel@tonic-gate if (*bn == NULL)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate if ((ret=BN_new()) == NULL) return(0);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate else
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate ret= *bn;
1970Sstevel@tonic-gate BN_zero(ret);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate /* i is the number of hex digests; */
2010Sstevel@tonic-gate if (bn_expand(ret,i*4) == NULL) goto err;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate j=i; /* least significant 'hex' */
2040Sstevel@tonic-gate m=0;
2050Sstevel@tonic-gate h=0;
2060Sstevel@tonic-gate while (j > 0)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate m=((BN_BYTES*2) <= j)?(BN_BYTES*2):j;
2090Sstevel@tonic-gate l=0;
2100Sstevel@tonic-gate for (;;)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate c=a[j-m];
2130Sstevel@tonic-gate if ((c >= '0') && (c <= '9')) k=c-'0';
2140Sstevel@tonic-gate else if ((c >= 'a') && (c <= 'f')) k=c-'a'+10;
2150Sstevel@tonic-gate else if ((c >= 'A') && (c <= 'F')) k=c-'A'+10;
2160Sstevel@tonic-gate else k=0; /* paranoia */
2170Sstevel@tonic-gate l=(l<<4)|k;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if (--m <= 0)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate ret->d[h++]=l;
2220Sstevel@tonic-gate break;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate j-=(BN_BYTES*2);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate ret->top=h;
228*2139Sjp161948 bn_correct_top(ret);
2290Sstevel@tonic-gate ret->neg=neg;
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate *bn=ret;
232*2139Sjp161948 bn_check_top(ret);
2330Sstevel@tonic-gate return(num);
2340Sstevel@tonic-gate err:
2350Sstevel@tonic-gate if (*bn == NULL) BN_free(ret);
2360Sstevel@tonic-gate return(0);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
BN_dec2bn(BIGNUM ** bn,const char * a)2390Sstevel@tonic-gate int BN_dec2bn(BIGNUM **bn, const char *a)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate BIGNUM *ret=NULL;
2420Sstevel@tonic-gate BN_ULONG l=0;
2430Sstevel@tonic-gate int neg=0,i,j;
2440Sstevel@tonic-gate int num;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate if ((a == NULL) || (*a == '\0')) return(0);
2470Sstevel@tonic-gate if (*a == '-') { neg=1; a++; }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate for (i=0; isdigit((unsigned char) a[i]); i++)
2500Sstevel@tonic-gate ;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate num=i+neg;
2530Sstevel@tonic-gate if (bn == NULL) return(num);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate /* a is the start of the digits, and it is 'i' long.
2560Sstevel@tonic-gate * We chop it into BN_DEC_NUM digits at a time */
2570Sstevel@tonic-gate if (*bn == NULL)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate if ((ret=BN_new()) == NULL) return(0);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate else
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate ret= *bn;
2640Sstevel@tonic-gate BN_zero(ret);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /* i is the number of digests, a bit of an over expand; */
2680Sstevel@tonic-gate if (bn_expand(ret,i*4) == NULL) goto err;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate j=BN_DEC_NUM-(i%BN_DEC_NUM);
2710Sstevel@tonic-gate if (j == BN_DEC_NUM) j=0;
2720Sstevel@tonic-gate l=0;
2730Sstevel@tonic-gate while (*a)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate l*=10;
2760Sstevel@tonic-gate l+= *a-'0';
2770Sstevel@tonic-gate a++;
2780Sstevel@tonic-gate if (++j == BN_DEC_NUM)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate BN_mul_word(ret,BN_DEC_CONV);
2810Sstevel@tonic-gate BN_add_word(ret,l);
2820Sstevel@tonic-gate l=0;
2830Sstevel@tonic-gate j=0;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate ret->neg=neg;
2870Sstevel@tonic-gate
288*2139Sjp161948 bn_correct_top(ret);
2890Sstevel@tonic-gate *bn=ret;
290*2139Sjp161948 bn_check_top(ret);
2910Sstevel@tonic-gate return(num);
2920Sstevel@tonic-gate err:
2930Sstevel@tonic-gate if (*bn == NULL) BN_free(ret);
2940Sstevel@tonic-gate return(0);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate #ifndef OPENSSL_NO_BIO
2980Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
BN_print_fp(FILE * fp,const BIGNUM * a)2990Sstevel@tonic-gate int BN_print_fp(FILE *fp, const BIGNUM *a)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate BIO *b;
3020Sstevel@tonic-gate int ret;
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate if ((b=BIO_new(BIO_s_file())) == NULL)
3050Sstevel@tonic-gate return(0);
3060Sstevel@tonic-gate BIO_set_fp(b,fp,BIO_NOCLOSE);
3070Sstevel@tonic-gate ret=BN_print(b,a);
3080Sstevel@tonic-gate BIO_free(b);
3090Sstevel@tonic-gate return(ret);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate #endif
3120Sstevel@tonic-gate
BN_print(BIO * bp,const BIGNUM * a)3130Sstevel@tonic-gate int BN_print(BIO *bp, const BIGNUM *a)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate int i,j,v,z=0;
3160Sstevel@tonic-gate int ret=0;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate if ((a->neg) && (BIO_write(bp,"-",1) != 1)) goto end;
319*2139Sjp161948 if (BN_is_zero(a) && (BIO_write(bp,"0",1) != 1)) goto end;
3200Sstevel@tonic-gate for (i=a->top-1; i >=0; i--)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate for (j=BN_BITS2-4; j >= 0; j-=4)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate /* strip leading zeros */
3250Sstevel@tonic-gate v=((int)(a->d[i]>>(long)j))&0x0f;
3260Sstevel@tonic-gate if (z || (v != 0))
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate if (BIO_write(bp,&(Hex[v]),1) != 1)
3290Sstevel@tonic-gate goto end;
3300Sstevel@tonic-gate z=1;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate ret=1;
3350Sstevel@tonic-gate end:
3360Sstevel@tonic-gate return(ret);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate #endif
339