Home | History | Annotate | Line # | Download | only in internal
      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 
     10 #ifndef OSSL_INTERNAL_HPKE_UTIL_H
     11 #define OSSL_INTERNAL_HPKE_UTIL_H
     12 #pragma once
     13 
     14 /* Constants from RFC 9180 Section 7.1 and 7.3 */
     15 #define OSSL_HPKE_MAX_SECRET 64
     16 #define OSSL_HPKE_MAX_PUBLIC 133
     17 #define OSSL_HPKE_MAX_PRIVATE 66
     18 #define OSSL_HPKE_MAX_KDF_INPUTLEN 64
     19 
     20 /*
     21  * max length of a base-nonce (the Nn field from OSSL_HPKE_AEAD_INFO), this
     22  * is used for a local stack array size
     23  */
     24 #define OSSL_HPKE_MAX_NONCELEN 12
     25 
     26 /*
     27  * @brief info about a KEM
     28  * Used to store constants from Section 7.1 "Table 2 KEM IDs"
     29  * and the bitmask for EC curves described in Section 7.1.3 DeriveKeyPair
     30  */
     31 typedef struct {
     32     uint16_t kem_id; /* code point for key encipherment method */
     33     const char *keytype; /* string form of algtype "EC"/"X25519"/"X448" */
     34     const char *groupname; /* string form of EC group for NIST curves  */
     35     const char *mdname; /* hash alg name for the HKDF */
     36     size_t Nsecret; /* size of secrets */
     37     size_t Nenc; /* length of encapsulated key */
     38     size_t Npk; /* length of public key */
     39     size_t Nsk; /* length of raw private key */
     40     uint8_t bitmask;
     41 } OSSL_HPKE_KEM_INFO;
     42 
     43 /*
     44  * @brief info about a KDF
     45  */
     46 typedef struct {
     47     uint16_t kdf_id; /* code point for KDF */
     48     const char *mdname; /* hash alg name for the HKDF */
     49     size_t Nh; /* length of hash/extract output */
     50 } OSSL_HPKE_KDF_INFO;
     51 
     52 /*
     53  * @brief info about an AEAD
     54  */
     55 typedef struct {
     56     uint16_t aead_id; /* code point for aead alg */
     57     const char *name; /* alg name */
     58     size_t taglen; /* aead tag len */
     59     size_t Nk; /* size of a key for this aead */
     60     size_t Nn; /* length of a nonce for this aead */
     61 } OSSL_HPKE_AEAD_INFO;
     62 
     63 const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_curve(const char *curve);
     64 const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_id(uint16_t kemid);
     65 const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_random(OSSL_LIB_CTX *ctx);
     66 const OSSL_HPKE_KDF_INFO *ossl_HPKE_KDF_INFO_find_id(uint16_t kdfid);
     67 const OSSL_HPKE_KDF_INFO *ossl_HPKE_KDF_INFO_find_random(OSSL_LIB_CTX *ctx);
     68 const OSSL_HPKE_AEAD_INFO *ossl_HPKE_AEAD_INFO_find_id(uint16_t aeadid);
     69 const OSSL_HPKE_AEAD_INFO *ossl_HPKE_AEAD_INFO_find_random(OSSL_LIB_CTX *ctx);
     70 
     71 int ossl_hpke_kdf_extract(EVP_KDF_CTX *kctx,
     72     unsigned char *prk, size_t prklen,
     73     const unsigned char *salt, size_t saltlen,
     74     const unsigned char *ikm, size_t ikmlen);
     75 
     76 int ossl_hpke_kdf_expand(EVP_KDF_CTX *kctx,
     77     unsigned char *okm, size_t okmlen,
     78     const unsigned char *prk, size_t prklen,
     79     const unsigned char *info, size_t infolen);
     80 
     81 int ossl_hpke_labeled_extract(EVP_KDF_CTX *kctx,
     82     unsigned char *prk, size_t prklen,
     83     const unsigned char *salt, size_t saltlen,
     84     const char *protocol_label,
     85     const unsigned char *suiteid, size_t suiteidlen,
     86     const char *label,
     87     const unsigned char *ikm, size_t ikmlen);
     88 int ossl_hpke_labeled_expand(EVP_KDF_CTX *kctx,
     89     unsigned char *okm, size_t okmlen,
     90     const unsigned char *prk, size_t prklen,
     91     const char *protocol_label,
     92     const unsigned char *suiteid, size_t suiteidlen,
     93     const char *label,
     94     const unsigned char *info, size_t infolen);
     95 
     96 EVP_KDF_CTX *ossl_kdf_ctx_create(const char *kdfname, const char *mdname,
     97     OSSL_LIB_CTX *libctx, const char *propq);
     98 
     99 int ossl_hpke_str2suite(const char *suitestr, OSSL_HPKE_SUITE *suite);
    100 #endif
    101