hpke.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2022 Fastly, Kazuho Oku
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include <stdlib.h>
  23. #include "picotls.h"
  24. #define HPKE_V1_LABEL "HPKE-v1"
  25. static int build_suite_id(ptls_buffer_t *buf, ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher)
  26. {
  27. int ret;
  28. if (cipher == NULL) {
  29. ptls_buffer_pushv(buf, "KEM", 3);
  30. ptls_buffer_push16(buf, kem->id);
  31. } else {
  32. ptls_buffer_pushv(buf, "HPKE", 4);
  33. ptls_buffer_push16(buf, kem->id);
  34. ptls_buffer_push16(buf, cipher->id.kdf);
  35. ptls_buffer_push16(buf, cipher->id.aead);
  36. }
  37. ret = 0;
  38. Exit:
  39. return ret;
  40. }
  41. static int labeled_extract(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, void *output, ptls_iovec_t salt,
  42. const char *label, ptls_iovec_t ikm)
  43. {
  44. ptls_buffer_t labeled_ikm;
  45. uint8_t labeled_ikm_smallbuf[64];
  46. int ret;
  47. ptls_buffer_init(&labeled_ikm, labeled_ikm_smallbuf, sizeof(labeled_ikm_smallbuf));
  48. ptls_buffer_pushv(&labeled_ikm, HPKE_V1_LABEL, strlen(HPKE_V1_LABEL));
  49. if ((ret = build_suite_id(&labeled_ikm, kem, cipher)) != 0)
  50. goto Exit;
  51. ptls_buffer_pushv(&labeled_ikm, label, strlen(label));
  52. ptls_buffer_pushv(&labeled_ikm, ikm.base, ikm.len);
  53. ret = ptls_hkdf_extract(cipher != NULL ? cipher->hash : kem->hash, output, salt,
  54. ptls_iovec_init(labeled_ikm.base, labeled_ikm.off));
  55. Exit:
  56. ptls_buffer_dispose(&labeled_ikm);
  57. return ret;
  58. }
  59. static int labeled_expand(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, void *output, size_t outlen, ptls_iovec_t prk,
  60. const char *label, ptls_iovec_t info)
  61. {
  62. ptls_buffer_t labeled_info;
  63. uint8_t labeled_info_smallbuf[64];
  64. int ret;
  65. assert(outlen < UINT16_MAX);
  66. ptls_buffer_init(&labeled_info, labeled_info_smallbuf, sizeof(labeled_info_smallbuf));
  67. ptls_buffer_push16(&labeled_info, (uint16_t)outlen);
  68. ptls_buffer_pushv(&labeled_info, HPKE_V1_LABEL, strlen(HPKE_V1_LABEL));
  69. if ((ret = build_suite_id(&labeled_info, kem, cipher)) != 0)
  70. goto Exit;
  71. ptls_buffer_pushv(&labeled_info, label, strlen(label));
  72. ptls_buffer_pushv(&labeled_info, info.base, info.len);
  73. ret = ptls_hkdf_expand(cipher != NULL ? cipher->hash : kem->hash, output, outlen, prk,
  74. ptls_iovec_init(labeled_info.base, labeled_info.off));
  75. Exit:
  76. ptls_buffer_dispose(&labeled_info);
  77. return ret;
  78. }
  79. static int extract_and_expand(ptls_hpke_kem_t *kem, void *secret, size_t secret_len, ptls_iovec_t pk_s, ptls_iovec_t pk_r,
  80. ptls_iovec_t dh)
  81. {
  82. ptls_buffer_t kem_context;
  83. uint8_t kem_context_smallbuf[128], eae_prk[PTLS_MAX_DIGEST_SIZE];
  84. int ret;
  85. ptls_buffer_init(&kem_context, kem_context_smallbuf, sizeof(kem_context_smallbuf));
  86. ptls_buffer_pushv(&kem_context, pk_s.base, pk_s.len);
  87. ptls_buffer_pushv(&kem_context, pk_r.base, pk_r.len);
  88. if ((ret = labeled_extract(kem, NULL, eae_prk, ptls_iovec_init("", 0), "eae_prk", dh)) != 0)
  89. goto Exit;
  90. if ((ret = labeled_expand(kem, NULL, secret, secret_len, ptls_iovec_init(eae_prk, kem->hash->digest_size), "shared_secret",
  91. ptls_iovec_init(kem_context.base, kem_context.off))) != 0)
  92. goto Exit;
  93. Exit:
  94. ptls_buffer_dispose(&kem_context);
  95. ptls_clear_memory(eae_prk, sizeof(eae_prk));
  96. return ret;
  97. }
  98. static int dh_derive(ptls_hpke_kem_t *kem, void *secret, ptls_iovec_t pk_s, ptls_iovec_t pk_r, ptls_iovec_t dh)
  99. {
  100. return extract_and_expand(kem, secret, kem->hash->digest_size, pk_s, pk_r, dh);
  101. }
  102. static int dh_encap(ptls_hpke_kem_t *kem, void *secret, ptls_iovec_t *pk_s, ptls_iovec_t pk_r)
  103. {
  104. ptls_iovec_t dh = {NULL};
  105. int ret;
  106. *pk_s = ptls_iovec_init(NULL, 0);
  107. if ((ret = kem->keyex->exchange(kem->keyex, pk_s, &dh, pk_r)) != 0) {
  108. assert(pk_s->base == NULL);
  109. assert(dh.base == NULL);
  110. goto Exit;
  111. }
  112. if ((ret = dh_derive(kem, secret, *pk_s, pk_r, dh)) != 0)
  113. goto Exit;
  114. Exit:
  115. if (dh.base != NULL) {
  116. ptls_clear_memory(dh.base, dh.len);
  117. free(dh.base);
  118. }
  119. if (ret != 0) {
  120. free(pk_s->base);
  121. *pk_s = ptls_iovec_init(NULL, 0);
  122. }
  123. return ret;
  124. }
  125. static int dh_decap(ptls_hpke_kem_t *kem, void *secret, ptls_key_exchange_context_t *keyex, ptls_iovec_t pk_s, ptls_iovec_t pk_r)
  126. {
  127. ptls_iovec_t dh = {NULL};
  128. int ret;
  129. if ((ret = keyex->on_exchange(&keyex, 0, &dh, pk_s)) != 0) {
  130. assert(dh.base == NULL);
  131. goto Exit;
  132. }
  133. if ((ret = dh_derive(kem, secret, pk_s, pk_r, dh)) != 0)
  134. goto Exit;
  135. Exit:
  136. if (dh.base != NULL) {
  137. ptls_clear_memory(dh.base, dh.len);
  138. free(dh.base);
  139. }
  140. return ret;
  141. }
  142. #include <stdio.h>
  143. static int key_schedule(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, ptls_aead_context_t **ctx, int is_enc,
  144. const void *shared_secret, ptls_iovec_t info)
  145. {
  146. ptls_buffer_t key_schedule_context;
  147. uint8_t key_schedule_context_smallbuf[128], secret[PTLS_MAX_DIGEST_SIZE], key[PTLS_MAX_SECRET_SIZE],
  148. base_nonce[PTLS_MAX_IV_SIZE];
  149. int ret;
  150. *ctx = NULL;
  151. ptls_buffer_init(&key_schedule_context, key_schedule_context_smallbuf, sizeof(key_schedule_context_smallbuf));
  152. /* key_schedule_context = concat(mode, LabeledExtract("", "psk_id_hash", psk_id), LabeledExtract("", "info_hash", info)) */
  153. ptls_buffer_push(&key_schedule_context, PTLS_HPKE_MODE_BASE);
  154. if ((ret = ptls_buffer_reserve(&key_schedule_context, cipher->hash->digest_size)) != 0 ||
  155. (ret = labeled_extract(kem, cipher, key_schedule_context.base + key_schedule_context.off, ptls_iovec_init(NULL, 0),
  156. "psk_id_hash", ptls_iovec_init(NULL, 0))) != 0)
  157. goto Exit;
  158. key_schedule_context.off += cipher->hash->digest_size;
  159. if ((ret = ptls_buffer_reserve(&key_schedule_context, cipher->hash->digest_size)) != 0 ||
  160. (ret = labeled_extract(kem, cipher, key_schedule_context.base + key_schedule_context.off, ptls_iovec_init(NULL, 0),
  161. "info_hash", info)) != 0)
  162. goto Exit;
  163. key_schedule_context.off += cipher->hash->digest_size;
  164. /* secret = LabeledExtract(shared_secret, "secret", psk) */
  165. if ((ret = labeled_extract(kem, cipher, secret, ptls_iovec_init(shared_secret, kem->hash->digest_size), "secret",
  166. ptls_iovec_init("", 0))) != 0)
  167. goto Exit;
  168. /* key, base_nonce */
  169. if ((ret = labeled_expand(kem, cipher, key, cipher->aead->key_size, ptls_iovec_init(secret, cipher->hash->digest_size), "key",
  170. ptls_iovec_init(key_schedule_context.base, key_schedule_context.off))) != 0)
  171. goto Exit;
  172. if ((ret = labeled_expand(kem, cipher, base_nonce, cipher->aead->iv_size, ptls_iovec_init(secret, cipher->hash->digest_size),
  173. "base_nonce", ptls_iovec_init(key_schedule_context.base, key_schedule_context.off))) != 0)
  174. goto Exit;
  175. *ctx = ptls_aead_new_direct(cipher->aead, is_enc, key, base_nonce);
  176. Exit:
  177. ptls_buffer_dispose(&key_schedule_context);
  178. ptls_clear_memory(secret, sizeof(secret));
  179. ptls_clear_memory(key, sizeof(key));
  180. ptls_clear_memory(base_nonce, sizeof(base_nonce));
  181. return ret;
  182. }
  183. int ptls_hpke_setup_base_s(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, ptls_iovec_t *pk_s, ptls_aead_context_t **ctx,
  184. ptls_iovec_t pk_r, ptls_iovec_t info)
  185. {
  186. uint8_t secret[PTLS_MAX_DIGEST_SIZE];
  187. int ret;
  188. *pk_s = ptls_iovec_init(NULL, 0);
  189. if ((ret = dh_encap(kem, secret, pk_s, pk_r)) != 0)
  190. goto Exit;
  191. if ((ret = key_schedule(kem, cipher, ctx, 1, secret, info)) != 0)
  192. goto Exit;
  193. Exit:
  194. if (ret != 0 && pk_s->len != 0) {
  195. ptls_clear_memory(pk_s->base, pk_s->len);
  196. free(pk_s->base);
  197. *pk_s = ptls_iovec_init(NULL, 0);
  198. }
  199. ptls_clear_memory(secret, sizeof(secret));
  200. return ret;
  201. }
  202. int ptls_hpke_setup_base_r(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, ptls_key_exchange_context_t *keyex,
  203. ptls_aead_context_t **ctx, ptls_iovec_t pk_s, ptls_iovec_t info)
  204. {
  205. uint8_t secret[PTLS_MAX_DIGEST_SIZE];
  206. int ret;
  207. if ((ret = dh_decap(kem, secret, keyex, pk_s, keyex->pubkey)) != 0)
  208. goto Exit;
  209. if ((ret = key_schedule(kem, cipher, ctx, 0, secret, info)) != 0)
  210. goto Exit;
  211. Exit:
  212. ptls_clear_memory(secret, sizeof(secret));
  213. return ret;
  214. }