diff --git a/include/libssh/bignum.h b/include/libssh/bignum.h new file mode 100644 index 00000000..e5f2a472 --- /dev/null +++ b/include/libssh/bignum.h @@ -0,0 +1,32 @@ +/* + * This file is part of the SSH Library + * + * Copyright (c) 2014 by Aris Adamantiadis + * + * This 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. + * + * This 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef BIGNUM_H_ +#define BIGNUM_H_ + +#include "libssh/libcrypto.h" +#include "libssh/libgcrypt.h" + +bignum make_string_bn(ssh_string string); +ssh_string make_bignum_string(bignum num); +void ssh_print_bignum(const char *which,bignum num); + + +#endif /* BIGNUM_H_ */ diff --git a/include/libssh/dh.h b/include/libssh/dh.h index e1039e24..95b76cdd 100644 --- a/include/libssh/dh.h +++ b/include/libssh/dh.h @@ -25,7 +25,6 @@ #include "libssh/crypto.h" -void ssh_print_bignum(const char *which,bignum num); int dh_generate_e(ssh_session session); int dh_generate_f(ssh_session session); int dh_generate_x(ssh_session session); @@ -48,8 +47,5 @@ int make_sessionid(ssh_session session); int hashbufin_add_cookie(ssh_session session, unsigned char *cookie); int hashbufout_add_cookie(ssh_session session); int generate_session_keys(ssh_session session); -bignum make_string_bn(ssh_string string); -ssh_string make_bignum_string(bignum num); - #endif /* DH_H_ */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ae8eb6dc..cad97a68 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -109,6 +109,7 @@ set(libssh_SRCS agent.c auth.c base64.c + bignum.c buffer.c callbacks.c channels.c diff --git a/src/bignum.c b/src/bignum.c new file mode 100644 index 00000000..6904c5d9 --- /dev/null +++ b/src/bignum.c @@ -0,0 +1,94 @@ +/* + * This file is part of the SSH Library + * + * Copyright (c) 2014 by Aris Adamantiadis + * + * 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 "libssh/priv.h" +#include "libssh/bignum.h" +#include "libssh/string.h" + +ssh_string make_bignum_string(bignum num) { + ssh_string ptr = NULL; + int pad = 0; + unsigned int len = bignum_num_bytes(num); + unsigned int bits = bignum_num_bits(num); + + if (len == 0) { + return NULL; + } + + /* If the first bit is set we have a negative number */ + if (!(bits % 8) && bignum_is_bit_set(num, bits - 1)) { + pad++; + } + +#ifdef DEBUG_CRYPTO + fprintf(stderr, "%d bits, %d bytes, %d padding\n", bits, len, pad); +#endif /* DEBUG_CRYPTO */ + + ptr = ssh_string_new(len + pad); + if (ptr == NULL) { + return NULL; + } + + /* We have a negative number so we need a leading zero */ + if (pad) { + ptr->data[0] = 0; + } + +#ifdef HAVE_LIBGCRYPT + bignum_bn2bin(num, len, ptr->data + pad); +#elif HAVE_LIBCRYPTO + bignum_bn2bin(num, ptr->data + pad); +#endif + + return ptr; +} + +bignum make_string_bn(ssh_string string){ + bignum bn = NULL; + unsigned int len = ssh_string_len(string); + +#ifdef DEBUG_CRYPTO + fprintf(stderr, "Importing a %d bits, %d bytes object ...\n", + len * 8, len); +#endif /* DEBUG_CRYPTO */ + +#ifdef HAVE_LIBGCRYPT + bignum_bin2bn(string->data, len, &bn); +#elif defined HAVE_LIBCRYPTO + bn = bignum_bin2bn(string->data, len, NULL); +#endif + + return bn; +} + +/* prints the bignum on stderr */ +void ssh_print_bignum(const char *which, bignum num) { +#ifdef HAVE_LIBGCRYPT + unsigned char *hex = NULL; + bignum_bn2hex(num, &hex); +#elif defined HAVE_LIBCRYPTO + char *hex = NULL; + hex = bignum_bn2hex(num); +#endif + fprintf(stderr, "%s value: ", which); + fprintf(stderr, "%s\n", (hex == NULL) ? "(null)" : (char *) hex); + SAFE_FREE(hex); +} diff --git a/src/buffer.c b/src/buffer.c index 7d4c7b3f..f286c531 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -34,7 +34,7 @@ #include "libssh/priv.h" #include "libssh/buffer.h" #include "libssh/misc.h" -#include "libssh/dh.h" +#include "libssh/bignum.h" /** * @defgroup libssh_buffer The SSH buffer functions. diff --git a/src/dh.c b/src/dh.c index c95e5b58..fc14ff60 100644 --- a/src/dh.c +++ b/src/dh.c @@ -60,6 +60,7 @@ #include "libssh/dh.h" #include "libssh/ssh2.h" #include "libssh/pki.h" +#include "libssh/bignum.h" /* todo: remove it */ #include "libssh/string.h" @@ -225,20 +226,6 @@ void ssh_crypto_finalize(void) { } } -/* prints the bignum on stderr */ -void ssh_print_bignum(const char *which, bignum num) { -#ifdef HAVE_LIBGCRYPT - unsigned char *hex = NULL; - bignum_bn2hex(num, &hex); -#elif defined HAVE_LIBCRYPTO - char *hex = NULL; - hex = bignum_bn2hex(num); -#endif - fprintf(stderr, "%s value: ", which); - fprintf(stderr, "%s\n", (hex == NULL) ? "(null)" : (char *) hex); - SAFE_FREE(hex); -} - int dh_generate_x(ssh_session session) { session->next_crypto->x = bignum_new(); if (session->next_crypto->x == NULL) { @@ -351,62 +338,6 @@ int dh_generate_f(ssh_session session) { return 0; } -ssh_string make_bignum_string(bignum num) { - ssh_string ptr = NULL; - int pad = 0; - unsigned int len = bignum_num_bytes(num); - unsigned int bits = bignum_num_bits(num); - - if (len == 0) { - return NULL; - } - - /* If the first bit is set we have a negative number */ - if (!(bits % 8) && bignum_is_bit_set(num, bits - 1)) { - pad++; - } - -#ifdef DEBUG_CRYPTO - fprintf(stderr, "%d bits, %d bytes, %d padding\n", bits, len, pad); -#endif /* DEBUG_CRYPTO */ - - ptr = ssh_string_new(len + pad); - if (ptr == NULL) { - return NULL; - } - - /* We have a negative number so we need a leading zero */ - if (pad) { - ptr->data[0] = 0; - } - -#ifdef HAVE_LIBGCRYPT - bignum_bn2bin(num, len, ptr->data + pad); -#elif HAVE_LIBCRYPTO - bignum_bn2bin(num, ptr->data + pad); -#endif - - return ptr; -} - -bignum make_string_bn(ssh_string string){ - bignum bn = NULL; - unsigned int len = ssh_string_len(string); - -#ifdef DEBUG_CRYPTO - fprintf(stderr, "Importing a %d bits, %d bytes object ...\n", - len * 8, len); -#endif /* DEBUG_CRYPTO */ - -#ifdef HAVE_LIBGCRYPT - bignum_bin2bn(string->data, len, &bn); -#elif defined HAVE_LIBCRYPTO - bn = bignum_bin2bn(string->data, len, NULL); -#endif - - return bn; -} - ssh_string dh_get_e(ssh_session session) { return make_bignum_string(session->next_crypto->e); } diff --git a/src/pki_crypto.c b/src/pki_crypto.c index 903cb910..425e535d 100644 --- a/src/pki_crypto.c +++ b/src/pki_crypto.c @@ -44,7 +44,7 @@ #include "libssh/session.h" #include "libssh/pki.h" #include "libssh/pki_priv.h" -#include "libssh/dh.h" +#include "libssh/bignum.h" struct pem_get_password_struct { ssh_auth_callback fn;