lsquic_crypto.h revision 10c41073
1/* Copyright (c) 2017 - 2020 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 crypto_init(void); 32 33 34int export_key_material_simple(unsigned char *ikm, uint32_t ikm_len, 35 unsigned char *salt, int salt_len, 36 char *label, uint32_t label_len, 37 const uint8_t *context, uint32_t context_len, 38 uint8_t *key, uint16_t key_len); 39 40int lsquic_export_key_material(const unsigned char *ikm, uint32_t ikm_len, 41 const unsigned char *salt, int salt_len, 42 const unsigned char *context, uint32_t context_len, 43 uint16_t c_key_len, uint8_t *c_key, 44 uint16_t s_key_len, uint8_t *s_key, 45 uint16_t c_key_iv_len, uint8_t *c_key_iv, 46 uint16_t s_key_iv_len, uint8_t *s_key_iv, 47 uint8_t *sub_key, 48 uint8_t *c_hp, uint8_t *s_hp); 49 50void c255_get_pub_key(unsigned char *priv_key, unsigned char pub_key[32]); 51int c255_gen_share_key(unsigned char *priv_key, unsigned char *peer_pub_key, unsigned char *shared_key); 52 53 54 55uint64_t fnv1a_64(const uint8_t * data, int len); 56void fnv1a_64_s(const uint8_t * data, int len, char *md); 57void fnv1a_128_s(const uint8_t * data , int len, uint8_t *md); 58uint128 fnv1a_128_3(const uint8_t * data1, int len1, 59 const uint8_t * data2, int len2, 60 const uint8_t * data3, int len3); 61void serialize_fnv128_short(uint128 v, uint8_t *md); 62 63 64/* Encrypt plaint text to cipher test */ 65int aes_aead_enc(struct evp_aead_ctx_st *key, 66 const uint8_t *ad, size_t ad_len, 67 const uint8_t *nonce, size_t nonce_len, 68 const uint8_t *plain, size_t plain_len, 69 uint8_t *cypher, size_t *cypher_len); 70 71int aes_aead_dec(struct evp_aead_ctx_st *key, 72 const uint8_t *ad, size_t ad_len, 73 const uint8_t *nonce, size_t nonce_len, 74 const uint8_t *cypher, size_t cypher_len, 75 uint8_t *plain, size_t *plain_len); 76 77/* 32 bytes client nonce with 4 bytes tm, 8 bytes orbit */ 78void gen_nonce_c(unsigned char *buf, uint64_t orbit); 79 80struct evp_pkey_st *PEM_to_key(const char *buf, int len); 81 82struct x509_st *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 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); 97int verify_prof0(const uint8_t *chlo_data, size_t chlo_data_len, 98 const uint8_t *scfg_data, uint32_t scfg_data_len, 99 const struct evp_pkey_st *pub_key, const uint8_t *buf, size_t len); 100 101int verify_prof(const uint8_t *chlo_data, size_t chlo_data_len, struct lsquic_str * scfg, 102 const struct evp_pkey_st *pub_key, const uint8_t *buf, size_t len); 103 104 105#ifdef __cplusplus 106} 107#endif 108 109#endif //__LSQUIC_CRYPTO_H__ 110