Path: blob/main/crypto/openssl/crypto/bn/bn_conv.c
34875 views
/*1* Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/err.h>10#include "crypto/ctype.h"11#include "bn_local.h"1213/* Must 'OPENSSL_free' the returned data */14char *BN_bn2hex(const BIGNUM *a)15{16int i, j, v, z = 0;17char *buf;18char *p;1920if (BN_is_zero(a))21return OPENSSL_strdup("0");22buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);23if (buf == NULL)24goto err;25p = buf;26if (a->neg)27*p++ = '-';28for (i = a->top - 1; i >= 0; i--) {29for (j = BN_BITS2 - 8; j >= 0; j -= 8) {30/* strip leading zeros */31v = (int)((a->d[i] >> j) & 0xff);32if (z || v != 0) {33p += ossl_to_hex(p, v);34z = 1;35}36}37}38*p = '\0';39err:40return buf;41}4243#ifndef FIPS_MODULE44/* No BIO_snprintf in FIPS_MODULE */45/* Must 'OPENSSL_free' the returned data */46char *BN_bn2dec(const BIGNUM *a)47{48int i = 0, num, ok = 0, n, tbytes;49char *buf = NULL;50char *p;51BIGNUM *t = NULL;52BN_ULONG *bn_data = NULL, *lp;53int bn_data_num;5455/*-56* get an upper bound for the length of the decimal integer57* num <= (BN_num_bits(a) + 1) * log(2)58* <= 3 * BN_num_bits(a) * 0.101 + log(2) + 1 (rounding error)59* <= 3 * BN_num_bits(a) / 10 + 3 * BN_num_bits / 1000 + 1 + 160*/61i = BN_num_bits(a) * 3;62num = (i / 10 + i / 1000 + 1) + 1;63tbytes = num + 3; /* negative and terminator and one spare? */64bn_data_num = num / BN_DEC_NUM + 1;65bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));66buf = OPENSSL_malloc(tbytes);67if (buf == NULL || bn_data == NULL)68goto err;69if ((t = BN_dup(a)) == NULL)70goto err;7172p = buf;73lp = bn_data;74if (BN_is_zero(t)) {75*p++ = '0';76*p++ = '\0';77} else {78if (BN_is_negative(t))79*p++ = '-';8081while (!BN_is_zero(t)) {82if (lp - bn_data >= bn_data_num)83goto err;84*lp = BN_div_word(t, BN_DEC_CONV);85if (*lp == (BN_ULONG)-1)86goto err;87lp++;88}89lp--;90/*91* We now have a series of blocks, BN_DEC_NUM chars in length, where92* the last one needs truncation. The blocks need to be reversed in93* order.94*/95n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT1, *lp);96if (n < 0)97goto err;98p += n;99while (lp != bn_data) {100lp--;101n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT2, *lp);102if (n < 0)103goto err;104p += n;105}106}107ok = 1;108err:109OPENSSL_free(bn_data);110BN_free(t);111if (ok)112return buf;113OPENSSL_free(buf);114return NULL;115}116#endif117118int BN_hex2bn(BIGNUM **bn, const char *a)119{120BIGNUM *ret = NULL;121BN_ULONG l = 0;122int neg = 0, h, m, i, j, k, c;123int num;124125if (a == NULL || *a == '\0')126return 0;127128if (*a == '-') {129neg = 1;130a++;131}132133for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)134continue;135136if (i == 0 || i > INT_MAX / 4)137return 0;138139num = i + neg;140if (bn == NULL)141return num;142143/* a is the start of the hex digits, and it is 'i' long */144if (*bn == NULL) {145if ((ret = BN_new()) == NULL)146return 0;147} else {148ret = *bn;149if (BN_get_flags(ret, BN_FLG_STATIC_DATA)) {150ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);151return 0;152}153BN_zero(ret);154}155156/* i is the number of hex digits */157if (bn_expand(ret, i * 4) == NULL)158goto err;159160j = i; /* least significant 'hex' */161m = 0;162h = 0;163while (j > 0) {164m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;165l = 0;166for (;;) {167c = a[j - m];168k = OPENSSL_hexchar2int(c);169if (k < 0)170k = 0; /* paranoia */171l = (l << 4) | k;172173if (--m <= 0) {174ret->d[h++] = l;175break;176}177}178j -= BN_BYTES * 2;179}180ret->top = h;181bn_correct_top(ret);182183*bn = ret;184bn_check_top(ret);185/* Don't set the negative flag if it's zero. */186if (ret->top != 0)187ret->neg = neg;188return num;189err:190if (*bn == NULL)191BN_free(ret);192return 0;193}194195int BN_dec2bn(BIGNUM **bn, const char *a)196{197BIGNUM *ret = NULL;198BN_ULONG l = 0;199int neg = 0, i, j;200int num;201202if (a == NULL || *a == '\0')203return 0;204if (*a == '-') {205neg = 1;206a++;207}208209for (i = 0; i <= INT_MAX / 4 && ossl_isdigit(a[i]); i++)210continue;211212if (i == 0 || i > INT_MAX / 4)213goto err;214215num = i + neg;216if (bn == NULL)217return num;218219/*220* a is the start of the digits, and it is 'i' long. We chop it into221* BN_DEC_NUM digits at a time222*/223if (*bn == NULL) {224if ((ret = BN_new()) == NULL)225return 0;226} else {227ret = *bn;228BN_zero(ret);229}230231/* i is the number of digits, a bit of an over expand */232if (bn_expand(ret, i * 4) == NULL)233goto err;234235j = BN_DEC_NUM - i % BN_DEC_NUM;236if (j == BN_DEC_NUM)237j = 0;238l = 0;239while (--i >= 0) {240l *= 10;241l += *a - '0';242a++;243if (++j == BN_DEC_NUM) {244if (!BN_mul_word(ret, BN_DEC_CONV)245|| !BN_add_word(ret, l))246goto err;247l = 0;248j = 0;249}250}251252bn_correct_top(ret);253*bn = ret;254bn_check_top(ret);255/* Don't set the negative flag if it's zero. */256if (ret->top != 0)257ret->neg = neg;258return num;259err:260if (*bn == NULL)261BN_free(ret);262return 0;263}264265int BN_asc2bn(BIGNUM **bn, const char *a)266{267const char *p = a;268269if (*p == '-')270p++;271272if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {273if (!BN_hex2bn(bn, p + 2))274return 0;275} else {276if (!BN_dec2bn(bn, p))277return 0;278}279/* Don't set the negative flag if it's zero. */280if (*a == '-' && (*bn)->top != 0)281(*bn)->neg = 1;282return 1;283}284285286