11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * ngtcp2
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Copyright (c) 2019 ngtcp2 contributors
51cb0ef41Sopenharmony_ci *
61cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
71cb0ef41Sopenharmony_ci * a copy of this software and associated documentation files (the
81cb0ef41Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
91cb0ef41Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
101cb0ef41Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
111cb0ef41Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
121cb0ef41Sopenharmony_ci * the following conditions:
131cb0ef41Sopenharmony_ci *
141cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be
151cb0ef41Sopenharmony_ci * included in all copies or substantial portions of the Software.
161cb0ef41Sopenharmony_ci *
171cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
181cb0ef41Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
191cb0ef41Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
201cb0ef41Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
211cb0ef41Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
221cb0ef41Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
231cb0ef41Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
241cb0ef41Sopenharmony_ci */
251cb0ef41Sopenharmony_ci#ifndef NGTCP2_SHARED_H
261cb0ef41Sopenharmony_ci#define NGTCP2_SHARED_H
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci#ifdef HAVE_CONFIG_H
291cb0ef41Sopenharmony_ci#  include <config.h>
301cb0ef41Sopenharmony_ci#endif /* HAVE_CONFIG_H */
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci#include <ngtcp2/ngtcp2_crypto.h>
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci/**
351cb0ef41Sopenharmony_ci * @macro
361cb0ef41Sopenharmony_ci *
371cb0ef41Sopenharmony_ci * :macro:`NGTCP2_INITIAL_SALT_DRAFT` is a salt value which is used to
381cb0ef41Sopenharmony_ci * derive initial secret.  It is used for QUIC draft versions.
391cb0ef41Sopenharmony_ci */
401cb0ef41Sopenharmony_ci#define NGTCP2_INITIAL_SALT_DRAFT                                              \
411cb0ef41Sopenharmony_ci  "\xaf\xbf\xec\x28\x99\x93\xd2\x4c\x9e\x97\x86\xf1\x9c\x61\x11\xe0\x43\x90"   \
421cb0ef41Sopenharmony_ci  "\xa8\x99"
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci/**
451cb0ef41Sopenharmony_ci * @macro
461cb0ef41Sopenharmony_ci *
471cb0ef41Sopenharmony_ci * :macro:`NGTCP2_INITIAL_SALT_V1` is a salt value which is used to
481cb0ef41Sopenharmony_ci * derive initial secret.  It is used for QUIC v1.
491cb0ef41Sopenharmony_ci */
501cb0ef41Sopenharmony_ci#define NGTCP2_INITIAL_SALT_V1                                                 \
511cb0ef41Sopenharmony_ci  "\x38\x76\x2c\xf7\xf5\x59\x34\xb3\x4d\x17\x9a\xe6\xa4\xc8\x0c\xad\xcc\xbb"   \
521cb0ef41Sopenharmony_ci  "\x7f\x0a"
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci/**
551cb0ef41Sopenharmony_ci * @macro
561cb0ef41Sopenharmony_ci *
571cb0ef41Sopenharmony_ci * :macro:`NGTCP2_INITIAL_SALT_V2_DRAFT` is a salt value which is used to
581cb0ef41Sopenharmony_ci * derive initial secret.  It is used for QUIC v2 draft.
591cb0ef41Sopenharmony_ci */
601cb0ef41Sopenharmony_ci#define NGTCP2_INITIAL_SALT_V2_DRAFT                                           \
611cb0ef41Sopenharmony_ci  "\xa7\x07\xc2\x03\xa5\x9b\x47\x18\x4a\x1d\x62\xca\x57\x04\x06\xea\x7a\xe3"   \
621cb0ef41Sopenharmony_ci  "\xe5\xd3"
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci/* Maximum key usage (encryption) limits */
651cb0ef41Sopenharmony_ci#define NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_GCM (1ULL << 23)
661cb0ef41Sopenharmony_ci#define NGTCP2_CRYPTO_MAX_ENCRYPTION_CHACHA20_POLY1305 (1ULL << 62)
671cb0ef41Sopenharmony_ci#define NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_CCM (2965820ULL)
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci/* Maximum authentication failure (decryption) limits during the
701cb0ef41Sopenharmony_ci   lifetime of a connection. */
711cb0ef41Sopenharmony_ci#define NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_GCM (1ULL << 52)
721cb0ef41Sopenharmony_ci#define NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_CHACHA20_POLY1305 (1ULL << 36)
731cb0ef41Sopenharmony_ci#define NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_CCM (2965820ULL)
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci/**
761cb0ef41Sopenharmony_ci * @function
771cb0ef41Sopenharmony_ci *
781cb0ef41Sopenharmony_ci * `ngtcp2_crypto_ctx_initial` initializes |ctx| for Initial packet
791cb0ef41Sopenharmony_ci * encryption and decryption.
801cb0ef41Sopenharmony_ci */
811cb0ef41Sopenharmony_cingtcp2_crypto_ctx *ngtcp2_crypto_ctx_initial(ngtcp2_crypto_ctx *ctx);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci/**
841cb0ef41Sopenharmony_ci * @function
851cb0ef41Sopenharmony_ci *
861cb0ef41Sopenharmony_ci * `ngtcp2_crypto_aead_init` initializes |aead| with the provided
871cb0ef41Sopenharmony_ci * |aead_native_handle| which is an underlying AEAD object.
881cb0ef41Sopenharmony_ci *
891cb0ef41Sopenharmony_ci * If libngtcp2_crypto_openssl is linked, |aead_native_handle| must be
901cb0ef41Sopenharmony_ci * a pointer to EVP_CIPHER.
911cb0ef41Sopenharmony_ci *
921cb0ef41Sopenharmony_ci * If libngtcp2_crypto_gnutls is linked, |aead_native_handle| must be
931cb0ef41Sopenharmony_ci * gnutls_cipher_algorithm_t casted to ``void *``.
941cb0ef41Sopenharmony_ci *
951cb0ef41Sopenharmony_ci * If libngtcp2_crypto_boringssl is linked, |aead_native_handle| must
961cb0ef41Sopenharmony_ci * be a pointer to EVP_AEAD.
971cb0ef41Sopenharmony_ci */
981cb0ef41Sopenharmony_cingtcp2_crypto_aead *ngtcp2_crypto_aead_init(ngtcp2_crypto_aead *aead,
991cb0ef41Sopenharmony_ci                                            void *aead_native_handle);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci/**
1021cb0ef41Sopenharmony_ci * @function
1031cb0ef41Sopenharmony_ci *
1041cb0ef41Sopenharmony_ci * `ngtcp2_crypto_aead_retry` initializes |aead| with the AEAD cipher
1051cb0ef41Sopenharmony_ci * AEAD_AES_128_GCM for Retry packet integrity protection.
1061cb0ef41Sopenharmony_ci */
1071cb0ef41Sopenharmony_cingtcp2_crypto_aead *ngtcp2_crypto_aead_retry(ngtcp2_crypto_aead *aead);
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci/**
1101cb0ef41Sopenharmony_ci * @function
1111cb0ef41Sopenharmony_ci *
1121cb0ef41Sopenharmony_ci * `ngtcp2_crypto_derive_initial_secrets` derives initial secrets.
1131cb0ef41Sopenharmony_ci * |rx_secret| and |tx_secret| must point to the buffer of at least 32
1141cb0ef41Sopenharmony_ci * bytes capacity.  rx for read and tx for write.  This function
1151cb0ef41Sopenharmony_ci * writes rx and tx secrets into |rx_secret| and |tx_secret|
1161cb0ef41Sopenharmony_ci * respectively.  The length of secret is 32 bytes long.
1171cb0ef41Sopenharmony_ci * |client_dcid| is the destination connection ID in first Initial
1181cb0ef41Sopenharmony_ci * packet of client.  If |initial_secret| is not NULL, the initial
1191cb0ef41Sopenharmony_ci * secret is written to it.  It must point to the buffer which has at
1201cb0ef41Sopenharmony_ci * least 32 bytes capacity.  The initial secret is 32 bytes long.
1211cb0ef41Sopenharmony_ci * |side| specifies the side of application.
1221cb0ef41Sopenharmony_ci *
1231cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
1241cb0ef41Sopenharmony_ci */
1251cb0ef41Sopenharmony_ciint ngtcp2_crypto_derive_initial_secrets(uint32_t version, uint8_t *rx_secret,
1261cb0ef41Sopenharmony_ci                                         uint8_t *tx_secret,
1271cb0ef41Sopenharmony_ci                                         uint8_t *initial_secret,
1281cb0ef41Sopenharmony_ci                                         const ngtcp2_cid *client_dcid,
1291cb0ef41Sopenharmony_ci                                         ngtcp2_crypto_side side);
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci/**
1321cb0ef41Sopenharmony_ci * @function
1331cb0ef41Sopenharmony_ci *
1341cb0ef41Sopenharmony_ci * `ngtcp2_crypto_derive_packet_protection_key` derives packet
1351cb0ef41Sopenharmony_ci * protection key.  This function writes packet protection key into
1361cb0ef41Sopenharmony_ci * the buffer pointed by |key|.  The length of derived key is
1371cb0ef41Sopenharmony_ci * `ngtcp2_crypto_aead_keylen(aead) <ngtcp2_crypto_aead_keylen>`
1381cb0ef41Sopenharmony_ci * bytes.  |key| must have enough capacity to store the key.  This
1391cb0ef41Sopenharmony_ci * function writes packet protection IV into |iv|.  The length of
1401cb0ef41Sopenharmony_ci * derived IV is `ngtcp2_crypto_packet_protection_ivlen(aead)
1411cb0ef41Sopenharmony_ci * <ngtcp2_crypto_packet_protection_ivlen>` bytes.  |iv| must have
1421cb0ef41Sopenharmony_ci * enough capacity to store the IV.
1431cb0ef41Sopenharmony_ci *
1441cb0ef41Sopenharmony_ci * If |hp| is not NULL, this function also derives packet header
1451cb0ef41Sopenharmony_ci * protection key and writes the key into the buffer pointed by |hp|.
1461cb0ef41Sopenharmony_ci * The length of derived key is `ngtcp2_crypto_aead_keylen(aead)
1471cb0ef41Sopenharmony_ci * <ngtcp2_crypto_aead_keylen>` bytes.  |hp|, if not NULL, must have
1481cb0ef41Sopenharmony_ci * enough capacity to store the key.
1491cb0ef41Sopenharmony_ci *
1501cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
1511cb0ef41Sopenharmony_ci */
1521cb0ef41Sopenharmony_ciint ngtcp2_crypto_derive_packet_protection_key(uint8_t *key, uint8_t *iv,
1531cb0ef41Sopenharmony_ci                                               uint8_t *hp, uint32_t version,
1541cb0ef41Sopenharmony_ci                                               const ngtcp2_crypto_aead *aead,
1551cb0ef41Sopenharmony_ci                                               const ngtcp2_crypto_md *md,
1561cb0ef41Sopenharmony_ci                                               const uint8_t *secret,
1571cb0ef41Sopenharmony_ci                                               size_t secretlen);
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci/**
1601cb0ef41Sopenharmony_ci * @function
1611cb0ef41Sopenharmony_ci *
1621cb0ef41Sopenharmony_ci * `ngtcp2_crypto_update_traffic_secret` derives the next generation
1631cb0ef41Sopenharmony_ci * of the traffic secret.  |secret| specifies the current secret and
1641cb0ef41Sopenharmony_ci * its length is given in |secretlen|.  The length of new key is the
1651cb0ef41Sopenharmony_ci * same as the current key.  This function writes new key into the
1661cb0ef41Sopenharmony_ci * buffer pointed by |dest|.  |dest| must have the enough capacity to
1671cb0ef41Sopenharmony_ci * store the new key.
1681cb0ef41Sopenharmony_ci *
1691cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
1701cb0ef41Sopenharmony_ci */
1711cb0ef41Sopenharmony_ciint ngtcp2_crypto_update_traffic_secret(uint8_t *dest,
1721cb0ef41Sopenharmony_ci                                        const ngtcp2_crypto_md *md,
1731cb0ef41Sopenharmony_ci                                        const uint8_t *secret,
1741cb0ef41Sopenharmony_ci                                        size_t secretlen);
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci/**
1771cb0ef41Sopenharmony_ci * @function
1781cb0ef41Sopenharmony_ci *
1791cb0ef41Sopenharmony_ci * `ngtcp2_crypto_set_local_transport_params` sets QUIC transport
1801cb0ef41Sopenharmony_ci * parameter, which is encoded in wire format and stored in the buffer
1811cb0ef41Sopenharmony_ci * pointed by |buf| of length |len|, to the native handle |tls|.
1821cb0ef41Sopenharmony_ci *
1831cb0ef41Sopenharmony_ci * |tls| points to a implementation dependent TLS session object.  If
1841cb0ef41Sopenharmony_ci * libngtcp2_crypto_openssl is linked, |tls| must be a pointer to SSL
1851cb0ef41Sopenharmony_ci * object.
1861cb0ef41Sopenharmony_ci *
1871cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
1881cb0ef41Sopenharmony_ci */
1891cb0ef41Sopenharmony_ciint ngtcp2_crypto_set_local_transport_params(void *tls, const uint8_t *buf,
1901cb0ef41Sopenharmony_ci                                             size_t len);
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci/**
1931cb0ef41Sopenharmony_ci * @function
1941cb0ef41Sopenharmony_ci *
1951cb0ef41Sopenharmony_ci * `ngtcp2_crypto_set_remote_transport_params` retrieves a remote QUIC
1961cb0ef41Sopenharmony_ci * transport parameters from |tls| and sets it to |conn| using
1971cb0ef41Sopenharmony_ci * `ngtcp2_conn_set_remote_transport_params`.
1981cb0ef41Sopenharmony_ci *
1991cb0ef41Sopenharmony_ci * |tls| points to a implementation dependent TLS session object.  If
2001cb0ef41Sopenharmony_ci * libngtcp2_crypto_openssl is linked, |tls| must be a pointer to SSL
2011cb0ef41Sopenharmony_ci * object.
2021cb0ef41Sopenharmony_ci *
2031cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
2041cb0ef41Sopenharmony_ci */
2051cb0ef41Sopenharmony_ciint ngtcp2_crypto_set_remote_transport_params(ngtcp2_conn *conn, void *tls);
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci/**
2081cb0ef41Sopenharmony_ci * @function
2091cb0ef41Sopenharmony_ci *
2101cb0ef41Sopenharmony_ci * `ngtcp2_crypto_derive_and_install_initial_key` derives initial
2111cb0ef41Sopenharmony_ci * keying materials and installs keys to |conn|.
2121cb0ef41Sopenharmony_ci *
2131cb0ef41Sopenharmony_ci * If |rx_secret| is not NULL, the secret for decryption is written to
2141cb0ef41Sopenharmony_ci * the buffer pointed by |rx_secret|.  The length of secret is 32
2151cb0ef41Sopenharmony_ci * bytes, and |rx_secret| must point to the buffer which has enough
2161cb0ef41Sopenharmony_ci * capacity.
2171cb0ef41Sopenharmony_ci *
2181cb0ef41Sopenharmony_ci * If |tx_secret| is not NULL, the secret for encryption is written to
2191cb0ef41Sopenharmony_ci * the buffer pointed by |tx_secret|.  The length of secret is 32
2201cb0ef41Sopenharmony_ci * bytes, and |tx_secret| must point to the buffer which has enough
2211cb0ef41Sopenharmony_ci * capacity.
2221cb0ef41Sopenharmony_ci *
2231cb0ef41Sopenharmony_ci * If |initial_secret| is not NULL, the initial secret is written to
2241cb0ef41Sopenharmony_ci * the buffer pointed by |initial_secret|.  The length of secret is 32
2251cb0ef41Sopenharmony_ci * bytes, and |initial_secret| must point to the buffer which has
2261cb0ef41Sopenharmony_ci * enough capacity.
2271cb0ef41Sopenharmony_ci *
2281cb0ef41Sopenharmony_ci * |client_dcid| is the destination connection ID in first Initial
2291cb0ef41Sopenharmony_ci * packet of client.
2301cb0ef41Sopenharmony_ci *
2311cb0ef41Sopenharmony_ci * If |rx_key| is not NULL, the derived packet protection key for
2321cb0ef41Sopenharmony_ci * decryption is written to the buffer pointed by |rx_key|.  If
2331cb0ef41Sopenharmony_ci * |rx_iv| is not NULL, the derived packet protection IV for
2341cb0ef41Sopenharmony_ci * decryption is written to the buffer pointed by |rx_iv|.  If |rx_hp|
2351cb0ef41Sopenharmony_ci * is not NULL, the derived header protection key for decryption is
2361cb0ef41Sopenharmony_ci * written to the buffer pointed by |rx_hp|.
2371cb0ef41Sopenharmony_ci *
2381cb0ef41Sopenharmony_ci * If |tx_key| is not NULL, the derived packet protection key for
2391cb0ef41Sopenharmony_ci * encryption is written to the buffer pointed by |tx_key|.  If
2401cb0ef41Sopenharmony_ci * |tx_iv| is not NULL, the derived packet protection IV for
2411cb0ef41Sopenharmony_ci * encryption is written to the buffer pointed by |tx_iv|.  If |tx_hp|
2421cb0ef41Sopenharmony_ci * is not NULL, the derived header protection key for encryption is
2431cb0ef41Sopenharmony_ci * written to the buffer pointed by |tx_hp|.
2441cb0ef41Sopenharmony_ci *
2451cb0ef41Sopenharmony_ci * The length of packet protection key and header protection key is 16
2461cb0ef41Sopenharmony_ci * bytes long.  The length of packet protection IV is 12 bytes long.
2471cb0ef41Sopenharmony_ci *
2481cb0ef41Sopenharmony_ci * This function calls `ngtcp2_conn_set_initial_crypto_ctx` to set
2491cb0ef41Sopenharmony_ci * initial AEAD and message digest algorithm.  After the successful
2501cb0ef41Sopenharmony_ci * call of this function, application can use
2511cb0ef41Sopenharmony_ci * `ngtcp2_conn_get_initial_crypto_ctx` to get the object.
2521cb0ef41Sopenharmony_ci *
2531cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
2541cb0ef41Sopenharmony_ci */
2551cb0ef41Sopenharmony_ciint ngtcp2_crypto_derive_and_install_initial_key(
2561cb0ef41Sopenharmony_ci    ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
2571cb0ef41Sopenharmony_ci    uint8_t *initial_secret, uint8_t *rx_key, uint8_t *rx_iv, uint8_t *rx_hp,
2581cb0ef41Sopenharmony_ci    uint8_t *tx_key, uint8_t *tx_iv, uint8_t *tx_hp, uint32_t version,
2591cb0ef41Sopenharmony_ci    const ngtcp2_cid *client_dcid);
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci/**
2621cb0ef41Sopenharmony_ci * @function
2631cb0ef41Sopenharmony_ci *
2641cb0ef41Sopenharmony_ci * `ngtcp2_crypto_derive_and_install_vneg_initial_key` derives initial
2651cb0ef41Sopenharmony_ci * keying materials and installs keys to |conn|.  This function is
2661cb0ef41Sopenharmony_ci * dedicated to install keys for |version| which is negotiated, or
2671cb0ef41Sopenharmony_ci * being negotiated.
2681cb0ef41Sopenharmony_ci *
2691cb0ef41Sopenharmony_ci * If |rx_secret| is not NULL, the secret for decryption is written to
2701cb0ef41Sopenharmony_ci * the buffer pointed by |rx_secret|.  The length of secret is 32
2711cb0ef41Sopenharmony_ci * bytes, and |rx_secret| must point to the buffer which has enough
2721cb0ef41Sopenharmony_ci * capacity.
2731cb0ef41Sopenharmony_ci *
2741cb0ef41Sopenharmony_ci * If |tx_secret| is not NULL, the secret for encryption is written to
2751cb0ef41Sopenharmony_ci * the buffer pointed by |tx_secret|.  The length of secret is 32
2761cb0ef41Sopenharmony_ci * bytes, and |tx_secret| must point to the buffer which has enough
2771cb0ef41Sopenharmony_ci * capacity.
2781cb0ef41Sopenharmony_ci *
2791cb0ef41Sopenharmony_ci * If |initial_secret| is not NULL, the initial secret is written to
2801cb0ef41Sopenharmony_ci * the buffer pointed by |initial_secret|.  The length of secret is 32
2811cb0ef41Sopenharmony_ci * bytes, and |initial_secret| must point to the buffer which has
2821cb0ef41Sopenharmony_ci * enough capacity.
2831cb0ef41Sopenharmony_ci *
2841cb0ef41Sopenharmony_ci * |client_dcid| is the destination connection ID in first Initial
2851cb0ef41Sopenharmony_ci * packet of client.
2861cb0ef41Sopenharmony_ci *
2871cb0ef41Sopenharmony_ci * If |rx_key| is not NULL, the derived packet protection key for
2881cb0ef41Sopenharmony_ci * decryption is written to the buffer pointed by |rx_key|.  If
2891cb0ef41Sopenharmony_ci * |rx_iv| is not NULL, the derived packet protection IV for
2901cb0ef41Sopenharmony_ci * decryption is written to the buffer pointed by |rx_iv|.  If |rx_hp|
2911cb0ef41Sopenharmony_ci * is not NULL, the derived header protection key for decryption is
2921cb0ef41Sopenharmony_ci * written to the buffer pointed by |rx_hp|.
2931cb0ef41Sopenharmony_ci *
2941cb0ef41Sopenharmony_ci * If |tx_key| is not NULL, the derived packet protection key for
2951cb0ef41Sopenharmony_ci * encryption is written to the buffer pointed by |tx_key|.  If
2961cb0ef41Sopenharmony_ci * |tx_iv| is not NULL, the derived packet protection IV for
2971cb0ef41Sopenharmony_ci * encryption is written to the buffer pointed by |tx_iv|.  If |tx_hp|
2981cb0ef41Sopenharmony_ci * is not NULL, the derived header protection key for encryption is
2991cb0ef41Sopenharmony_ci * written to the buffer pointed by |tx_hp|.
3001cb0ef41Sopenharmony_ci *
3011cb0ef41Sopenharmony_ci * The length of packet protection key and header protection key is 16
3021cb0ef41Sopenharmony_ci * bytes long.  The length of packet protection IV is 12 bytes long.
3031cb0ef41Sopenharmony_ci *
3041cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
3051cb0ef41Sopenharmony_ci */
3061cb0ef41Sopenharmony_ciint ngtcp2_crypto_derive_and_install_vneg_initial_key(
3071cb0ef41Sopenharmony_ci    ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
3081cb0ef41Sopenharmony_ci    uint8_t *initial_secret, uint8_t *rx_key, uint8_t *rx_iv, uint8_t *rx_hp,
3091cb0ef41Sopenharmony_ci    uint8_t *tx_key, uint8_t *tx_iv, uint8_t *tx_hp, uint32_t version,
3101cb0ef41Sopenharmony_ci    const ngtcp2_cid *client_dcid);
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci/**
3131cb0ef41Sopenharmony_ci * @function
3141cb0ef41Sopenharmony_ci *
3151cb0ef41Sopenharmony_ci * `ngtcp2_crypto_cipher_ctx_encrypt_init` initializes |cipher_ctx|
3161cb0ef41Sopenharmony_ci * with new cipher context object for encryption which is constructed
3171cb0ef41Sopenharmony_ci * to use |key| as encryption key.  |cipher| specifies cipher to use.
3181cb0ef41Sopenharmony_ci *
3191cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
3201cb0ef41Sopenharmony_ci */
3211cb0ef41Sopenharmony_ciint ngtcp2_crypto_cipher_ctx_encrypt_init(ngtcp2_crypto_cipher_ctx *cipher_ctx,
3221cb0ef41Sopenharmony_ci                                          const ngtcp2_crypto_cipher *cipher,
3231cb0ef41Sopenharmony_ci                                          const uint8_t *key);
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci/**
3261cb0ef41Sopenharmony_ci * @function
3271cb0ef41Sopenharmony_ci *
3281cb0ef41Sopenharmony_ci * `ngtcp2_crypto_cipher_ctx_free` frees up resources used by
3291cb0ef41Sopenharmony_ci * |cipher_ctx|.  This function does not free the memory pointed by
3301cb0ef41Sopenharmony_ci * |cipher_ctx| itself.
3311cb0ef41Sopenharmony_ci */
3321cb0ef41Sopenharmony_civoid ngtcp2_crypto_cipher_ctx_free(ngtcp2_crypto_cipher_ctx *cipher_ctx);
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ci/*
3351cb0ef41Sopenharmony_ci * `ngtcp2_crypto_md_sha256` initializes |md| with SHA256 message
3361cb0ef41Sopenharmony_ci * digest algorithm and returns |md|.
3371cb0ef41Sopenharmony_ci */
3381cb0ef41Sopenharmony_cingtcp2_crypto_md *ngtcp2_crypto_md_sha256(ngtcp2_crypto_md *md);
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_cingtcp2_crypto_aead *ngtcp2_crypto_aead_aes_128_gcm(ngtcp2_crypto_aead *aead);
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_ci/*
3431cb0ef41Sopenharmony_ci * `ngtcp2_crypto_random` writes cryptographically-secure random
3441cb0ef41Sopenharmony_ci * |datalen| bytes into the buffer pointed by |data|.
3451cb0ef41Sopenharmony_ci *
3461cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
3471cb0ef41Sopenharmony_ci */
3481cb0ef41Sopenharmony_ciint ngtcp2_crypto_random(uint8_t *data, size_t datalen);
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ci#endif /* NGTCP2_SHARED_H */
351