Skip to content

Commit bb93c6e

Browse files
committed
Make SHA256 state memcpy()'able again.
Related-to: #742 Fixes: 874e095 ("SHA-256 & SHA-224 x86") Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 61cd00e commit bb93c6e

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/hashes/sha2/sha256.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,22 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
233233
@param inlen The length of the data (octets)
234234
@return CRYPT_OK if successful
235235
*/
236-
HASH_PROCESS(sha256_c_process,s_sha256_compress, sha256, 64)
236+
static HASH_PROCESS(s_sha256_c_process,s_sha256_compress, sha256, 64)
237+
238+
static LTC_INLINE void s_sha256_c_fixup_state(hash_state * md)
239+
{
240+
ulong32 *state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
241+
if (state != md->sha256.state) {
242+
XMEMMOVE(state, md->sha256.state, 8 * sizeof(ulong32));
243+
md->sha256.state = state;
244+
}
245+
}
246+
247+
int sha256_c_process (hash_state * md, const unsigned char *in, unsigned long inlen)
248+
{
249+
s_sha256_c_fixup_state(md);
250+
return s_sha256_c_process(md, in, inlen);
251+
}
237252

238253
/**
239254
Terminate the hash to get the digest
@@ -252,6 +267,7 @@ int sha256_c_done(hash_state * md, unsigned char *out)
252267
return CRYPT_INVALID_ARG;
253268
}
254269

270+
s_sha256_c_fixup_state(md);
255271

256272
/* increase the length of the message */
257273
md->sha256.length += md->sha256.curlen * 8;

src/hashes/sha2/sha256_x86.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,22 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
265265
@param inlen The length of the data (octets)
266266
@return CRYPT_OK if successful
267267
*/
268-
HASH_PROCESS(sha256_x86_process,s_sha256_x86_compress, sha256, 64)
268+
static HASH_PROCESS(s_sha256_x86_process,s_sha256_x86_compress, sha256, 64)
269+
270+
static LTC_INLINE void s_sha256_x86_fixup_state(hash_state * md)
271+
{
272+
ulong32 *state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
273+
if (state != md->sha256.state) {
274+
XMEMMOVE(state, md->sha256.state, 8 * sizeof(ulong32));
275+
md->sha256.state = state;
276+
}
277+
}
278+
279+
int sha256_x86_process (hash_state * md, const unsigned char *in, unsigned long inlen)
280+
{
281+
s_sha256_x86_fixup_state(md);
282+
return s_sha256_x86_process(md, in, inlen);
283+
}
269284

270285
/**
271286
Terminate the hash to get the digest
@@ -284,6 +299,7 @@ int sha256_x86_done(hash_state * md, unsigned char *out)
284299
return CRYPT_INVALID_ARG;
285300
}
286301

302+
s_sha256_x86_fixup_state(md);
287303

288304
/* increase the length of the message */
289305
md->sha256.length += md->sha256.curlen * 8;

0 commit comments

Comments
 (0)