aboutsummaryrefslogtreecommitdiff
path: root/src/mbedcrypto_missing.c
blob: be290d2cdc8ffafaf0af02285478768facc0f29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * This file is part of the SSH Library
 *
 * Copyright (c) 2017 Sartura d.o.o.
 *
 * Author: Juraj Vijtiuk <juraj.vijtiuk@sartura.hr>
 *
 * The SSH Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * The SSH Library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SSH Library; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include "config.h"

#include "libssh/priv.h"
#include "libssh/libmbedcrypto.h"

#ifdef HAVE_LIBMBEDCRYPTO
bignum ssh_mbedcry_bn_new(void)
{
    bignum bn;

    bn = malloc(sizeof(mbedtls_mpi));
    if (bn) {
        mbedtls_mpi_init(bn);
    }

    return bn;
}

void ssh_mbedcry_bn_free(bignum bn)
{
    mbedtls_mpi_free(bn);
    SAFE_FREE(bn);
}

unsigned char *ssh_mbedcry_bn2num(bignum num, int radix)
{
    char *buf = NULL;
    size_t olen;
    int rc;

    rc = mbedtls_mpi_write_string(num, radix, buf, 0, &olen);
    if (rc != 0 && rc != MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL) {
        return NULL;
    }

    buf = malloc(olen);
    if (buf == NULL) {
        return NULL;
    }

    rc = mbedtls_mpi_write_string(num, radix, buf, olen, &olen);
    if (rc != 0) {
        SAFE_FREE(buf);
        return NULL;
    }

    return (unsigned char *) buf;
}

int ssh_mbedcry_rand(bignum rnd, int bits, int top, int bottom)
{
    size_t len;
    int rc;
    int i;

    if (bits <= 0) {
        return 0;
    }

    len = bits / 8 + 1;
    /* FIXME weird bug: over 1024, fill_random function returns an error code
     * MBEDTLS_ERR_MPI_BAD_INPUT_DATA   -0x0004
     */
    if (len > 1024){
        len = 1024;
    }
    rc = mbedtls_mpi_fill_random(rnd,
                                 len,
                                 mbedtls_ctr_drbg_random,
                                 ssh_get_mbedtls_ctr_drbg_context());
    if (rc != 0) {
        return 0;
    }

    for (i = len * 8 - 1; i >= bits; i--) {
        rc = mbedtls_mpi_set_bit(rnd, i, 0);
        if (rc != 0) {
            return 0;
        }
    }

    if (top == 0) {
        rc = mbedtls_mpi_set_bit(rnd, bits - 1, 0);
    }

    if (top == 1) {
        if (bits < 2) {
            return 0;
        }

        rc = mbedtls_mpi_set_bit(rnd, bits - 2, 0);
        if (rc != 0) {
            return 0;
        }
    }

    if (bottom) {
        rc = mbedtls_mpi_set_bit(rnd, 0, 1);
        if (rc != 0) {
            return 0;
        }
    }

    return 1;
}

int ssh_mbedcry_is_bit_set(bignum num, size_t pos)
{
    int bit;
    bit = mbedtls_mpi_get_bit(num, pos);
    return bit;
}

/** @brief generates a random integer between 0 and max
 * @returns 1 in case of success, 0 otherwise
 */
int ssh_mbedcry_rand_range(bignum dest, bignum max)
{
    size_t bits;
    bignum rnd;
    int rc;

    bits = bignum_num_bits(max) + 64;
    rnd = bignum_new();
    if (rnd == NULL){
        return 0;
    }
    rc = bignum_rand(rnd, bits);
    if (rc != 1){
        return rc;
    }
    mbedtls_mpi_mod_mpi(dest, rnd, max);
    bignum_safe_free(rnd);
    return 1;
}

int ssh_mbedcry_hex2bn(bignum *dest, char *data)
{
    int rc;

    *dest = bignum_new();
    if (*dest == NULL){
        return 0;
    }
    rc = mbedtls_mpi_read_string(*dest, 16, data);
    if (rc == 0) {
        return 1;
    }

    return 0;
}

#endif