Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/bn/bn_conv.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
12
#include <openssl/err.h>
13
#include "crypto/ctype.h"
14
#include "bn_local.h"
15
16
/* Must 'OPENSSL_free' the returned data */
17
char *BN_bn2hex(const BIGNUM *a)
18
119k
{
19
119k
    int i, j, v, z = 0;
20
119k
    char *buf;
21
119k
    char *p;
22
23
119k
    if (BN_is_zero(a))
24
111
        return OPENSSL_strdup("0");
25
119k
    buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
26
119k
    if (buf == NULL)
27
0
        goto err;
28
119k
    p = buf;
29
119k
    if (a->neg)
30
11.0k
        *p++ = '-';
31
10.9M
    for (i = a->top - 1; i >= 0; i--) {
32
97.2M
        for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
33
            /* strip leading zeros */
34
86.4M
            v = (int)((a->d[i] >> j) & 0xff);
35
86.4M
            if (z || v != 0) {
36
85.9M
                p += ossl_to_hex(p, v);
37
85.9M
                z = 1;
38
85.9M
            }
39
86.4M
        }
40
10.8M
    }
41
119k
    *p = '\0';
42
119k
err:
43
119k
    return buf;
44
119k
}
45
46
#ifndef FIPS_MODULE
47
/* No BIO_snprintf in FIPS_MODULE */
48
/* Must 'OPENSSL_free' the returned data */
49
char *BN_bn2dec(const BIGNUM *a)
50
1.19M
{
51
1.19M
    int i = 0, num, ok = 0, n, tbytes;
52
1.19M
    char *buf = NULL;
53
1.19M
    char *p;
54
1.19M
    BIGNUM *t = NULL;
55
1.19M
    BN_ULONG *bn_data = NULL, *lp;
56
1.19M
    int bn_data_num;
57
58
    /*-
59
     * get an upper bound for the length of the decimal integer
60
     * num <= (BN_num_bits(a) + 1) * log(2)
61
     *     <= 3 * BN_num_bits(a) * 0.101 + log(2) + 1     (rounding error)
62
     *     <= 3 * BN_num_bits(a) / 10 + 3 * BN_num_bits / 1000 + 1 + 1
63
     */
64
1.19M
    i = BN_num_bits(a) * 3;
65
1.19M
    num = (i / 10 + i / 1000 + 1) + 1;
66
1.19M
    tbytes = num + 3; /* negative and terminator and one spare? */
67
1.19M
    bn_data_num = num / BN_DEC_NUM + 1;
68
1.19M
    bn_data = OPENSSL_malloc_array(bn_data_num, sizeof(BN_ULONG));
69
1.19M
    buf = OPENSSL_malloc(tbytes);
70
1.19M
    if (buf == NULL || bn_data == NULL)
71
0
        goto err;
72
1.19M
    if ((t = BN_dup(a)) == NULL)
73
0
        goto err;
74
75
1.19M
    p = buf;
76
1.19M
    lp = bn_data;
77
1.19M
    if (BN_is_zero(t)) {
78
12.7k
        *p++ = '0';
79
12.7k
        *p++ = '\0';
80
1.18M
    } else {
81
1.18M
        if (BN_is_negative(t))
82
30.8k
            *p++ = '-';
83
84
2.48M
        while (!BN_is_zero(t)) {
85
1.30M
            if (lp - bn_data >= bn_data_num)
86
0
                goto err;
87
1.30M
            *lp = BN_div_word(t, BN_DEC_CONV);
88
1.30M
            if (*lp == (BN_ULONG)-1)
89
0
                goto err;
90
1.30M
            lp++;
91
1.30M
        }
92
1.18M
        lp--;
93
        /*
94
         * We now have a series of blocks, BN_DEC_NUM chars in length, where
95
         * the last one needs truncation. The blocks need to be reversed in
96
         * order.
97
         */
98
1.18M
        n = snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT1, *lp);
99
1.18M
        if (n < 0 || (size_t)n >= tbytes - (size_t)(p - buf))
100
0
            goto err;
101
1.18M
        p += n;
102
1.30M
        while (lp != bn_data) {
103
118k
            lp--;
104
118k
            n = snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT2, *lp);
105
118k
            if (n < 0 || (size_t)n >= tbytes - (size_t)(p - buf))
106
0
                goto err;
107
118k
            p += n;
108
118k
        }
109
1.18M
    }
110
1.19M
    ok = 1;
111
1.19M
err:
112
1.19M
    OPENSSL_free(bn_data);
113
1.19M
    BN_free(t);
114
1.19M
    if (ok)
115
1.19M
        return buf;
116
0
    OPENSSL_free(buf);
117
0
    return NULL;
118
1.19M
}
119
#endif
120
121
int BN_hex2bn(BIGNUM **bn, const char *a)
122
815
{
123
815
    BIGNUM *ret = NULL;
124
815
    BN_ULONG l = 0;
125
815
    int neg = 0, h, m, i, j, k, c;
126
815
    int num;
127
128
815
    if (a == NULL || *a == '\0')
129
13
        return 0;
130
131
802
    if (*a == '-') {
132
64
        neg = 1;
133
64
        a++;
134
64
    }
135
136
149k
    for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)
137
148k
        continue;
138
139
802
    if (i == 0 || i > INT_MAX / 4)
140
52
        return 0;
141
142
750
    num = i + neg;
143
750
    if (bn == NULL)
144
0
        return num;
145
146
    /* a is the start of the hex digits, and it is 'i' long */
147
750
    if (*bn == NULL) {
148
0
        if ((ret = BN_new()) == NULL)
149
0
            return 0;
150
750
    } else {
151
750
        ret = *bn;
152
750
        if (BN_get_flags(ret, BN_FLG_STATIC_DATA)) {
153
0
            ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
154
0
            return 0;
155
0
        }
156
750
        BN_zero(ret);
157
750
    }
158
159
    /* i is the number of hex digits */
160
750
    if (bn_expand(ret, i * 4) == NULL)
161
0
        goto err;
162
163
750
    j = i; /* least significant 'hex' */
164
750
    m = 0;
165
750
    h = 0;
166
10.5k
    while (j > 0) {
167
9.77k
        m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;
168
9.77k
        l = 0;
169
148k
        for (;;) {
170
148k
            c = a[j - m];
171
148k
            k = OPENSSL_hexchar2int(c);
172
148k
            if (k < 0)
173
0
                k = 0; /* paranoia */
174
148k
            l = (l << 4) | k;
175
176
148k
            if (--m <= 0) {
177
9.77k
                ret->d[h++] = l;
178
9.77k
                break;
179
9.77k
            }
180
148k
        }
181
9.77k
        j -= BN_BYTES * 2;
182
9.77k
    }
183
750
    ret->top = h;
184
750
    bn_correct_top(ret);
185
186
750
    *bn = ret;
187
750
    bn_check_top(ret);
188
    /* Don't set the negative flag if it's zero. */
189
750
    if (ret->top != 0)
190
617
        ret->neg = neg;
191
750
    return num;
192
0
err:
193
0
    if (*bn == NULL)
194
0
        BN_free(ret);
195
0
    return 0;
196
750
}
197
198
int BN_dec2bn(BIGNUM **bn, const char *a)
199
33.2k
{
200
33.2k
    BIGNUM *ret = NULL;
201
33.2k
    BN_ULONG l = 0;
202
33.2k
    int neg = 0, i, j;
203
33.2k
    int num;
204
205
33.2k
    if (a == NULL || *a == '\0')
206
69
        return 0;
207
33.2k
    if (*a == '-') {
208
765
        neg = 1;
209
765
        a++;
210
765
    }
211
212
1.93M
    for (i = 0; i <= INT_MAX / 4 && ossl_isdigit(a[i]); i++)
213
1.89M
        continue;
214
215
33.2k
    if (i == 0 || i > INT_MAX / 4)
216
235
        goto err;
217
218
32.9k
    num = i + neg;
219
32.9k
    if (bn == NULL)
220
0
        return num;
221
222
    /*
223
     * a is the start of the digits, and it is 'i' long. We chop it into
224
     * BN_DEC_NUM digits at a time
225
     */
226
32.9k
    if (*bn == NULL) {
227
0
        if ((ret = BN_new()) == NULL)
228
0
            return 0;
229
32.9k
    } else {
230
32.9k
        ret = *bn;
231
32.9k
        BN_zero(ret);
232
32.9k
    }
233
234
    /* i is the number of digits, a bit of an over expand */
235
32.9k
    if (bn_expand(ret, i * 4) == NULL)
236
0
        goto err;
237
238
32.9k
    j = BN_DEC_NUM - i % BN_DEC_NUM;
239
32.9k
    if (j == BN_DEC_NUM)
240
688
        j = 0;
241
32.9k
    l = 0;
242
1.93M
    while (--i >= 0) {
243
1.89M
        l *= 10;
244
1.89M
        l += *a - '0';
245
1.89M
        a++;
246
1.89M
        if (++j == BN_DEC_NUM) {
247
124k
            if (!BN_mul_word(ret, BN_DEC_CONV)
248
124k
                || !BN_add_word(ret, l))
249
0
                goto err;
250
124k
            l = 0;
251
124k
            j = 0;
252
124k
        }
253
1.89M
    }
254
255
32.9k
    bn_correct_top(ret);
256
32.9k
    *bn = ret;
257
32.9k
    bn_check_top(ret);
258
    /* Don't set the negative flag if it's zero. */
259
32.9k
    if (ret->top != 0)
260
26.9k
        ret->neg = neg;
261
32.9k
    return num;
262
235
err:
263
235
    if (*bn == NULL)
264
0
        BN_free(ret);
265
235
    return 0;
266
32.9k
}
267
268
int BN_asc2bn(BIGNUM **bn, const char *a)
269
0
{
270
0
    const char *p = a;
271
272
0
    if (*p == '-')
273
0
        p++;
274
275
0
    if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
276
0
        if (!BN_hex2bn(bn, p + 2))
277
0
            return 0;
278
0
    } else {
279
0
        if (!BN_dec2bn(bn, p))
280
0
            return 0;
281
0
    }
282
    /* Don't set the negative flag if it's zero. */
283
0
    if (*a == '-' && (*bn)->top != 0)
284
0
        (*bn)->neg = 1;
285
0
    return 1;
286
0
}