From 765691195394a0daa8215beb7899b5653247cc08 Mon Sep 17 00:00:00 2001 From: Anderson Toshiyuki Sasaki Date: Fri, 7 Jun 2019 19:05:01 +0200 Subject: bignum: Define bignum_dup(bignum orig, bignum *dest) The macro is defined for each crypto back end. If (*dest) is NULL, a new bignum is allocated. Otherwise the value of orig is copied to (*dest). Signed-off-by: Anderson Toshiyuki Sasaki --- include/libssh/libcrypto.h | 8 ++++++++ include/libssh/libgcrypt.h | 7 +++++++ include/libssh/libmbedcrypto.h | 8 ++++++++ 3 files changed, 23 insertions(+) (limited to 'include') diff --git a/include/libssh/libcrypto.h b/include/libssh/libcrypto.h index 27cb6ad9..541912b5 100644 --- a/include/libssh/libcrypto.h +++ b/include/libssh/libcrypto.h @@ -102,6 +102,14 @@ typedef BN_CTX* bignum_CTX; #define bignum_bn2bin(num,len, ptr) BN_bn2bin(num, ptr) #define bignum_cmp(num1,num2) BN_cmp(num1,num2) #define bignum_rshift1(dest, src) BN_rshift1(dest, src) +#define bignum_dup(orig, dest) do { \ + if (*(dest) == NULL) { \ + *(dest) = BN_dup(orig); \ + } else { \ + BN_copy(*(dest), orig); \ + } \ + } while(0) + /* Returns true if the OpenSSL is operating in FIPS mode */ #define ssh_fips_mode() (FIPS_mode() != 0) diff --git a/include/libssh/libgcrypt.h b/include/libssh/libgcrypt.h index 4d98ece3..795d33e1 100644 --- a/include/libssh/libgcrypt.h +++ b/include/libssh/libgcrypt.h @@ -92,6 +92,13 @@ int ssh_gcry_rand_range(bignum rnd, bignum max); #define bignum_sub(dst, a, b) gcry_mpi_sub(dst, a, b) #define bignum_mod(dst, a, b, ctx) 1,gcry_mpi_mod(dst, a, b) #define bignum_rand_range(rnd, max) ssh_gcry_rand_range(rnd, max); +#define bignum_dup(orig, dest) do { \ + if (*(dest) == NULL) { \ + *(dest) = gcry_mpi_copy(orig); \ + } else { \ + gcry_mpi_set(*(dest), orig); \ + } \ + } while(0) /* Helper functions for data conversions. */ /* Extract an MPI from the given s-expression SEXP named NAME which is diff --git a/include/libssh/libmbedcrypto.h b/include/libssh/libmbedcrypto.h index c2afddf5..00d5ba8c 100644 --- a/include/libssh/libmbedcrypto.h +++ b/include/libssh/libmbedcrypto.h @@ -118,6 +118,14 @@ int ssh_mbedcry_hex2bn(bignum *dest, char *data); mbedtls_mpi_size(num)) #define bignum_cmp(num1, num2) mbedtls_mpi_cmp_mpi(num1, num2) #define bignum_rshift1(dest, src) mbedtls_mpi_copy(dest, src), mbedtls_mpi_shift_r(dest, 1) +#define bignum_dup(orig, dest) do { \ + if (*(dest) == NULL) { \ + *(dest) = bignum_new(); \ + } \ + if (*(dest) != NULL) { \ + mbedtls_mpi_copy(orig, *(dest)); \ + } \ + } while(0) mbedtls_ctr_drbg_context *ssh_get_mbedtls_ctr_drbg_context(void); -- cgit v1.2.3