forked from ANSSI-FR/libdrbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshake.c
115 lines (92 loc) · 3.13 KB
/
shake.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (C) 2022 - This file is part of libdrbg project
*
* Author: Ryad BENADJILA <ryad.benadjila@ssi.gouv.fr>
* Contributor: Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "libhash_config.h"
#if defined(WITH_HASH_SHAKE256)
#include "shake.h"
/* Init function depending on the digest size */
int _shake_init(shake_context *ctx, uint8_t digest_size, uint8_t block_size)
{
int ret;
MUST_HAVE((ctx != NULL), ret, err);
/* Zeroize the internal state */
memset(ctx->shake_state, 0, sizeof(ctx->shake_state));
ctx->shake_idx = 0;
ctx->shake_digest_size = digest_size;
ctx->shake_block_size = block_size;
/* Detect endianness */
ctx->shake_endian = arch_is_big_endian() ? SHAKE_BIG : SHAKE_LITTLE;
ret = 0;
err:
return ret;
}
/* Update hash function */
int _shake_update(shake_context *ctx, const uint8_t *input, uint32_t ilen)
{
uint32_t i;
uint8_t *state;
int ret;
MUST_HAVE((ctx != NULL) && ((input != NULL) || (ilen == 0)), ret, err);
state = (uint8_t*)(ctx->shake_state);
for(i = 0; i < ilen; i++){
/* Compute the index depending on the endianness */
uint64_t idx = (ctx->shake_endian == SHAKE_LITTLE) ? ctx->shake_idx : SWAP64_Idx(ctx->shake_idx);
ctx->shake_idx++;
/* Update the state, and adapt endianness order */
state[idx] ^= input[i];
if(ctx->shake_idx == ctx->shake_block_size){
KECCAKF(ctx->shake_state);
ctx->shake_idx = 0;
}
}
ret = 0;
err:
return ret;
}
/* Finalize hash function */
int _shake_finalize(shake_context *ctx, uint8_t *output)
{
unsigned int i;
uint8_t *state;
int ret;
MUST_HAVE((ctx != NULL) && (output != NULL), ret, err);
MUST_HAVE((ctx->shake_digest_size <= sizeof(ctx->shake_state)), ret, err);
state = (uint8_t*)(ctx->shake_state);
/* Proceed with the padding of the last block */
/* Compute the index depending on the endianness */
if(ctx->shake_endian == SHAKE_LITTLE){
/* Little endian case */
state[ctx->shake_idx] ^= 0x1f;
state[ctx->shake_block_size - 1] ^= 0x80;
}
else{
/* Big endian case */
state[SWAP64_Idx(ctx->shake_idx)] ^= 0x1f;
state[SWAP64_Idx(ctx->shake_block_size - 1)] ^= 0x80;
}
/* Produce the output.
* NOTE: we should have a fixed version of SHAKE producing an output size
* with size less than the state size.
*/
KECCAKF(ctx->shake_state);
for(i = 0; i < ctx->shake_digest_size; i++){
output[i] = (ctx->shake_endian == SHAKE_LITTLE) ? state[i] : state[SWAP64_Idx(i)];
}
/* Uninit our context magic */
ctx->magic = (uint64_t)0;
ret = 0;
err:
return ret;
}
#else
/*
* Dummy definition to avoid the empty translation unit ISO C warning
*/
typedef int dummy;
#endif