1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-11-21 14:00:51 +03:00

wincng: make more use of new helper functions (#496)

This commit is contained in:
Marc Hoersken
2020-07-06 21:43:03 +02:00
parent 1894b30b5c
commit 5eaa7aa1c0

View File

@@ -423,6 +423,24 @@ _libssh2_wincng_safe_free(void *buf, int len)
free(buf); free(buf);
} }
/* Copy a big endian set of bits from src to dest.
* if the size of src is smaller than dest then pad the "left" (MSB)
* end with zeroes and copy the bits into the "right" (LSB) end. */
static void
memcpy_with_be_padding(unsigned char *dest, unsigned long dest_len,
unsigned char *src, unsigned long src_len)
{
if(dest_len > src_len) {
memset(dest, 0, dest_len - src_len);
}
memcpy((dest + dest_len) - src_len, src, src_len);
}
static int
round_down(int number, int multiple)
{
return (number / multiple) * multiple;
}
/*******************************************************************/ /*******************************************************************/
/* /*
@@ -2060,6 +2078,7 @@ _libssh2_wincng_bignum_mod_exp(_libssh2_bn *r,
offset += p->length; offset += p->length;
memcpy(key + offset, m->bignum, m->length); memcpy(key + offset, m->bignum, m->length);
offset = 0;
ret = BCryptImportKeyPair(_libssh2_wincng.hAlgRSA, NULL, ret = BCryptImportKeyPair(_libssh2_wincng.hAlgRSA, NULL,
BCRYPT_RSAPUBLIC_BLOB, &hKey, key, keylen, 0); BCRYPT_RSAPUBLIC_BLOB, &hKey, key, keylen, 0);
@@ -2071,9 +2090,8 @@ _libssh2_wincng_bignum_mod_exp(_libssh2_bn *r,
length = max(a->length, length); length = max(a->length, length);
bignum = malloc(length); bignum = malloc(length);
if(bignum) { if(bignum) {
offset = length - a->length; memcpy_with_be_padding(bignum, length,
memset(bignum, 0, offset); a->bignum, a->length);
memcpy(bignum + offset, a->bignum, a->length);
ret = BCryptEncrypt(hKey, bignum, length, NULL, NULL, 0, ret = BCryptEncrypt(hKey, bignum, length, NULL, NULL, 0,
r->bignum, r->length, &offset, r->bignum, r->length, &offset,
@@ -2204,6 +2222,7 @@ _libssh2_wincng_bignum_free(_libssh2_bn *bn)
} }
} }
/*******************************************************************/ /*******************************************************************/
/* /*
* Windows CNG backend: Diffie-Hellman support. * Windows CNG backend: Diffie-Hellman support.
@@ -2237,25 +2256,6 @@ _libssh2_dh_dtor(_libssh2_dh_ctx *dhctx)
} }
} }
/* Copy a big endian set of bits from src to dest.
* if the size of src is smaller than dest then pad the "left" (MSB)
* end with zeroes and copy the bits into the "right" (LSB) end. */
static void
memcpy_with_be_padding(unsigned char *dest, unsigned long dest_len,
unsigned char *src, unsigned long src_len)
{
if(dest_len > src_len) {
memset(dest, 0, dest_len - src_len);
}
memcpy(dest + dest_len - src_len, src, src_len);
}
static int
round_down(int number, int multiple)
{
return (number / multiple) * multiple;
}
/* Generates a Diffie-Hellman key pair using base `g', prime `p' and the given /* Generates a Diffie-Hellman key pair using base `g', prime `p' and the given
* `group_order'. Can use the given big number context `bnctx' if needed. The * `group_order'. Can use the given big number context `bnctx' if needed. The
* private key is stored as opaque in the Diffie-Hellman context `*dhctx' and * private key is stored as opaque in the Diffie-Hellman context `*dhctx' and