aboutsummaryrefslogtreecommitdiff
path: root/src/mbedcrypto_missing.c
blob: 44ac7ddca5e301aabaa47bf9cb7186751790a0dd (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
/*
 * 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);
}

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) {
        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 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;
    rc = mbedtls_mpi_fill_random(rnd, len, mbedtls_ctr_drbg_random,
            &ssh_mbedtls_ctr_drbg);
    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;
}
#endif