37 #if defined(HKDF_ALG_NAME)
39 #define HKDF_CONCAT_INNER(name,suffix) name##suffix
40 #define HKDF_CONCAT(name,suffix) HKDF_CONCAT_INNER(name,suffix)
43 (
unsigned char *out,
size_t outlen,
44 const unsigned char *key,
size_t keylen,
45 const unsigned char *salt,
size_t saltlen,
46 const unsigned char *info,
size_t infolen)
49 if (outlen > (
size_t)(HKDF_HMAC_SIZE * 255))
51 HKDF_CONCAT(HKDF_ALG_NAME,_extract)(&state, key, keylen, salt, saltlen);
52 HKDF_CONCAT(HKDF_ALG_NAME,_expand)(&state, info, infolen, out, outlen);
53 aead_clean(&state,
sizeof(state));
57 void HKDF_CONCAT(HKDF_ALG_NAME,_extract)
59 const unsigned char *key,
size_t keylen,
60 const unsigned char *salt,
size_t saltlen)
63 HKDF_HMAC_INIT(&hmac, salt, saltlen);
64 HKDF_HMAC_UPDATE(&hmac, key, keylen);
65 HKDF_HMAC_FINALIZE(&hmac, salt, saltlen, state->prk);
67 state->posn = HKDF_HMAC_SIZE;
68 aead_clean(&hmac,
sizeof(hmac));
71 int HKDF_CONCAT(HKDF_ALG_NAME,_expand)
73 const unsigned char *info,
size_t infolen,
74 unsigned char *out,
size_t outlen)
80 len = HKDF_HMAC_SIZE - state->posn;
83 memcpy(out, state->out + state->posn, len);
91 if (state->counter == 0) {
92 memset(out, 0, outlen);
93 aead_clean(&hmac,
sizeof(hmac));
98 HKDF_HMAC_INIT(&hmac, state->prk,
sizeof(state->prk));
99 if (state->counter != 1)
100 HKDF_HMAC_UPDATE(&hmac, state->out,
sizeof(state->out));
101 HKDF_HMAC_UPDATE(&hmac, info, infolen);
102 HKDF_HMAC_UPDATE(&hmac, &(state->counter), 1);
103 HKDF_HMAC_FINALIZE(&hmac, state->prk,
sizeof(state->prk), state->out);
107 len = HKDF_HMAC_SIZE;
110 memcpy(out, state->out, len);
115 aead_clean(&hmac,
sizeof(hmac));
119 void HKDF_CONCAT(HKDF_ALG_NAME,_free)(HKDF_STATE *state)
121 aead_clean(state,
sizeof(HKDF_STATE));
130 #undef HKDF_HMAC_SIZE
131 #undef HKDF_HMAC_STATE
132 #undef HKDF_HMAC_INIT
133 #undef HKDF_HMAC_UPDATE
134 #undef HKDF_HMAC_FINALIZE