| 1 | /* |
| 2 | * ngtcp2 |
| 3 | * |
| 4 | * Copyright (c) 2019 ngtcp2 contributors |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | * a copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be |
| 15 | * included in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | #ifndef NGTCP2_CRYPTO_H |
| 26 | #define NGTCP2_CRYPTO_H |
| 27 | |
| 28 | #include <ngtcp2/ngtcp2.h> |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif /* defined(__cplusplus) */ |
| 33 | |
| 34 | #ifdef WIN32 |
| 35 | # ifndef WIN32_LEAN_AND_MEAN |
| 36 | # define WIN32_LEAN_AND_MEAN |
| 37 | # endif /* !defined(WIN32_LEAN_AND_MEAN) */ |
| 38 | # include <ws2tcpip.h> |
| 39 | #endif /* defined(WIN32) */ |
| 40 | |
| 41 | /** |
| 42 | * @macrosection |
| 43 | * |
| 44 | * ngtcp2 crypto library error codes |
| 45 | */ |
| 46 | |
| 47 | /** |
| 48 | * @macro |
| 49 | * |
| 50 | * :macro:`NGTCP2_CRYPTO_ERR_INTERNAL` indicates an internal error. |
| 51 | */ |
| 52 | #define NGTCP2_CRYPTO_ERR_INTERNAL -201 |
| 53 | |
| 54 | /** |
| 55 | * @macro |
| 56 | * |
| 57 | * :macro:`NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN` indicates that a token |
| 58 | * is unreadable because it is not correctly formatted; or verifying |
| 59 | * the integrity protection failed. |
| 60 | */ |
| 61 | #define NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN -202 |
| 62 | |
| 63 | /** |
| 64 | * @macro |
| 65 | * |
| 66 | * :macro:`NGTCP2_CRYPTO_ERR_VERIFY_TOKEN` indicates that a token does |
| 67 | * not probe the client address; or the token validity has expired; or |
| 68 | * it contains invalid Connection ID. |
| 69 | */ |
| 70 | #define NGTCP2_CRYPTO_ERR_VERIFY_TOKEN -203 |
| 71 | |
| 72 | /** |
| 73 | * @macro |
| 74 | * |
| 75 | * :macro:`NGTCP2_CRYPTO_ERR_NOMEM` indicates out of memory. |
| 76 | */ |
| 77 | #define NGTCP2_CRYPTO_ERR_NOMEM -501 |
| 78 | |
| 79 | /** |
| 80 | * @function |
| 81 | * |
| 82 | * `ngtcp2_crypto_ctx_tls` initializes |ctx| by extracting negotiated |
| 83 | * ciphers and message digests from native TLS session |
| 84 | * |tls_native_handle|. This is used for encrypting/decrypting |
| 85 | * Handshake and 1-RTT packets. If it is unable to obtain necessary |
| 86 | * data from |tls_native_handle|, this function returns NULL. |
| 87 | * |
| 88 | * If libngtcp2_crypto_quictls is linked, |tls_native_handle| must be |
| 89 | * a pointer to SSL object. |
| 90 | */ |
| 91 | NGTCP2_EXTERN ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_tls(ngtcp2_crypto_ctx *ctx, |
| 92 | void *tls_native_handle); |
| 93 | |
| 94 | /** |
| 95 | * @function |
| 96 | * |
| 97 | * `ngtcp2_crypto_ctx_tls_early` initializes |ctx| by extracting early |
| 98 | * ciphers and message digests from native TLS session |
| 99 | * |tls_native_handle|. This is used for encrypting/decrypting 0-RTT |
| 100 | * packets. If it is unable to obtain necessary data from |
| 101 | * |tls_native_handle|, this function returns NULL. |
| 102 | * |
| 103 | * If libngtcp2_crypto_quictls is linked, |tls_native_handle| must be |
| 104 | * a pointer to SSL object. |
| 105 | */ |
| 106 | NGTCP2_EXTERN ngtcp2_crypto_ctx * |
| 107 | ngtcp2_crypto_ctx_tls_early(ngtcp2_crypto_ctx *ctx, void *tls_native_handle); |
| 108 | |
| 109 | /** |
| 110 | * @function |
| 111 | * |
| 112 | * `ngtcp2_crypto_md_init` initializes |md| with the provided |
| 113 | * |md_native_handle| which is an underlying message digest object. |
| 114 | * |
| 115 | * If libngtcp2_crypto_quictls is linked, |md_native_handle| must be a |
| 116 | * pointer to EVP_MD. |
| 117 | * |
| 118 | * If libngtcp2_crypto_gnutls is linked, |md_native_handle| must be |
| 119 | * gnutls_mac_algorithm_t casted to ``void *``. |
| 120 | * |
| 121 | * If libngtcp2_crypto_boringssl is linked, |md_native_handle| must be |
| 122 | * a pointer to EVP_MD. |
| 123 | */ |
| 124 | NGTCP2_EXTERN ngtcp2_crypto_md *ngtcp2_crypto_md_init(ngtcp2_crypto_md *md, |
| 125 | void *md_native_handle); |
| 126 | |
| 127 | /** |
| 128 | * @function |
| 129 | * |
| 130 | * `ngtcp2_crypto_md_hashlen` returns the length of |md| output. |
| 131 | */ |
| 132 | NGTCP2_EXTERN size_t ngtcp2_crypto_md_hashlen(const ngtcp2_crypto_md *md); |
| 133 | |
| 134 | /** |
| 135 | * @function |
| 136 | * |
| 137 | * `ngtcp2_crypto_aead_keylen` returns the length of key for |aead|. |
| 138 | */ |
| 139 | NGTCP2_EXTERN size_t ngtcp2_crypto_aead_keylen(const ngtcp2_crypto_aead *aead); |
| 140 | |
| 141 | /** |
| 142 | * @function |
| 143 | * |
| 144 | * `ngtcp2_crypto_aead_noncelen` returns the length of nonce for |
| 145 | * |aead|. |
| 146 | */ |
| 147 | NGTCP2_EXTERN size_t |
| 148 | ngtcp2_crypto_aead_noncelen(const ngtcp2_crypto_aead *aead); |
| 149 | |
| 150 | /** |
| 151 | * @function |
| 152 | * |
| 153 | * `ngtcp2_crypto_hkdf_extract` performs HKDF extract operation. |
| 154 | * |
| 155 | * The length of output is `ngtcp2_crypto_md_hashlen(md) |
| 156 | * <ngtcp2_crypto_md_hashlen>`. The output is stored in the buffer |
| 157 | * pointed by |dest|. The caller is responsible to specify the buffer |
| 158 | * that has enough capacity to store the output. |
| 159 | * |
| 160 | * This function returns 0 if it succeeds, or -1. |
| 161 | */ |
| 162 | NGTCP2_EXTERN int |
| 163 | ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md, |
| 164 | const uint8_t *secret, size_t secretlen, |
| 165 | const uint8_t *salt, size_t saltlen); |
| 166 | |
| 167 | /** |
| 168 | * @function |
| 169 | * |
| 170 | * `ngtcp2_crypto_hkdf_expand` performs HKDF expand operation. The |
| 171 | * result is |destlen| bytes long, and is stored in the buffer pointed |
| 172 | * by |dest|. |
| 173 | * |
| 174 | * This function returns 0 if it succeeds, or -1. |
| 175 | */ |
| 176 | NGTCP2_EXTERN int ngtcp2_crypto_hkdf_expand( |
| 177 | uint8_t *dest, size_t destlen, const ngtcp2_crypto_md *md, |
| 178 | const uint8_t *secret, size_t secretlen, const uint8_t *info, size_t infolen); |
| 179 | |
| 180 | /** |
| 181 | * @function |
| 182 | * |
| 183 | * `ngtcp2_crypto_hkdf` performs HKDF operation. The result is |
| 184 | * |destlen| bytes long, and is stored in the buffer pointed by |
| 185 | * |dest|. |
| 186 | * |
| 187 | * This function returns 0 if it succeeds, or -1. |
| 188 | */ |
| 189 | NGTCP2_EXTERN int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen, |
| 190 | const ngtcp2_crypto_md *md, |
| 191 | const uint8_t *secret, size_t secretlen, |
| 192 | const uint8_t *salt, size_t saltlen, |
| 193 | const uint8_t *info, size_t infolen); |
| 194 | |
| 195 | /** |
| 196 | * @function |
| 197 | * |
| 198 | * `ngtcp2_crypto_packet_protection_ivlen` returns the length of IV |
| 199 | * used to encrypt QUIC packet. |
| 200 | */ |
| 201 | NGTCP2_EXTERN size_t |
| 202 | ngtcp2_crypto_packet_protection_ivlen(const ngtcp2_crypto_aead *aead); |
| 203 | |
| 204 | /** |
| 205 | * @function |
| 206 | * |
| 207 | * `ngtcp2_crypto_encrypt` encrypts |plaintext| of length |
| 208 | * |plaintextlen| and writes the ciphertext into the buffer pointed by |
| 209 | * |dest|. The length of ciphertext is |plaintextlen| + |
| 210 | * :member:`aead->max_overhead <ngtcp2_crypto_aead.max_overhead>` |
| 211 | * bytes long. |dest| must have enough capacity to store the |
| 212 | * ciphertext. |dest| and |plaintext| may point to the same buffer. |
| 213 | * |
| 214 | * This function returns 0 if it succeeds, or -1. |
| 215 | */ |
| 216 | NGTCP2_EXTERN int ngtcp2_crypto_encrypt(uint8_t *dest, |
| 217 | const ngtcp2_crypto_aead *aead, |
| 218 | const ngtcp2_crypto_aead_ctx *aead_ctx, |
| 219 | const uint8_t *plaintext, |
| 220 | size_t plaintextlen, |
| 221 | const uint8_t *nonce, size_t noncelen, |
| 222 | const uint8_t *aad, size_t aadlen); |
| 223 | |
| 224 | /** |
| 225 | * @function |
| 226 | * |
| 227 | * `ngtcp2_crypto_encrypt_cb` is a wrapper function around |
| 228 | * `ngtcp2_crypto_encrypt`. It can be directly passed to |
| 229 | * :member:`ngtcp2_callbacks.encrypt` field. |
| 230 | * |
| 231 | * This function returns 0 if it succeeds, or |
| 232 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`. |
| 233 | */ |
| 234 | NGTCP2_EXTERN int |
| 235 | ngtcp2_crypto_encrypt_cb(uint8_t *dest, const ngtcp2_crypto_aead *aead, |
| 236 | const ngtcp2_crypto_aead_ctx *aead_ctx, |
| 237 | const uint8_t *plaintext, size_t plaintextlen, |
| 238 | const uint8_t *nonce, size_t noncelen, |
| 239 | const uint8_t *aad, size_t aadlen); |
| 240 | |
| 241 | /** |
| 242 | * @function |
| 243 | * |
| 244 | * `ngtcp2_crypto_decrypt` decrypts |ciphertext| of length |
| 245 | * |ciphertextlen| and writes the plaintext into the buffer pointed by |
| 246 | * |dest|. The length of plaintext is |ciphertextlen| - |
| 247 | * :member:`aead->max_overhead <ngtcp2_crypto_aead.max_overhead>` |
| 248 | * bytes long. |dest| must have enough capacity to store the |
| 249 | * plaintext. |dest| and |ciphertext| may point to the same buffer. |
| 250 | * |
| 251 | * This function returns 0 if it succeeds, or -1. |
| 252 | */ |
| 253 | NGTCP2_EXTERN int ngtcp2_crypto_decrypt(uint8_t *dest, |
| 254 | const ngtcp2_crypto_aead *aead, |
| 255 | const ngtcp2_crypto_aead_ctx *aead_ctx, |
| 256 | const uint8_t *ciphertext, |
| 257 | size_t ciphertextlen, |
| 258 | const uint8_t *nonce, size_t noncelen, |
| 259 | const uint8_t *aad, size_t aadlen); |
| 260 | |
| 261 | /** |
| 262 | * @function |
| 263 | * |
| 264 | * `ngtcp2_crypto_decrypt_cb` is a wrapper function around |
| 265 | * `ngtcp2_crypto_decrypt`. It can be directly passed to |
| 266 | * :member:`ngtcp2_callbacks.decrypt` field. |
| 267 | * |
| 268 | * This function returns 0 if it succeeds, or |
| 269 | * :macro:`NGTCP2_ERR_TLS_DECRYPT`. |
| 270 | */ |
| 271 | NGTCP2_EXTERN int |
| 272 | ngtcp2_crypto_decrypt_cb(uint8_t *dest, const ngtcp2_crypto_aead *aead, |
| 273 | const ngtcp2_crypto_aead_ctx *aead_ctx, |
| 274 | const uint8_t *ciphertext, size_t ciphertextlen, |
| 275 | const uint8_t *nonce, size_t noncelen, |
| 276 | const uint8_t *aad, size_t aadlen); |
| 277 | |
| 278 | /** |
| 279 | * @function |
| 280 | * |
| 281 | * `ngtcp2_crypto_hp_mask` generates a mask which is used in packet |
| 282 | * header encryption. The mask is written to the buffer pointed by |
| 283 | * |dest|. The sample is passed as |sample| which is |
| 284 | * :macro:`NGTCP2_HP_SAMPLELEN` bytes long. The length of mask must |
| 285 | * be at least :macro:`NGTCP2_HP_MASKLEN`. The library only uses the |
| 286 | * first :macro:`NGTCP2_HP_MASKLEN` bytes of the produced mask. The |
| 287 | * buffer pointed by |dest| must have at least |
| 288 | * :macro:`NGTCP2_HP_SAMPLELEN` bytes available. |
| 289 | * |
| 290 | * This function returns 0 if it succeeds, or -1. |
| 291 | */ |
| 292 | NGTCP2_EXTERN int ngtcp2_crypto_hp_mask(uint8_t *dest, |
| 293 | const ngtcp2_crypto_cipher *hp, |
| 294 | const ngtcp2_crypto_cipher_ctx *hp_ctx, |
| 295 | const uint8_t *sample); |
| 296 | |
| 297 | /** |
| 298 | * @function |
| 299 | * |
| 300 | * `ngtcp2_crypto_hp_mask_cb` is a wrapper function around |
| 301 | * `ngtcp2_crypto_hp_mask`. It can be directly passed to |
| 302 | * :member:`ngtcp2_callbacks.hp_mask` field. |
| 303 | * |
| 304 | * This function returns 0 if it succeeds, or |
| 305 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`. |
| 306 | */ |
| 307 | NGTCP2_EXTERN int |
| 308 | ngtcp2_crypto_hp_mask_cb(uint8_t *dest, const ngtcp2_crypto_cipher *hp, |
| 309 | const ngtcp2_crypto_cipher_ctx *hp_ctx, |
| 310 | const uint8_t *sample); |
| 311 | |
| 312 | /** |
| 313 | * @function |
| 314 | * |
| 315 | * `ngtcp2_crypto_derive_and_install_rx_key` derives the decryption |
| 316 | * keying materials from |secret|, and installs them to |conn|. |
| 317 | * |
| 318 | * If |key| is not NULL, the derived packet protection key is written |
| 319 | * to the buffer pointed by |key|. If |iv| is not NULL, the derived |
| 320 | * packet protection IV is written to the buffer pointed by |iv|. If |
| 321 | * |hp| is not NULL, the derived header protection key is written to |
| 322 | * the buffer pointed by |hp|. |
| 323 | * |
| 324 | * |secretlen| specifies the length of |secret|. |
| 325 | * |
| 326 | * The length of packet protection key and header protection key is |
| 327 | * `ngtcp2_crypto_aead_keylen(ctx->aead) <ngtcp2_crypto_aead_keylen>`, |
| 328 | * and the length of packet protection IV is |
| 329 | * `ngtcp2_crypto_packet_protection_ivlen(ctx->aead) |
| 330 | * <ngtcp2_crypto_packet_protection_ivlen>` where ctx is obtained by |
| 331 | * `ngtcp2_crypto_ctx_tls` (or `ngtcp2_crypto_ctx_tls_early` if |
| 332 | * |level| == |
| 333 | * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_0RTT`). |
| 334 | * |
| 335 | * In the first call of this function, it calls |
| 336 | * `ngtcp2_conn_set_crypto_ctx` (or `ngtcp2_conn_set_early_crypto_ctx` |
| 337 | * if |level| == |
| 338 | * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_0RTT`) to |
| 339 | * set negotiated AEAD and message digest algorithm. After the |
| 340 | * successful call of this function, application can use |
| 341 | * `ngtcp2_conn_get_crypto_ctx` (or `ngtcp2_conn_get_0rtt_crypto_ctx` |
| 342 | * if |level| == |
| 343 | * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_0RTT`) to |
| 344 | * get :type:`ngtcp2_crypto_ctx`. |
| 345 | * |
| 346 | * If |conn| is initialized as client, and |level| is |
| 347 | * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_1RTT`, this |
| 348 | * function retrieves a remote QUIC transport parameters extension |
| 349 | * from an object obtained by `ngtcp2_conn_get_tls_native_handle`, and |
| 350 | * sets it to |conn| by calling |
| 351 | * `ngtcp2_conn_decode_and_set_remote_transport_params`. |
| 352 | * |
| 353 | * This function returns 0 if it succeeds, or -1. |
| 354 | */ |
| 355 | NGTCP2_EXTERN int ngtcp2_crypto_derive_and_install_rx_key( |
| 356 | ngtcp2_conn *conn, uint8_t *key, uint8_t *iv, uint8_t *hp, |
| 357 | ngtcp2_encryption_level level, const uint8_t *secret, size_t secretlen); |
| 358 | |
| 359 | /** |
| 360 | * @function |
| 361 | * |
| 362 | * `ngtcp2_crypto_derive_and_install_tx_key` derives the encryption |
| 363 | * keying materials from |secret|, and installs new keys to |conn|. |
| 364 | * |
| 365 | * If |key| is not NULL, the derived packet protection key is written |
| 366 | * to the buffer pointed by |key|. If |iv| is not NULL, the derived |
| 367 | * packet protection IV is written to the buffer pointed by |iv|. If |
| 368 | * |hp| is not NULL, the derived header protection key is written to |
| 369 | * the buffer pointed by |hp|. |
| 370 | * |
| 371 | * |secretlen| specifies the length of |secret|. |
| 372 | * |
| 373 | * The length of packet protection key and header protection key is |
| 374 | * `ngtcp2_crypto_aead_keylen(ctx->aead) <ngtcp2_crypto_aead_keylen>`, |
| 375 | * and the length of packet protection IV is |
| 376 | * `ngtcp2_crypto_packet_protection_ivlen(ctx->aead) |
| 377 | * <ngtcp2_crypto_packet_protection_ivlen>` where ctx is obtained by |
| 378 | * `ngtcp2_crypto_ctx_tls` (or `ngtcp2_crypto_ctx_tls_early` if |
| 379 | * |level| == |
| 380 | * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_0RTT`). |
| 381 | * |
| 382 | * In the first call of this function, it calls |
| 383 | * `ngtcp2_conn_set_crypto_ctx` (or `ngtcp2_conn_set_early_crypto_ctx` |
| 384 | * if |level| == |
| 385 | * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_0RTT`) to |
| 386 | * set negotiated AEAD and message digest algorithm. After the |
| 387 | * successful call of this function, application can use |
| 388 | * `ngtcp2_conn_get_crypto_ctx` (or `ngtcp2_conn_get_0rtt_crypto_ctx` |
| 389 | * if |level| == |
| 390 | * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_0RTT`) to |
| 391 | * get :type:`ngtcp2_crypto_ctx`. |
| 392 | * |
| 393 | * If |conn| is initialized as server, and |level| is |
| 394 | * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_1RTT`, this |
| 395 | * function retrieves a remote QUIC transport parameters extension |
| 396 | * from an object obtained by `ngtcp2_conn_get_tls_native_handle`, and |
| 397 | * sets it to |conn| by calling |
| 398 | * `ngtcp2_conn_decode_and_set_remote_transport_params`. |
| 399 | * |
| 400 | * This function returns 0 if it succeeds, or -1. |
| 401 | */ |
| 402 | NGTCP2_EXTERN int ngtcp2_crypto_derive_and_install_tx_key( |
| 403 | ngtcp2_conn *conn, uint8_t *key, uint8_t *iv, uint8_t *hp, |
| 404 | ngtcp2_encryption_level level, const uint8_t *secret, size_t secretlen); |
| 405 | |
| 406 | /** |
| 407 | * @function |
| 408 | * |
| 409 | * `ngtcp2_crypto_update_key` updates traffic keying materials. |
| 410 | * |
| 411 | * The new decryption traffic secret is written to the buffer pointed |
| 412 | * by |rx_secret|. The length of secret is |secretlen| bytes, and |
| 413 | * |rx_secret| must point to the buffer which has enough capacity. |
| 414 | * |
| 415 | * The new encryption traffic secret is written to the buffer pointed |
| 416 | * by |tx_secret|. The length of secret is |secretlen| bytes, and |
| 417 | * |tx_secret| must point to the buffer which has enough capacity. |
| 418 | * |
| 419 | * The derived decryption packet protection key is written to the |
| 420 | * buffer pointed by |rx_key|. The derived decryption packet |
| 421 | * protection IV is written to the buffer pointed by |rx_iv|. |
| 422 | * |rx_aead_ctx| is initialized with the derived key and IV. |
| 423 | * |
| 424 | * The derived encryption packet protection key is written to the |
| 425 | * buffer pointed by |tx_key|. The derived encryption packet |
| 426 | * protection IV is written to the buffer pointed by |tx_iv|. |
| 427 | * |tx_aead_ctx| is initialized with the derived key and IV. |
| 428 | * |
| 429 | * |current_rx_secret| and |current_tx_secret| are the current |
| 430 | * decryption and encryption traffic secrets respectively. They share |
| 431 | * the same length with |rx_secret| and |tx_secret|. |
| 432 | * |
| 433 | * The length of packet protection key and header protection key is |
| 434 | * `ngtcp2_crypto_aead_keylen(ctx->aead) <ngtcp2_crypto_aead_keylen>`, |
| 435 | * and the length of packet protection IV is |
| 436 | * `ngtcp2_crypto_packet_protection_ivlen(ctx->aead) |
| 437 | * <ngtcp2_crypto_packet_protection_ivlen>` where ctx is obtained by |
| 438 | * `ngtcp2_crypto_ctx_tls`. |
| 439 | * |
| 440 | * This function returns 0 if it succeeds, or -1. |
| 441 | */ |
| 442 | NGTCP2_EXTERN int ngtcp2_crypto_update_key( |
| 443 | ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret, |
| 444 | ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_key, uint8_t *rx_iv, |
| 445 | ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_key, uint8_t *tx_iv, |
| 446 | const uint8_t *current_rx_secret, const uint8_t *current_tx_secret, |
| 447 | size_t secretlen); |
| 448 | |
| 449 | /** |
| 450 | * @function |
| 451 | * |
| 452 | * `ngtcp2_crypto_update_key_cb` is a wrapper function around |
| 453 | * `ngtcp2_crypto_update_key`. It can be directly passed to |
| 454 | * :member:`ngtcp2_callbacks.update_key` field. |
| 455 | * |
| 456 | * This function returns 0 if it succeeds, or |
| 457 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`. |
| 458 | */ |
| 459 | NGTCP2_EXTERN int ngtcp2_crypto_update_key_cb( |
| 460 | ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret, |
| 461 | ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv, |
| 462 | ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv, |
| 463 | const uint8_t *current_rx_secret, const uint8_t *current_tx_secret, |
| 464 | size_t secretlen, void *user_data); |
| 465 | |
| 466 | /** |
| 467 | * @function |
| 468 | * |
| 469 | * `ngtcp2_crypto_client_initial_cb` installs initial secrets and |
| 470 | * encryption keys, and sets QUIC transport parameters. |
| 471 | * |
| 472 | * This function can be directly passed to |
| 473 | * :member:`ngtcp2_callbacks.client_initial` field. It is only used |
| 474 | * by client. |
| 475 | * |
| 476 | * This function returns 0 if it succeeds, or |
| 477 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`. |
| 478 | */ |
| 479 | NGTCP2_EXTERN int ngtcp2_crypto_client_initial_cb(ngtcp2_conn *conn, |
| 480 | void *user_data); |
| 481 | |
| 482 | /** |
| 483 | * @function |
| 484 | * |
| 485 | * `ngtcp2_crypto_recv_retry_cb` re-installs initial secrets in |
| 486 | * response to incoming Retry packet. |
| 487 | * |
| 488 | * This function can be directly passed to |
| 489 | * :member:`ngtcp2_callbacks.recv_retry` field. It is only used by |
| 490 | * client. |
| 491 | * |
| 492 | * This function returns 0 if it succeeds, or |
| 493 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`. |
| 494 | */ |
| 495 | NGTCP2_EXTERN int ngtcp2_crypto_recv_retry_cb(ngtcp2_conn *conn, |
| 496 | const ngtcp2_pkt_hd *hd, |
| 497 | void *user_data); |
| 498 | |
| 499 | /** |
| 500 | * @function |
| 501 | * |
| 502 | * `ngtcp2_crypto_recv_client_initial_cb` installs initial secrets in |
| 503 | * response to an incoming Initial packet from client, and sets QUIC |
| 504 | * transport parameters. |
| 505 | * |
| 506 | * This function can be directly passed to |
| 507 | * :member:`ngtcp2_callbacks.recv_client_initial` field. It is only |
| 508 | * used by server. |
| 509 | * |
| 510 | * This function returns 0 if it succeeds, or |
| 511 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`. |
| 512 | */ |
| 513 | NGTCP2_EXTERN int ngtcp2_crypto_recv_client_initial_cb(ngtcp2_conn *conn, |
| 514 | const ngtcp2_cid *dcid, |
| 515 | void *user_data); |
| 516 | |
| 517 | /** |
| 518 | * @function |
| 519 | * |
| 520 | * `ngtcp2_crypto_read_write_crypto_data` reads CRYPTO data |data| of |
| 521 | * length |datalen| in an encryption level |encryption_level|, and may |
| 522 | * feed outgoing CRYPTO data to |conn|. This function can drive |
| 523 | * handshake. This function can be also used after handshake |
| 524 | * completes. It is allowed to call this function with |datalen| == |
| 525 | * 0. In this case, no additional read operation is done. |
| 526 | * |
| 527 | * This function returns 0 if it succeeds, or a negative error code. |
| 528 | * The generic error code is -1 if a specific error code is not |
| 529 | * suitable. The error codes less than -10000 are specific to |
| 530 | * underlying TLS implementation. For quictls, the error codes are |
| 531 | * defined in *ngtcp2_crypto_quictls.h*. |
| 532 | */ |
| 533 | NGTCP2_EXTERN int |
| 534 | ngtcp2_crypto_read_write_crypto_data(ngtcp2_conn *conn, |
| 535 | ngtcp2_encryption_level encryption_level, |
| 536 | const uint8_t *data, size_t datalen); |
| 537 | |
| 538 | /** |
| 539 | * @function |
| 540 | * |
| 541 | * `ngtcp2_crypto_recv_crypto_data_cb` is a wrapper function around |
| 542 | * `ngtcp2_crypto_read_write_crypto_data`. It can be directly passed |
| 543 | * to :member:`ngtcp2_callbacks.recv_crypto_data` field. |
| 544 | * |
| 545 | * If this function is used, the TLS implementation specific error |
| 546 | * codes described in `ngtcp2_crypto_read_write_crypto_data` are |
| 547 | * treated as if it returns -1. Do not use this function if an |
| 548 | * application wishes to use the TLS implementation specific error |
| 549 | * codes. |
| 550 | */ |
| 551 | NGTCP2_EXTERN int ngtcp2_crypto_recv_crypto_data_cb( |
| 552 | ngtcp2_conn *conn, ngtcp2_encryption_level encryption_level, uint64_t offset, |
| 553 | const uint8_t *data, size_t datalen, void *user_data); |
| 554 | |
| 555 | /** |
| 556 | * @function |
| 557 | * |
| 558 | * `ngtcp2_crypto_generate_stateless_reset_token` generates a |
| 559 | * stateless reset token using HKDF extraction using the given |cid| |
| 560 | * and |secret| as input. The token will be written to the buffer |
| 561 | * pointed by |token|, and it must have a capacity of at least |
| 562 | * :macro:`NGTCP2_STATELESS_RESET_TOKENLEN` bytes. |
| 563 | * |
| 564 | * This function returns 0 if it succeeds, or -1. |
| 565 | */ |
| 566 | NGTCP2_EXTERN int ngtcp2_crypto_generate_stateless_reset_token( |
| 567 | uint8_t *token, const uint8_t *secret, size_t secretlen, |
| 568 | const ngtcp2_cid *cid); |
| 569 | |
| 570 | /** |
| 571 | * @macro |
| 572 | * |
| 573 | * :macro:`NGTCP2_CRYPTO_TOKEN_RAND_DATALEN` is the length of random |
| 574 | * data added to a token generated by |
| 575 | * `ngtcp2_crypto_generate_retry_token` or |
| 576 | * `ngtcp2_crypto_generate_regular_token`. |
| 577 | */ |
| 578 | #define NGTCP2_CRYPTO_TOKEN_RAND_DATALEN 16 |
| 579 | |
| 580 | /** |
| 581 | * @macro |
| 582 | * |
| 583 | * :macro:`NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY` is the magic byte for |
| 584 | * Retry token generated by `ngtcp2_crypto_generate_retry_token`. |
| 585 | */ |
| 586 | #define NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY 0xb6 |
| 587 | |
| 588 | /** |
| 589 | * @macro |
| 590 | * |
| 591 | * :macro:`NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY2` is the magic byte for |
| 592 | * Retry token generated by `ngtcp2_crypto_generate_retry_token2`. |
| 593 | */ |
| 594 | #define NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY2 0xb7 |
| 595 | |
| 596 | /** |
| 597 | * @macro |
| 598 | * |
| 599 | * :macro:`NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR` is the magic byte for a |
| 600 | * token generated by `ngtcp2_crypto_generate_regular_token`. |
| 601 | */ |
| 602 | #define NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR 0x36 |
| 603 | |
| 604 | /** |
| 605 | * @macro |
| 606 | * |
| 607 | * :macro:`NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN` is the maximum length of |
| 608 | * a token generated by `ngtcp2_crypto_generate_retry_token`. |
| 609 | */ |
| 610 | #define NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN \ |
| 611 | (/* magic = */ 1 + /* cid len = */ 1 + NGTCP2_MAX_CIDLEN + \ |
| 612 | sizeof(ngtcp2_tstamp) + /* aead tag = */ 16 + \ |
| 613 | NGTCP2_CRYPTO_TOKEN_RAND_DATALEN) |
| 614 | |
| 615 | /** |
| 616 | * @macro |
| 617 | * |
| 618 | * :macro:`NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN2` is the maximum length of |
| 619 | * a token generated by `ngtcp2_crypto_generate_retry_token2`. |
| 620 | */ |
| 621 | #define NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN2 \ |
| 622 | (/* magic = */ 1 + sizeof(ngtcp2_sockaddr_union) + /* cid len = */ 1 + \ |
| 623 | NGTCP2_MAX_CIDLEN + sizeof(ngtcp2_tstamp) + /* aead tag = */ 16 + \ |
| 624 | NGTCP2_CRYPTO_TOKEN_RAND_DATALEN) |
| 625 | |
| 626 | /** |
| 627 | * @macro |
| 628 | * |
| 629 | * :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN` is the maximum length |
| 630 | * of a token generated by `ngtcp2_crypto_generate_regular_token`. |
| 631 | * `ngtcp2_crypto_generate_regular_token2` generates a token of length |
| 632 | * at most :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN` bytes + the |
| 633 | * length of the provided opaque data. |
| 634 | */ |
| 635 | #define NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN \ |
| 636 | (/* magic = */ 1 + sizeof(ngtcp2_tstamp) + /* aead tag = */ 16 + \ |
| 637 | NGTCP2_CRYPTO_TOKEN_RAND_DATALEN) |
| 638 | |
| 639 | /** |
| 640 | * @function |
| 641 | * |
| 642 | * `ngtcp2_crypto_generate_retry_token` generates a token in the |
| 643 | * buffer pointed by |token| that is sent with Retry packet. The |
| 644 | * buffer pointed by |token| must have at least |
| 645 | * :macro:`NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN` bytes long. The |
| 646 | * successfully generated token starts with |
| 647 | * :macro:`NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY`. |secret| of length |
| 648 | * |secretlen| is a keying material to generate keys to encrypt the |
| 649 | * token. |version| is QUIC version. |remote_addr| of length |
| 650 | * |remote_addrlen| is an address of client. |retry_scid| is a Source |
| 651 | * Connection ID chosen by server, and set in Retry packet. |odcid| |
| 652 | * is a Destination Connection ID in Initial packet sent by client. |
| 653 | * |ts| is the timestamp when the token is generated. |
| 654 | * |
| 655 | * See also `ngtcp2_crypto_generate_retry_token2`. |
| 656 | * |
| 657 | * This function returns the length of generated token if it succeeds, |
| 658 | * or -1. |
| 659 | */ |
| 660 | NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_generate_retry_token( |
| 661 | uint8_t *token, const uint8_t *secret, size_t secretlen, uint32_t version, |
| 662 | const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen, |
| 663 | const ngtcp2_cid *retry_scid, const ngtcp2_cid *odcid, ngtcp2_tstamp ts); |
| 664 | |
| 665 | /** |
| 666 | * @function |
| 667 | * |
| 668 | * `ngtcp2_crypto_verify_retry_token` verifies Retry token stored in |
| 669 | * the buffer pointed by |token| of length |tokenlen|. |secret| of |
| 670 | * length |secretlen| is a keying material to generate keys to decrypt |
| 671 | * the token. |version| is QUIC version of the Initial packet that |
| 672 | * contains this token. |remote_addr| of length |remote_addrlen| is |
| 673 | * an address of client. |dcid| is a Destination Connection ID in |
| 674 | * Initial packet sent by client. |timeout| is the period during |
| 675 | * which the token is valid. |ts| is the current timestamp. When |
| 676 | * validation succeeds, the extracted Destination Connection ID (which |
| 677 | * is the Destination Connection ID in Initial packet sent by client |
| 678 | * that triggered Retry packet) is stored in the buffer pointed by |
| 679 | * |odcid|. |
| 680 | * |
| 681 | * This function returns 0 if it succeeds, or -1. |
| 682 | */ |
| 683 | NGTCP2_EXTERN int ngtcp2_crypto_verify_retry_token( |
| 684 | ngtcp2_cid *odcid, const uint8_t *token, size_t tokenlen, |
| 685 | const uint8_t *secret, size_t secretlen, uint32_t version, |
| 686 | const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen, |
| 687 | const ngtcp2_cid *dcid, ngtcp2_duration timeout, ngtcp2_tstamp ts); |
| 688 | |
| 689 | /** |
| 690 | * @function |
| 691 | * |
| 692 | * `ngtcp2_crypto_generate_retry_token2` generates a token in the |
| 693 | * buffer pointed by |token| that is sent with Retry packet. The |
| 694 | * buffer pointed by |token| must have at least |
| 695 | * :macro:`NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN2` bytes long. The |
| 696 | * successfully generated token starts with |
| 697 | * :macro:`NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY2`. |secret| of length |
| 698 | * |secretlen| is a keying material to generate keys to encrypt the |
| 699 | * token. |version| is QUIC version. |remote_addr| of length |
| 700 | * |remote_addrlen| is an address of client. |retry_scid| is a Source |
| 701 | * Connection ID chosen by server, and set in Retry packet. |odcid| |
| 702 | * is a Destination Connection ID in Initial packet sent by client. |
| 703 | * |ts| is the timestamp when the token is generated. |
| 704 | * |
| 705 | * Use this function instead of `ngtcp2_crypto_generate_retry_token` |
| 706 | * if more detailed error handling is required when verifying the |
| 707 | * token. `ngtcp2_crypto_verify_retry_token2` must be used to verify |
| 708 | * the token. |
| 709 | * |
| 710 | * This function returns the length of generated token if it succeeds, |
| 711 | * or -1. |
| 712 | */ |
| 713 | NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_generate_retry_token2( |
| 714 | uint8_t *token, const uint8_t *secret, size_t secretlen, uint32_t version, |
| 715 | const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen, |
| 716 | const ngtcp2_cid *retry_scid, const ngtcp2_cid *odcid, ngtcp2_tstamp ts); |
| 717 | |
| 718 | /** |
| 719 | * @function |
| 720 | * |
| 721 | * `ngtcp2_crypto_verify_retry_token2` verifies Retry token stored in |
| 722 | * the buffer pointed by |token| of length |tokenlen|. |secret| of |
| 723 | * length |secretlen| is a keying material to generate keys to decrypt |
| 724 | * the token. |version| is QUIC version of the Initial packet that |
| 725 | * contains this token. |remote_addr| of length |remote_addrlen| is |
| 726 | * an address of client. |dcid| is a Destination Connection ID in |
| 727 | * Initial packet sent by client. |timeout| is the period during |
| 728 | * which the token is valid. |ts| is the current timestamp. When |
| 729 | * validation succeeds, the extracted Destination Connection ID (which |
| 730 | * is the Destination Connection ID in Initial packet sent by client |
| 731 | * that triggered Retry packet) is stored in the buffer pointed by |
| 732 | * |odcid|. |
| 733 | * |
| 734 | * The token must be generated by |
| 735 | * `ngtcp2_crypto_generate_retry_token2`. |
| 736 | * |
| 737 | * This function returns 0 if it succeeds, or one of the following |
| 738 | * negative error codes: |
| 739 | * |
| 740 | * :macro:`NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN` |
| 741 | * A token is badly formatted; or verifying the integrity |
| 742 | * protection failed. |
| 743 | * :macro:`NGTCP2_CRYPTO_ERR_VERIFY_TOKEN` |
| 744 | * A token does not probe the client address; or the token |
| 745 | * validity has expired; or it contains invalid Connection ID. |
| 746 | * :macro:`NGTCP2_CRYPTO_ERR_INTERNAL` |
| 747 | * Internal error occurred. |
| 748 | */ |
| 749 | NGTCP2_EXTERN int ngtcp2_crypto_verify_retry_token2( |
| 750 | ngtcp2_cid *odcid, const uint8_t *token, size_t tokenlen, |
| 751 | const uint8_t *secret, size_t secretlen, uint32_t version, |
| 752 | const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen, |
| 753 | const ngtcp2_cid *dcid, ngtcp2_duration timeout, ngtcp2_tstamp ts); |
| 754 | |
| 755 | /** |
| 756 | * @function |
| 757 | * |
| 758 | * `ngtcp2_crypto_generate_regular_token` generates a token in the |
| 759 | * buffer pointed by |token| that is sent with NEW_TOKEN frame. The |
| 760 | * buffer pointed by |token| must have at least |
| 761 | * :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN` bytes long. The |
| 762 | * successfully generated token starts with |
| 763 | * :macro:`NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR`. |secret| of length |
| 764 | * |secretlen| is a keying material to generate keys to encrypt the |
| 765 | * token. |remote_addr| of length |remote_addrlen| is an address of |
| 766 | * client. |ts| is the timestamp when the token is generated. |
| 767 | * |
| 768 | * This function returns the length of generated token if it succeeds, |
| 769 | * or -1. |
| 770 | */ |
| 771 | NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_generate_regular_token( |
| 772 | uint8_t *token, const uint8_t *secret, size_t secretlen, |
| 773 | const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen, |
| 774 | ngtcp2_tstamp ts); |
| 775 | |
| 776 | /** |
| 777 | * @function |
| 778 | * |
| 779 | * `ngtcp2_crypto_verify_regular_token` verifies a regular token |
| 780 | * stored in the buffer pointed by |token| of length |tokenlen|. |
| 781 | * |secret| of length |secretlen| is a keying material to generate |
| 782 | * keys to decrypt the token. |remote_addr| of length |
| 783 | * |remote_addrlen| is an address of client. |timeout| is the period |
| 784 | * during which the token is valid. |ts| is the current timestamp. |
| 785 | * |
| 786 | * This function returns 0 if it succeeds, or -1. |
| 787 | */ |
| 788 | NGTCP2_EXTERN int ngtcp2_crypto_verify_regular_token( |
| 789 | const uint8_t *token, size_t tokenlen, const uint8_t *secret, |
| 790 | size_t secretlen, const ngtcp2_sockaddr *remote_addr, |
| 791 | ngtcp2_socklen remote_addrlen, ngtcp2_duration timeout, ngtcp2_tstamp ts); |
| 792 | |
| 793 | /** |
| 794 | * @function |
| 795 | * |
| 796 | * `ngtcp2_crypto_generate_regular_token2` generates a token in the |
| 797 | * buffer pointed by |token| that is sent with NEW_TOKEN frame. The |
| 798 | * buffer pointed by |token| must have at least |
| 799 | * :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN` + |datalen| bytes long. |
| 800 | * The successfully generated token starts with |
| 801 | * :macro:`NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR`. |secret| of length |
| 802 | * |secretlen| is a keying material to generate keys to encrypt the |
| 803 | * token. |remote_addr| of length |remote_addrlen| is an address of |
| 804 | * client. |ts| is the timestamp when the token is generated. |data| |
| 805 | * of length |datalen| is an opaque data embedded in the token. |
| 806 | * |datalen| must be less than or equal to 256. |
| 807 | * |
| 808 | * Calling this function with |datalen| = 0 is equivalent to calling |
| 809 | * `ngtcp2_crypto_generate_regular_token`. |
| 810 | * |
| 811 | * To get the opaque data after successful verification, use |
| 812 | * `ngtcp2_crypto_verify_regular_token2`. |
| 813 | * `ngtcp2_crypto_verify_regular_token` can verify the token with |
| 814 | * |datalen| > 0, but it discards the opaque data. |
| 815 | * |
| 816 | * This function returns the length of generated token if it succeeds, |
| 817 | * or -1. |
| 818 | */ |
| 819 | NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_generate_regular_token2( |
| 820 | uint8_t *token, const uint8_t *secret, size_t secretlen, |
| 821 | const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen, |
| 822 | const void *data, size_t datalen, ngtcp2_tstamp ts); |
| 823 | |
| 824 | /** |
| 825 | * @function |
| 826 | * |
| 827 | * `ngtcp2_crypto_verify_regular_token2` verifies a regular token |
| 828 | * stored in the buffer pointed by |token| of length |tokenlen|. |
| 829 | * |secret| of length |secretlen| is a keying material to generate |
| 830 | * keys to decrypt the token. |remote_addr| of length |
| 831 | * |remote_addrlen| is an address of client. |timeout| is the period |
| 832 | * during which the token is valid. |ts| is the current timestamp. |
| 833 | * |data| is the pointer to the buffer of length at least |
| 834 | * |max_datalen| bytes. If the token is verified successfully, the |
| 835 | * opaque data embedded in the token is copied to the buffer pointed |
| 836 | * by |data|. |
| 837 | * |
| 838 | * If |tokenlen| is less than |
| 839 | * :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN`, this function returns |
| 840 | * :macro:`NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN`. |
| 841 | * |
| 842 | * If the length of opaque data is larger than |max_datalen|, the |
| 843 | * verification still succeeds, but nothing is written to the buffer |
| 844 | * pointed by |data|, and this function returns 0. In other words, |
| 845 | * the opaque data is discarded. |
| 846 | * |
| 847 | * This function returns the number of the opaque data written to the |
| 848 | * buffer pointed by |data| if it succeeds, or one of the following |
| 849 | * negative error codes: |
| 850 | * |
| 851 | * :macro:`NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN` |
| 852 | * A token is badly formatted; or verifying the integrity |
| 853 | * protection failed. |
| 854 | * :macro:`NGTCP2_CRYPTO_ERR_VERIFY_TOKEN` |
| 855 | * A token validity has expired. |
| 856 | * :macro:`NGTCP2_CRYPTO_ERR_INTERNAL` |
| 857 | * Internal error occurred. |
| 858 | */ |
| 859 | NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_verify_regular_token2( |
| 860 | void *data, size_t max_datalen, const uint8_t *token, size_t tokenlen, |
| 861 | const uint8_t *secret, size_t secretlen, const ngtcp2_sockaddr *remote_addr, |
| 862 | ngtcp2_socklen remote_addrlen, ngtcp2_duration timeout, ngtcp2_tstamp ts); |
| 863 | |
| 864 | /** |
| 865 | * @function |
| 866 | * |
| 867 | * `ngtcp2_crypto_write_connection_close` writes Initial packet |
| 868 | * containing CONNECTION_CLOSE with the given |error_code| and the |
| 869 | * optional |reason| of length |reasonlen| to the buffer pointed by |
| 870 | * |dest| of length |destlen|. This function is designed for server |
| 871 | * to close connection without committing the state when validating |
| 872 | * Retry token fails. This function must not be used by client. The |
| 873 | * |dcid| must be the Source Connection ID in Initial packet from |
| 874 | * client. The |scid| must be the Destination Connection ID in |
| 875 | * Initial packet from client. |scid| is used to derive initial |
| 876 | * keying materials. |
| 877 | * |
| 878 | * This function wraps around `ngtcp2_pkt_write_connection_close` for |
| 879 | * easier use. |
| 880 | * |
| 881 | * This function returns 0 if it succeeds, or -1. |
| 882 | */ |
| 883 | NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_write_connection_close( |
| 884 | uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid, |
| 885 | const ngtcp2_cid *scid, uint64_t error_code, const uint8_t *reason, |
| 886 | size_t reasonlen); |
| 887 | |
| 888 | /** |
| 889 | * @function |
| 890 | * |
| 891 | * `ngtcp2_crypto_write_retry` writes Retry packet to the buffer |
| 892 | * pointed by |dest| of length |destlen|. |dcid| is the Connection ID |
| 893 | * which appeared in a packet as a Source Connection ID sent by |
| 894 | * client. |scid| is a server chosen Source Connection ID. |odcid| |
| 895 | * specifies Original Destination Connection ID which appeared in a |
| 896 | * packet as a Destination Connection ID sent by client. |token| |
| 897 | * specifies Retry Token, and |tokenlen| specifies its length. |
| 898 | * |
| 899 | * This function wraps around `ngtcp2_pkt_write_retry` for easier use. |
| 900 | * |
| 901 | * This function returns 0 if it succeeds, or -1. |
| 902 | */ |
| 903 | NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_write_retry( |
| 904 | uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid, |
| 905 | const ngtcp2_cid *scid, const ngtcp2_cid *odcid, const uint8_t *token, |
| 906 | size_t tokenlen); |
| 907 | |
| 908 | /** |
| 909 | * @function |
| 910 | * |
| 911 | * `ngtcp2_crypto_aead_ctx_encrypt_init` initializes |aead_ctx| with |
| 912 | * new AEAD cipher context object for encryption which is constructed |
| 913 | * to use |key| as encryption key. |aead| specifies AEAD cipher to |
| 914 | * use. |noncelen| is the length of nonce. |
| 915 | * |
| 916 | * This function returns 0 if it succeeds, or -1. |
| 917 | */ |
| 918 | NGTCP2_EXTERN int |
| 919 | ngtcp2_crypto_aead_ctx_encrypt_init(ngtcp2_crypto_aead_ctx *aead_ctx, |
| 920 | const ngtcp2_crypto_aead *aead, |
| 921 | const uint8_t *key, size_t noncelen); |
| 922 | |
| 923 | /** |
| 924 | * @function |
| 925 | * |
| 926 | * `ngtcp2_crypto_aead_ctx_decrypt_init` initializes |aead_ctx| with |
| 927 | * new AEAD cipher context object for decryption which is constructed |
| 928 | * to use |key| as decryption key. |aead| specifies AEAD cipher to |
| 929 | * use. |noncelen| is the length of nonce. |
| 930 | * |
| 931 | * This function returns 0 if it succeeds, or -1. |
| 932 | */ |
| 933 | NGTCP2_EXTERN int |
| 934 | ngtcp2_crypto_aead_ctx_decrypt_init(ngtcp2_crypto_aead_ctx *aead_ctx, |
| 935 | const ngtcp2_crypto_aead *aead, |
| 936 | const uint8_t *key, size_t noncelen); |
| 937 | |
| 938 | /** |
| 939 | * @function |
| 940 | * |
| 941 | * `ngtcp2_crypto_aead_ctx_free` frees up resources used by |
| 942 | * |aead_ctx|. This function does not free the memory pointed by |
| 943 | * |aead_ctx| itself. |
| 944 | */ |
| 945 | NGTCP2_EXTERN void |
| 946 | ngtcp2_crypto_aead_ctx_free(ngtcp2_crypto_aead_ctx *aead_ctx); |
| 947 | |
| 948 | /** |
| 949 | * @function |
| 950 | * |
| 951 | * `ngtcp2_crypto_delete_crypto_aead_ctx_cb` deletes the given |
| 952 | * |aead_ctx|. |
| 953 | * |
| 954 | * This function can be directly passed to |
| 955 | * :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` field. |
| 956 | */ |
| 957 | NGTCP2_EXTERN void ngtcp2_crypto_delete_crypto_aead_ctx_cb( |
| 958 | ngtcp2_conn *conn, ngtcp2_crypto_aead_ctx *aead_ctx, void *user_data); |
| 959 | |
| 960 | /** |
| 961 | * @function |
| 962 | * |
| 963 | * `ngtcp2_crypto_delete_crypto_cipher_ctx_cb` deletes the given |
| 964 | * |cipher_ctx|. |
| 965 | * |
| 966 | * This function can be directly passed to |
| 967 | * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` field. |
| 968 | */ |
| 969 | NGTCP2_EXTERN void ngtcp2_crypto_delete_crypto_cipher_ctx_cb( |
| 970 | ngtcp2_conn *conn, ngtcp2_crypto_cipher_ctx *cipher_ctx, void *user_data); |
| 971 | |
| 972 | /** |
| 973 | * @function |
| 974 | * |
| 975 | * `ngtcp2_crypto_get_path_challenge_data_cb` writes unpredictable |
| 976 | * sequence of :macro:`NGTCP2_PATH_CHALLENGE_DATALEN` bytes to |data| |
| 977 | * which is sent with PATH_CHALLENGE frame. |
| 978 | * |
| 979 | * This function can be directly passed to |
| 980 | * :member:`ngtcp2_callbacks.get_path_challenge_data` field. |
| 981 | */ |
| 982 | NGTCP2_EXTERN int ngtcp2_crypto_get_path_challenge_data_cb(ngtcp2_conn *conn, |
| 983 | uint8_t *data, |
| 984 | void *user_data); |
| 985 | |
| 986 | /** |
| 987 | * @function |
| 988 | * |
| 989 | * `ngtcp2_crypto_version_negotiation_cb` installs Initial keys for |
| 990 | * |version| which is negotiated or being negotiated. |client_dcid| |
| 991 | * is the destination connection ID in first Initial packet from |
| 992 | * client. |
| 993 | * |
| 994 | * This function can be directly passed to |
| 995 | * :member:`ngtcp2_callbacks.version_negotiation` field. |
| 996 | */ |
| 997 | NGTCP2_EXTERN int |
| 998 | ngtcp2_crypto_version_negotiation_cb(ngtcp2_conn *conn, uint32_t version, |
| 999 | const ngtcp2_cid *client_dcid, |
| 1000 | void *user_data); |
| 1001 | |
| 1002 | typedef struct ngtcp2_crypto_conn_ref ngtcp2_crypto_conn_ref; |
| 1003 | |
| 1004 | /** |
| 1005 | * @functypedef |
| 1006 | * |
| 1007 | * :type:`ngtcp2_crypto_get_conn` is a callback function to get a |
| 1008 | * pointer to :type:`ngtcp2_conn` from |conn_ref|. The implementation |
| 1009 | * must return non-NULL :type:`ngtcp2_conn` object. |
| 1010 | */ |
| 1011 | typedef ngtcp2_conn *(*ngtcp2_crypto_get_conn)( |
| 1012 | ngtcp2_crypto_conn_ref *conn_ref); |
| 1013 | |
| 1014 | /** |
| 1015 | * @struct |
| 1016 | * |
| 1017 | * :type:`ngtcp2_crypto_conn_ref` is a structure to get a pointer to |
| 1018 | * :type:`ngtcp2_conn`. It is meant to be set to TLS native handle as |
| 1019 | * an application specific data (e.g. SSL_set_app_data in quictls). |
| 1020 | */ |
| 1021 | typedef struct ngtcp2_crypto_conn_ref { |
| 1022 | /** |
| 1023 | * :member:`get_conn` is a callback function to get a pointer to |
| 1024 | * :type:`ngtcp2_conn` object. |
| 1025 | */ |
| 1026 | ngtcp2_crypto_get_conn get_conn; |
| 1027 | /** |
| 1028 | * :member:`user_data` is a pointer to arbitrary user data. |
| 1029 | */ |
| 1030 | void *user_data; |
| 1031 | } ngtcp2_crypto_conn_ref; |
| 1032 | |
| 1033 | #ifdef __cplusplus |
| 1034 | } |
| 1035 | #endif /* defined(__cplusplus) */ |
| 1036 | |
| 1037 | #endif /* !defined(NGTCP2_CRYPTO_H) */ |