Coverage Report

Created: 2026-01-17 06:09

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
0
{
32
0
    ssh_string ptr = NULL;
33
0
    size_t pad = 0;
34
0
    size_t len = bignum_num_bytes(num);
35
0
    size_t bits = bignum_num_bits(num);
36
37
0
    if (pad_to_len == 0) {
38
        /* If the first bit is set we have a negative number */
39
0
        if (!(bits % 8) && bignum_is_bit_set(num, bits - 1)) {
40
0
            pad++;
41
0
        }
42
0
    } else {
43
0
        if (len > pad_to_len) {
44
0
            return NULL;
45
0
        }
46
0
        pad = pad_to_len - len;
47
0
    }
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
0
    ptr = ssh_string_new(len + pad);
54
0
    if (ptr == NULL) {
55
0
        return NULL;
56
0
    }
57
58
    /* We have a negative number so we need a leading zero */
59
0
    if (pad) {
60
0
        memset(ptr->data, 0, pad);
61
0
    }
62
63
0
    bignum_bn2bin(num, len, ptr->data + pad);
64
65
0
    return ptr;
66
0
}
67
68
ssh_string ssh_make_bignum_string(bignum num)
69
0
{
70
0
    return make_bignum_string(num, 0);
71
0
}
72
73
ssh_string ssh_make_padded_bignum_string(bignum num, size_t pad_len)
74
0
{
75
0
    return make_bignum_string(num, pad_len);
76
0
}
77
78
bignum ssh_make_string_bn(ssh_string string)
79
2
{
80
2
    bignum bn = NULL;
81
2
    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
2
    bignum_bin2bn(string->data, (int)len, &bn);
91
92
2
    return bn;
93
2
}
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
}