Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit e0c2f28

Browse files
committed
kdf: Avoid endianess issues
The key_type is only a letter, if we use and `int` and then cast it to (const char *) we will end up with a 0 value on big endian. Signed-off-by: Andreas Schneider <asn@cryptomilk.org> Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
1 parent 9bb91df commit e0c2f28

6 files changed

Lines changed: 8 additions & 9 deletions

File tree

include/libssh/crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ struct ssh_cipher_struct {
219219
const struct ssh_cipher_struct *ssh_get_chacha20poly1305_cipher(void);
220220
int sshkdf_derive_key(struct ssh_crypto_struct *crypto,
221221
unsigned char *key, size_t key_len,
222-
int key_type, unsigned char *output,
222+
uint8_t key_type, unsigned char *output,
223223
size_t requested_len);
224224

225225
int secure_memcmp(const void *s1, const void *s2, size_t n);

include/libssh/wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ size_t hmac_digest_len(enum ssh_hmac_e type);
103103

104104
int ssh_kdf(struct ssh_crypto_struct *crypto,
105105
unsigned char *key, size_t key_len,
106-
int key_type, unsigned char *output,
106+
uint8_t key_type, unsigned char *output,
107107
size_t requested_len);
108108

109109
int crypt_set_algorithms_client(ssh_session session);

src/kdf.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,13 @@ static void ssh_mac_final(unsigned char *md, ssh_mac_ctx ctx)
116116

117117
int sshkdf_derive_key(struct ssh_crypto_struct *crypto,
118118
unsigned char *key, size_t key_len,
119-
int key_type, unsigned char *output,
119+
uint8_t key_type, unsigned char *output,
120120
size_t requested_len)
121121
{
122122
/* Can't use VLAs with Visual Studio, so allocate the biggest
123123
* digest buffer we can possibly need */
124124
unsigned char digest[DIGEST_MAX_LEN];
125125
size_t output_len = crypto->digest_len;
126-
char letter = key_type;
127126
ssh_mac_ctx ctx;
128127

129128
if (DIGEST_MAX_LEN < crypto->digest_len) {
@@ -137,7 +136,7 @@ int sshkdf_derive_key(struct ssh_crypto_struct *crypto,
137136

138137
ssh_mac_update(ctx, key, key_len);
139138
ssh_mac_update(ctx, crypto->secret_hash, crypto->digest_len);
140-
ssh_mac_update(ctx, &letter, 1);
139+
ssh_mac_update(ctx, &key_type, 1);
141140
ssh_mac_update(ctx, crypto->session_id, crypto->session_id_len);
142141
ssh_mac_final(digest, ctx);
143142

src/libcrypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static const char *sshkdf_digest_to_md(enum ssh_kdf_digest digest_type)
214214

215215
int ssh_kdf(struct ssh_crypto_struct *crypto,
216216
unsigned char *key, size_t key_len,
217-
int key_type, unsigned char *output,
217+
uint8_t key_type, unsigned char *output,
218218
size_t requested_len)
219219
{
220220
int rc = -1;
@@ -330,7 +330,7 @@ int ssh_kdf(struct ssh_crypto_struct *crypto,
330330
#else
331331
int ssh_kdf(struct ssh_crypto_struct *crypto,
332332
unsigned char *key, size_t key_len,
333-
int key_type, unsigned char *output,
333+
uint8_t key_type, unsigned char *output,
334334
size_t requested_len)
335335
{
336336
return sshkdf_derive_key(crypto, key, key_len,

src/libgcrypt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void evp_final(EVPCTX ctx, unsigned char *md, unsigned int *mdlen)
124124

125125
int ssh_kdf(struct ssh_crypto_struct *crypto,
126126
unsigned char *key, size_t key_len,
127-
int key_type, unsigned char *output,
127+
uint8_t key_type, unsigned char *output,
128128
size_t requested_len)
129129
{
130130
return sshkdf_derive_key(crypto, key, key_len,

src/libmbedcrypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void evp_final(EVPCTX ctx, unsigned char *md, unsigned int *mdlen)
127127

128128
int ssh_kdf(struct ssh_crypto_struct *crypto,
129129
unsigned char *key, size_t key_len,
130-
int key_type, unsigned char *output,
130+
uint8_t key_type, unsigned char *output,
131131
size_t requested_len)
132132
{
133133
return sshkdf_derive_key(crypto, key, key_len,

0 commit comments

Comments
 (0)