| 1 | /* |
| 2 | * ngtcp2 |
| 3 | * |
| 4 | * Copyright (c) 2017 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 | #ifdef HAVE_CONFIG_H |
| 29 | # include <config.h> |
| 30 | #endif /* defined(HAVE_CONFIG_H) */ |
| 31 | |
| 32 | #include <ngtcp2/ngtcp2.h> |
| 33 | |
| 34 | #include "ngtcp2_mem.h" |
| 35 | |
| 36 | /* NGTCP2_INITIAL_AEAD_OVERHEAD is an overhead of AEAD used by Initial |
| 37 | packets. Because QUIC uses AEAD_AES_128_GCM, the overhead is 16 |
| 38 | bytes. */ |
| 39 | #define NGTCP2_INITIAL_AEAD_OVERHEAD 16 |
| 40 | |
| 41 | /* NGTCP2_MAX_AEAD_OVERHEAD is the maximum AEAD overhead. */ |
| 42 | #define NGTCP2_MAX_AEAD_OVERHEAD 16 |
| 43 | |
| 44 | /* NGTCP2_CRYPTO_KM_FLAG_NONE indicates that no flag is set. */ |
| 45 | #define NGTCP2_CRYPTO_KM_FLAG_NONE 0x00u |
| 46 | /* NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE is set if key phase bit is |
| 47 | set. */ |
| 48 | #define NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE 0x01u |
| 49 | |
| 50 | typedef struct ngtcp2_crypto_km { |
| 51 | ngtcp2_vec secret; |
| 52 | ngtcp2_crypto_aead_ctx aead_ctx; |
| 53 | ngtcp2_vec iv; |
| 54 | /* pkt_num is a packet number of a packet which uses this keying |
| 55 | material. For encryption key, it is the lowest packet number of |
| 56 | a packet. For decryption key, it is the lowest packet number of |
| 57 | a packet which can be decrypted with this keying material. */ |
| 58 | int64_t pkt_num; |
| 59 | /* use_count is the number of encryption applied with this key. */ |
| 60 | uint64_t use_count; |
| 61 | /* flags is the bitwise OR of zero or more of |
| 62 | NGTCP2_CRYPTO_KM_FLAG_*. */ |
| 63 | uint8_t flags; |
| 64 | } ngtcp2_crypto_km; |
| 65 | |
| 66 | /* |
| 67 | * ngtcp2_crypto_km_new creates new ngtcp2_crypto_km object, and |
| 68 | * assigns its pointer to |*pckm|. The |secret| of length |
| 69 | * |secretlen|, |aead_ctx|, and the |iv| of length |ivlen| are copied |
| 70 | * to |*pckm|. If |secretlen| == 0, the function assumes no secret is |
| 71 | * given which is acceptable. The sole reason to store secret is |
| 72 | * update keys. Only 1RTT key can be updated. |
| 73 | */ |
| 74 | int ngtcp2_crypto_km_new(ngtcp2_crypto_km **pckm, const uint8_t *secret, |
| 75 | size_t secretlen, |
| 76 | const ngtcp2_crypto_aead_ctx *aead_ctx, |
| 77 | const uint8_t *iv, size_t ivlen, |
| 78 | const ngtcp2_mem *mem); |
| 79 | |
| 80 | /* |
| 81 | * ngtcp2_crypto_km_nocopy_new is similar to ngtcp2_crypto_km_new, but |
| 82 | * it does not copy secret, aead context, and IV. |
| 83 | */ |
| 84 | int ngtcp2_crypto_km_nocopy_new(ngtcp2_crypto_km **pckm, size_t secretlen, |
| 85 | size_t ivlen, const ngtcp2_mem *mem); |
| 86 | |
| 87 | void ngtcp2_crypto_km_del(ngtcp2_crypto_km *ckm, const ngtcp2_mem *mem); |
| 88 | |
| 89 | typedef struct ngtcp2_crypto_cc { |
| 90 | ngtcp2_crypto_aead aead; |
| 91 | ngtcp2_crypto_cipher hp; |
| 92 | ngtcp2_crypto_km *ckm; |
| 93 | ngtcp2_crypto_cipher_ctx hp_ctx; |
| 94 | ngtcp2_encrypt encrypt; |
| 95 | ngtcp2_decrypt decrypt; |
| 96 | ngtcp2_hp_mask hp_mask; |
| 97 | } ngtcp2_crypto_cc; |
| 98 | |
| 99 | void ngtcp2_crypto_create_nonce(uint8_t *dest, const uint8_t *iv, size_t ivlen, |
| 100 | int64_t pkt_num); |
| 101 | |
| 102 | #endif /* !defined(NGTCP2_CRYPTO_H) */ |