lsquic_crypto.c revision 5392f7a3
1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc. See LICENSE. */ 250aadb33SDmitri Tikhonov#include <assert.h> 350aadb33SDmitri Tikhonov#include <string.h> 450aadb33SDmitri Tikhonov 550aadb33SDmitri Tikhonov#include <openssl/ssl.h> 650aadb33SDmitri Tikhonov#include <openssl/crypto.h> 750aadb33SDmitri Tikhonov#include <openssl/stack.h> 850aadb33SDmitri Tikhonov#include <openssl/x509.h> 950aadb33SDmitri Tikhonov#include <openssl/rand.h> 1050aadb33SDmitri Tikhonov#include <openssl/curve25519.h> 11e0197994SDmitri Tikhonov#include <openssl/hmac.h> 1250aadb33SDmitri Tikhonov 1350aadb33SDmitri Tikhonov#include <zlib.h> 14461e84d8SAmol Deshpande#ifdef WIN32 15461e84d8SAmol Deshpande#include <vc_compat.h> 16461e84d8SAmol Deshpande#endif 1750aadb33SDmitri Tikhonov 1850aadb33SDmitri Tikhonov#include "lsquic_types.h" 1950aadb33SDmitri Tikhonov#include "lsquic_crypto.h" 2050aadb33SDmitri Tikhonov#include "lsquic_parse.h" 2150aadb33SDmitri Tikhonov#include "lsquic_util.h" 2250aadb33SDmitri Tikhonov#include "lsquic_str.h" 2350aadb33SDmitri Tikhonov 2450aadb33SDmitri Tikhonov#define LSQUIC_LOGGER_MODULE LSQLM_CRYPTO 2550aadb33SDmitri Tikhonov#include "lsquic_logger.h" 2650aadb33SDmitri Tikhonov 2750aadb33SDmitri Tikhonov 2850aadb33SDmitri Tikhonovstatic const char s_hs_signature[] = "QUIC CHLO and server config signature"; 2950aadb33SDmitri Tikhonovstatic int crypto_inited = 0; 3050aadb33SDmitri Tikhonov 3150aadb33SDmitri Tikhonov 3250aadb33SDmitri Tikhonovvoid rand_bytes(void *data, int len) 3350aadb33SDmitri Tikhonov{ 3450aadb33SDmitri Tikhonov RAND_bytes(data, len); 3550aadb33SDmitri Tikhonov} 3650aadb33SDmitri Tikhonov 3750aadb33SDmitri Tikhonov 3850aadb33SDmitri Tikhonovuint64_t fnv1a_64(const uint8_t * data, int len) 3950aadb33SDmitri Tikhonov{ 4050aadb33SDmitri Tikhonov uint64_t hash = UINT64_C(14695981039346656037); 4150aadb33SDmitri Tikhonov const uint8_t *end = data + len; 4250aadb33SDmitri Tikhonov while(data < end) 4350aadb33SDmitri Tikhonov { 4450aadb33SDmitri Tikhonov hash ^= *data; 4550aadb33SDmitri Tikhonov hash *= UINT64_C(1099511628211); 4650aadb33SDmitri Tikhonov ++data; 4750aadb33SDmitri Tikhonov } 4850aadb33SDmitri Tikhonov return hash; 4950aadb33SDmitri Tikhonov} 5050aadb33SDmitri Tikhonov 5150aadb33SDmitri Tikhonov 5250aadb33SDmitri Tikhonovvoid fnv1a_64_s(const uint8_t * data, int len, char *md) 5350aadb33SDmitri Tikhonov{ 5450aadb33SDmitri Tikhonov uint64_t hash = fnv1a_64(data, len); 5550aadb33SDmitri Tikhonov memcpy(md, (void *)&hash, 8); 5650aadb33SDmitri Tikhonov} 5750aadb33SDmitri Tikhonov 5850aadb33SDmitri Tikhonov 5950aadb33SDmitri Tikhonov#if defined( __x86_64 )||defined( __x86_64__ ) 6050aadb33SDmitri Tikhonov 6150aadb33SDmitri Tikhonovstatic uint128 s_prime; 6250aadb33SDmitri Tikhonovstatic uint128 s_init_hash; 6350aadb33SDmitri Tikhonov 6450aadb33SDmitri Tikhonov 6550aadb33SDmitri Tikhonovstatic inline void make_uint128(uint128 *v, uint64_t hi, uint64_t lo) 6650aadb33SDmitri Tikhonov{ 6750aadb33SDmitri Tikhonov *v = hi; 6850aadb33SDmitri Tikhonov *v <<= 64; 6950aadb33SDmitri Tikhonov *v += lo; 7050aadb33SDmitri Tikhonov} 7150aadb33SDmitri Tikhonov 7250aadb33SDmitri Tikhonov 7350aadb33SDmitri Tikhonovvoid fnv1a_inc(uint128 *hash, const uint8_t *data, int len) 7450aadb33SDmitri Tikhonov{ 7550aadb33SDmitri Tikhonov const uint8_t* end = data + len; 7650aadb33SDmitri Tikhonov while(data < end) 7750aadb33SDmitri Tikhonov { 7850aadb33SDmitri Tikhonov *hash = (*hash ^ (*data)) * s_prime; 7950aadb33SDmitri Tikhonov ++data; 8050aadb33SDmitri Tikhonov } 8150aadb33SDmitri Tikhonov} 8250aadb33SDmitri Tikhonov 8350aadb33SDmitri Tikhonovuint128 fnv1a_128_3(const uint8_t *data1, int len1, 8450aadb33SDmitri Tikhonov const uint8_t *data2, int len2, 8550aadb33SDmitri Tikhonov const uint8_t *data3, int len3) 8650aadb33SDmitri Tikhonov{ 8750aadb33SDmitri Tikhonov uint128 hash; 8850aadb33SDmitri Tikhonov memcpy(&hash, &s_init_hash, 16); 8950aadb33SDmitri Tikhonov 9050aadb33SDmitri Tikhonov fnv1a_inc(&hash, data1, len1); 9150aadb33SDmitri Tikhonov fnv1a_inc(&hash, data2, len2); 9250aadb33SDmitri Tikhonov fnv1a_inc(&hash, data3, len3); 9350aadb33SDmitri Tikhonov return hash; 9450aadb33SDmitri Tikhonov} 9550aadb33SDmitri Tikhonov 9650aadb33SDmitri Tikhonov/* HS_PKT_HASH_LENGTH bytes of md */ 9750aadb33SDmitri Tikhonovvoid serialize_fnv128_short(uint128 v, uint8_t *md) 9850aadb33SDmitri Tikhonov{ 9950aadb33SDmitri Tikhonov memcpy(md, (void *)&v, 12); 10050aadb33SDmitri Tikhonov} 10150aadb33SDmitri Tikhonov 10250aadb33SDmitri Tikhonov#else 10350aadb33SDmitri Tikhonovuint128 *uint128_times(uint128 *v, const uint128 *factor) 10450aadb33SDmitri Tikhonov{ 10550aadb33SDmitri Tikhonov uint64_t a96 = v->hi_ >> 32; 10650aadb33SDmitri Tikhonov uint64_t a64 = v->hi_ & 0xffffffffu; 10750aadb33SDmitri Tikhonov uint64_t a32 = v->lo_ >> 32; 10850aadb33SDmitri Tikhonov uint64_t a00 = v->lo_ & 0xffffffffu; 10950aadb33SDmitri Tikhonov uint64_t b96 = factor->hi_ >> 32; 11050aadb33SDmitri Tikhonov uint64_t b64 = factor->hi_ & 0xffffffffu; 11150aadb33SDmitri Tikhonov uint64_t b32 = factor->lo_ >> 32; 11250aadb33SDmitri Tikhonov uint64_t b00 = factor->lo_ & 0xffffffffu; 11350aadb33SDmitri Tikhonov uint64_t tmp, lolo; 11450aadb33SDmitri Tikhonov // multiply [a96 .. a00] x [b96 .. b00] 11550aadb33SDmitri Tikhonov // terms higher than c96 disappear off the high side 11650aadb33SDmitri Tikhonov // terms c96 and c64 are safe to ignore carry bit 11750aadb33SDmitri Tikhonov uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96; 11850aadb33SDmitri Tikhonov uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64; 11950aadb33SDmitri Tikhonov v->hi_ = (c96 << 32) + c64; 12050aadb33SDmitri Tikhonov v->lo_ = 0; 12150aadb33SDmitri Tikhonov 12250aadb33SDmitri Tikhonov tmp = a32 * b00; 12350aadb33SDmitri Tikhonov v->hi_ += tmp >> 32; 12450aadb33SDmitri Tikhonov v->lo_ += tmp << 32; 12550aadb33SDmitri Tikhonov 12650aadb33SDmitri Tikhonov tmp = a00 * b32; 12750aadb33SDmitri Tikhonov v->hi_ += tmp >> 32; 12850aadb33SDmitri Tikhonov v->lo_ += tmp << 32; 12950aadb33SDmitri Tikhonov 13050aadb33SDmitri Tikhonov tmp = a00 * b00; 13150aadb33SDmitri Tikhonov lolo = v->lo_ + tmp; 13250aadb33SDmitri Tikhonov if (lolo < v->lo_) 13350aadb33SDmitri Tikhonov ++v->hi_; 13450aadb33SDmitri Tikhonov v->lo_ = lolo; 13550aadb33SDmitri Tikhonov 13650aadb33SDmitri Tikhonov return v; 13750aadb33SDmitri Tikhonov} 13850aadb33SDmitri Tikhonov 13950aadb33SDmitri Tikhonovvoid fnv1a_inc(uint128 *hash, const uint8_t * data, int len) 14050aadb33SDmitri Tikhonov{ 14150aadb33SDmitri Tikhonov static const uint128 kPrime = {16777216, 315}; 14250aadb33SDmitri Tikhonov const uint8_t* end = data + len; 14350aadb33SDmitri Tikhonov while(data < end) 14450aadb33SDmitri Tikhonov { 14550aadb33SDmitri Tikhonov hash->lo_ = (hash->lo_ ^ (uint64_t)*data); 14650aadb33SDmitri Tikhonov uint128_times(hash, &kPrime); 14750aadb33SDmitri Tikhonov ++data; 14850aadb33SDmitri Tikhonov } 14950aadb33SDmitri Tikhonov} 15050aadb33SDmitri Tikhonov 15150aadb33SDmitri Tikhonov 15250aadb33SDmitri Tikhonovuint128 fnv1a_128_3(const uint8_t * data1, int len1, 15350aadb33SDmitri Tikhonov const uint8_t * data2, int len2, 15450aadb33SDmitri Tikhonov const uint8_t * data3, int len3) 15550aadb33SDmitri Tikhonov{ 15650aadb33SDmitri Tikhonov uint128 hash = {UINT64_C(7809847782465536322), UINT64_C(7113472399480571277)}; 15750aadb33SDmitri Tikhonov fnv1a_inc(&hash, data1, len1); 15850aadb33SDmitri Tikhonov fnv1a_inc(&hash, data2, len2); 15950aadb33SDmitri Tikhonov fnv1a_inc(&hash, data3, len3); 16050aadb33SDmitri Tikhonov return hash; 16150aadb33SDmitri Tikhonov} 16250aadb33SDmitri Tikhonov 16350aadb33SDmitri Tikhonov 16450aadb33SDmitri Tikhonov/* HS_PKT_HASH_LENGTH bytes of md */ 16550aadb33SDmitri Tikhonovvoid serialize_fnv128_short(uint128 v, uint8_t *md) 16650aadb33SDmitri Tikhonov{ 16750aadb33SDmitri Tikhonov assert(HS_PKT_HASH_LENGTH == 8 + 4); 16850aadb33SDmitri Tikhonov memcpy(md, (void *)&v.lo_, 8); 16950aadb33SDmitri Tikhonov memcpy(md + 8, (void *)&v.hi_, 4); 17050aadb33SDmitri Tikhonov} 17150aadb33SDmitri Tikhonov 17250aadb33SDmitri Tikhonov#endif 17350aadb33SDmitri Tikhonov 17450aadb33SDmitri Tikhonov 1755392f7a3SLiteSpeed Techstatic void sha256(const uint8_t *buf, int len, uint8_t *h) 17650aadb33SDmitri Tikhonov{ 17750aadb33SDmitri Tikhonov SHA256_CTX ctx; 17850aadb33SDmitri Tikhonov SHA256_Init(&ctx); 17950aadb33SDmitri Tikhonov SHA256_Update(&ctx, buf, len); 18050aadb33SDmitri Tikhonov SHA256_Final(h, &ctx); 18150aadb33SDmitri Tikhonov} 18250aadb33SDmitri Tikhonov 18350aadb33SDmitri Tikhonov 18450aadb33SDmitri Tikhonov/* base on rfc 5869 with sha256, prk is 32 bytes*/ 18550aadb33SDmitri Tikhonovvoid lshkdf_extract(const unsigned char *ikm, int ikm_len, const unsigned char *salt, 18650aadb33SDmitri Tikhonov int salt_len, unsigned char *prk) 18750aadb33SDmitri Tikhonov{ 18850aadb33SDmitri Tikhonov#ifndef NDEBUG 18950aadb33SDmitri Tikhonov unsigned char *out; 19050aadb33SDmitri Tikhonov unsigned int out_len; 19150aadb33SDmitri Tikhonov out = 19250aadb33SDmitri Tikhonov#endif 19350aadb33SDmitri Tikhonov HMAC(EVP_sha256(), salt, salt_len, ikm, ikm_len, prk, 19450aadb33SDmitri Tikhonov#ifndef NDEBUG 19550aadb33SDmitri Tikhonov &out_len 19650aadb33SDmitri Tikhonov#else 19750aadb33SDmitri Tikhonov NULL 19850aadb33SDmitri Tikhonov#endif 19950aadb33SDmitri Tikhonov ); 20050aadb33SDmitri Tikhonov assert(out); 20150aadb33SDmitri Tikhonov assert(out_len == 32); 20250aadb33SDmitri Tikhonov} 20350aadb33SDmitri Tikhonov 20450aadb33SDmitri Tikhonov 205461e84d8SAmol Deshpande#define SHA256LEN 32 20650aadb33SDmitri Tikhonovint lshkdf_expand(const unsigned char *prk, const unsigned char *info, int info_len, 20750aadb33SDmitri Tikhonov uint16_t c_key_len, uint8_t *c_key, 20850aadb33SDmitri Tikhonov uint16_t s_key_len, uint8_t *s_key, 20950aadb33SDmitri Tikhonov uint16_t c_key_iv_len, uint8_t *c_key_iv, 21050aadb33SDmitri Tikhonov uint16_t s_key_iv_len, uint8_t *s_key_iv, 21150aadb33SDmitri Tikhonov uint16_t sub_key_len, uint8_t *sub_key) 21250aadb33SDmitri Tikhonov{ 21350aadb33SDmitri Tikhonov int L = c_key_len + s_key_len + c_key_iv_len + s_key_iv_len + sub_key_len; 21450aadb33SDmitri Tikhonov int N = (L + SHA256LEN - 1) / SHA256LEN; 21550aadb33SDmitri Tikhonov unsigned char *p_org; 21650aadb33SDmitri Tikhonov uint8_t *buf; 21750aadb33SDmitri Tikhonov unsigned char *p; 21850aadb33SDmitri Tikhonov unsigned char T[SHA256LEN + 1]; 21950aadb33SDmitri Tikhonov int T_len = 0; 22050aadb33SDmitri Tikhonov int i; 22150aadb33SDmitri Tikhonov uint8_t *pb; 22250aadb33SDmitri Tikhonov 22350aadb33SDmitri Tikhonov p_org = malloc(N * SHA256LEN); 22450aadb33SDmitri Tikhonov if (!p_org) 22550aadb33SDmitri Tikhonov return -1; 22650aadb33SDmitri Tikhonov 22750aadb33SDmitri Tikhonov buf = malloc(SHA256LEN + info_len + 13); 22850aadb33SDmitri Tikhonov if (!buf) 22950aadb33SDmitri Tikhonov { 23050aadb33SDmitri Tikhonov free(p_org); 23150aadb33SDmitri Tikhonov return -1; 23250aadb33SDmitri Tikhonov } 23350aadb33SDmitri Tikhonov 23450aadb33SDmitri Tikhonov p = p_org; 23550aadb33SDmitri Tikhonov 23650aadb33SDmitri Tikhonov for (i = 1; i <= N; ++i) 23750aadb33SDmitri Tikhonov { 23850aadb33SDmitri Tikhonov pb = buf; 23950aadb33SDmitri Tikhonov if (T_len > 0) 24050aadb33SDmitri Tikhonov { 24150aadb33SDmitri Tikhonov memcpy(pb, T, T_len); 24250aadb33SDmitri Tikhonov pb += T_len; 24350aadb33SDmitri Tikhonov } 24450aadb33SDmitri Tikhonov 24550aadb33SDmitri Tikhonov memcpy(pb, info, info_len); 24650aadb33SDmitri Tikhonov pb += info_len; 24750aadb33SDmitri Tikhonov *pb = i; 24850aadb33SDmitri Tikhonov ++pb; 24950aadb33SDmitri Tikhonov 25050aadb33SDmitri Tikhonov HMAC(EVP_sha256(), prk, SHA256LEN, buf, pb - buf, T, NULL); 25150aadb33SDmitri Tikhonov if (i != N) 25250aadb33SDmitri Tikhonov T_len = SHA256LEN; 25350aadb33SDmitri Tikhonov else 25450aadb33SDmitri Tikhonov T_len = L - (N - 1) * SHA256LEN; 25550aadb33SDmitri Tikhonov 25650aadb33SDmitri Tikhonov memcpy(p, T, T_len); 25750aadb33SDmitri Tikhonov p += T_len; 25850aadb33SDmitri Tikhonov } 25950aadb33SDmitri Tikhonov 26050aadb33SDmitri Tikhonov free(buf); 26150aadb33SDmitri Tikhonov 26250aadb33SDmitri Tikhonov p = p_org; 26350aadb33SDmitri Tikhonov if (c_key_len) 26450aadb33SDmitri Tikhonov { 26550aadb33SDmitri Tikhonov memcpy(c_key, p, c_key_len); 26650aadb33SDmitri Tikhonov p += c_key_len; 26750aadb33SDmitri Tikhonov } 26850aadb33SDmitri Tikhonov if (s_key_len) 26950aadb33SDmitri Tikhonov { 27050aadb33SDmitri Tikhonov memcpy(s_key, p, s_key_len); 27150aadb33SDmitri Tikhonov p += s_key_len; 27250aadb33SDmitri Tikhonov } 27350aadb33SDmitri Tikhonov if (c_key_iv_len) 27450aadb33SDmitri Tikhonov { 27550aadb33SDmitri Tikhonov memcpy(c_key_iv, p, c_key_iv_len); 27650aadb33SDmitri Tikhonov p += c_key_iv_len; 27750aadb33SDmitri Tikhonov } 27850aadb33SDmitri Tikhonov if (s_key_iv_len) 27950aadb33SDmitri Tikhonov { 28050aadb33SDmitri Tikhonov memcpy(s_key_iv, p, s_key_iv_len); 28150aadb33SDmitri Tikhonov p += s_key_iv_len; 28250aadb33SDmitri Tikhonov } 28350aadb33SDmitri Tikhonov if (sub_key_len && sub_key) 28450aadb33SDmitri Tikhonov { 28550aadb33SDmitri Tikhonov memcpy(sub_key, p, sub_key_len); 28650aadb33SDmitri Tikhonov p += sub_key_len; 28750aadb33SDmitri Tikhonov } 28850aadb33SDmitri Tikhonov 28950aadb33SDmitri Tikhonov free(p_org); 29050aadb33SDmitri Tikhonov return 0; 29150aadb33SDmitri Tikhonov} 29250aadb33SDmitri Tikhonov 29350aadb33SDmitri Tikhonov 29450aadb33SDmitri Tikhonovint export_key_material_simple(unsigned char *ikm, uint32_t ikm_len, 29550aadb33SDmitri Tikhonov unsigned char *salt, int salt_len, 29650aadb33SDmitri Tikhonov char *label, uint32_t label_len, 29750aadb33SDmitri Tikhonov const uint8_t *context, uint32_t context_len, 29850aadb33SDmitri Tikhonov uint8_t *key, uint16_t key_len) 29950aadb33SDmitri Tikhonov{ 30050aadb33SDmitri Tikhonov unsigned char prk[32]; 30150aadb33SDmitri Tikhonov int info_len; 30250aadb33SDmitri Tikhonov uint8_t *info = NULL; 30350aadb33SDmitri Tikhonov info = (uint8_t *)malloc(label_len + 1 + sizeof(uint32_t) + context_len); 30450aadb33SDmitri Tikhonov if (!info) 30550aadb33SDmitri Tikhonov return -1; 30650aadb33SDmitri Tikhonov 30750aadb33SDmitri Tikhonov lshkdf_extract(ikm, ikm_len, salt, salt_len, prk); 30850aadb33SDmitri Tikhonov memcpy(info, label, label_len); 30950aadb33SDmitri Tikhonov info[label_len] = 0x00; 31050aadb33SDmitri Tikhonov info_len = label_len + 1; 31150aadb33SDmitri Tikhonov memcpy(info + info_len, &context_len, sizeof(uint32_t)); 31250aadb33SDmitri Tikhonov info_len += sizeof(uint32_t); 31350aadb33SDmitri Tikhonov memcpy(info + info_len, context, context_len); 31450aadb33SDmitri Tikhonov info_len += context_len; 31550aadb33SDmitri Tikhonov lshkdf_expand(prk, info, info_len, key_len, key, 31650aadb33SDmitri Tikhonov 0, NULL, 0, NULL,0, NULL, 0, NULL); 31750aadb33SDmitri Tikhonov free(info); 31850aadb33SDmitri Tikhonov return 0; 31950aadb33SDmitri Tikhonov} 32050aadb33SDmitri Tikhonov 32150aadb33SDmitri Tikhonov 32250aadb33SDmitri Tikhonovint export_key_material(const unsigned char *ikm, uint32_t ikm_len, 32350aadb33SDmitri Tikhonov const unsigned char *salt, int salt_len, 32450aadb33SDmitri Tikhonov const unsigned char *context, uint32_t context_len, 32550aadb33SDmitri Tikhonov uint16_t c_key_len, uint8_t *c_key, 32650aadb33SDmitri Tikhonov uint16_t s_key_len, uint8_t *s_key, 32750aadb33SDmitri Tikhonov uint16_t c_key_iv_len, uint8_t *c_key_iv, 32850aadb33SDmitri Tikhonov uint16_t s_key_iv_len, uint8_t *s_key_iv, 32950aadb33SDmitri Tikhonov uint8_t *sub_key) 33050aadb33SDmitri Tikhonov{ 33150aadb33SDmitri Tikhonov unsigned char prk[32]; 33250aadb33SDmitri Tikhonov uint16_t sub_key_len = ikm_len; 33350aadb33SDmitri Tikhonov 33450aadb33SDmitri Tikhonov lshkdf_extract(ikm, ikm_len, salt, salt_len, prk); 33550aadb33SDmitri Tikhonov lshkdf_expand(prk, context, context_len, c_key_len, c_key, 33650aadb33SDmitri Tikhonov s_key_len, s_key, c_key_iv_len, c_key_iv, s_key_iv_len, 33750aadb33SDmitri Tikhonov s_key_iv, sub_key_len, sub_key); 33850aadb33SDmitri Tikhonov return 0; 33950aadb33SDmitri Tikhonov} 34050aadb33SDmitri Tikhonov 34150aadb33SDmitri Tikhonovvoid c255_get_pub_key(unsigned char *priv_key, unsigned char pub_key[32]) 34250aadb33SDmitri Tikhonov{ 34350aadb33SDmitri Tikhonov X25519_public_from_private(pub_key, priv_key); 34450aadb33SDmitri Tikhonov} 34550aadb33SDmitri Tikhonov 34650aadb33SDmitri Tikhonov 34750aadb33SDmitri Tikhonovint c255_gen_share_key(unsigned char *priv_key, unsigned char *peer_pub_key, unsigned char *shared_key) 34850aadb33SDmitri Tikhonov{ 34950aadb33SDmitri Tikhonov return X25519(shared_key, priv_key, peer_pub_key); 35050aadb33SDmitri Tikhonov} 35150aadb33SDmitri Tikhonov 35250aadb33SDmitri Tikhonov 35350aadb33SDmitri Tikhonov 35450aadb33SDmitri Tikhonov/* AEAD nonce is always zero */ 35550aadb33SDmitri Tikhonov/* return 0 for OK */ 35650aadb33SDmitri Tikhonovint aes_aead_enc(EVP_AEAD_CTX *key, 35750aadb33SDmitri Tikhonov const uint8_t *ad, size_t ad_len, 35850aadb33SDmitri Tikhonov const uint8_t *nonce, size_t nonce_len, 35950aadb33SDmitri Tikhonov const uint8_t *plain, size_t plain_len, 36050aadb33SDmitri Tikhonov uint8_t *cypher, size_t *cypher_len) 36150aadb33SDmitri Tikhonov{ 36250aadb33SDmitri Tikhonov int ret = 0; 36350aadb33SDmitri Tikhonov size_t max_out_len; 36450aadb33SDmitri Tikhonov max_out_len = *cypher_len;//plain_len + EVP_AEAD_max_overhead(aead_); 36550aadb33SDmitri Tikhonov assert(*cypher_len >= max_out_len); 36650aadb33SDmitri Tikhonov 36750aadb33SDmitri Tikhonov LSQ_DEBUG("***aes_aead_enc data %s", get_bin_str(plain, plain_len, 40)); 36850aadb33SDmitri Tikhonov ret = EVP_AEAD_CTX_seal(key, cypher, cypher_len, max_out_len, 36950aadb33SDmitri Tikhonov nonce, nonce_len, plain, plain_len, ad, ad_len); 37050aadb33SDmitri Tikhonov// LSQ_DEBUG("***aes_aead_enc nonce: %s", get_bin_str(nonce, nonce_len)); 37150aadb33SDmitri Tikhonov// LSQ_DEBUG("***aes_aead_enc AD: %s", get_bin_str(ad, ad_len)); 37250aadb33SDmitri Tikhonov// LSQ_DEBUG("***aes_aead_enc return %d", (ret ? 0 : -1)); 37350aadb33SDmitri Tikhonov if (ret) 37450aadb33SDmitri Tikhonov { 37550aadb33SDmitri Tikhonov LSQ_DEBUG("***aes_aead_enc succeed, cypher content %s", 37650aadb33SDmitri Tikhonov get_bin_str(cypher, *cypher_len, 40)); 37750aadb33SDmitri Tikhonov return 0; 37850aadb33SDmitri Tikhonov } 37950aadb33SDmitri Tikhonov else 38050aadb33SDmitri Tikhonov { 38150aadb33SDmitri Tikhonov LSQ_DEBUG("***aes_aead_enc failed."); 38250aadb33SDmitri Tikhonov return -1; 38350aadb33SDmitri Tikhonov } 38450aadb33SDmitri Tikhonov} 38550aadb33SDmitri Tikhonov 38650aadb33SDmitri Tikhonov 38750aadb33SDmitri Tikhonov/* return 0 for OK */ 38850aadb33SDmitri Tikhonovint aes_aead_dec(EVP_AEAD_CTX *key, 38950aadb33SDmitri Tikhonov const uint8_t *ad, size_t ad_len, 39050aadb33SDmitri Tikhonov const uint8_t *nonce, size_t nonce_len, 39150aadb33SDmitri Tikhonov const uint8_t *cypher, size_t cypher_len, 39250aadb33SDmitri Tikhonov uint8_t *plain, size_t *plain_len) 39350aadb33SDmitri Tikhonov{ 39450aadb33SDmitri Tikhonov int ret = 0; 39550aadb33SDmitri Tikhonov size_t max_out_len = *plain_len; 39650aadb33SDmitri Tikhonov assert(max_out_len >= cypher_len); 39750aadb33SDmitri Tikhonov 39850aadb33SDmitri Tikhonov LSQ_DEBUG("***aes_aead_dec data %s", get_bin_str(cypher, cypher_len, 40)); 39950aadb33SDmitri Tikhonov 40050aadb33SDmitri Tikhonov 40150aadb33SDmitri Tikhonov ret = EVP_AEAD_CTX_open(key, plain, plain_len, max_out_len, 40250aadb33SDmitri Tikhonov nonce, nonce_len, cypher, cypher_len, ad, ad_len); 40350aadb33SDmitri Tikhonov 40450aadb33SDmitri Tikhonov// LSQ_DEBUG("***aes_aead_dec nonce: %s", get_bin_str(nonce, nonce_len)); 40550aadb33SDmitri Tikhonov// LSQ_DEBUG("***aes_aead_dec AD: %s", get_bin_str(ad, ad_len)); 40650aadb33SDmitri Tikhonov// LSQ_DEBUG("***aes_aead_dec return %d", (ret ? 0 : -1)); 40750aadb33SDmitri Tikhonov if (ret) 40850aadb33SDmitri Tikhonov { 40950aadb33SDmitri Tikhonov LSQ_DEBUG("***aes_aead_dec succeed, plain content %s", 41050aadb33SDmitri Tikhonov get_bin_str(plain, *plain_len, 20)); 41150aadb33SDmitri Tikhonov return 0; 41250aadb33SDmitri Tikhonov } 41350aadb33SDmitri Tikhonov else 41450aadb33SDmitri Tikhonov { 41550aadb33SDmitri Tikhonov LSQ_DEBUG("***aes_aead_dec failed."); 41650aadb33SDmitri Tikhonov return -1; 41750aadb33SDmitri Tikhonov } 41850aadb33SDmitri Tikhonov} 41950aadb33SDmitri Tikhonov 42050aadb33SDmitri Tikhonov/* 32 bytes client nonce with 4 bytes tm, 8 bytes orbit */ 42150aadb33SDmitri Tikhonovvoid gen_nonce_c(unsigned char *buf, uint64_t orbit) 42250aadb33SDmitri Tikhonov{ 42350aadb33SDmitri Tikhonov time_t tm = time(NULL); 42450aadb33SDmitri Tikhonov unsigned char *p = buf; 42550aadb33SDmitri Tikhonov memcpy(p, &tm, 4); 42650aadb33SDmitri Tikhonov p += 4; 42750aadb33SDmitri Tikhonov memcpy(p, &orbit, 8); 42850aadb33SDmitri Tikhonov p += 8; 42950aadb33SDmitri Tikhonov rand_bytes(p, 20); 43050aadb33SDmitri Tikhonov p += 20; 43150aadb33SDmitri Tikhonov} 43250aadb33SDmitri Tikhonov 43350aadb33SDmitri Tikhonov 43450aadb33SDmitri TikhonovEVP_PKEY *PEM_to_key(const char *buf, int len) 43550aadb33SDmitri Tikhonov{ 43650aadb33SDmitri Tikhonov RSA *rsa = NULL; 43750aadb33SDmitri Tikhonov EVP_PKEY *key = EVP_PKEY_new(); 43850aadb33SDmitri Tikhonov BIO *bio = BIO_new_mem_buf(buf, len); 43950aadb33SDmitri Tikhonov if (!bio || !key) 44050aadb33SDmitri Tikhonov return NULL; 44150aadb33SDmitri Tikhonov 44250aadb33SDmitri Tikhonov rsa = PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL); 44350aadb33SDmitri Tikhonov if (!rsa) 44450aadb33SDmitri Tikhonov return NULL; 44550aadb33SDmitri Tikhonov 44650aadb33SDmitri Tikhonov EVP_PKEY_assign_RSA(key, rsa); 44750aadb33SDmitri Tikhonov return key; 44850aadb33SDmitri Tikhonov} 44950aadb33SDmitri Tikhonov 45050aadb33SDmitri Tikhonov 45150aadb33SDmitri Tikhonov/* type 0 DER, 1: PEM */ 45250aadb33SDmitri TikhonovX509 *bio_to_crt(const void *buf, int len, int type) 45350aadb33SDmitri Tikhonov{ 45450aadb33SDmitri Tikhonov X509 *crt = NULL; 45550aadb33SDmitri Tikhonov BIO *bio = BIO_new_mem_buf(buf, len); 45650aadb33SDmitri Tikhonov if (bio == NULL) 45750aadb33SDmitri Tikhonov return NULL; 45850aadb33SDmitri Tikhonov 45950aadb33SDmitri Tikhonov if (type == 0) 46050aadb33SDmitri Tikhonov crt = d2i_X509_bio(bio, NULL); 46150aadb33SDmitri Tikhonov else 46250aadb33SDmitri Tikhonov crt = PEM_read_bio_X509(bio, &crt, 0 , NULL); 46350aadb33SDmitri Tikhonov BIO_free(bio); 46450aadb33SDmitri Tikhonov return crt; 46550aadb33SDmitri Tikhonov} 46650aadb33SDmitri Tikhonov 46750aadb33SDmitri Tikhonov 46850aadb33SDmitri Tikhonovint gen_prof(const uint8_t *chlo_data, size_t chlo_data_len, 46950aadb33SDmitri Tikhonov const uint8_t *scfg_data, uint32_t scfg_data_len, 47050aadb33SDmitri Tikhonov const EVP_PKEY *priv_key, uint8_t *buf, size_t *buf_len) 47150aadb33SDmitri Tikhonov{ 47250aadb33SDmitri Tikhonov uint8_t chlo_hash[32] = {0}; 47350aadb33SDmitri Tikhonov size_t chlo_hash_len = 32; /* SHA256 */ 47450aadb33SDmitri Tikhonov EVP_MD_CTX sign_context; 47550aadb33SDmitri Tikhonov EVP_PKEY_CTX* pkey_ctx = NULL; 47650aadb33SDmitri Tikhonov 47750aadb33SDmitri Tikhonov sha256(chlo_data, chlo_data_len, chlo_hash); 47850aadb33SDmitri Tikhonov EVP_MD_CTX_init(&sign_context); 47950aadb33SDmitri Tikhonov if (!EVP_DigestSignInit(&sign_context, &pkey_ctx, EVP_sha256(), NULL, (EVP_PKEY *)priv_key)) 48050aadb33SDmitri Tikhonov return -1; 48150aadb33SDmitri Tikhonov 48250aadb33SDmitri Tikhonov EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING); 48350aadb33SDmitri Tikhonov EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1); 48450aadb33SDmitri Tikhonov 48550aadb33SDmitri Tikhonov if (!EVP_DigestSignUpdate(&sign_context, s_hs_signature, sizeof(s_hs_signature)) || 48650aadb33SDmitri Tikhonov !EVP_DigestSignUpdate(&sign_context, (const uint8_t*)(&chlo_hash_len), 4) || 48750aadb33SDmitri Tikhonov !EVP_DigestSignUpdate(&sign_context, chlo_hash, chlo_hash_len) || 48850aadb33SDmitri Tikhonov !EVP_DigestSignUpdate(&sign_context, scfg_data, scfg_data_len)) 48950aadb33SDmitri Tikhonov { 49050aadb33SDmitri Tikhonov return -1; 49150aadb33SDmitri Tikhonov } 49250aadb33SDmitri Tikhonov 49350aadb33SDmitri Tikhonov size_t len = 0; 49450aadb33SDmitri Tikhonov if (!EVP_DigestSignFinal(&sign_context, NULL, &len)) { 49550aadb33SDmitri Tikhonov return -1; 49650aadb33SDmitri Tikhonov } 49750aadb33SDmitri Tikhonov 49850aadb33SDmitri Tikhonov if (len > *buf_len) 49950aadb33SDmitri Tikhonov return -2; 50050aadb33SDmitri Tikhonov if (buf) 50150aadb33SDmitri Tikhonov EVP_DigestSignFinal(&sign_context, buf, buf_len); 50250aadb33SDmitri Tikhonov 50350aadb33SDmitri Tikhonov EVP_MD_CTX_cleanup(&sign_context); 50450aadb33SDmitri Tikhonov return 0; 50550aadb33SDmitri Tikhonov} 50650aadb33SDmitri Tikhonov 50750aadb33SDmitri Tikhonov 50850aadb33SDmitri Tikhonovint verify_prof(const uint8_t *chlo_data, size_t chlo_data_len, lsquic_str_t * scfg, 50950aadb33SDmitri Tikhonov const EVP_PKEY *pub_key, const uint8_t *buf, size_t len) 51050aadb33SDmitri Tikhonov{ 51150aadb33SDmitri Tikhonov return verify_prof0(chlo_data, chlo_data_len, 51250aadb33SDmitri Tikhonov (const uint8_t *)lsquic_str_buf(scfg), 51350aadb33SDmitri Tikhonov lsquic_str_len(scfg), pub_key, buf, len); 51450aadb33SDmitri Tikhonov} 51550aadb33SDmitri Tikhonov 51650aadb33SDmitri Tikhonov 51750aadb33SDmitri Tikhonov 51850aadb33SDmitri Tikhonov 51950aadb33SDmitri Tikhonov/* -3 internal error, -1: verify failed, 0: Success */ 52050aadb33SDmitri Tikhonovint verify_prof0(const uint8_t *chlo_data, size_t chlo_data_len, 52150aadb33SDmitri Tikhonov const uint8_t *scfg_data, uint32_t scfg_data_len, 52250aadb33SDmitri Tikhonov const EVP_PKEY *pub_key, const uint8_t *buf, size_t len) 52350aadb33SDmitri Tikhonov{ 52450aadb33SDmitri Tikhonov uint8_t chlo_hash[32] = {0}; 52550aadb33SDmitri Tikhonov size_t chlo_hash_len = 32; /* SHA256 */ 52650aadb33SDmitri Tikhonov EVP_MD_CTX sign_context; 52750aadb33SDmitri Tikhonov EVP_PKEY_CTX* pkey_ctx = NULL; 52850aadb33SDmitri Tikhonov int ret = 0; 52950aadb33SDmitri Tikhonov EVP_MD_CTX_init(&sign_context); 53050aadb33SDmitri Tikhonov sha256(chlo_data, chlo_data_len, chlo_hash); 53150aadb33SDmitri Tikhonov 53250aadb33SDmitri Tikhonov // discarding const below to quiet compiler warning on call to ssl library code 53350aadb33SDmitri Tikhonov if (!EVP_DigestVerifyInit(&sign_context, &pkey_ctx, EVP_sha256(), NULL, (EVP_PKEY *)pub_key)) 53450aadb33SDmitri Tikhonov return -4; 53550aadb33SDmitri Tikhonov 53650aadb33SDmitri Tikhonov EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING); 53750aadb33SDmitri Tikhonov EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1); 53850aadb33SDmitri Tikhonov 53950aadb33SDmitri Tikhonov 54050aadb33SDmitri Tikhonov if (!EVP_DigestVerifyUpdate(&sign_context, s_hs_signature, sizeof(s_hs_signature)) || 54150aadb33SDmitri Tikhonov !EVP_DigestVerifyUpdate(&sign_context, (const uint8_t*)(&chlo_hash_len), 4) || 54250aadb33SDmitri Tikhonov !EVP_DigestVerifyUpdate(&sign_context, chlo_hash, chlo_hash_len) || 54350aadb33SDmitri Tikhonov !EVP_DigestVerifyUpdate(&sign_context, scfg_data, scfg_data_len)) 54450aadb33SDmitri Tikhonov { 54550aadb33SDmitri Tikhonov return -3; /* set to -3, to avoid same as "not enough data" -2 */ 54650aadb33SDmitri Tikhonov } 54750aadb33SDmitri Tikhonov 54850aadb33SDmitri Tikhonov ret = EVP_DigestVerifyFinal(&sign_context, buf, len); 54950aadb33SDmitri Tikhonov EVP_MD_CTX_cleanup(&sign_context); 55050aadb33SDmitri Tikhonov 55150aadb33SDmitri Tikhonov if (ret == 1) 55250aadb33SDmitri Tikhonov return 0; //OK 55350aadb33SDmitri Tikhonov else 55450aadb33SDmitri Tikhonov return -1; //failed 55550aadb33SDmitri Tikhonov} 55650aadb33SDmitri Tikhonov 55750aadb33SDmitri Tikhonov 55867b0dc15SDmitri Tikhonovvoid crypto_init(void) 55950aadb33SDmitri Tikhonov{ 56050aadb33SDmitri Tikhonov if (crypto_inited) 56150aadb33SDmitri Tikhonov return ; 56250aadb33SDmitri Tikhonov 56350aadb33SDmitri Tikhonov //SSL_library_init(); 56450aadb33SDmitri Tikhonov CRYPTO_library_init(); 56567b0dc15SDmitri Tikhonov /* XXX Should we seed? If yes, wherewith? */ // RAND_seed(seed, seed_len); 56650aadb33SDmitri Tikhonov 56750aadb33SDmitri Tikhonov#if defined( __x86_64 )||defined( __x86_64__ ) 56850aadb33SDmitri Tikhonov make_uint128(&s_prime, 16777216, 315); 56950aadb33SDmitri Tikhonov make_uint128(&s_init_hash, 7809847782465536322, 7113472399480571277); 57050aadb33SDmitri Tikhonov#endif 57150aadb33SDmitri Tikhonov 57250aadb33SDmitri Tikhonov /* MORE .... */ 57350aadb33SDmitri Tikhonov crypto_inited = 1; 57450aadb33SDmitri Tikhonov} 57550aadb33SDmitri Tikhonov 576