lsquic_crypto.h revision 06b2a236
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2
3#ifndef __LSQUIC_CRYPTO_H__
4#define __LSQUIC_CRYPTO_H__
5
6#include <stdint.h>
7
8#define HS_PKT_HASH_LENGTH 12
9
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15struct lsquic_str;
16struct evp_aead_ctx_st;
17struct evp_pkey_st;
18struct x509_st;
19
20#if defined( __x86_64 )||defined( __x86_64__ )
21    typedef __uint128_t uint128;
22#else
23    typedef struct uint128_st
24    {
25        uint64_t hi_;
26        uint64_t lo_;
27    } uint128;
28#endif
29
30
31void lsquic_crypto_init(void);
32
33
34#ifndef NDEBUG
35int lsquic_export_key_material_simple(unsigned char *ikm, uint32_t ikm_len,
36                        unsigned char *salt, int salt_len,
37                        char *label, uint32_t label_len,
38                        const uint8_t *context, uint32_t context_len,
39                        uint8_t *key, uint16_t key_len);
40#endif
41
42int lsquic_export_key_material(const unsigned char *ikm, uint32_t ikm_len,
43                        const unsigned char *salt, int salt_len,
44                        const unsigned char *context, uint32_t context_len,
45                        uint16_t c_key_len, uint8_t *c_key,
46                        uint16_t s_key_len, uint8_t *s_key,
47                        uint16_t c_key_iv_len, uint8_t *c_key_iv,
48                        uint16_t s_key_iv_len, uint8_t *s_key_iv,
49                        uint8_t *sub_key,
50                        uint8_t *c_hp, uint8_t *s_hp);
51
52void lsquic_c255_get_pub_key(unsigned char *priv_key, unsigned char pub_key[32]);
53int lsquic_c255_gen_share_key(unsigned char *priv_key, unsigned char *peer_pub_key, unsigned char *shared_key);
54
55
56
57uint64_t lsquic_fnv1a_64(const uint8_t * data, int len);
58void lsquic_fnv1a_64_s(const uint8_t * data, int len, char *md);
59void lsquic_fnv1a_128_s(const uint8_t * data , int len, uint8_t  *md);
60uint128 lsquic_fnv1a_128_3(const uint8_t * data1, int len1,
61                      const uint8_t * data2, int len2,
62                      const uint8_t * data3, int len3);
63void lsquic_serialize_fnv128_short(uint128 v, uint8_t *md);
64
65
66/* Encrypt plaint text to cipher test */
67int lsquic_aes_aead_enc(struct evp_aead_ctx_st *key,
68              const uint8_t *ad, size_t ad_len,
69              const uint8_t *nonce, size_t nonce_len,
70              const uint8_t *plain, size_t plain_len,
71              uint8_t *cypher, size_t *cypher_len);
72
73int lsquic_aes_aead_dec(struct evp_aead_ctx_st *key,
74              const uint8_t *ad, size_t ad_len,
75              const uint8_t *nonce, size_t nonce_len,
76              const uint8_t *cypher, size_t cypher_len,
77              uint8_t *plain, size_t *plain_len);
78
79/* 32 bytes client nonce with 4 bytes tm, 8 bytes orbit */
80void lsquic_gen_nonce_c(unsigned char *buf, uint64_t orbit);
81
82struct x509_st *lsquic_bio_to_crt(const void *buf, int len, int type);
83
84int lshkdf_expand(const unsigned char *prk, const unsigned char *info, int info_len,
85                uint16_t c_key_len, uint8_t *c_key,
86                uint16_t s_key_len, uint8_t *s_key,
87                uint16_t c_key_iv_len, uint8_t *c_key_iv,
88                uint16_t s_key_iv_len, uint8_t *s_key_iv,
89                uint16_t sub_key_len, uint8_t *sub_key,
90                uint8_t *c_hp, uint8_t *s_hp);
91void lshkdf_extract(const unsigned char *ikm, int ikm_len, const unsigned char *salt,
92                  int salt_len, unsigned char *prk);
93
94int lsquic_gen_prof(const uint8_t *chlo_data, size_t chlo_data_len,
95             const uint8_t *scfg_data, uint32_t scfg_data_len,
96             const struct evp_pkey_st *priv_key, uint8_t *buf, size_t *len);
97
98int lsquic_verify_prof(const uint8_t *chlo_data, size_t chlo_data_len, struct lsquic_str * scfg,
99                const struct evp_pkey_st *pub_key, const uint8_t *buf, size_t len);
100
101
102#ifdef __cplusplus
103}
104#endif
105
106#endif //__LSQUIC_CRYPTO_H__
107