Skip to content
Snippets Groups Projects
Commit bdd640e7 authored by James M Snell's avatar James M Snell Committed by Tatsuhiro Tsujikawa
Browse files

Additional crypto helper function

* `ngtcp2_crypto_generate_stateless_reset_token` - Used to generate
  a stateless reset token as an HKDF extraction using the CID and
  a token secret as input.
parent 45103f25
Branches
No related merge requests found
...@@ -492,6 +492,21 @@ NGTCP2_EXTERN int ...@@ -492,6 +492,21 @@ NGTCP2_EXTERN int
ngtcp2_crypto_set_remote_transport_params(ngtcp2_conn *conn, void *tls, ngtcp2_crypto_set_remote_transport_params(ngtcp2_conn *conn, void *tls,
ngtcp2_crypto_side side); ngtcp2_crypto_side side);
/**
* @function
*
* `ngtcp2_crypto_generate_stateless_reset_token` generates a
* stateless reset token using HKDF extraction with |md| using the
* given |cid| and static key |secret| as input. The token will be
* written to the buffer pointed by |token| and it must have a
* capacity of at least NGTCP2_STATELESS_RESET_TOKENLEN bytes.
*
* This function returns 0 if it succeeds, or -1.
*/
NGTCP2_EXTERN int ngtcp2_crypto_generate_stateless_reset_token(
uint8_t *token, const ngtcp2_crypto_md *md, const uint8_t *secret,
size_t secretlen, const ngtcp2_cid *cid);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "shared.h" #include "shared.h"
#include <string.h> #include <string.h>
#include <assert.h>
#include "ngtcp2_macro.h" #include "ngtcp2_macro.h"
...@@ -419,3 +420,25 @@ int ngtcp2_crypto_update_key_cb(ngtcp2_conn *conn, uint8_t *rx_secret, ...@@ -419,3 +420,25 @@ int ngtcp2_crypto_update_key_cb(ngtcp2_conn *conn, uint8_t *rx_secret,
} }
return 0; return 0;
} }
int ngtcp2_crypto_generate_stateless_reset_token(uint8_t *token,
const ngtcp2_crypto_md *md,
const uint8_t *secret,
size_t secretlen,
const ngtcp2_cid *cid) {
uint8_t buf[64];
int rv;
assert(ngtcp2_crypto_md_hashlen(md) <= sizeof(buf));
assert(NGTCP2_STATELESS_RESET_TOKENLEN <= sizeof(buf));
rv = ngtcp2_crypto_hkdf_extract(buf, md, secret, secretlen, cid->data,
cid->datalen);
if (rv != 0) {
return -1;
}
memcpy(token, buf, NGTCP2_STATELESS_RESET_TOKENLEN);
return 0;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment