Coverage Report

Created: 2026-01-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/src/bignum.c
Line
Count
Source
1
/*
2
 * This file is part of the SSH Library
3
 *
4
 * Copyright (c) 2014 by Aris Adamantiadis <aris@badcode.be>
5
 *
6
 * The SSH Library is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or (at your
9
 * option) any later version.
10
 *
11
 * The SSH Library is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14
 * License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with the SSH Library; see the file COPYING.  If not, write to
18
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19
 * MA 02111-1307, USA.
20
 */
21
22
#include "config.h"
23
24
#include <stdio.h>
25
26
#include "libssh/priv.h"
27
#include "libssh/bignum.h"
28
#include "libssh/string.h"
29
30
static ssh_string make_bignum_string(bignum num, size_t pad_to_len)
31
70.3k
{
32
70.3k
    ssh_string ptr = NULL;
33
70.3k
    size_t pad = 0;
34
70.3k
    size_t len = bignum_num_bytes(num);
35
70.3k
    size_t bits = bignum_num_bits(num);
36
37
70.3k
    if (pad_to_len == 0) {
38
        /* If the first bit is set we have a negative number */
39
70.3k
        if (!(bits % 8) && bignum_is_bit_set(num, bits - 1)) {
40
44.8k
            pad++;
41
44.8k
        }
42
70.3k
    } else {
43
30
        if (len > pad_to_len) {
44
0
            return NULL;
45
0
        }
46
30
        pad = pad_to_len - len;
47
30
    }
48
49
#ifdef DEBUG_CRYPTO
50
    SSH_LOG(SSH_LOG_TRACE, "%zu bits, %zu bytes, %zu padding", bits, len, pad);
51
#endif /* DEBUG_CRYPTO */
52
53
70.3k
    ptr = ssh_string_new(len + pad);
54
70.3k
    if (ptr == NULL) {
55
0
        return NULL;
56
0
    }
57
58
    /* We have a negative number so we need a leading zero */
59
70.3k
    if (pad) {
60
44.8k
        memset(ptr->data, 0, pad);
61
44.8k
    }
62
63
70.3k
    bignum_bn2bin(num, len, ptr->data + pad);
64
65
70.3k
    return ptr;
66
70.3k
}
67
68
ssh_string ssh_make_bignum_string(bignum num)
69
70.3k
{
70
70.3k
    return make_bignum_string(num, 0);
71
70.3k
}
72
73
ssh_string ssh_make_padded_bignum_string(bignum num, size_t pad_len)
74
30
{
75
30
    return make_bignum_string(num, pad_len);
76
30
}
77
78
bignum ssh_make_string_bn(ssh_string string)
79
46.7k
{
80
46.7k
    bignum bn = NULL;
81
46.7k
    size_t len = ssh_string_len(string);
82
83
#ifdef DEBUG_CRYPTO
84
    SSH_LOG(SSH_LOG_TRACE,
85
            "Importing a %zu bits, %zu bytes object ...",
86
            len * 8,
87
            len);
88
#endif /* DEBUG_CRYPTO */
89
90
46.7k
    bignum_bin2bn(string->data, (int)len, &bn);
91
92
46.7k
    return bn;
93
46.7k
}
94
95
/* prints the bignum on stderr */
96
void ssh_print_bignum(const char *name, const_bignum num)
97
0
{
98
0
    unsigned char *hex = NULL;
99
0
    if (num != NULL) {
100
0
        bignum_bn2hex(num, &hex);
101
0
    }
102
0
    SSH_LOG(SSH_LOG_DEBUG,
103
0
            "%s value: %s",
104
0
            name,
105
0
            (hex == NULL) ? "(null)" : (char *)hex);
106
    ssh_crypto_free(hex);
107
0
}