155714Skris/* crypto/bn/bn_print.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <ctype.h> 61296341Sdelphij#include <limits.h> 6255714Skris#include "cryptlib.h" 6355714Skris#include <openssl/buffer.h> 6455714Skris#include "bn_lcl.h" 6555714Skris 66296341Sdelphijstatic const char Hex[] = "0123456789ABCDEF"; 6755714Skris 6868651Skris/* Must 'OPENSSL_free' the returned data */ 6955714Skrischar *BN_bn2hex(const BIGNUM *a) 70296341Sdelphij{ 71296341Sdelphij int i, j, v, z = 0; 72296341Sdelphij char *buf; 73296341Sdelphij char *p; 7455714Skris 75296341Sdelphij if (a->neg && BN_is_zero(a)) { 76296341Sdelphij /* "-0" == 3 bytes including NULL terminator */ 77296341Sdelphij buf = OPENSSL_malloc(3); 78296341Sdelphij } else { 79296341Sdelphij buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2); 80296341Sdelphij } 81296341Sdelphij if (buf == NULL) { 82296341Sdelphij BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE); 83296341Sdelphij goto err; 84296341Sdelphij } 85296341Sdelphij p = buf; 86296341Sdelphij if (a->neg) 87296341Sdelphij *(p++) = '-'; 88296341Sdelphij if (BN_is_zero(a)) 89296341Sdelphij *(p++) = '0'; 90296341Sdelphij for (i = a->top - 1; i >= 0; i--) { 91296341Sdelphij for (j = BN_BITS2 - 8; j >= 0; j -= 8) { 92296341Sdelphij /* strip leading zeros */ 93296341Sdelphij v = ((int)(a->d[i] >> (long)j)) & 0xff; 94296341Sdelphij if (z || (v != 0)) { 95296341Sdelphij *(p++) = Hex[v >> 4]; 96296341Sdelphij *(p++) = Hex[v & 0x0f]; 97296341Sdelphij z = 1; 98296341Sdelphij } 99296341Sdelphij } 100296341Sdelphij } 101296341Sdelphij *p = '\0'; 102296341Sdelphij err: 103296341Sdelphij return (buf); 104296341Sdelphij} 10555714Skris 10668651Skris/* Must 'OPENSSL_free' the returned data */ 10755714Skrischar *BN_bn2dec(const BIGNUM *a) 108296341Sdelphij{ 109296341Sdelphij int i = 0, num, ok = 0; 110296341Sdelphij char *buf = NULL; 111296341Sdelphij char *p; 112296341Sdelphij BIGNUM *t = NULL; 113296341Sdelphij BN_ULONG *bn_data = NULL, *lp; 114306230Sdelphij int bn_data_num; 11555714Skris 116296341Sdelphij /*- 117296341Sdelphij * get an upper bound for the length of the decimal integer 118296341Sdelphij * num <= (BN_num_bits(a) + 1) * log(2) 119296341Sdelphij * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error) 120296341Sdelphij * <= BN_num_bits(a)/10 + BN_num_bits/1000 + 1 + 1 121296341Sdelphij */ 122296341Sdelphij i = BN_num_bits(a) * 3; 123296341Sdelphij num = (i / 10 + i / 1000 + 1) + 1; 124306230Sdelphij bn_data_num = num / BN_DEC_NUM + 1; 125306230Sdelphij bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG)); 126296341Sdelphij buf = (char *)OPENSSL_malloc(num + 3); 127296341Sdelphij if ((buf == NULL) || (bn_data == NULL)) { 128296341Sdelphij BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE); 129296341Sdelphij goto err; 130296341Sdelphij } 131296341Sdelphij if ((t = BN_dup(a)) == NULL) 132296341Sdelphij goto err; 13355714Skris 134127128Snectar#define BUF_REMAIN (num+3 - (size_t)(p - buf)) 135296341Sdelphij p = buf; 136296341Sdelphij lp = bn_data; 137296341Sdelphij if (BN_is_zero(t)) { 138296341Sdelphij *(p++) = '0'; 139296341Sdelphij *(p++) = '\0'; 140296341Sdelphij } else { 141296341Sdelphij if (BN_is_negative(t)) 142296341Sdelphij *p++ = '-'; 143160814Ssimon 144296341Sdelphij while (!BN_is_zero(t)) { 145306336Sdelphij if (lp - bn_data >= bn_data_num) 146306336Sdelphij goto err; 147296341Sdelphij *lp = BN_div_word(t, BN_DEC_CONV); 148306230Sdelphij if (*lp == (BN_ULONG)-1) 149306230Sdelphij goto err; 150296341Sdelphij lp++; 151296341Sdelphij } 152296341Sdelphij lp--; 153296341Sdelphij /* 154296341Sdelphij * We now have a series of blocks, BN_DEC_NUM chars in length, where 155296341Sdelphij * the last one needs truncation. The blocks need to be reversed in 156296341Sdelphij * order. 157296341Sdelphij */ 158296341Sdelphij BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp); 159296341Sdelphij while (*p) 160296341Sdelphij p++; 161296341Sdelphij while (lp != bn_data) { 162296341Sdelphij lp--; 163296341Sdelphij BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp); 164296341Sdelphij while (*p) 165296341Sdelphij p++; 166296341Sdelphij } 167296341Sdelphij } 168296341Sdelphij ok = 1; 169296341Sdelphij err: 170296341Sdelphij if (bn_data != NULL) 171296341Sdelphij OPENSSL_free(bn_data); 172296341Sdelphij if (t != NULL) 173296341Sdelphij BN_free(t); 174296341Sdelphij if (!ok && buf) { 175296341Sdelphij OPENSSL_free(buf); 176296341Sdelphij buf = NULL; 177296341Sdelphij } 178160814Ssimon 179296341Sdelphij return (buf); 180296341Sdelphij} 18155714Skris 18255714Skrisint BN_hex2bn(BIGNUM **bn, const char *a) 183296341Sdelphij{ 184296341Sdelphij BIGNUM *ret = NULL; 185296341Sdelphij BN_ULONG l = 0; 186296341Sdelphij int neg = 0, h, m, i, j, k, c; 187296341Sdelphij int num; 18855714Skris 189296341Sdelphij if ((a == NULL) || (*a == '\0')) 190296341Sdelphij return (0); 19155714Skris 192296341Sdelphij if (*a == '-') { 193296341Sdelphij neg = 1; 194296341Sdelphij a++; 195296341Sdelphij } 19655714Skris 197296341Sdelphij for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++) 198296341Sdelphij continue; 19955714Skris 200296341Sdelphij if (i > INT_MAX/4) 201296341Sdelphij goto err; 20255714Skris 203296341Sdelphij num = i + neg; 204296341Sdelphij if (bn == NULL) 205296341Sdelphij return (num); 20655714Skris 207296341Sdelphij /* a is the start of the hex digits, and it is 'i' long */ 208296341Sdelphij if (*bn == NULL) { 209296341Sdelphij if ((ret = BN_new()) == NULL) 210296341Sdelphij return (0); 211296341Sdelphij } else { 212296341Sdelphij ret = *bn; 213296341Sdelphij BN_zero(ret); 214296341Sdelphij } 21555714Skris 216296341Sdelphij /* i is the number of hex digits */ 217296341Sdelphij if (bn_expand(ret, i * 4) == NULL) 218296341Sdelphij goto err; 21955714Skris 220296341Sdelphij j = i; /* least significant 'hex' */ 221296341Sdelphij m = 0; 222296341Sdelphij h = 0; 223296341Sdelphij while (j > 0) { 224296341Sdelphij m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j; 225296341Sdelphij l = 0; 226296341Sdelphij for (;;) { 227296341Sdelphij c = a[j - m]; 228296341Sdelphij if ((c >= '0') && (c <= '9')) 229296341Sdelphij k = c - '0'; 230296341Sdelphij else if ((c >= 'a') && (c <= 'f')) 231296341Sdelphij k = c - 'a' + 10; 232296341Sdelphij else if ((c >= 'A') && (c <= 'F')) 233296341Sdelphij k = c - 'A' + 10; 234296341Sdelphij else 235296341Sdelphij k = 0; /* paranoia */ 236296341Sdelphij l = (l << 4) | k; 23755714Skris 238296341Sdelphij if (--m <= 0) { 239296341Sdelphij ret->d[h++] = l; 240296341Sdelphij break; 241296341Sdelphij } 242296341Sdelphij } 243296341Sdelphij j -= (BN_BYTES * 2); 244296341Sdelphij } 245296341Sdelphij ret->top = h; 246296341Sdelphij bn_correct_top(ret); 247296341Sdelphij ret->neg = neg; 24855714Skris 249296341Sdelphij *bn = ret; 250296341Sdelphij bn_check_top(ret); 251296341Sdelphij return (num); 252296341Sdelphij err: 253296341Sdelphij if (*bn == NULL) 254296341Sdelphij BN_free(ret); 255296341Sdelphij return (0); 256296341Sdelphij} 257296341Sdelphij 25855714Skrisint BN_dec2bn(BIGNUM **bn, const char *a) 259296341Sdelphij{ 260296341Sdelphij BIGNUM *ret = NULL; 261296341Sdelphij BN_ULONG l = 0; 262296341Sdelphij int neg = 0, i, j; 263296341Sdelphij int num; 26455714Skris 265296341Sdelphij if ((a == NULL) || (*a == '\0')) 266296341Sdelphij return (0); 267296341Sdelphij if (*a == '-') { 268296341Sdelphij neg = 1; 269296341Sdelphij a++; 270296341Sdelphij } 27155714Skris 272296341Sdelphij for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++) 273296341Sdelphij continue; 27455714Skris 275296341Sdelphij if (i > INT_MAX/4) 276296341Sdelphij goto err; 27755714Skris 278296341Sdelphij num = i + neg; 279296341Sdelphij if (bn == NULL) 280296341Sdelphij return (num); 28155714Skris 282296341Sdelphij /* 283296341Sdelphij * a is the start of the digits, and it is 'i' long. We chop it into 284296341Sdelphij * BN_DEC_NUM digits at a time 285296341Sdelphij */ 286296341Sdelphij if (*bn == NULL) { 287296341Sdelphij if ((ret = BN_new()) == NULL) 288296341Sdelphij return (0); 289296341Sdelphij } else { 290296341Sdelphij ret = *bn; 291296341Sdelphij BN_zero(ret); 292296341Sdelphij } 29355714Skris 294296341Sdelphij /* i is the number of digits, a bit of an over expand */ 295296341Sdelphij if (bn_expand(ret, i * 4) == NULL) 296296341Sdelphij goto err; 29755714Skris 298296341Sdelphij j = BN_DEC_NUM - (i % BN_DEC_NUM); 299296341Sdelphij if (j == BN_DEC_NUM) 300296341Sdelphij j = 0; 301296341Sdelphij l = 0; 302296341Sdelphij while (*a) { 303296341Sdelphij l *= 10; 304296341Sdelphij l += *a - '0'; 305296341Sdelphij a++; 306296341Sdelphij if (++j == BN_DEC_NUM) { 307296341Sdelphij BN_mul_word(ret, BN_DEC_CONV); 308296341Sdelphij BN_add_word(ret, l); 309296341Sdelphij l = 0; 310296341Sdelphij j = 0; 311296341Sdelphij } 312296341Sdelphij } 313296341Sdelphij ret->neg = neg; 31455714Skris 315296341Sdelphij bn_correct_top(ret); 316296341Sdelphij *bn = ret; 317296341Sdelphij bn_check_top(ret); 318296341Sdelphij return (num); 319296341Sdelphij err: 320296341Sdelphij if (*bn == NULL) 321296341Sdelphij BN_free(ret); 322296341Sdelphij return (0); 323296341Sdelphij} 324296341Sdelphij 325238405Sjkimint BN_asc2bn(BIGNUM **bn, const char *a) 326296341Sdelphij{ 327296341Sdelphij const char *p = a; 328296341Sdelphij if (*p == '-') 329296341Sdelphij p++; 330238405Sjkim 331296341Sdelphij if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) { 332296341Sdelphij if (!BN_hex2bn(bn, p + 2)) 333296341Sdelphij return 0; 334296341Sdelphij } else { 335296341Sdelphij if (!BN_dec2bn(bn, p)) 336296341Sdelphij return 0; 337296341Sdelphij } 338296341Sdelphij if (*a == '-') 339296341Sdelphij (*bn)->neg = 1; 340296341Sdelphij return 1; 341296341Sdelphij} 342238405Sjkim 343109998Smarkm#ifndef OPENSSL_NO_BIO 344296341Sdelphij# ifndef OPENSSL_NO_FP_API 34559191Skrisint BN_print_fp(FILE *fp, const BIGNUM *a) 346296341Sdelphij{ 347296341Sdelphij BIO *b; 348296341Sdelphij int ret; 34955714Skris 350296341Sdelphij if ((b = BIO_new(BIO_s_file())) == NULL) 351296341Sdelphij return (0); 352296341Sdelphij BIO_set_fp(b, fp, BIO_NOCLOSE); 353296341Sdelphij ret = BN_print(b, a); 354296341Sdelphij BIO_free(b); 355296341Sdelphij return (ret); 356296341Sdelphij} 357296341Sdelphij# endif 35855714Skris 35955714Skrisint BN_print(BIO *bp, const BIGNUM *a) 360296341Sdelphij{ 361296341Sdelphij int i, j, v, z = 0; 362296341Sdelphij int ret = 0; 36355714Skris 364296341Sdelphij if ((a->neg) && (BIO_write(bp, "-", 1) != 1)) 365296341Sdelphij goto end; 366296341Sdelphij if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1)) 367296341Sdelphij goto end; 368296341Sdelphij for (i = a->top - 1; i >= 0; i--) { 369296341Sdelphij for (j = BN_BITS2 - 4; j >= 0; j -= 4) { 370296341Sdelphij /* strip leading zeros */ 371296341Sdelphij v = ((int)(a->d[i] >> (long)j)) & 0x0f; 372296341Sdelphij if (z || (v != 0)) { 373296341Sdelphij if (BIO_write(bp, &(Hex[v]), 1) != 1) 374296341Sdelphij goto end; 375296341Sdelphij z = 1; 376296341Sdelphij } 377296341Sdelphij } 378296341Sdelphij } 379296341Sdelphij ret = 1; 380296341Sdelphij end: 381296341Sdelphij return (ret); 382296341Sdelphij} 38359191Skris#endif 384238405Sjkim 385238405Sjkimchar *BN_options(void) 386296341Sdelphij{ 387296341Sdelphij static int init = 0; 388296341Sdelphij static char data[16]; 389238405Sjkim 390296341Sdelphij if (!init) { 391296341Sdelphij init++; 392238405Sjkim#ifdef BN_LLONG 393296341Sdelphij BIO_snprintf(data, sizeof data, "bn(%d,%d)", 394296341Sdelphij (int)sizeof(BN_ULLONG) * 8, (int)sizeof(BN_ULONG) * 8); 395238405Sjkim#else 396296341Sdelphij BIO_snprintf(data, sizeof data, "bn(%d,%d)", 397296341Sdelphij (int)sizeof(BN_ULONG) * 8, (int)sizeof(BN_ULONG) * 8); 398238405Sjkim#endif 399296341Sdelphij } 400296341Sdelphij return (data); 401296341Sdelphij} 402