shared.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 SHARED_H
  26. #define SHARED_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2_crypto.h>
  31. /**
  32. * @macro
  33. *
  34. * :macro:`NGTCP2_INITIAL_SALT_V1` is a salt value which is used to
  35. * derive initial secret. It is used for QUIC v1.
  36. */
  37. #define NGTCP2_INITIAL_SALT_V1 \
  38. "\x38\x76\x2c\xf7\xf5\x59\x34\xb3\x4d\x17\x9a\xe6\xa4\xc8\x0c\xad\xcc\xbb" \
  39. "\x7f\x0a"
  40. /**
  41. * @macro
  42. *
  43. * :macro:`NGTCP2_INITIAL_SALT_V2` is a salt value which is used to
  44. * derive initial secret. It is used for QUIC v2.
  45. */
  46. #define NGTCP2_INITIAL_SALT_V2 \
  47. "\x0d\xed\xe3\xde\xf7\x00\xa6\xdb\x81\x93\x81\xbe\x6e\x26\x9d\xcb\xf9\xbd" \
  48. "\x2e\xd9"
  49. /* Maximum key usage (encryption) limits */
  50. #define NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_GCM (1ULL << 23)
  51. #define NGTCP2_CRYPTO_MAX_ENCRYPTION_CHACHA20_POLY1305 (1ULL << 62)
  52. #define NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_CCM (2965820ULL)
  53. /* Maximum authentication failure (decryption) limits during the
  54. lifetime of a connection. */
  55. #define NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_GCM (1ULL << 52)
  56. #define NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_CHACHA20_POLY1305 (1ULL << 36)
  57. #define NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_CCM (2965820ULL)
  58. /**
  59. * @macro
  60. *
  61. * :macro:`NGTCP2_CRYPTO_INITIAL_SECRETLEN` is the length of secret
  62. * for Initial packets.
  63. */
  64. #define NGTCP2_CRYPTO_INITIAL_SECRETLEN 32
  65. /**
  66. * @macro
  67. *
  68. * :macro:`NGTCP2_CRYPTO_INITIAL_KEYLEN` is the length of key for
  69. * Initial packets.
  70. */
  71. #define NGTCP2_CRYPTO_INITIAL_KEYLEN 16
  72. /**
  73. * @macro
  74. *
  75. * :macro:`NGTCP2_CRYPTO_INITIAL_IVLEN` is the length of IV for
  76. * Initial packets.
  77. */
  78. #define NGTCP2_CRYPTO_INITIAL_IVLEN 12
  79. /**
  80. * @function
  81. *
  82. * `ngtcp2_crypto_ctx_initial` initializes |ctx| for Initial packet
  83. * encryption and decryption.
  84. */
  85. ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_initial(ngtcp2_crypto_ctx *ctx);
  86. /**
  87. * @function
  88. *
  89. * `ngtcp2_crypto_aead_init` initializes |aead| with the provided
  90. * |aead_native_handle| which is an underlying AEAD object.
  91. *
  92. * If libngtcp2_crypto_quictls is linked, |aead_native_handle| must be
  93. * a pointer to EVP_CIPHER.
  94. *
  95. * If libngtcp2_crypto_gnutls is linked, |aead_native_handle| must be
  96. * gnutls_cipher_algorithm_t casted to ``void *``.
  97. *
  98. * If libngtcp2_crypto_boringssl is linked, |aead_native_handle| must
  99. * be a pointer to EVP_AEAD.
  100. */
  101. ngtcp2_crypto_aead *ngtcp2_crypto_aead_init(ngtcp2_crypto_aead *aead,
  102. void *aead_native_handle);
  103. /**
  104. * @function
  105. *
  106. * `ngtcp2_crypto_aead_retry` initializes |aead| with the AEAD cipher
  107. * AEAD_AES_128_GCM for Retry packet integrity protection.
  108. */
  109. ngtcp2_crypto_aead *ngtcp2_crypto_aead_retry(ngtcp2_crypto_aead *aead);
  110. /**
  111. * @enum
  112. *
  113. * :type:`ngtcp2_crypto_side` indicates which side the application
  114. * implements; client or server.
  115. */
  116. typedef enum ngtcp2_crypto_side {
  117. /**
  118. * :enum:`NGTCP2_CRYPTO_SIDE_CLIENT` indicates that the application
  119. * is client.
  120. */
  121. NGTCP2_CRYPTO_SIDE_CLIENT,
  122. /**
  123. * :enum:`NGTCP2_CRYPTO_SIDE_SERVER` indicates that the application
  124. * is server.
  125. */
  126. NGTCP2_CRYPTO_SIDE_SERVER
  127. } ngtcp2_crypto_side;
  128. /**
  129. * @function
  130. *
  131. * `ngtcp2_crypto_derive_initial_secrets` derives initial secrets.
  132. * |rx_secret| and |tx_secret| must point to the buffer of at least 32
  133. * bytes capacity. rx for read and tx for write. This function
  134. * writes rx and tx secrets into |rx_secret| and |tx_secret|
  135. * respectively. The length of secret is 32 bytes long.
  136. * |client_dcid| is the destination connection ID in first Initial
  137. * packet of client. If |initial_secret| is not NULL, the initial
  138. * secret is written to it. It must point to the buffer which has at
  139. * least 32 bytes capacity. The initial secret is 32 bytes long.
  140. * |side| specifies the side of application.
  141. *
  142. * This function returns 0 if it succeeds, or -1.
  143. */
  144. int ngtcp2_crypto_derive_initial_secrets(uint8_t *rx_secret, uint8_t *tx_secret,
  145. uint8_t *initial_secret,
  146. uint32_t version,
  147. const ngtcp2_cid *client_dcid,
  148. ngtcp2_crypto_side side);
  149. /**
  150. * @function
  151. *
  152. * `ngtcp2_crypto_derive_packet_protection_key` derives packet
  153. * protection key. This function writes packet protection key into
  154. * the buffer pointed by |key|. The length of derived key is
  155. * `ngtcp2_crypto_aead_keylen(aead) <ngtcp2_crypto_aead_keylen>`
  156. * bytes. |key| must have enough capacity to store the key. This
  157. * function writes packet protection IV into |iv|. The length of
  158. * derived IV is `ngtcp2_crypto_packet_protection_ivlen(aead)
  159. * <ngtcp2_crypto_packet_protection_ivlen>` bytes. |iv| must have
  160. * enough capacity to store the IV.
  161. *
  162. * If |hp| is not NULL, this function also derives packet header
  163. * protection key and writes the key into the buffer pointed by |hp|.
  164. * The length of derived key is `ngtcp2_crypto_aead_keylen(aead)
  165. * <ngtcp2_crypto_aead_keylen>` bytes. |hp|, if not NULL, must have
  166. * enough capacity to store the key.
  167. *
  168. * This function returns 0 if it succeeds, or -1.
  169. */
  170. int ngtcp2_crypto_derive_packet_protection_key(uint8_t *key, uint8_t *iv,
  171. uint8_t *hp, uint32_t version,
  172. const ngtcp2_crypto_aead *aead,
  173. const ngtcp2_crypto_md *md,
  174. const uint8_t *secret,
  175. size_t secretlen);
  176. /**
  177. * @function
  178. *
  179. * `ngtcp2_crypto_update_traffic_secret` derives the next generation
  180. * of the traffic secret. |secret| specifies the current secret and
  181. * its length is given in |secretlen|. The length of new key is the
  182. * same as the current key. This function writes new key into the
  183. * buffer pointed by |dest|. |dest| must have the enough capacity to
  184. * store the new key.
  185. *
  186. * This function returns 0 if it succeeds, or -1.
  187. */
  188. int ngtcp2_crypto_update_traffic_secret(uint8_t *dest, uint32_t version,
  189. const ngtcp2_crypto_md *md,
  190. const uint8_t *secret,
  191. size_t secretlen);
  192. /**
  193. * @function
  194. *
  195. * `ngtcp2_crypto_set_local_transport_params` sets QUIC transport
  196. * parameter, which is encoded in wire format and stored in the buffer
  197. * pointed by |buf| of length |len|, to the native handle |tls|.
  198. *
  199. * |tls| points to a implementation dependent TLS session object. If
  200. * libngtcp2_crypto_quictls is linked, |tls| must be a pointer to SSL
  201. * object.
  202. *
  203. * This function returns 0 if it succeeds, or -1.
  204. */
  205. int ngtcp2_crypto_set_local_transport_params(void *tls, const uint8_t *buf,
  206. size_t len);
  207. /**
  208. * @function
  209. *
  210. * `ngtcp2_crypto_set_remote_transport_params` retrieves a remote QUIC
  211. * transport parameters from |tls| and sets it to |conn| using
  212. * `ngtcp2_conn_set_remote_transport_params`.
  213. *
  214. * |tls| points to a implementation dependent TLS session object. If
  215. * libngtcp2_crypto_quictls is linked, |tls| must be a pointer to SSL
  216. * object.
  217. *
  218. * This function returns 0 if it succeeds, or -1.
  219. */
  220. int ngtcp2_crypto_set_remote_transport_params(ngtcp2_conn *conn, void *tls);
  221. /**
  222. * @function
  223. *
  224. * `ngtcp2_crypto_derive_and_install_initial_key` derives initial
  225. * keying materials and installs keys to |conn|.
  226. *
  227. * If |rx_secret| is not NULL, the secret for decryption is written to
  228. * the buffer pointed by |rx_secret|. The length of secret is 32
  229. * bytes, and |rx_secret| must point to the buffer which has enough
  230. * capacity.
  231. *
  232. * If |tx_secret| is not NULL, the secret for encryption is written to
  233. * the buffer pointed by |tx_secret|. The length of secret is 32
  234. * bytes, and |tx_secret| must point to the buffer which has enough
  235. * capacity.
  236. *
  237. * If |initial_secret| is not NULL, the initial secret is written to
  238. * the buffer pointed by |initial_secret|. The length of secret is 32
  239. * bytes, and |initial_secret| must point to the buffer which has
  240. * enough capacity.
  241. *
  242. * |client_dcid| is the destination connection ID in first Initial
  243. * packet of client.
  244. *
  245. * If |rx_key| is not NULL, the derived packet protection key for
  246. * decryption is written to the buffer pointed by |rx_key|. If
  247. * |rx_iv| is not NULL, the derived packet protection IV for
  248. * decryption is written to the buffer pointed by |rx_iv|. If |rx_hp|
  249. * is not NULL, the derived header protection key for decryption is
  250. * written to the buffer pointed by |rx_hp|.
  251. *
  252. * If |tx_key| is not NULL, the derived packet protection key for
  253. * encryption is written to the buffer pointed by |tx_key|. If
  254. * |tx_iv| is not NULL, the derived packet protection IV for
  255. * encryption is written to the buffer pointed by |tx_iv|. If |tx_hp|
  256. * is not NULL, the derived header protection key for encryption is
  257. * written to the buffer pointed by |tx_hp|.
  258. *
  259. * The length of packet protection key and header protection key is 16
  260. * bytes long. The length of packet protection IV is 12 bytes long.
  261. *
  262. * This function calls `ngtcp2_conn_set_initial_crypto_ctx` to set
  263. * initial AEAD and message digest algorithm. After the successful
  264. * call of this function, application can use
  265. * `ngtcp2_conn_get_initial_crypto_ctx` to get the object.
  266. *
  267. * This function returns 0 if it succeeds, or -1.
  268. */
  269. int ngtcp2_crypto_derive_and_install_initial_key(
  270. ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  271. uint8_t *initial_secret, uint8_t *rx_key, uint8_t *rx_iv, uint8_t *rx_hp,
  272. uint8_t *tx_key, uint8_t *tx_iv, uint8_t *tx_hp, uint32_t version,
  273. const ngtcp2_cid *client_dcid);
  274. /**
  275. * @function
  276. *
  277. * `ngtcp2_crypto_derive_and_install_vneg_initial_key` derives initial
  278. * keying materials and installs keys to |conn|. This function is
  279. * dedicated to install keys for |version| which is negotiated, or
  280. * being negotiated.
  281. *
  282. * If |rx_secret| is not NULL, the secret for decryption is written to
  283. * the buffer pointed by |rx_secret|. The length of secret is 32
  284. * bytes, and |rx_secret| must point to the buffer which has enough
  285. * capacity.
  286. *
  287. * If |tx_secret| is not NULL, the secret for encryption is written to
  288. * the buffer pointed by |tx_secret|. The length of secret is 32
  289. * bytes, and |tx_secret| must point to the buffer which has enough
  290. * capacity.
  291. *
  292. * If |initial_secret| is not NULL, the initial secret is written to
  293. * the buffer pointed by |initial_secret|. The length of secret is 32
  294. * bytes, and |initial_secret| must point to the buffer which has
  295. * enough capacity.
  296. *
  297. * |client_dcid| is the destination connection ID in first Initial
  298. * packet of client.
  299. *
  300. * If |rx_key| is not NULL, the derived packet protection key for
  301. * decryption is written to the buffer pointed by |rx_key|. If
  302. * |rx_iv| is not NULL, the derived packet protection IV for
  303. * decryption is written to the buffer pointed by |rx_iv|. If |rx_hp|
  304. * is not NULL, the derived header protection key for decryption is
  305. * written to the buffer pointed by |rx_hp|.
  306. *
  307. * If |tx_key| is not NULL, the derived packet protection key for
  308. * encryption is written to the buffer pointed by |tx_key|. If
  309. * |tx_iv| is not NULL, the derived packet protection IV for
  310. * encryption is written to the buffer pointed by |tx_iv|. If |tx_hp|
  311. * is not NULL, the derived header protection key for encryption is
  312. * written to the buffer pointed by |tx_hp|.
  313. *
  314. * The length of packet protection key and header protection key is 16
  315. * bytes long. The length of packet protection IV is 12 bytes long.
  316. *
  317. * This function returns 0 if it succeeds, or -1.
  318. */
  319. int ngtcp2_crypto_derive_and_install_vneg_initial_key(
  320. ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  321. uint8_t *initial_secret, uint8_t *rx_key, uint8_t *rx_iv, uint8_t *rx_hp,
  322. uint8_t *tx_key, uint8_t *tx_iv, uint8_t *tx_hp, uint32_t version,
  323. const ngtcp2_cid *client_dcid);
  324. /**
  325. * @function
  326. *
  327. * `ngtcp2_crypto_cipher_ctx_encrypt_init` initializes |cipher_ctx|
  328. * with new cipher context object for encryption which is constructed
  329. * to use |key| as encryption key. |cipher| specifies cipher to use.
  330. *
  331. * This function returns 0 if it succeeds, or -1.
  332. */
  333. int ngtcp2_crypto_cipher_ctx_encrypt_init(ngtcp2_crypto_cipher_ctx *cipher_ctx,
  334. const ngtcp2_crypto_cipher *cipher,
  335. const uint8_t *key);
  336. /**
  337. * @function
  338. *
  339. * `ngtcp2_crypto_cipher_ctx_free` frees up resources used by
  340. * |cipher_ctx|. This function does not free the memory pointed by
  341. * |cipher_ctx| itself.
  342. */
  343. void ngtcp2_crypto_cipher_ctx_free(ngtcp2_crypto_cipher_ctx *cipher_ctx);
  344. /*
  345. * `ngtcp2_crypto_md_sha256` initializes |md| with SHA256 message
  346. * digest algorithm and returns |md|.
  347. */
  348. ngtcp2_crypto_md *ngtcp2_crypto_md_sha256(ngtcp2_crypto_md *md);
  349. ngtcp2_crypto_aead *ngtcp2_crypto_aead_aes_128_gcm(ngtcp2_crypto_aead *aead);
  350. /*
  351. * `ngtcp2_crypto_random` writes cryptographically-secure random
  352. * |datalen| bytes into the buffer pointed by |data|.
  353. *
  354. * This function returns 0 if it succeeds, or -1.
  355. */
  356. int ngtcp2_crypto_random(uint8_t *data, size_t datalen);
  357. /**
  358. * @function
  359. *
  360. * `ngtcp2_crypto_hkdf_expand_label` performs HKDF expand label. The
  361. * result is |destlen| bytes long, and is stored to the buffer pointed
  362. * by |dest|.
  363. *
  364. * This function returns 0 if it succeeds, or -1.
  365. */
  366. int ngtcp2_crypto_hkdf_expand_label(uint8_t *dest, size_t destlen,
  367. const ngtcp2_crypto_md *md,
  368. const uint8_t *secret, size_t secretlen,
  369. const uint8_t *label, size_t labellen);
  370. #endif /* !defined(SHARED_H) */