hcrypto: Fix UB

This commit is contained in:
Nicolas Williams
2022-10-26 01:54:02 -05:00
parent e4311f3a82
commit 5b1c69b424
3 changed files with 31 additions and 30 deletions

View File

@@ -344,14 +344,14 @@ load(const unsigned char *b, uint32_t v[2])
static void static void
store(const uint32_t v[2], unsigned char *b) store(const uint32_t v[2], unsigned char *b)
{ {
b[0] = (v[0] >> 24) & 0xff; b[0] = (v[0] >> 24) & 0xffU;
b[1] = (v[0] >> 16) & 0xff; b[1] = (v[0] >> 16) & 0xffU;
b[2] = (v[0] >> 8) & 0xff; b[2] = (v[0] >> 8) & 0xffU;
b[3] = (v[0] >> 0) & 0xff; b[3] = (v[0] >> 0) & 0xffU;
b[4] = (v[1] >> 24) & 0xff; b[4] = (v[1] >> 24) & 0xffU;
b[5] = (v[1] >> 16) & 0xff; b[5] = (v[1] >> 16) & 0xffU;
b[6] = (v[1] >> 8) & 0xff; b[6] = (v[1] >> 8) & 0xffU;
b[7] = (v[1] >> 0) & 0xff; b[7] = (v[1] >> 0) & 0xffU;
} }
/** /**

View File

@@ -51,9 +51,9 @@ struct hash_foo {
const char *name; const char *name;
size_t psize; size_t psize;
size_t hsize; size_t hsize;
void (*init)(void*); int (*init)(void*);
void (*update)(void*, const void*, size_t); int (*update)(void*, const void*, size_t);
void (*final)(void*, void*); int (*final)(void*, void*);
const EVP_MD * (*evp)(void); const EVP_MD * (*evp)(void);
} md2 = { } md2 = {
"MD2", "MD2",
@@ -67,52 +67,52 @@ struct hash_foo {
"MD4", "MD4",
sizeof(MD4_CTX), sizeof(MD4_CTX),
16, 16,
(void (*)(void*))MD4_Init, (int (*)(void*))MD4_Init,
(void (*)(void*,const void*, size_t))MD4_Update, (int (*)(void*,const void*, size_t))MD4_Update,
(void (*)(void*, void*))MD4_Final, (int (*)(void*, void*))MD4_Final,
EVP_md4 EVP_md4
}, md5 = { }, md5 = {
"MD5", "MD5",
sizeof(MD5_CTX), sizeof(MD5_CTX),
16, 16,
(void (*)(void*))MD5_Init, (int (*)(void*))MD5_Init,
(void (*)(void*,const void*, size_t))MD5_Update, (int (*)(void*,const void*, size_t))MD5_Update,
(void (*)(void*, void*))MD5_Final, (int (*)(void*, void*))MD5_Final,
EVP_md5 EVP_md5
}, sha1 = { }, sha1 = {
"SHA-1", "SHA-1",
sizeof(struct sha), sizeof(struct sha),
20, 20,
(void (*)(void*))SHA1_Init, (int (*)(void*))SHA1_Init,
(void (*)(void*,const void*, size_t))SHA1_Update, (int (*)(void*,const void*, size_t))SHA1_Update,
(void (*)(void*, void*))SHA1_Final, (int (*)(void*, void*))SHA1_Final,
EVP_sha1 EVP_sha1
}; };
struct hash_foo sha256 = { struct hash_foo sha256 = {
"SHA-256", "SHA-256",
sizeof(SHA256_CTX), sizeof(SHA256_CTX),
32, 32,
(void (*)(void*))SHA256_Init, (int (*)(void*))SHA256_Init,
(void (*)(void*,const void*, size_t))SHA256_Update, (int (*)(void*,const void*, size_t))SHA256_Update,
(void (*)(void*, void*))SHA256_Final, (int (*)(void*, void*))SHA256_Final,
EVP_sha256 EVP_sha256
}; };
struct hash_foo sha384 = { struct hash_foo sha384 = {
"SHA-384", "SHA-384",
sizeof(SHA384_CTX), sizeof(SHA384_CTX),
48, 48,
(void (*)(void*))SHA384_Init, (int (*)(void*))SHA384_Init,
(void (*)(void*,const void*, size_t))SHA384_Update, (int (*)(void*,const void*, size_t))SHA384_Update,
(void (*)(void*, void*))SHA384_Final, (int (*)(void*, void*))SHA384_Final,
EVP_sha384 EVP_sha384
}; };
struct hash_foo sha512 = { struct hash_foo sha512 = {
"SHA-512", "SHA-512",
sizeof(SHA512_CTX), sizeof(SHA512_CTX),
64, 64,
(void (*)(void*))SHA512_Init, (int (*)(void*))SHA512_Init,
(void (*)(void*,const void*, size_t))SHA512_Update, (int (*)(void*,const void*, size_t))SHA512_Update,
(void (*)(void*, void*))SHA512_Final, (int (*)(void*, void*))SHA512_Final,
EVP_sha512 EVP_sha512
}; };

View File

@@ -85,7 +85,8 @@ PKCS5_PBKDF2_HMAC(const void * password, size_t password_len,
data = &tmpcksum[checksumsize]; data = &tmpcksum[checksumsize];
memcpy(data, salt, salt_len); if (salt_len)
memcpy(data, salt, salt_len);
keypart = 1; keypart = 1;
leftofkey = keylen; leftofkey = keylen;