Skip to content

Commit 4d1344f

Browse files
authored
Merge branch 'develop' into sha512x86
2 parents 407658c + ff52a70 commit 4d1344f

8 files changed

Lines changed: 32 additions & 27 deletions

File tree

src/hashes/sha1_desc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ int sha1_test(void)
132132
int sha1_test_desc(const struct ltc_hash_descriptor *desc, const char *name)
133133
{
134134
#ifndef LTC_TEST
135+
(void)desc;
136+
(void)name;
135137
return CRYPT_NOP;
136138
#else
137139
static const struct {

src/mac/hmac/hmac_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int hmac_file(int hash, const char *fname,
3939
int err;
4040

4141
LTC_ARGCHK(fname != NULL);
42-
LTC_ARGCHK(key != NULL);
42+
LTC_ARGCHK(key != NULL || keylen == 0);
4343
LTC_ARGCHK(out != NULL);
4444
LTC_ARGCHK(outlen != NULL);
4545

src/mac/hmac/hmac_init.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
2727
int err;
2828

2929
LTC_ARGCHK(hmac != NULL);
30-
LTC_ARGCHK(key != NULL);
30+
LTC_ARGCHK(key != NULL || keylen == 0);
3131

3232
/* valid hash? */
3333
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
@@ -36,11 +36,6 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
3636
hmac->hash = hash;
3737
hashsize = hash_descriptor[hash].hashsize;
3838

39-
/* valid key length? */
40-
if (keylen == 0) {
41-
return CRYPT_INVALID_KEYSIZE;
42-
}
43-
4439
/* allocate ram for buf */
4540
buf = XMALLOC(LTC_HMAC_BLOCKSIZE);
4641
if (buf == NULL) {
@@ -60,7 +55,7 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
6055
goto LBL_ERR;
6156
}
6257
keylen = hashsize;
63-
} else {
58+
} else if (keylen != 0) {
6459
XMEMCPY(hmac->key, key, (size_t)keylen);
6560
}
6661

src/mac/hmac/hmac_memory.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ int hmac_memory(int hash,
2828
hmac_state *hmac;
2929
int err;
3030

31-
LTC_ARGCHK(key != NULL);
32-
LTC_ARGCHK(in != NULL);
31+
LTC_ARGCHK(key != NULL || keylen == 0);
32+
LTC_ARGCHK(in != NULL || inlen == 0);
3333
LTC_ARGCHK(out != NULL);
3434
LTC_ARGCHK(outlen != NULL);
3535

src/mac/hmac/hmac_memory_multi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ int hmac_memory_multi(int hash,
3434
const unsigned char *curptr;
3535
unsigned long curlen;
3636

37-
LTC_ARGCHK(key != NULL);
38-
LTC_ARGCHK(in != NULL);
37+
LTC_ARGCHK(key != NULL || keylen == 0);
38+
LTC_ARGCHK(in != NULL || inlen == 0);
3939
LTC_ARGCHK(out != NULL);
4040
LTC_ARGCHK(outlen != NULL);
4141

src/mac/hmac/hmac_process.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen)
2020
{
2121
int err;
2222
LTC_ARGCHK(hmac != NULL);
23-
LTC_ARGCHK(in != NULL);
23+
LTC_ARGCHK(in != NULL || inlen == 0);
24+
if (inlen == 0) return CRYPT_OK;
2425
if ((err = hash_is_valid(hmac->hash)) != CRYPT_OK) {
2526
return err;
2627
}

src/mac/hmac/hmac_test.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ int hmac_test(void)
4848
return CRYPT_NOP;
4949
#else
5050
unsigned char digest[MAXBLOCKSIZE];
51+
static const unsigned char empty_key_sha256_expected[32] = {
52+
0xfd, 0x7a, 0xdb, 0x15, 0x2c, 0x05, 0xef, 0x80,
53+
0xdc, 0xcf, 0x50, 0xa1, 0xfa, 0x4c, 0x05, 0xd5,
54+
0xa3, 0xec, 0x6d, 0xa9, 0x55, 0x75, 0xfc, 0x31,
55+
0x2a, 0xe7, 0xc5, 0xd0, 0x91, 0x83, 0x63, 0x51
56+
};
57+
static const unsigned char empty_key_msg[] = { 'a', 'b', 'c' };
5158
int i;
5259

5360
static const unsigned char hmac_test_case_keys[][136] = {
@@ -610,6 +617,17 @@ int hmac_test(void)
610617
if (tested == 0) {
611618
return CRYPT_NOP;
612619
}
620+
621+
i = find_hash("sha256");
622+
if (i != -1) {
623+
outlen = sizeof(digest);
624+
if ((err = hmac_memory(i, NULL, 0, empty_key_msg, sizeof(empty_key_msg), digest, &outlen)) != CRYPT_OK) {
625+
return err;
626+
}
627+
if (ltc_compare_testvector(digest, outlen, empty_key_sha256_expected, sizeof(empty_key_sha256_expected), "empty-key sha256", (unsigned long)i)) {
628+
return CRYPT_FAIL_TESTVECTOR;
629+
}
630+
}
613631
return CRYPT_OK;
614632
#endif
615633
}

src/misc/scrypt/scrypt.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ int scrypt_pbkdf(const unsigned char *password, unsigned long password_len,
123123
unsigned char *out, unsigned long outlen)
124124
{
125125
unsigned char *B = NULL, *V = NULL, *XY = NULL;
126-
const unsigned char *pwd;
127-
unsigned long pwd_len, Blen, Vlen, XYlen, i;
128-
unsigned char zero_byte = 0;
126+
unsigned long Blen, Vlen, XYlen, i;
129127
int err, hash_idx;
130128

131129
LTC_ARGCHK(out != NULL);
@@ -143,15 +141,6 @@ int scrypt_pbkdf(const unsigned char *password, unsigned long password_len,
143141
hash_idx = find_hash("sha256");
144142
if (hash_idx == -1) return CRYPT_INVALID_HASH;
145143

146-
/* WORKAROUND: HMAC rejects zero-length keys; a single zero byte
147-
* produces the same zero-padded key block as an empty key. */
148-
pwd = password;
149-
pwd_len = password_len;
150-
if (pwd_len == 0) {
151-
pwd = &zero_byte;
152-
pwd_len = 1;
153-
}
154-
155144
Blen = 128 * r * p;
156145
Vlen = 128 * r * N;
157146
XYlen = 256 * r;
@@ -167,7 +156,7 @@ int scrypt_pbkdf(const unsigned char *password, unsigned long password_len,
167156
/* 1: B = PBKDF2-HMAC-SHA256(password, salt, 1, p * 128 * r) */
168157
{
169158
unsigned long blen_out = Blen;
170-
err = pkcs_5_alg2(pwd, pwd_len, salt, salt_len, 1, hash_idx, B, &blen_out);
159+
err = pkcs_5_alg2(password, password_len, salt, salt_len, 1, hash_idx, B, &blen_out);
171160
if (err != CRYPT_OK) goto cleanup;
172161
}
173162
/* 2: for i = 0 to p-1: B[i] = ROMix(r, B[i], N) */
@@ -177,7 +166,7 @@ int scrypt_pbkdf(const unsigned char *password, unsigned long password_len,
177166
/* 3: DK = PBKDF2-HMAC-SHA256(password, B, 1, dkLen) */
178167
{
179168
unsigned long outlen_out = outlen;
180-
err = pkcs_5_alg2(pwd, pwd_len, B, Blen, 1, hash_idx, out, &outlen_out);
169+
err = pkcs_5_alg2(password, password_len, B, Blen, 1, hash_idx, out, &outlen_out);
181170
}
182171

183172
cleanup:

0 commit comments

Comments
 (0)