42 #if defined(KMAC_ALG_NAME)
44 #define KMAC_CONCAT_INNER(name,suffix) name##suffix
45 #define KMAC_CONCAT(name,suffix) KMAC_CONCAT_INNER(name,suffix)
48 (
const unsigned char *key,
size_t keylen,
49 const unsigned char *in,
size_t inlen,
50 const unsigned char *custom,
size_t customlen,
51 unsigned char *out,
size_t outlen)
54 KMAC_CONCAT(KMAC_ALG_NAME,_init)(&state, key, keylen, custom, customlen);
55 KMAC_XOF_ABSORB(&state, in, inlen);
56 KMAC_CONCAT(KMAC_ALG_NAME,_set_output_length)(&state, outlen);
57 KMAC_XOF_SQUEEZE(&state, out, outlen);
58 aead_clean(&state,
sizeof(state));
69 static size_t KMAC_CONCAT(KMAC_ALG_NAME,_encode_length)
70 (
unsigned char buf[
sizeof(size_t) + 1],
size_t value)
72 uint64_t val = value * 8ULL;
81 buf[0] = (
unsigned char)size;
82 for (posn = 1; posn <= size; ++posn)
83 buf[posn] = (
unsigned char)(val >> ((size - posn) * 8));
92 void KMAC_CONCAT(KMAC_ALG_NAME,_init)
93 (KMAC_STATE *state,
const unsigned char *key,
size_t keylen,
94 const unsigned char *custom,
size_t customlen)
96 unsigned char buf[
sizeof(uint64_t) + 1];
101 static unsigned char const kmac_prefix[] = {
103 #if !defined(KMAC_XOF_PREINIT)
104 , 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43
110 #if defined(KMAC_XOF_PREINIT)
111 KMAC_XOF_PREINIT(state);
113 KMAC_XOF_INIT(state);
114 KMAC_XOF_ABSORB(state, kmac_prefix,
sizeof(kmac_prefix));
118 len = KMAC_CONCAT(KMAC_ALG_NAME,_encode_length)(buf, customlen);
119 KMAC_XOF_ABSORB(state, buf, len);
120 KMAC_XOF_ABSORB(state, custom, customlen);
124 KMAC_XOF_ABSORB(state, kmac_prefix, 2);
125 len = KMAC_CONCAT(KMAC_ALG_NAME,_encode_length)(buf, keylen);
126 KMAC_XOF_ABSORB(state, buf, len);
127 KMAC_XOF_ABSORB(state, key, keylen);
131 void KMAC_CONCAT(KMAC_ALG_NAME,_absorb)
132 (KMAC_STATE *state,
const unsigned char *in,
size_t inlen)
134 KMAC_XOF_ABSORB(state, in, inlen);
144 static void KMAC_CONCAT(KMAC_ALG_NAME,_encode_output_length)
145 (KMAC_STATE *state,
size_t outlen)
148 unsigned char buf[
sizeof(uint64_t) + 1];
149 size_t len = KMAC_CONCAT(KMAC_ALG_NAME,_encode_length)(buf, outlen);
150 KMAC_XOF_ABSORB(state, buf + 1, len - 1);
151 KMAC_XOF_ABSORB(state, buf, 1);
154 void KMAC_CONCAT(KMAC_ALG_NAME,_set_output_length)
155 (KMAC_STATE *state,
size_t outlen)
157 if (KMAC_XOF_IS_ABSORBING(state)) {
159 KMAC_CONCAT(KMAC_ALG_NAME,_encode_output_length)(state, outlen);
162 KMAC_XOF_SQUEEZE(state, 0, 0);
166 void KMAC_CONCAT(KMAC_ALG_NAME,_squeeze)
167 (KMAC_STATE *state,
unsigned char *out,
size_t outlen)
169 if (KMAC_XOF_IS_ABSORBING(state)) {
172 KMAC_CONCAT(KMAC_ALG_NAME,_encode_output_length)(state, 0);
174 KMAC_XOF_SQUEEZE(state, out, outlen);
177 void KMAC_CONCAT(KMAC_ALG_NAME,_finalize)
178 (KMAC_STATE *state,
unsigned char out[KMAC_SIZE])
180 if (KMAC_XOF_IS_ABSORBING(state)) {
183 KMAC_CONCAT(KMAC_ALG_NAME,_encode_output_length)(state, KMAC_SIZE);
185 KMAC_XOF_SQUEEZE(state, out, KMAC_SIZE);
197 #undef KMAC_XOF_PREINIT
198 #undef KMAC_XOF_ABSORB
199 #undef KMAC_XOF_SQUEEZE
201 #undef KMAC_XOF_IS_ABSORBING
202 #undef KMAC_CONCAT_INNER