|
Packit |
6c0a39 |
/*
|
|
Packit |
6c0a39 |
* This file is part of the SSH Library
|
|
Packit |
6c0a39 |
*
|
|
Packit |
6c0a39 |
* Copyright (c) 2014 by Aris Adamantiadis <aris@badcode.be>
|
|
Packit |
6c0a39 |
*
|
|
Packit |
6c0a39 |
* The SSH Library is free software; you can redistribute it and/or modify
|
|
Packit |
6c0a39 |
* it under the terms of the GNU Lesser General Public License as published by
|
|
Packit |
6c0a39 |
* the Free Software Foundation; either version 2.1 of the License, or (at your
|
|
Packit |
6c0a39 |
* option) any later version.
|
|
Packit |
6c0a39 |
*
|
|
Packit |
6c0a39 |
* The SSH Library is distributed in the hope that it will be useful, but
|
|
Packit |
6c0a39 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
Packit |
6c0a39 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
Packit |
6c0a39 |
* License for more details.
|
|
Packit |
6c0a39 |
*
|
|
Packit |
6c0a39 |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit |
6c0a39 |
* along with the SSH Library; see the file COPYING. If not, write to
|
|
Packit |
6c0a39 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
|
Packit |
6c0a39 |
* MA 02111-1307, USA.
|
|
Packit |
6c0a39 |
*/
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
#include "config.h"
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
#include <stdio.h>
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
#include "libssh/priv.h"
|
|
Packit |
6c0a39 |
#include "libssh/bignum.h"
|
|
Packit |
6c0a39 |
#include "libssh/string.h"
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
ssh_string ssh_make_bignum_string(bignum num) {
|
|
Packit |
6c0a39 |
ssh_string ptr = NULL;
|
|
Packit Service |
fcc0d2 |
size_t pad = 0;
|
|
Packit Service |
fcc0d2 |
size_t len = bignum_num_bytes(num);
|
|
Packit Service |
fcc0d2 |
size_t bits = bignum_num_bits(num);
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
if (len == 0) {
|
|
Packit |
6c0a39 |
return NULL;
|
|
Packit |
6c0a39 |
}
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
/* If the first bit is set we have a negative number */
|
|
Packit |
6c0a39 |
if (!(bits % 8) && bignum_is_bit_set(num, bits - 1)) {
|
|
Packit |
6c0a39 |
pad++;
|
|
Packit |
6c0a39 |
}
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
#ifdef DEBUG_CRYPTO
|
|
Packit Service |
fcc0d2 |
SSH_LOG(SSH_LOG_TRACE,
|
|
Packit Service |
fcc0d2 |
"%zu bits, %zu bytes, %zu padding\n",
|
|
Packit Service |
fcc0d2 |
bits, len, pad);
|
|
Packit |
6c0a39 |
#endif /* DEBUG_CRYPTO */
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
ptr = ssh_string_new(len + pad);
|
|
Packit |
6c0a39 |
if (ptr == NULL) {
|
|
Packit |
6c0a39 |
return NULL;
|
|
Packit |
6c0a39 |
}
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
/* We have a negative number so we need a leading zero */
|
|
Packit |
6c0a39 |
if (pad) {
|
|
Packit |
6c0a39 |
ptr->data[0] = 0;
|
|
Packit |
6c0a39 |
}
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
bignum_bn2bin(num, len, ptr->data + pad);
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
return ptr;
|
|
Packit |
6c0a39 |
}
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
bignum ssh_make_string_bn(ssh_string string)
|
|
Packit |
6c0a39 |
{
|
|
Packit |
6c0a39 |
bignum bn = NULL;
|
|
Packit |
6c0a39 |
size_t len = ssh_string_len(string);
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
#ifdef DEBUG_CRYPTO
|
|
Packit Service |
fcc0d2 |
SSH_LOG(SSH_LOG_TRACE,
|
|
Packit Service |
fcc0d2 |
"Importing a %zu bits, %zu bytes object ...\n",
|
|
Packit |
6c0a39 |
len * 8, len);
|
|
Packit |
6c0a39 |
#endif /* DEBUG_CRYPTO */
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
bignum_bin2bn(string->data, len, &bn);
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
return bn;
|
|
Packit |
6c0a39 |
}
|
|
Packit |
6c0a39 |
|
|
Packit |
6c0a39 |
/* prints the bignum on stderr */
|
|
Packit Service |
fcc0d2 |
void ssh_print_bignum(const char *name, const_bignum num)
|
|
Packit |
6c0a39 |
{
|
|
Packit |
6c0a39 |
unsigned char *hex = NULL;
|
|
Packit |
6c0a39 |
if (num != NULL) {
|
|
Packit |
6c0a39 |
bignum_bn2hex(num, &hex;;
|
|
Packit |
6c0a39 |
}
|
|
Packit |
6c0a39 |
fprintf(stderr, "%s value: %s\n", name, (hex == NULL) ? "(null)" : (char *) hex);
|
|
Packit |
6c0a39 |
#ifdef HAVE_LIBGCRYPT
|
|
Packit |
6c0a39 |
SAFE_FREE(hex);
|
|
Packit |
6c0a39 |
#elif defined HAVE_LIBCRYPTO
|
|
Packit |
6c0a39 |
OPENSSL_free(hex);
|
|
Packit |
6c0a39 |
#elif defined HAVE_LIBMBEDCRYPTO
|
|
Packit |
6c0a39 |
SAFE_FREE(hex);
|
|
Packit |
6c0a39 |
#endif
|
|
Packit |
6c0a39 |
}
|