Path: blob/main/crypto/openssl/ssl/quic/quic_srt_gen.c
48261 views
/*1* Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/8#include "internal/quic_srt_gen.h"9#include <openssl/core_names.h>10#include <openssl/evp.h>1112struct quic_srt_gen_st {13EVP_MAC *mac;14EVP_MAC_CTX *mac_ctx;15};1617/*18* Simple HMAC-SHA256-based stateless reset token generator.19*/2021QUIC_SRT_GEN *ossl_quic_srt_gen_new(OSSL_LIB_CTX *libctx, const char *propq,22const unsigned char *key, size_t key_len)23{24QUIC_SRT_GEN *srt_gen;25OSSL_PARAM params[3], *p = params;2627if ((srt_gen = OPENSSL_zalloc(sizeof(*srt_gen))) == NULL)28return NULL;2930if ((srt_gen->mac = EVP_MAC_fetch(libctx, "HMAC", propq)) == NULL)31goto err;3233if ((srt_gen->mac_ctx = EVP_MAC_CTX_new(srt_gen->mac)) == NULL)34goto err;3536*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, "SHA256", 7);37if (propq != NULL)38*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,39(char *)propq, 0);40*p++ = OSSL_PARAM_construct_end();4142if (!EVP_MAC_init(srt_gen->mac_ctx, key, key_len, params))43goto err;4445return srt_gen;4647err:48ossl_quic_srt_gen_free(srt_gen);49return NULL;50}5152void ossl_quic_srt_gen_free(QUIC_SRT_GEN *srt_gen)53{54if (srt_gen == NULL)55return;5657EVP_MAC_CTX_free(srt_gen->mac_ctx);58EVP_MAC_free(srt_gen->mac);59OPENSSL_free(srt_gen);60}6162int ossl_quic_srt_gen_calculate_token(QUIC_SRT_GEN *srt_gen,63const QUIC_CONN_ID *dcid,64QUIC_STATELESS_RESET_TOKEN *token)65{66size_t outl = 0;67unsigned char mac[SHA256_DIGEST_LENGTH];6869if (!EVP_MAC_init(srt_gen->mac_ctx, NULL, 0, NULL))70return 0;7172if (!EVP_MAC_update(srt_gen->mac_ctx, (const unsigned char *)dcid->id,73dcid->id_len))74return 0;7576if (!EVP_MAC_final(srt_gen->mac_ctx, mac, &outl, sizeof(mac))77|| outl != sizeof(mac))78return 0;7980assert(sizeof(mac) >= sizeof(token->token));81memcpy(token->token, mac, sizeof(token->token));82return 1;83}848586