1
1
libssh/src/mbedcrypto_missing.c
Juraj Vijtiuk 778652460f add mbedtls crypto support
Summary:
This patch adds support for mbedTLS as a crypto backend for libssh.
mbedTLS is an SSL/TLS library that has been designed to mainly be used
in embedded systems.  It is loosely coupled and has a low memory
footprint.  mbedTLS also provides a cryptography library (libmbedcrypto)
that can be used without the TLS modules.
The patch is unfortunately quite big, since several new files had to
be added.
DSA is disabled at compile time, since mbedTLS doesn't support DSA
Patch review and feedback would be appreciated, and if any issues or
suggestions appear, I'm willing to work on them.

Signed-off-by: Juraj Vijtiuk <juraj.vijtiuk@sartura.hr>

Test Plan:
* The patch has been tested with a Debug and MinSizeRel build, with
libssh unit tests, client tests and the pkd tests.
* All the tests have been run with valgrind's memcheck, drd and helgrind
tools.
* The examples/samplessh client works when built with the patch.

Reviewers: asn, aris

Subscribers: simonsj

Differential Revision: https://bugs.libssh.org/D1
2017-12-28 11:17:39 +01:00

129 строки
2.7 KiB
C

/*
* 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