mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-07-31 00:03:07 +03:00
131 lines
2.8 KiB
C
131 lines
2.8 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_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;
|
|
}
|
|
#endif
|