hpke_util.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_HPKE_UTIL_H
  10. # define OSSL_INTERNAL_HPKE_UTIL_H
  11. # pragma once
  12. /* Constants from RFC 9180 Section 7.1 and 7.3 */
  13. # define OSSL_HPKE_MAX_SECRET 64
  14. # define OSSL_HPKE_MAX_PUBLIC 133
  15. # define OSSL_HPKE_MAX_PRIVATE 66
  16. # define OSSL_HPKE_MAX_KDF_INPUTLEN 64
  17. /*
  18. * max length of a base-nonce (the Nn field from OSSL_HPKE_AEAD_INFO), this
  19. * is used for a local stack array size
  20. */
  21. # define OSSL_HPKE_MAX_NONCELEN 12
  22. /*
  23. * @brief info about a KEM
  24. * Used to store constants from Section 7.1 "Table 2 KEM IDs"
  25. * and the bitmask for EC curves described in Section 7.1.3 DeriveKeyPair
  26. */
  27. typedef struct {
  28. uint16_t kem_id; /* code point for key encipherment method */
  29. const char *keytype; /* string form of algtype "EC"/"X25519"/"X448" */
  30. const char *groupname; /* string form of EC group for NIST curves */
  31. const char *mdname; /* hash alg name for the HKDF */
  32. size_t Nsecret; /* size of secrets */
  33. size_t Nenc; /* length of encapsulated key */
  34. size_t Npk; /* length of public key */
  35. size_t Nsk; /* length of raw private key */
  36. uint8_t bitmask;
  37. } OSSL_HPKE_KEM_INFO;
  38. /*
  39. * @brief info about a KDF
  40. */
  41. typedef struct {
  42. uint16_t kdf_id; /* code point for KDF */
  43. const char *mdname; /* hash alg name for the HKDF */
  44. size_t Nh; /* length of hash/extract output */
  45. } OSSL_HPKE_KDF_INFO;
  46. /*
  47. * @brief info about an AEAD
  48. */
  49. typedef struct {
  50. uint16_t aead_id; /* code point for aead alg */
  51. const char *name; /* alg name */
  52. size_t taglen; /* aead tag len */
  53. size_t Nk; /* size of a key for this aead */
  54. size_t Nn; /* length of a nonce for this aead */
  55. } OSSL_HPKE_AEAD_INFO;
  56. const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_curve(const char *curve);
  57. const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_id(uint16_t kemid);
  58. const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_random(OSSL_LIB_CTX *ctx);
  59. const OSSL_HPKE_KDF_INFO *ossl_HPKE_KDF_INFO_find_id(uint16_t kdfid);
  60. const OSSL_HPKE_KDF_INFO *ossl_HPKE_KDF_INFO_find_random(OSSL_LIB_CTX *ctx);
  61. const OSSL_HPKE_AEAD_INFO *ossl_HPKE_AEAD_INFO_find_id(uint16_t aeadid);
  62. const OSSL_HPKE_AEAD_INFO *ossl_HPKE_AEAD_INFO_find_random(OSSL_LIB_CTX *ctx);
  63. int ossl_hpke_kdf_extract(EVP_KDF_CTX *kctx,
  64. unsigned char *prk, size_t prklen,
  65. const unsigned char *salt, size_t saltlen,
  66. const unsigned char *ikm, size_t ikmlen);
  67. int ossl_hpke_kdf_expand(EVP_KDF_CTX *kctx,
  68. unsigned char *okm, size_t okmlen,
  69. const unsigned char *prk, size_t prklen,
  70. const unsigned char *info, size_t infolen);
  71. int ossl_hpke_labeled_extract(EVP_KDF_CTX *kctx,
  72. unsigned char *prk, size_t prklen,
  73. const unsigned char *salt, size_t saltlen,
  74. const char *protocol_label,
  75. const unsigned char *suiteid, size_t suiteidlen,
  76. const char *label,
  77. const unsigned char *ikm, size_t ikmlen);
  78. int ossl_hpke_labeled_expand(EVP_KDF_CTX *kctx,
  79. unsigned char *okm, size_t okmlen,
  80. const unsigned char *prk, size_t prklen,
  81. const char *protocol_label,
  82. const unsigned char *suiteid, size_t suiteidlen,
  83. const char *label,
  84. const unsigned char *info, size_t infolen);
  85. EVP_KDF_CTX *ossl_kdf_ctx_create(const char *kdfname, const char *mdname,
  86. OSSL_LIB_CTX *libctx, const char *propq);
  87. int ossl_hpke_str2suite(const char *suitestr, OSSL_HPKE_SUITE *suite);
  88. #endif