aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2015-12-31 10:48:34 +0100
committerAndreas Schneider <asn@cryptomilk.org>2019-01-24 11:04:44 +0100
commitafe2673cfa5a94013d4b76670c802e8590216217 (patch)
treec00368df224193d919978e5d5582b9d4a3c6382c /include
parent43a4f86b6e14a907b3a298d10d5cd7efb59f6a09 (diff)
downloadlibssh-afe2673cfa5a94013d4b76670c802e8590216217.tar.gz
libssh-afe2673cfa5a94013d4b76670c802e8590216217.tar.xz
libssh-afe2673cfa5a94013d4b76670c802e8590216217.zip
bignum: harmonize gcrypt, libcrypto and libmcrypt bignum
Ensure most of the abstraction around the 3 libs are consistent. Signed-off-by: Aris Adamantiadis <aris@0xbadc0de.be> Reviewed-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'include')
-rw-r--r--include/libssh/bignum.h1
-rw-r--r--include/libssh/libcrypto.h21
-rw-r--r--include/libssh/libgcrypt.h22
-rw-r--r--include/libssh/libmbedcrypto.h23
4 files changed, 51 insertions, 16 deletions
diff --git a/include/libssh/bignum.h b/include/libssh/bignum.h
index 32727050..3f82bc64 100644
--- a/include/libssh/bignum.h
+++ b/include/libssh/bignum.h
@@ -26,7 +26,6 @@
#include "libssh/libmbedcrypto.h"
bignum ssh_make_string_bn(ssh_string string);
-void ssh_make_string_bn_inplace(ssh_string string, bignum bnout);
ssh_string ssh_make_bignum_string(bignum num);
void ssh_print_bignum(const char *which, const bignum num);
diff --git a/include/libssh/libcrypto.h b/include/libssh/libcrypto.h
index cee28bac..e9e35ec8 100644
--- a/include/libssh/libcrypto.h
+++ b/include/libssh/libcrypto.h
@@ -74,19 +74,32 @@ typedef BN_CTX* bignum_CTX;
} \
} while(0)
#define bignum_set_word(bn,n) BN_set_word(bn,n)
-#define bignum_bin2bn(bn,datalen,data) BN_bin2bn(bn,datalen,data)
+#define bignum_bin2bn(data, datalen, dest) \
+ do { \
+ (*dest) = BN_new(); \
+ if ((*dest) != NULL) { \
+ BN_bin2bn(data,datalen,(*dest)); \
+ } \
+ } while(0)
#define bignum_bn2dec(num) BN_bn2dec(num)
-#define bignum_dec2bn(bn,data) BN_dec2bn(data,bn)
-#define bignum_bn2hex(num) BN_bn2hex(num)
+#define bignum_dec2bn(data, bn) BN_dec2bn(bn, data)
+#define bignum_hex2bn(data, bn) BN_hex2bn(bn, data)
+#define bignum_bn2hex(num, dest) (*dest)=(unsigned char *)BN_bn2hex(num)
#define bignum_rand(rnd, bits) BN_rand(rnd, bits, 0, 1)
+#define bignum_rand_range(rnd, max) BN_rand_range(rnd, max)
#define bignum_ctx_new() BN_CTX_new()
#define bignum_ctx_free(num) BN_CTX_free(num)
+#define bignum_ctx_invalid(ctx) ((ctx) == NULL)
#define bignum_mod_exp(dest,generator,exp,modulo,ctx) BN_mod_exp(dest,generator,exp,modulo,ctx)
+#define bignum_add(dest, a, b) BN_add(dest, a, b)
+#define bignum_sub(dest, a, b) BN_sub(dest, a, b)
+#define bignum_mod(dest, a, b, ctx) BN_mod(dest, a, b, ctx)
#define bignum_num_bytes(num) BN_num_bytes(num)
#define bignum_num_bits(num) BN_num_bits(num)
#define bignum_is_bit_set(num,bit) BN_is_bit_set(num,bit)
-#define bignum_bn2bin(num,ptr) BN_bn2bin(num,ptr)
+#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)
#endif /* HAVE_LIBCRYPTO */
diff --git a/include/libssh/libgcrypt.h b/include/libssh/libgcrypt.h
index 56956637..0d5d6c1b 100644
--- a/include/libssh/libgcrypt.h
+++ b/include/libssh/libgcrypt.h
@@ -50,6 +50,7 @@ typedef gcry_md_hd_t EVPCTX;
#define EVP_DIGEST_LEN EVP_MAX_MD_SIZE
typedef gcry_mpi_t bignum;
+typedef void* bignum_CTX;
/* Constants for curves. */
#define NID_gcrypt_nistp256 0
@@ -59,6 +60,7 @@ typedef gcry_mpi_t bignum;
/* missing gcrypt functions */
int ssh_gcry_dec2bn(bignum *bn, const char *data);
char *ssh_gcry_bn2dec(bignum bn);
+int ssh_gcry_rand_range(bignum rnd, bignum max);
#define bignum_new() gcry_mpi_new(0)
#define bignum_safe_free(num) do { \
@@ -67,20 +69,28 @@ char *ssh_gcry_bn2dec(bignum bn);
(num)=NULL; \
} \
} while (0)
-#define bignum_set_word(bn,n) gcry_mpi_set_ui(bn,n)
-#define bignum_bin2bn(bn,datalen,data) gcry_mpi_scan(data,GCRYMPI_FMT_USG,bn,datalen,NULL)
+#define bignum_free(num) gcry_mpi_release(num)
+#define bignum_ctx_new() NULL
+#define bignum_ctx_free(ctx) do {(ctx) = NULL;} while(0)
+#define bignum_ctx_invalid(ctx) (ctx != NULL)
+#define bignum_set_word(bn,n) (gcry_mpi_set_ui(bn,n)!=NULL ? 1 : 0)
+#define bignum_bin2bn(data,datalen,dest) gcry_mpi_scan(dest,GCRYMPI_FMT_USG,data,datalen,NULL)
#define bignum_bn2dec(num) ssh_gcry_bn2dec(num)
#define bignum_dec2bn(num, data) ssh_gcry_dec2bn(data, num)
#define bignum_bn2hex(num,data) gcry_mpi_aprint(GCRYMPI_FMT_HEX,data,NULL,num)
-#define bignum_hex2bn(num,datalen,data) gcry_mpi_scan(num,GCRYMPI_FMT_HEX,data,datalen,NULL)
-#define bignum_rand(num,bits) gcry_mpi_randomize(num,bits,GCRY_STRONG_RANDOM),gcry_mpi_set_bit(num,bits-1),gcry_mpi_set_bit(num,0)
-#define bignum_mod_exp(dest,generator,exp,modulo) gcry_mpi_powm(dest,generator,exp,modulo)
+#define bignum_hex2bn(data, num) (gcry_mpi_scan(num,GCRYMPI_FMT_HEX,data,0,NULL)==0?1:0)
+#define bignum_rand(num,bits) 1,gcry_mpi_randomize(num,bits,GCRY_STRONG_RANDOM),gcry_mpi_set_bit(num,bits-1),gcry_mpi_set_bit(num,0)
+#define bignum_mod_exp(dest,generator,exp,modulo, ctx) 1,gcry_mpi_powm(dest,generator,exp,modulo)
#define bignum_num_bits(num) gcry_mpi_get_nbits(num)
#define bignum_num_bytes(num) ((gcry_mpi_get_nbits(num)+7)/8)
#define bignum_is_bit_set(num,bit) gcry_mpi_test_bit(num,bit)
#define bignum_bn2bin(num,datalen,data) gcry_mpi_print(GCRYMPI_FMT_USG,data,datalen,NULL,num)
#define bignum_cmp(num1,num2) gcry_mpi_cmp(num1,num2)
-
+#define bignum_rshift1(dest, src) gcry_mpi_rshift (dest, src, 1)
+#define bignum_add(dst, a, b) gcry_mpi_add(dst, a, b)
+#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);
/* 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 559f1b16..13b41c99 100644
--- a/include/libssh/libmbedcrypto.h
+++ b/include/libssh/libmbedcrypto.h
@@ -60,6 +60,7 @@ typedef mbedtls_md_context_t *EVPCTX;
#define EVP_DIGEST_LEN EVP_MAX_MD_SIZE
typedef mbedtls_mpi *bignum;
+typedef void* bignum_CTX;
/* Constants for curves */
#define NID_mbedtls_nistp256 0
@@ -73,9 +74,11 @@ struct mbedtls_ecdsa_sig {
bignum ssh_mbedcry_bn_new(void);
void ssh_mbedcry_bn_free(bignum num);
-char *ssh_mbedcry_bn2num(bignum num, int radix);
+unsigned char *ssh_mbedcry_bn2num(bignum num, int radix);
int ssh_mbedcry_rand(bignum rnd, int bits, int top, int bottom);
int ssh_mbedcry_is_bit_set(bignum num, size_t pos);
+int ssh_mbedcry_rand_range(bignum dest, bignum max);
+int ssh_mbedcry_hex2bn(bignum *dest, char *data);
#define bignum_new() ssh_mbedcry_bn_new()
#define bignum_safe_free(num) do { \
@@ -84,22 +87,32 @@ int ssh_mbedcry_is_bit_set(bignum num, size_t pos);
(num)=NULL; \
} \
} while(0)
-#define bignum_set_word(bn, n) mbedtls_mpi_lset(bn, n) /* TODO fix
+#define bignum_ctx_new() NULL
+#define bignum_ctx_free(num) do {(num) = NULL;} while(0)
+#define bignum_ctx_invalid(ctx) (ctx == NULL?0:1)
+#define bignum_set_word(bn, n) (mbedtls_mpi_lset(bn, n)==0?1:0) /* TODO fix
overflow/underflow */
#define bignum_bin2bn(data, datalen, bn) mbedtls_mpi_read_binary(bn, data, \
datalen)
#define bignum_bn2dec(num) ssh_mbedcry_bn2num(num, 10)
#define bignum_dec2bn(data, bn) mbedtls_mpi_read_string(bn, 10, data)
-#define bignum_bn2hex(num) ssh_mbedcry_bn2num(num, 16)
+#define bignum_bn2hex(num, dest) (*dest)=ssh_mbedcry_bn2num(num, 16)
+#define bignum_hex2bn(data, dest) ssh_mbedcry_hex2bn(dest, data)
#define bignum_rand(rnd, bits) ssh_mbedcry_rand((rnd), (bits), 0, 1)
+#define bignum_rand_range(rnd, max) ssh_mbedcry_rand_range(rnd, max)
#define bignum_mod_exp(dest, generator, exp, modulo, ctx) \
- mbedtls_mpi_exp_mod(dest, generator, exp, modulo, NULL)
+ (mbedtls_mpi_exp_mod(dest, generator, exp, modulo, NULL)==0?1:0)
+#define bignum_add(dest, a, b) mbedtls_mpi_add_mpi(dest, a, b)
+#define bignum_sub(dest, a, b) mbedtls_mpi_sub_mpi(dest, a, b)
+#define bignum_mod(dest, a, b, ctx) \
+ (mbedtls_mpi_mod_mpi(dest, a, b) == 0 ? 1 : 0)
#define bignum_num_bytes(num) mbedtls_mpi_size(num)
#define bignum_num_bits(num) mbedtls_mpi_bitlen(num)
#define bignum_is_bit_set(num, bit) ssh_mbedcry_is_bit_set(num, bit)
-#define bignum_bn2bin(num, ptr) mbedtls_mpi_write_binary(num, ptr, \
+#define bignum_bn2bin(num, len, ptr) mbedtls_mpi_write_binary(num, ptr, \
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)
mbedtls_ctr_drbg_context *ssh_get_mbedtls_ctr_drbg_context(void);