1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 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>
117a8b2eceSDmitri Tikhonov#include <openssl/hkdf.h>
12e0197994SDmitri Tikhonov#include <openssl/hmac.h>
1350aadb33SDmitri Tikhonov
1450aadb33SDmitri Tikhonov#include <zlib.h>
15461e84d8SAmol Deshpande#ifdef WIN32
16461e84d8SAmol Deshpande#include <vc_compat.h>
17461e84d8SAmol Deshpande#endif
1850aadb33SDmitri Tikhonov
1950aadb33SDmitri Tikhonov#include "lsquic_types.h"
2050aadb33SDmitri Tikhonov#include "lsquic_crypto.h"
2150aadb33SDmitri Tikhonov#include "lsquic_parse.h"
2250aadb33SDmitri Tikhonov#include "lsquic_util.h"
2350aadb33SDmitri Tikhonov#include "lsquic_str.h"
2450aadb33SDmitri Tikhonov
2550aadb33SDmitri Tikhonov#define LSQUIC_LOGGER_MODULE LSQLM_CRYPTO
2650aadb33SDmitri Tikhonov#include "lsquic_logger.h"
2750aadb33SDmitri Tikhonov
2850aadb33SDmitri Tikhonov
2950aadb33SDmitri Tikhonovstatic const char s_hs_signature[] = "QUIC CHLO and server config signature";
3050aadb33SDmitri Tikhonovstatic int crypto_inited = 0;
3150aadb33SDmitri Tikhonov
3250aadb33SDmitri Tikhonov
33a5fa05f9SDmitri Tikhonovuint64_t lsquic_fnv1a_64(const uint8_t * data, int len)
3450aadb33SDmitri Tikhonov{
3550aadb33SDmitri Tikhonov    uint64_t hash = UINT64_C(14695981039346656037);
3650aadb33SDmitri Tikhonov    const uint8_t *end = data + len;
3750aadb33SDmitri Tikhonov    while(data < end)
3850aadb33SDmitri Tikhonov    {
3950aadb33SDmitri Tikhonov        hash ^= *data;
4050aadb33SDmitri Tikhonov        hash *= UINT64_C(1099511628211);
4150aadb33SDmitri Tikhonov        ++data;
4250aadb33SDmitri Tikhonov    }
4350aadb33SDmitri Tikhonov    return hash;
4450aadb33SDmitri Tikhonov}
4550aadb33SDmitri Tikhonov
4650aadb33SDmitri Tikhonov
47a5fa05f9SDmitri Tikhonovvoid lsquic_fnv1a_64_s(const uint8_t * data, int len, char *md)
4850aadb33SDmitri Tikhonov{
49a5fa05f9SDmitri Tikhonov    uint64_t hash = lsquic_fnv1a_64(data, len);
5050aadb33SDmitri Tikhonov    memcpy(md, (void *)&hash, 8);
5150aadb33SDmitri Tikhonov}
5250aadb33SDmitri Tikhonov
5350aadb33SDmitri Tikhonov
5450aadb33SDmitri Tikhonov#if defined( __x86_64 )||defined( __x86_64__ )
5550aadb33SDmitri Tikhonov
5650aadb33SDmitri Tikhonovstatic uint128 s_prime;
5750aadb33SDmitri Tikhonovstatic uint128 s_init_hash;
5850aadb33SDmitri Tikhonov
5950aadb33SDmitri Tikhonov
6050aadb33SDmitri Tikhonovstatic inline void make_uint128(uint128 *v, uint64_t hi, uint64_t lo)
6150aadb33SDmitri Tikhonov{
6250aadb33SDmitri Tikhonov    *v = hi;
6350aadb33SDmitri Tikhonov    *v <<= 64;
6450aadb33SDmitri Tikhonov    *v += lo;
6550aadb33SDmitri Tikhonov}
6650aadb33SDmitri Tikhonov
6750aadb33SDmitri Tikhonov
68a5fa05f9SDmitri Tikhonovvoid lsquic_fnv1a_inc(uint128 *hash, const uint8_t *data, int len)
6950aadb33SDmitri Tikhonov{
7050aadb33SDmitri Tikhonov    const uint8_t* end = data + len;
7150aadb33SDmitri Tikhonov    while(data < end)
7250aadb33SDmitri Tikhonov    {
7350aadb33SDmitri Tikhonov        *hash = (*hash ^ (*data)) * s_prime;
7450aadb33SDmitri Tikhonov        ++data;
7550aadb33SDmitri Tikhonov    }
7650aadb33SDmitri Tikhonov}
7750aadb33SDmitri Tikhonov
78a5fa05f9SDmitri Tikhonovuint128 lsquic_fnv1a_128_3(const uint8_t *data1, int len1,
7950aadb33SDmitri Tikhonov                      const uint8_t *data2, int len2,
8050aadb33SDmitri Tikhonov                      const uint8_t *data3, int len3)
8150aadb33SDmitri Tikhonov{
8250aadb33SDmitri Tikhonov    uint128 hash;
8350aadb33SDmitri Tikhonov    memcpy(&hash, &s_init_hash, 16);
8450aadb33SDmitri Tikhonov
85a5fa05f9SDmitri Tikhonov    lsquic_fnv1a_inc(&hash, data1, len1);
86a5fa05f9SDmitri Tikhonov    lsquic_fnv1a_inc(&hash, data2, len2);
87a5fa05f9SDmitri Tikhonov    lsquic_fnv1a_inc(&hash, data3, len3);
8850aadb33SDmitri Tikhonov    return hash;
8950aadb33SDmitri Tikhonov}
9050aadb33SDmitri Tikhonov
9150aadb33SDmitri Tikhonov/* HS_PKT_HASH_LENGTH bytes of md */
92a5fa05f9SDmitri Tikhonovvoid lsquic_serialize_fnv128_short(uint128 v, uint8_t *md)
9350aadb33SDmitri Tikhonov{
9450aadb33SDmitri Tikhonov    memcpy(md, (void *)&v, 12);
9550aadb33SDmitri Tikhonov}
9650aadb33SDmitri Tikhonov
9750aadb33SDmitri Tikhonov#else
9850aadb33SDmitri Tikhonovuint128  *uint128_times(uint128 *v, const uint128 *factor)
9950aadb33SDmitri Tikhonov{
10050aadb33SDmitri Tikhonov    uint64_t a96 = v->hi_ >> 32;
10150aadb33SDmitri Tikhonov    uint64_t a64 = v->hi_ & 0xffffffffu;
10250aadb33SDmitri Tikhonov    uint64_t a32 = v->lo_ >> 32;
10350aadb33SDmitri Tikhonov    uint64_t a00 = v->lo_ & 0xffffffffu;
10450aadb33SDmitri Tikhonov    uint64_t b96 = factor->hi_ >> 32;
10550aadb33SDmitri Tikhonov    uint64_t b64 = factor->hi_ & 0xffffffffu;
10650aadb33SDmitri Tikhonov    uint64_t b32 = factor->lo_ >> 32;
10750aadb33SDmitri Tikhonov    uint64_t b00 = factor->lo_ & 0xffffffffu;
10850aadb33SDmitri Tikhonov    uint64_t tmp, lolo;
10950aadb33SDmitri Tikhonov    // multiply [a96 .. a00] x [b96 .. b00]
11050aadb33SDmitri Tikhonov    // terms higher than c96 disappear off the high side
11150aadb33SDmitri Tikhonov    // terms c96 and c64 are safe to ignore carry bit
11250aadb33SDmitri Tikhonov    uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
11350aadb33SDmitri Tikhonov    uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64;
11450aadb33SDmitri Tikhonov    v->hi_ = (c96 << 32) + c64;
11550aadb33SDmitri Tikhonov    v->lo_ = 0;
11650aadb33SDmitri Tikhonov
11750aadb33SDmitri Tikhonov    tmp = a32 * b00;
11850aadb33SDmitri Tikhonov    v->hi_ += tmp >> 32;
11950aadb33SDmitri Tikhonov    v->lo_ += tmp << 32;
12050aadb33SDmitri Tikhonov
12150aadb33SDmitri Tikhonov    tmp = a00 * b32;
12250aadb33SDmitri Tikhonov    v->hi_ += tmp >> 32;
12350aadb33SDmitri Tikhonov    v->lo_ += tmp << 32;
12450aadb33SDmitri Tikhonov
12550aadb33SDmitri Tikhonov    tmp = a00 * b00;
12650aadb33SDmitri Tikhonov    lolo = v->lo_ + tmp;
12750aadb33SDmitri Tikhonov    if (lolo < v->lo_)
12850aadb33SDmitri Tikhonov        ++v->hi_;
12950aadb33SDmitri Tikhonov    v->lo_ = lolo;
13050aadb33SDmitri Tikhonov
13150aadb33SDmitri Tikhonov    return v;
13250aadb33SDmitri Tikhonov}
13350aadb33SDmitri Tikhonov
134a5fa05f9SDmitri Tikhonovvoid lsquic_fnv1a_inc(uint128 *hash, const uint8_t * data, int len)
13550aadb33SDmitri Tikhonov{
13650aadb33SDmitri Tikhonov    static const uint128 kPrime = {16777216, 315};
13750aadb33SDmitri Tikhonov    const uint8_t* end = data + len;
13850aadb33SDmitri Tikhonov    while(data < end)
13950aadb33SDmitri Tikhonov    {
14050aadb33SDmitri Tikhonov        hash->lo_ = (hash->lo_ ^ (uint64_t)*data);
14150aadb33SDmitri Tikhonov        uint128_times(hash, &kPrime);
14250aadb33SDmitri Tikhonov        ++data;
14350aadb33SDmitri Tikhonov    }
14450aadb33SDmitri Tikhonov}
14550aadb33SDmitri Tikhonov
14650aadb33SDmitri Tikhonov
147a5fa05f9SDmitri Tikhonovuint128 lsquic_fnv1a_128_3(const uint8_t * data1, int len1,
14850aadb33SDmitri Tikhonov                      const uint8_t * data2, int len2,
14950aadb33SDmitri Tikhonov                      const uint8_t * data3, int len3)
15050aadb33SDmitri Tikhonov{
15150aadb33SDmitri Tikhonov    uint128 hash = {UINT64_C(7809847782465536322), UINT64_C(7113472399480571277)};
152a5fa05f9SDmitri Tikhonov    lsquic_fnv1a_inc(&hash, data1, len1);
153a5fa05f9SDmitri Tikhonov    lsquic_fnv1a_inc(&hash, data2, len2);
154a5fa05f9SDmitri Tikhonov    lsquic_fnv1a_inc(&hash, data3, len3);
15550aadb33SDmitri Tikhonov    return hash;
15650aadb33SDmitri Tikhonov}
15750aadb33SDmitri Tikhonov
15850aadb33SDmitri Tikhonov
15950aadb33SDmitri Tikhonov/* HS_PKT_HASH_LENGTH bytes of md */
160a5fa05f9SDmitri Tikhonovvoid lsquic_serialize_fnv128_short(uint128 v, uint8_t *md)
16150aadb33SDmitri Tikhonov{
16250aadb33SDmitri Tikhonov    assert(HS_PKT_HASH_LENGTH == 8 + 4);
16350aadb33SDmitri Tikhonov    memcpy(md, (void *)&v.lo_, 8);
16450aadb33SDmitri Tikhonov    memcpy(md + 8, (void *)&v.hi_, 4);
16550aadb33SDmitri Tikhonov}
16650aadb33SDmitri Tikhonov
16750aadb33SDmitri Tikhonov#endif
16850aadb33SDmitri Tikhonov
16950aadb33SDmitri Tikhonov
1705392f7a3SLiteSpeed Techstatic void sha256(const uint8_t *buf, int len, uint8_t *h)
17150aadb33SDmitri Tikhonov{
17250aadb33SDmitri Tikhonov    SHA256_CTX ctx;
17350aadb33SDmitri Tikhonov    SHA256_Init(&ctx);
17450aadb33SDmitri Tikhonov    SHA256_Update(&ctx, buf, len);
17550aadb33SDmitri Tikhonov    SHA256_Final(h, &ctx);
17650aadb33SDmitri Tikhonov}
17750aadb33SDmitri Tikhonov
17850aadb33SDmitri Tikhonov
17950aadb33SDmitri Tikhonov/* base on rfc 5869 with sha256, prk is 32 bytes*/
18050aadb33SDmitri Tikhonovvoid lshkdf_extract(const unsigned char *ikm, int ikm_len, const unsigned char *salt,
18150aadb33SDmitri Tikhonov                  int salt_len, unsigned char *prk)
18250aadb33SDmitri Tikhonov{
18350aadb33SDmitri Tikhonov#ifndef NDEBUG
18450aadb33SDmitri Tikhonov    unsigned char *out;
18550aadb33SDmitri Tikhonov    unsigned int out_len;
18650aadb33SDmitri Tikhonov    out =
18750aadb33SDmitri Tikhonov#endif
18850aadb33SDmitri Tikhonov        HMAC(EVP_sha256(), salt, salt_len, ikm, ikm_len, prk,
18950aadb33SDmitri Tikhonov#ifndef NDEBUG
19050aadb33SDmitri Tikhonov                                                              &out_len
19150aadb33SDmitri Tikhonov#else
19250aadb33SDmitri Tikhonov                                                              NULL
19350aadb33SDmitri Tikhonov#endif
19450aadb33SDmitri Tikhonov                                                                      );
19550aadb33SDmitri Tikhonov    assert(out);
19650aadb33SDmitri Tikhonov    assert(out_len == 32);
19750aadb33SDmitri Tikhonov}
19850aadb33SDmitri Tikhonov
19950aadb33SDmitri Tikhonov
200461e84d8SAmol Deshpande#define SHA256LEN   32
20150aadb33SDmitri Tikhonovint lshkdf_expand(const unsigned char *prk, const unsigned char *info, int info_len,
20250aadb33SDmitri Tikhonov                uint16_t c_key_len, uint8_t *c_key,
20350aadb33SDmitri Tikhonov                uint16_t s_key_len, uint8_t *s_key,
20450aadb33SDmitri Tikhonov                uint16_t c_key_iv_len, uint8_t *c_key_iv,
20550aadb33SDmitri Tikhonov                uint16_t s_key_iv_len, uint8_t *s_key_iv,
2067a8b2eceSDmitri Tikhonov                uint16_t sub_key_len, uint8_t *sub_key,
2077a8b2eceSDmitri Tikhonov                uint8_t *c_hp, uint8_t *s_hp)
20850aadb33SDmitri Tikhonov{
2097a8b2eceSDmitri Tikhonov    const unsigned L = c_key_len + s_key_len + c_key_iv_len + s_key_iv_len
2107a8b2eceSDmitri Tikhonov            + sub_key_len
2117a8b2eceSDmitri Tikhonov            + (c_hp ? c_key_len : 0)
2127a8b2eceSDmitri Tikhonov            + (s_hp ? s_key_len : 0)
2137a8b2eceSDmitri Tikhonov            ;
21450aadb33SDmitri Tikhonov    unsigned char *p;
2157a8b2eceSDmitri Tikhonov    unsigned char output[
2167a8b2eceSDmitri Tikhonov        EVP_MAX_KEY_LENGTH * 2  /* Keys */
2177a8b2eceSDmitri Tikhonov      + EVP_MAX_IV_LENGTH * 2   /* IVs */
2187a8b2eceSDmitri Tikhonov      + 32                      /* Subkey */
2197a8b2eceSDmitri Tikhonov      + EVP_MAX_KEY_LENGTH * 2  /* Header protection */
2207a8b2eceSDmitri Tikhonov    ];
22150aadb33SDmitri Tikhonov
2227a8b2eceSDmitri Tikhonov    assert((size_t) L <= sizeof(output));
22350aadb33SDmitri Tikhonov
2247a8b2eceSDmitri Tikhonov#ifndef NDEBUG
2257a8b2eceSDmitri Tikhonov    const int s =
2267a8b2eceSDmitri Tikhonov#endif
2277a8b2eceSDmitri Tikhonov    HKDF_expand(output, L, EVP_sha256(), prk, 32, info, info_len);
2287a8b2eceSDmitri Tikhonov    assert(s);
2297a8b2eceSDmitri Tikhonov    p = output;
23050aadb33SDmitri Tikhonov    if (c_key_len)
23150aadb33SDmitri Tikhonov    {
23250aadb33SDmitri Tikhonov        memcpy(c_key, p, c_key_len);
23350aadb33SDmitri Tikhonov        p += c_key_len;
23450aadb33SDmitri Tikhonov    }
23550aadb33SDmitri Tikhonov    if (s_key_len)
23650aadb33SDmitri Tikhonov    {
23750aadb33SDmitri Tikhonov        memcpy(s_key, p, s_key_len);
23850aadb33SDmitri Tikhonov        p += s_key_len;
23950aadb33SDmitri Tikhonov    }
24050aadb33SDmitri Tikhonov    if (c_key_iv_len)
24150aadb33SDmitri Tikhonov    {
24250aadb33SDmitri Tikhonov        memcpy(c_key_iv, p, c_key_iv_len);
24350aadb33SDmitri Tikhonov        p += c_key_iv_len;
24450aadb33SDmitri Tikhonov    }
24550aadb33SDmitri Tikhonov    if (s_key_iv_len)
24650aadb33SDmitri Tikhonov    {
24750aadb33SDmitri Tikhonov        memcpy(s_key_iv, p, s_key_iv_len);
24850aadb33SDmitri Tikhonov        p += s_key_iv_len;
24950aadb33SDmitri Tikhonov    }
25050aadb33SDmitri Tikhonov    if (sub_key_len && sub_key)
25150aadb33SDmitri Tikhonov    {
25250aadb33SDmitri Tikhonov        memcpy(sub_key, p, sub_key_len);
25350aadb33SDmitri Tikhonov        p += sub_key_len;
25450aadb33SDmitri Tikhonov    }
2557a8b2eceSDmitri Tikhonov    if (c_key_len && c_hp)
2567a8b2eceSDmitri Tikhonov    {
2577a8b2eceSDmitri Tikhonov        memcpy(c_hp, p, c_key_len);
2587a8b2eceSDmitri Tikhonov        p += c_key_len;
2597a8b2eceSDmitri Tikhonov    }
2607a8b2eceSDmitri Tikhonov    if (s_key_len && s_hp)
2617a8b2eceSDmitri Tikhonov    {
2627a8b2eceSDmitri Tikhonov        memcpy(s_hp, p, s_key_len);
2637a8b2eceSDmitri Tikhonov        p += s_key_len;
2647a8b2eceSDmitri Tikhonov    }
26550aadb33SDmitri Tikhonov    return 0;
26650aadb33SDmitri Tikhonov}
26750aadb33SDmitri Tikhonov
26850aadb33SDmitri Tikhonov
269a5fa05f9SDmitri Tikhonov#ifndef NDEBUG
270a5fa05f9SDmitri Tikhonovint lsquic_export_key_material_simple(unsigned char *ikm, uint32_t ikm_len,
27150aadb33SDmitri Tikhonov                        unsigned char *salt, int salt_len,
27250aadb33SDmitri Tikhonov                        char *label, uint32_t label_len,
27350aadb33SDmitri Tikhonov                        const uint8_t *context, uint32_t context_len,
27450aadb33SDmitri Tikhonov                        uint8_t *key, uint16_t key_len)
27550aadb33SDmitri Tikhonov{
27650aadb33SDmitri Tikhonov    unsigned char prk[32];
27750aadb33SDmitri Tikhonov    int info_len;
27850aadb33SDmitri Tikhonov    uint8_t *info = NULL;
27950aadb33SDmitri Tikhonov    info = (uint8_t *)malloc(label_len + 1 + sizeof(uint32_t) + context_len);
28050aadb33SDmitri Tikhonov    if (!info)
28150aadb33SDmitri Tikhonov        return -1;
28250aadb33SDmitri Tikhonov
28350aadb33SDmitri Tikhonov    lshkdf_extract(ikm, ikm_len, salt, salt_len, prk);
28450aadb33SDmitri Tikhonov    memcpy(info, label, label_len);
28550aadb33SDmitri Tikhonov    info[label_len] = 0x00;
28650aadb33SDmitri Tikhonov    info_len = label_len + 1;
28750aadb33SDmitri Tikhonov    memcpy(info + info_len, &context_len, sizeof(uint32_t));
28850aadb33SDmitri Tikhonov    info_len += sizeof(uint32_t);
28950aadb33SDmitri Tikhonov    memcpy(info + info_len, context, context_len);
29050aadb33SDmitri Tikhonov    info_len += context_len;
29150aadb33SDmitri Tikhonov    lshkdf_expand(prk, info, info_len, key_len, key,
2927a8b2eceSDmitri Tikhonov                0, NULL, 0, NULL,0, NULL, 0, NULL, NULL, NULL);
29350aadb33SDmitri Tikhonov    free(info);
29450aadb33SDmitri Tikhonov    return 0;
29550aadb33SDmitri Tikhonov}
296a5fa05f9SDmitri Tikhonov#endif
29750aadb33SDmitri Tikhonov
29850aadb33SDmitri Tikhonov
2997a8b2eceSDmitri Tikhonovint
3007a8b2eceSDmitri Tikhonovlsquic_export_key_material(const unsigned char *ikm, uint32_t ikm_len,
30150aadb33SDmitri Tikhonov                        const unsigned char *salt, int salt_len,
30250aadb33SDmitri Tikhonov                        const unsigned char *context, uint32_t context_len,
30350aadb33SDmitri Tikhonov                        uint16_t c_key_len, uint8_t *c_key,
30450aadb33SDmitri Tikhonov                        uint16_t s_key_len, uint8_t *s_key,
30550aadb33SDmitri Tikhonov                        uint16_t c_key_iv_len, uint8_t *c_key_iv,
30650aadb33SDmitri Tikhonov                        uint16_t s_key_iv_len, uint8_t *s_key_iv,
3077a8b2eceSDmitri Tikhonov                        uint8_t *sub_key, uint8_t *c_hp, uint8_t *s_hp)
30850aadb33SDmitri Tikhonov{
30950aadb33SDmitri Tikhonov    unsigned char prk[32];
31050aadb33SDmitri Tikhonov    uint16_t sub_key_len = ikm_len;
31150aadb33SDmitri Tikhonov
31250aadb33SDmitri Tikhonov    lshkdf_extract(ikm, ikm_len, salt, salt_len, prk);
31350aadb33SDmitri Tikhonov    lshkdf_expand(prk, context, context_len, c_key_len, c_key,
31450aadb33SDmitri Tikhonov                s_key_len, s_key, c_key_iv_len, c_key_iv, s_key_iv_len,
3157a8b2eceSDmitri Tikhonov                s_key_iv, sub_key_len, sub_key, c_hp, s_hp);
31650aadb33SDmitri Tikhonov    return 0;
31750aadb33SDmitri Tikhonov}
31850aadb33SDmitri Tikhonov
319a5fa05f9SDmitri Tikhonovvoid lsquic_c255_get_pub_key(unsigned char *priv_key, unsigned char pub_key[32])
32050aadb33SDmitri Tikhonov{
32150aadb33SDmitri Tikhonov    X25519_public_from_private(pub_key, priv_key);
32250aadb33SDmitri Tikhonov}
32350aadb33SDmitri Tikhonov
32450aadb33SDmitri Tikhonov
325a5fa05f9SDmitri Tikhonovint lsquic_c255_gen_share_key(unsigned char *priv_key, unsigned char *peer_pub_key, unsigned char *shared_key)
32650aadb33SDmitri Tikhonov{
32750aadb33SDmitri Tikhonov    return X25519(shared_key, priv_key, peer_pub_key);
32850aadb33SDmitri Tikhonov}
32950aadb33SDmitri Tikhonov
33050aadb33SDmitri Tikhonov
33150aadb33SDmitri Tikhonov
33250aadb33SDmitri Tikhonov/* AEAD nonce is always zero */
33350aadb33SDmitri Tikhonov/* return 0 for OK */
334a5fa05f9SDmitri Tikhonovint lsquic_aes_aead_enc(EVP_AEAD_CTX *key,
33550aadb33SDmitri Tikhonov              const uint8_t *ad, size_t ad_len,
33650aadb33SDmitri Tikhonov              const uint8_t *nonce, size_t nonce_len,
33750aadb33SDmitri Tikhonov              const uint8_t *plain, size_t plain_len,
33850aadb33SDmitri Tikhonov              uint8_t *cypher, size_t *cypher_len)
33950aadb33SDmitri Tikhonov{
34050aadb33SDmitri Tikhonov    int ret = 0;
34150aadb33SDmitri Tikhonov    size_t max_out_len;
34250aadb33SDmitri Tikhonov    max_out_len = *cypher_len;//plain_len + EVP_AEAD_max_overhead(aead_);
34350aadb33SDmitri Tikhonov    assert(*cypher_len >= max_out_len);
34450aadb33SDmitri Tikhonov
345a5fa05f9SDmitri Tikhonov    LSQ_DEBUG("***lsquic_aes_aead_enc data %s", lsquic_get_bin_str(plain, plain_len, 40));
34650aadb33SDmitri Tikhonov    ret = EVP_AEAD_CTX_seal(key, cypher, cypher_len, max_out_len,
34750aadb33SDmitri Tikhonov                            nonce, nonce_len, plain, plain_len, ad, ad_len);
348a5fa05f9SDmitri Tikhonov//     LSQ_DEBUG("***lsquic_aes_aead_enc nonce: %s", lsquic_get_bin_str(nonce, nonce_len));
349a5fa05f9SDmitri Tikhonov//     LSQ_DEBUG("***lsquic_aes_aead_enc AD: %s", lsquic_get_bin_str(ad, ad_len));
350a5fa05f9SDmitri Tikhonov//     LSQ_DEBUG("***lsquic_aes_aead_enc return %d", (ret ? 0 : -1));
35150aadb33SDmitri Tikhonov    if (ret)
35250aadb33SDmitri Tikhonov    {
353a5fa05f9SDmitri Tikhonov        LSQ_DEBUG("***lsquic_aes_aead_enc succeed, cypher content %s",
354a5fa05f9SDmitri Tikhonov                  lsquic_get_bin_str(cypher, *cypher_len, 40));
35550aadb33SDmitri Tikhonov        return 0;
35650aadb33SDmitri Tikhonov    }
35750aadb33SDmitri Tikhonov    else
35850aadb33SDmitri Tikhonov    {
359a5fa05f9SDmitri Tikhonov        LSQ_DEBUG("***lsquic_aes_aead_enc failed.");
36050aadb33SDmitri Tikhonov        return -1;
36150aadb33SDmitri Tikhonov    }
36250aadb33SDmitri Tikhonov}
36350aadb33SDmitri Tikhonov
36450aadb33SDmitri Tikhonov
36550aadb33SDmitri Tikhonov/* return 0 for OK */
366a5fa05f9SDmitri Tikhonovint lsquic_aes_aead_dec(EVP_AEAD_CTX *key,
36750aadb33SDmitri Tikhonov              const uint8_t *ad, size_t ad_len,
36850aadb33SDmitri Tikhonov              const uint8_t *nonce, size_t nonce_len,
36950aadb33SDmitri Tikhonov              const uint8_t *cypher, size_t cypher_len,
37050aadb33SDmitri Tikhonov              uint8_t *plain, size_t *plain_len)
37150aadb33SDmitri Tikhonov{
37250aadb33SDmitri Tikhonov    int ret = 0;
37350aadb33SDmitri Tikhonov    size_t max_out_len = *plain_len;
37450aadb33SDmitri Tikhonov    assert(max_out_len >= cypher_len);
37550aadb33SDmitri Tikhonov
376a5fa05f9SDmitri Tikhonov    LSQ_DEBUG("***lsquic_aes_aead_dec data %s", lsquic_get_bin_str(cypher, cypher_len, 40));
37750aadb33SDmitri Tikhonov
37850aadb33SDmitri Tikhonov
37950aadb33SDmitri Tikhonov    ret = EVP_AEAD_CTX_open(key, plain, plain_len, max_out_len,
38050aadb33SDmitri Tikhonov                            nonce, nonce_len, cypher, cypher_len, ad, ad_len);
38150aadb33SDmitri Tikhonov
382a5fa05f9SDmitri Tikhonov//    LSQ_DEBUG("***lsquic_aes_aead_dec nonce: %s", lsquic_get_bin_str(nonce, nonce_len));
383a5fa05f9SDmitri Tikhonov//    LSQ_DEBUG("***lsquic_aes_aead_dec AD: %s", lsquic_get_bin_str(ad, ad_len));
384a5fa05f9SDmitri Tikhonov//    LSQ_DEBUG("***lsquic_aes_aead_dec return %d", (ret ? 0 : -1));
38550aadb33SDmitri Tikhonov    if (ret)
38650aadb33SDmitri Tikhonov    {
387a5fa05f9SDmitri Tikhonov        LSQ_DEBUG("***lsquic_aes_aead_dec succeed, plain content %s",
388a5fa05f9SDmitri Tikhonov              lsquic_get_bin_str(plain, *plain_len, 20));
38950aadb33SDmitri Tikhonov        return 0;
39050aadb33SDmitri Tikhonov    }
39150aadb33SDmitri Tikhonov    else
39250aadb33SDmitri Tikhonov    {
393a5fa05f9SDmitri Tikhonov        LSQ_DEBUG("***lsquic_aes_aead_dec failed.");
39450aadb33SDmitri Tikhonov        return -1;
39550aadb33SDmitri Tikhonov    }
39650aadb33SDmitri Tikhonov}
39750aadb33SDmitri Tikhonov
39850aadb33SDmitri Tikhonov/* 32 bytes client nonce with 4 bytes tm, 8 bytes orbit */
399a5fa05f9SDmitri Tikhonovvoid lsquic_gen_nonce_c(unsigned char *buf, uint64_t orbit)
40050aadb33SDmitri Tikhonov{
40150aadb33SDmitri Tikhonov    time_t tm = time(NULL);
40250aadb33SDmitri Tikhonov    unsigned char *p = buf;
40350aadb33SDmitri Tikhonov    memcpy(p, &tm, 4);
40450aadb33SDmitri Tikhonov    p += 4;
40550aadb33SDmitri Tikhonov    memcpy(p, &orbit, 8);
40650aadb33SDmitri Tikhonov    p += 8;
40710c41073SDmitri Tikhonov    RAND_bytes(p, 20);
40850aadb33SDmitri Tikhonov    p += 20;
40950aadb33SDmitri Tikhonov}
41050aadb33SDmitri Tikhonov
41150aadb33SDmitri Tikhonov
41250aadb33SDmitri Tikhonov/* type 0 DER, 1: PEM */
413a5fa05f9SDmitri TikhonovX509 *
414a5fa05f9SDmitri Tikhonovlsquic_bio_to_crt (const void *buf, int len, int type)
41550aadb33SDmitri Tikhonov{
41650aadb33SDmitri Tikhonov    X509 *crt = NULL;
41750aadb33SDmitri Tikhonov    BIO *bio = BIO_new_mem_buf(buf, len);
41850aadb33SDmitri Tikhonov    if (bio == NULL)
41950aadb33SDmitri Tikhonov        return NULL;
42050aadb33SDmitri Tikhonov
42150aadb33SDmitri Tikhonov    if (type == 0)
42250aadb33SDmitri Tikhonov        crt = d2i_X509_bio(bio, NULL);
42350aadb33SDmitri Tikhonov    else
42450aadb33SDmitri Tikhonov        crt = PEM_read_bio_X509(bio, &crt, 0 , NULL);
42550aadb33SDmitri Tikhonov    BIO_free(bio);
42650aadb33SDmitri Tikhonov    return crt;
42750aadb33SDmitri Tikhonov}
42850aadb33SDmitri Tikhonov
42950aadb33SDmitri Tikhonov
430a5fa05f9SDmitri Tikhonovint
431a5fa05f9SDmitri Tikhonovlsquic_gen_prof (const uint8_t *chlo_data, size_t chlo_data_len,
43250aadb33SDmitri Tikhonov             const uint8_t *scfg_data, uint32_t scfg_data_len,
43350aadb33SDmitri Tikhonov             const EVP_PKEY *priv_key, uint8_t *buf, size_t *buf_len)
43450aadb33SDmitri Tikhonov{
43550aadb33SDmitri Tikhonov    uint8_t chlo_hash[32] = {0};
43650aadb33SDmitri Tikhonov    size_t chlo_hash_len = 32; /* SHA256 */
43750aadb33SDmitri Tikhonov    EVP_MD_CTX sign_context;
43850aadb33SDmitri Tikhonov    EVP_PKEY_CTX* pkey_ctx = NULL;
43950aadb33SDmitri Tikhonov
44050aadb33SDmitri Tikhonov    sha256(chlo_data, chlo_data_len, chlo_hash);
44150aadb33SDmitri Tikhonov    EVP_MD_CTX_init(&sign_context);
44250aadb33SDmitri Tikhonov    if (!EVP_DigestSignInit(&sign_context, &pkey_ctx, EVP_sha256(), NULL, (EVP_PKEY *)priv_key))
44350aadb33SDmitri Tikhonov        return -1;
44450aadb33SDmitri Tikhonov
44550aadb33SDmitri Tikhonov    EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING);
44650aadb33SDmitri Tikhonov    EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1);
44750aadb33SDmitri Tikhonov
44850aadb33SDmitri Tikhonov    if (!EVP_DigestSignUpdate(&sign_context, s_hs_signature, sizeof(s_hs_signature)) ||
44950aadb33SDmitri Tikhonov        !EVP_DigestSignUpdate(&sign_context, (const uint8_t*)(&chlo_hash_len), 4) ||
45050aadb33SDmitri Tikhonov        !EVP_DigestSignUpdate(&sign_context, chlo_hash, chlo_hash_len) ||
45150aadb33SDmitri Tikhonov        !EVP_DigestSignUpdate(&sign_context, scfg_data, scfg_data_len))
45250aadb33SDmitri Tikhonov    {
45350aadb33SDmitri Tikhonov        return -1;
45450aadb33SDmitri Tikhonov    }
45550aadb33SDmitri Tikhonov
45650aadb33SDmitri Tikhonov    size_t len = 0;
45750aadb33SDmitri Tikhonov    if (!EVP_DigestSignFinal(&sign_context, NULL, &len)) {
45850aadb33SDmitri Tikhonov        return -1;
45950aadb33SDmitri Tikhonov    }
46050aadb33SDmitri Tikhonov
46150aadb33SDmitri Tikhonov    if (len > *buf_len)
46250aadb33SDmitri Tikhonov        return -2;
46350aadb33SDmitri Tikhonov    if (buf)
46450aadb33SDmitri Tikhonov        EVP_DigestSignFinal(&sign_context, buf, buf_len);
46550aadb33SDmitri Tikhonov
46650aadb33SDmitri Tikhonov    EVP_MD_CTX_cleanup(&sign_context);
46750aadb33SDmitri Tikhonov    return 0;
46850aadb33SDmitri Tikhonov}
46950aadb33SDmitri Tikhonov
47050aadb33SDmitri Tikhonov
47150aadb33SDmitri Tikhonov/* -3 internal error, -1: verify failed, 0: Success */
472a5fa05f9SDmitri Tikhonovstatic int
473a5fa05f9SDmitri Tikhonovverify_prof0 (const uint8_t *chlo_data, size_t chlo_data_len,
47450aadb33SDmitri Tikhonov                const uint8_t *scfg_data, uint32_t scfg_data_len,
47550aadb33SDmitri Tikhonov                const EVP_PKEY *pub_key, const uint8_t *buf, size_t len)
47650aadb33SDmitri Tikhonov{
47750aadb33SDmitri Tikhonov    uint8_t chlo_hash[32] = {0};
47850aadb33SDmitri Tikhonov    size_t chlo_hash_len = 32; /* SHA256 */
47950aadb33SDmitri Tikhonov    EVP_MD_CTX sign_context;
48050aadb33SDmitri Tikhonov    EVP_PKEY_CTX* pkey_ctx = NULL;
48150aadb33SDmitri Tikhonov    int ret = 0;
48250aadb33SDmitri Tikhonov    EVP_MD_CTX_init(&sign_context);
48350aadb33SDmitri Tikhonov    sha256(chlo_data, chlo_data_len, chlo_hash);
48450aadb33SDmitri Tikhonov
48550aadb33SDmitri Tikhonov    // discarding const below to quiet compiler warning on call to ssl library code
48650aadb33SDmitri Tikhonov    if (!EVP_DigestVerifyInit(&sign_context, &pkey_ctx, EVP_sha256(), NULL, (EVP_PKEY *)pub_key))
48750aadb33SDmitri Tikhonov        return -4;
48850aadb33SDmitri Tikhonov
48950aadb33SDmitri Tikhonov    EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING);
49050aadb33SDmitri Tikhonov    EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1);
49150aadb33SDmitri Tikhonov
49250aadb33SDmitri Tikhonov
49350aadb33SDmitri Tikhonov    if (!EVP_DigestVerifyUpdate(&sign_context, s_hs_signature, sizeof(s_hs_signature)) ||
49450aadb33SDmitri Tikhonov        !EVP_DigestVerifyUpdate(&sign_context, (const uint8_t*)(&chlo_hash_len), 4) ||
49550aadb33SDmitri Tikhonov        !EVP_DigestVerifyUpdate(&sign_context, chlo_hash, chlo_hash_len) ||
49650aadb33SDmitri Tikhonov        !EVP_DigestVerifyUpdate(&sign_context, scfg_data, scfg_data_len))
49750aadb33SDmitri Tikhonov    {
49850aadb33SDmitri Tikhonov        return -3;  /* set to -3, to avoid same as "not enough data" -2 */
49950aadb33SDmitri Tikhonov    }
50050aadb33SDmitri Tikhonov
50150aadb33SDmitri Tikhonov    ret = EVP_DigestVerifyFinal(&sign_context, buf, len);
50250aadb33SDmitri Tikhonov    EVP_MD_CTX_cleanup(&sign_context);
50350aadb33SDmitri Tikhonov
50450aadb33SDmitri Tikhonov    if (ret == 1)
50550aadb33SDmitri Tikhonov        return 0; //OK
50650aadb33SDmitri Tikhonov    else
50750aadb33SDmitri Tikhonov        return -1;  //failed
50850aadb33SDmitri Tikhonov}
50950aadb33SDmitri Tikhonov
51050aadb33SDmitri Tikhonov
511a5fa05f9SDmitri Tikhonovint
512a5fa05f9SDmitri Tikhonovlsquic_verify_prof (const uint8_t *chlo_data, size_t chlo_data_len,
513a5fa05f9SDmitri Tikhonov    lsquic_str_t *scfg, const EVP_PKEY *pub_key, const uint8_t *buf, size_t len)
514a5fa05f9SDmitri Tikhonov{
515a5fa05f9SDmitri Tikhonov    return verify_prof0(chlo_data, chlo_data_len,
516a5fa05f9SDmitri Tikhonov                        (const uint8_t *)lsquic_str_buf(scfg),
517a5fa05f9SDmitri Tikhonov                        lsquic_str_len(scfg), pub_key, buf, len);
518a5fa05f9SDmitri Tikhonov}
519a5fa05f9SDmitri Tikhonov
520a5fa05f9SDmitri Tikhonov
521a5fa05f9SDmitri Tikhonovvoid
522a5fa05f9SDmitri Tikhonovlsquic_crypto_init (void)
52350aadb33SDmitri Tikhonov{
52450aadb33SDmitri Tikhonov    if (crypto_inited)
52550aadb33SDmitri Tikhonov        return ;
52650aadb33SDmitri Tikhonov
52750aadb33SDmitri Tikhonov    //SSL_library_init();
52850aadb33SDmitri Tikhonov    CRYPTO_library_init();
52967b0dc15SDmitri Tikhonov    /* XXX Should we seed? If yes, wherewith? */ // RAND_seed(seed, seed_len);
53050aadb33SDmitri Tikhonov
53150aadb33SDmitri Tikhonov#if defined( __x86_64 )||defined( __x86_64__ )
53250aadb33SDmitri Tikhonov    make_uint128(&s_prime, 16777216, 315);
53350aadb33SDmitri Tikhonov    make_uint128(&s_init_hash, 7809847782465536322, 7113472399480571277);
53450aadb33SDmitri Tikhonov#endif
53550aadb33SDmitri Tikhonov
53650aadb33SDmitri Tikhonov    /* MORE .... */
53750aadb33SDmitri Tikhonov    crypto_inited = 1;
53850aadb33SDmitri Tikhonov}
53950aadb33SDmitri Tikhonov
540