Some CBB_init_fixed simplifications.

CBB_init_fixed callers no longer need to check the return value, or
handle any cleanup. The hpke.c instance was even already (incorrectly at
the time) assuming this.

Change-Id: I2f4cb124454fc7ba7ff6d2075d99f537a58c6c6b
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/54647
Reviewed-by: Bob Beck <bbe@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
fips-20230428
David Benjamin 3 years ago committed by Boringssl LUCI CQ
parent 15ba28f839
commit 254b8e1139
  1. 6
      crypto/ecdsa_extra/ecdsa_asn1.c
  2. 16
      crypto/hpke/hpke.c
  3. 44
      crypto/trust_token/trust_token.c

@ -81,13 +81,11 @@ int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
}
CBB cbb;
CBB_zero(&cbb);
CBB_init_fixed(&cbb, sig, ECDSA_size(eckey));
size_t len;
if (!CBB_init_fixed(&cbb, sig, ECDSA_size(eckey)) ||
!ECDSA_SIG_marshal(&cbb, s) ||
if (!ECDSA_SIG_marshal(&cbb, s) ||
!CBB_finish(&cbb, NULL, &len)) {
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR);
CBB_cleanup(&cbb);
*sig_len = 0;
goto err;
}

@ -366,13 +366,11 @@ const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead) {
static int hpke_build_suite_id(const EVP_HPKE_CTX *ctx,
uint8_t out[HPKE_SUITE_ID_LEN]) {
CBB cbb;
int ret = CBB_init_fixed(&cbb, out, HPKE_SUITE_ID_LEN) &&
add_label_string(&cbb, "HPKE") && //
CBB_add_u16(&cbb, ctx->kem->id) && //
CBB_add_u16(&cbb, ctx->kdf->id) && //
CBB_add_u16(&cbb, ctx->aead->id);
CBB_cleanup(&cbb);
return ret;
CBB_init_fixed(&cbb, out, HPKE_SUITE_ID_LEN);
return add_label_string(&cbb, "HPKE") && //
CBB_add_u16(&cbb, ctx->kem->id) && //
CBB_add_u16(&cbb, ctx->kdf->id) && //
CBB_add_u16(&cbb, ctx->aead->id);
}
#define HPKE_MODE_BASE 0
@ -409,8 +407,8 @@ static int hpke_key_schedule(EVP_HPKE_CTX *ctx, const uint8_t *shared_secret,
uint8_t context[sizeof(uint8_t) + 2 * EVP_MAX_MD_SIZE];
size_t context_len;
CBB context_cbb;
if (!CBB_init_fixed(&context_cbb, context, sizeof(context)) ||
!CBB_add_u8(&context_cbb, HPKE_MODE_BASE) ||
CBB_init_fixed(&context_cbb, context, sizeof(context));
if (!CBB_add_u8(&context_cbb, HPKE_MODE_BASE) ||
!CBB_add_bytes(&context_cbb, psk_id_hash, psk_id_hash_len) ||
!CBB_add_bytes(&context_cbb, info_hash, info_hash_len) ||
!CBB_finish(&context_cbb, NULL, &context_len)) {

@ -113,34 +113,26 @@ int TRUST_TOKEN_generate_key(const TRUST_TOKEN_METHOD *method,
size_t *out_pub_key_len, size_t max_pub_key_len,
uint32_t id) {
// Prepend the key ID in front of the PMBTokens format.
int ret = 0;
CBB priv_cbb, pub_cbb;
CBB_zero(&priv_cbb);
CBB_zero(&pub_cbb);
if (!CBB_init_fixed(&priv_cbb, out_priv_key, max_priv_key_len) ||
!CBB_init_fixed(&pub_cbb, out_pub_key, max_pub_key_len) ||
!CBB_add_u32(&priv_cbb, id) ||
CBB_init_fixed(&priv_cbb, out_priv_key, max_priv_key_len);
CBB_init_fixed(&pub_cbb, out_pub_key, max_pub_key_len);
if (!CBB_add_u32(&priv_cbb, id) || //
!CBB_add_u32(&pub_cbb, id)) {
OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL);
goto err;
return 0;
}
if (!method->generate_key(&priv_cbb, &pub_cbb)) {
goto err;
return 0;
}
if (!CBB_finish(&priv_cbb, NULL, out_priv_key_len) ||
!CBB_finish(&pub_cbb, NULL, out_pub_key_len)) {
OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL);
goto err;
return 0;
}
ret = 1;
err:
CBB_cleanup(&priv_cbb);
CBB_cleanup(&pub_cbb);
return ret;
return 1;
}
int TRUST_TOKEN_derive_key_from_secret(
@ -149,35 +141,27 @@ int TRUST_TOKEN_derive_key_from_secret(
size_t *out_pub_key_len, size_t max_pub_key_len, uint32_t id,
const uint8_t *secret, size_t secret_len) {
// Prepend the key ID in front of the PMBTokens format.
int ret = 0;
CBB priv_cbb, pub_cbb;
CBB_zero(&priv_cbb);
CBB_zero(&pub_cbb);
if (!CBB_init_fixed(&priv_cbb, out_priv_key, max_priv_key_len) ||
!CBB_init_fixed(&pub_cbb, out_pub_key, max_pub_key_len) ||
!CBB_add_u32(&priv_cbb, id) ||
CBB_init_fixed(&priv_cbb, out_priv_key, max_priv_key_len);
CBB_init_fixed(&pub_cbb, out_pub_key, max_pub_key_len);
if (!CBB_add_u32(&priv_cbb, id) || //
!CBB_add_u32(&pub_cbb, id)) {
OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL);
goto err;
return 0;
}
if (!method->derive_key_from_secret(&priv_cbb, &pub_cbb, secret,
secret_len)) {
goto err;
return 0;
}
if (!CBB_finish(&priv_cbb, NULL, out_priv_key_len) ||
!CBB_finish(&pub_cbb, NULL, out_pub_key_len)) {
OPENSSL_PUT_ERROR(TRUST_TOKEN, TRUST_TOKEN_R_BUFFER_TOO_SMALL);
goto err;
return 0;
}
ret = 1;
err:
CBB_cleanup(&priv_cbb);
CBB_cleanup(&pub_cbb);
return ret;
return 1;
}
TRUST_TOKEN_CLIENT *TRUST_TOKEN_CLIENT_new(const TRUST_TOKEN_METHOD *method,

Loading…
Cancel
Save