1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov
350aadb33SDmitri Tikhonov#ifndef __LSQUIC_CRYPTO_H__
450aadb33SDmitri Tikhonov#define __LSQUIC_CRYPTO_H__
550aadb33SDmitri Tikhonov
650aadb33SDmitri Tikhonov#include <stdint.h>
750aadb33SDmitri Tikhonov
850aadb33SDmitri Tikhonov#define HS_PKT_HASH_LENGTH 12
950aadb33SDmitri Tikhonov
1050aadb33SDmitri Tikhonov
1150aadb33SDmitri Tikhonov#ifdef __cplusplus
1250aadb33SDmitri Tikhonovextern "C" {
1350aadb33SDmitri Tikhonov#endif
1450aadb33SDmitri Tikhonov
1550aadb33SDmitri Tikhonovstruct lsquic_str;
165392f7a3SLiteSpeed Techstruct evp_aead_ctx_st;
175392f7a3SLiteSpeed Techstruct evp_pkey_st;
185392f7a3SLiteSpeed Techstruct x509_st;
1950aadb33SDmitri Tikhonov
2050aadb33SDmitri Tikhonov#if defined( __x86_64 )||defined( __x86_64__ )
2150aadb33SDmitri Tikhonov    typedef __uint128_t uint128;
2250aadb33SDmitri Tikhonov#else
2350aadb33SDmitri Tikhonov    typedef struct uint128_st
2450aadb33SDmitri Tikhonov    {
2550aadb33SDmitri Tikhonov        uint64_t hi_;
2650aadb33SDmitri Tikhonov        uint64_t lo_;
2750aadb33SDmitri Tikhonov    } uint128;
2850aadb33SDmitri Tikhonov#endif
2950aadb33SDmitri Tikhonov
3050aadb33SDmitri Tikhonov
31a5fa05f9SDmitri Tikhonovvoid lsquic_crypto_init(void);
3250aadb33SDmitri Tikhonov
3350aadb33SDmitri Tikhonov
34a5fa05f9SDmitri Tikhonov#ifndef NDEBUG
35a5fa05f9SDmitri Tikhonovint lsquic_export_key_material_simple(unsigned char *ikm, uint32_t ikm_len,
3650aadb33SDmitri Tikhonov                        unsigned char *salt, int salt_len,
3750aadb33SDmitri Tikhonov                        char *label, uint32_t label_len,
3850aadb33SDmitri Tikhonov                        const uint8_t *context, uint32_t context_len,
3950aadb33SDmitri Tikhonov                        uint8_t *key, uint16_t key_len);
40a5fa05f9SDmitri Tikhonov#endif
4150aadb33SDmitri Tikhonov
427a8b2eceSDmitri Tikhonovint lsquic_export_key_material(const unsigned char *ikm, uint32_t ikm_len,
4350aadb33SDmitri Tikhonov                        const unsigned char *salt, int salt_len,
4450aadb33SDmitri Tikhonov                        const unsigned char *context, uint32_t context_len,
4550aadb33SDmitri Tikhonov                        uint16_t c_key_len, uint8_t *c_key,
4650aadb33SDmitri Tikhonov                        uint16_t s_key_len, uint8_t *s_key,
4750aadb33SDmitri Tikhonov                        uint16_t c_key_iv_len, uint8_t *c_key_iv,
4850aadb33SDmitri Tikhonov                        uint16_t s_key_iv_len, uint8_t *s_key_iv,
497a8b2eceSDmitri Tikhonov                        uint8_t *sub_key,
507a8b2eceSDmitri Tikhonov                        uint8_t *c_hp, uint8_t *s_hp);
5150aadb33SDmitri Tikhonov
52a5fa05f9SDmitri Tikhonovvoid lsquic_c255_get_pub_key(unsigned char *priv_key, unsigned char pub_key[32]);
53a5fa05f9SDmitri Tikhonovint lsquic_c255_gen_share_key(unsigned char *priv_key, unsigned char *peer_pub_key, unsigned char *shared_key);
5450aadb33SDmitri Tikhonov
5550aadb33SDmitri Tikhonov
5650aadb33SDmitri Tikhonov
57a5fa05f9SDmitri Tikhonovuint64_t lsquic_fnv1a_64(const uint8_t * data, int len);
58a5fa05f9SDmitri Tikhonovvoid lsquic_fnv1a_64_s(const uint8_t * data, int len, char *md);
59a5fa05f9SDmitri Tikhonovvoid lsquic_fnv1a_128_s(const uint8_t * data , int len, uint8_t  *md);
60a5fa05f9SDmitri Tikhonovuint128 lsquic_fnv1a_128_3(const uint8_t * data1, int len1,
6150aadb33SDmitri Tikhonov                      const uint8_t * data2, int len2,
6250aadb33SDmitri Tikhonov                      const uint8_t * data3, int len3);
63a5fa05f9SDmitri Tikhonovvoid lsquic_serialize_fnv128_short(uint128 v, uint8_t *md);
6450aadb33SDmitri Tikhonov
6550aadb33SDmitri Tikhonov
6650aadb33SDmitri Tikhonov/* Encrypt plaint text to cipher test */
67a5fa05f9SDmitri Tikhonovint lsquic_aes_aead_enc(struct evp_aead_ctx_st *key,
6850aadb33SDmitri Tikhonov              const uint8_t *ad, size_t ad_len,
6950aadb33SDmitri Tikhonov              const uint8_t *nonce, size_t nonce_len,
7050aadb33SDmitri Tikhonov              const uint8_t *plain, size_t plain_len,
7150aadb33SDmitri Tikhonov              uint8_t *cypher, size_t *cypher_len);
7250aadb33SDmitri Tikhonov
73a5fa05f9SDmitri Tikhonovint lsquic_aes_aead_dec(struct evp_aead_ctx_st *key,
7450aadb33SDmitri Tikhonov              const uint8_t *ad, size_t ad_len,
7550aadb33SDmitri Tikhonov              const uint8_t *nonce, size_t nonce_len,
7650aadb33SDmitri Tikhonov              const uint8_t *cypher, size_t cypher_len,
7750aadb33SDmitri Tikhonov              uint8_t *plain, size_t *plain_len);
7850aadb33SDmitri Tikhonov
7950aadb33SDmitri Tikhonov/* 32 bytes client nonce with 4 bytes tm, 8 bytes orbit */
80a5fa05f9SDmitri Tikhonovvoid lsquic_gen_nonce_c(unsigned char *buf, uint64_t orbit);
8150aadb33SDmitri Tikhonov
82a5fa05f9SDmitri Tikhonovstruct x509_st *lsquic_bio_to_crt(const void *buf, int len, int type);
8350aadb33SDmitri Tikhonov
8450aadb33SDmitri Tikhonovint lshkdf_expand(const unsigned char *prk, const unsigned char *info, int info_len,
8550aadb33SDmitri Tikhonov                uint16_t c_key_len, uint8_t *c_key,
8650aadb33SDmitri Tikhonov                uint16_t s_key_len, uint8_t *s_key,
8750aadb33SDmitri Tikhonov                uint16_t c_key_iv_len, uint8_t *c_key_iv,
8850aadb33SDmitri Tikhonov                uint16_t s_key_iv_len, uint8_t *s_key_iv,
897a8b2eceSDmitri Tikhonov                uint16_t sub_key_len, uint8_t *sub_key,
907a8b2eceSDmitri Tikhonov                uint8_t *c_hp, uint8_t *s_hp);
9150aadb33SDmitri Tikhonovvoid lshkdf_extract(const unsigned char *ikm, int ikm_len, const unsigned char *salt,
9250aadb33SDmitri Tikhonov                  int salt_len, unsigned char *prk);
9350aadb33SDmitri Tikhonov
94a5fa05f9SDmitri Tikhonovint lsquic_gen_prof(const uint8_t *chlo_data, size_t chlo_data_len,
9550aadb33SDmitri Tikhonov             const uint8_t *scfg_data, uint32_t scfg_data_len,
965392f7a3SLiteSpeed Tech             const struct evp_pkey_st *priv_key, uint8_t *buf, size_t *len);
9750aadb33SDmitri Tikhonov
98a5fa05f9SDmitri Tikhonovint lsquic_verify_prof(const uint8_t *chlo_data, size_t chlo_data_len, struct lsquic_str * scfg,
995392f7a3SLiteSpeed Tech                const struct evp_pkey_st *pub_key, const uint8_t *buf, size_t len);
10050aadb33SDmitri Tikhonov
10150aadb33SDmitri Tikhonov
10250aadb33SDmitri Tikhonov#ifdef __cplusplus
10350aadb33SDmitri Tikhonov}
10450aadb33SDmitri Tikhonov#endif
10550aadb33SDmitri Tikhonov
10650aadb33SDmitri Tikhonov#endif //__LSQUIC_CRYPTO_H__
107