Added TALER_age_restriction_commitment_free_inside

cleanup function for ->pub and ->priv (and zeroing keys)
This commit is contained in:
Özgür Kesim 2022-02-05 23:51:09 +01:00
parent 7b50b2d17c
commit d42394de9b
Signed by: oec
GPG Key ID: 3D76A56D79EDD9D7
2 changed files with 52 additions and 21 deletions

View File

@ -2656,14 +2656,13 @@ TALER_age_commitment_hash (
const struct TALER_AgeCommitment *commitment,
struct TALER_AgeCommitmentHash *hash);
/*
* @brief Generates an age commitent for the given age.
*
* @param mask The age mask the defines the age groups
* @param age The actual age for which an age commitment is generated
* @param seed The seed that goes into the key generation. MUST be choosen uniformly random.
* @param commitment[out] The generated age commitment, allocated via GNUNET_malloc on success
* @param commitment[out] The generated age commitment, ->priv and ->pub allocated via GNUNET_malloc on success
* @return GNUNET_OK on success, GNUNET_SYSERR otherwise
*/
enum GNUNET_GenericReturnValue
@ -2671,21 +2670,29 @@ TALER_age_restriction_commit (
const struct TALER_AgeMask *mask,
const uint8_t age,
const uint32_t seed,
struct TALER_AgeCommitment **commitment);
struct TALER_AgeCommitment *commitment);
/*
* @brief Derives another, equivalent age commitment for a given one.
*
* @param orig Original age commitment
* @param seed Used to move the points on the elliptic curve in order to generate another, equivalent commitment.
* @param derived[out] The resulting age commitment, allocated via GNUNET_malloc on success.
* @param derived[out] The resulting age commitment, ->priv and ->pub allocated via GNUNET_malloc on success.
* @return GNUNET_OK on success, GNUNET_SYSERR otherwise
*/
enum GNUNET_GenericReturnValue
TALER_age_restriction_derive (
const struct TALER_AgeCommitment *orig,
const uint32_t seed,
struct TALER_AgeCommitment **derived);
struct TALER_AgeCommitment *derived);
/*
* @brief helper function to free memory inside a struct TALER_AgeCommitment
* @param cmt the commitment from which internal memory should be freed. Note
* that cmt itself is NOT freed!
*/
void
TALER_age_restriction_commitment_free_inside (
struct TALER_AgeCommitment *cmt);
#endif

View File

@ -406,8 +406,10 @@ enum GNUNET_GenericReturnValue
TALER_age_restriction_derive (
const struct TALER_AgeCommitment *orig,
const uint32_t seed,
struct TALER_AgeCommitment **derived)
struct TALER_AgeCommitment *new)
{
struct GNUNET_CRYPTO_EccScalar val;
/*
* age commitment consists of GNUNET_CRYPTO_Eddsa{Private,Public}Key
*
@ -434,15 +436,11 @@ TALER_age_restriction_derive (
* GNUNET_CRYPTO_EccScalar which is a
* unsigned car v[256 / 8];
* */
struct GNUNET_CRYPTO_EccScalar val;
struct TALER_AgeCommitment *new;
GNUNET_assert (orig->num_pub == __builtin_popcount (orig->mask.mask) -1);
GNUNET_assert (NULL != new);
GNUNET_assert (orig->num_pub == __builtin_popcount (orig->mask.mask) - 1);
GNUNET_assert (orig->num_priv <= orig->num_pub);
*derived = NULL;
new = GNUNET_malloc (sizeof(struct TALER_AgeCommitment));
new->mask = orig->mask;
new->num_pub = orig->num_pub;
new->num_priv = orig->num_priv;
@ -519,13 +517,11 @@ TALER_age_restriction_derive (
gcry_ctx_release (ctx);
}
*derived = new;
return GNUNET_OK;
FAIL:
GNUNET_free (new->pub);
GNUNET_free (new->priv);
GNUNET_free (new);
return GNUNET_SYSERR;
}
@ -558,24 +554,27 @@ TALER_age_restriction_commit (
const struct TALER_AgeMask *mask,
const uint8_t age,
const uint32_t seed,
struct TALER_AgeCommitment **commitment)
struct TALER_AgeCommitment *new)
{
struct TALER_AgeCommitment *new;
uint8_t num_pub = __builtin_popcount (mask->mask) - 1;
uint8_t num_priv = get_age_group (mask, age) - 1;
size_t i;
GNUNET_assert (NULL != new);
GNUNET_assert (mask->mask & 1); /* fist bit must have been set */
GNUNET_assert (0 <= num_priv);
GNUNET_assert (31 > num_priv);
new = GNUNET_malloc (sizeof(struct TALER_AgeCommitment));
new->mask.mask = mask->mask;
new->num_pub = num_pub;
new->num_priv = num_priv;
new->pub = GNUNET_new_array (num_pub, struct TALER_AgeCommitmentPublicKeyP);
new->priv = GNUNET_new_array (num_priv, struct TALER_AgeCommitmentPrivateKeyP);
new->pub = GNUNET_new_array (
num_pub,
struct TALER_AgeCommitmentPublicKeyP);
new->priv = GNUNET_new_array (
num_priv,
struct TALER_AgeCommitmentPrivateKeyP);
/* Create as many private keys as we need */
for (i = 0; i < num_priv; i++)
@ -603,15 +602,40 @@ TALER_age_restriction_commit (
&new->pub[i],
sizeof(new->pub[i]));
*commitment = new;
return GNUNET_OK;
FAIL:
GNUNET_free (new->pub);
GNUNET_free (new->priv);
GNUNET_free (new);
return GNUNET_SYSERR;
}
void
TALER_age_restriction_commmitment_free_inside (
struct TALER_AgeCommitment *commitment)
{
if (NULL == commitment)
return;
if (NULL != commitment->priv)
{
GNUNET_CRYPTO_zero_keys (
commitment->priv,
sizeof(*commitment->priv) * commitment->num_priv);
GNUNET_free (commitment->priv);
commitment->priv = NULL;
}
if (NULL != commitment->pub)
{
GNUNET_free (commitment->pub);
commitment->priv = NULL;
}
/* Caller is responsible for commitment itself */
}
/* end of crypto.c */