Skip to content

Commit 1592639

Browse files
Merge pull request #10182 from gilles-peskine-arm/f_rng-documentation-3.6
3.6 only: document f_rng callbacks
2 parents 23a0d48 + addf8fc commit 1592639

File tree

15 files changed

+132
-72
lines changed

15 files changed

+132
-72
lines changed

include/mbedtls/bignum.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "mbedtls/private_access.h"
1313

1414
#include "mbedtls/build_info.h"
15+
#include "mbedtls/platform_util.h"
1516

1617
#include <stddef.h>
1718
#include <stdint.h>
@@ -928,7 +929,7 @@ int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
928929
* be relevant in applications like deterministic ECDSA.
929930
*/
930931
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
931-
int (*f_rng)(void *, unsigned char *, size_t),
932+
mbedtls_f_rng_t *f_rng,
932933
void *p_rng);
933934

934935
/** Generate a random number uniformly in a range.
@@ -966,7 +967,7 @@ int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
966967
int mbedtls_mpi_random(mbedtls_mpi *X,
967968
mbedtls_mpi_sint min,
968969
const mbedtls_mpi *N,
969-
int (*f_rng)(void *, unsigned char *, size_t),
970+
mbedtls_f_rng_t *f_rng,
970971
void *p_rng);
971972

972973
/**
@@ -1030,7 +1031,7 @@ int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
10301031
* \return Another negative error code on other kinds of failure.
10311032
*/
10321033
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
1033-
int (*f_rng)(void *, unsigned char *, size_t),
1034+
mbedtls_f_rng_t *f_rng,
10341035
void *p_rng);
10351036
/**
10361037
* \brief Flags for mbedtls_mpi_gen_prime()
@@ -1063,7 +1064,7 @@ typedef enum {
10631064
* \c 3 and #MBEDTLS_MPI_MAX_BITS.
10641065
*/
10651066
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
1066-
int (*f_rng)(void *, unsigned char *, size_t),
1067+
mbedtls_f_rng_t *f_rng,
10671068
void *p_rng);
10681069

10691070
#if defined(MBEDTLS_SELF_TEST)

include/mbedtls/dhm.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ int mbedtls_dhm_read_params(mbedtls_dhm_context *ctx,
183183
*/
184184
int mbedtls_dhm_make_params(mbedtls_dhm_context *ctx, int x_size,
185185
unsigned char *output, size_t *olen,
186-
int (*f_rng)(void *, unsigned char *, size_t),
186+
mbedtls_f_rng_t *f_rng,
187187
void *p_rng);
188188

189189
/**
@@ -250,7 +250,7 @@ int mbedtls_dhm_read_public(mbedtls_dhm_context *ctx,
250250
*/
251251
int mbedtls_dhm_make_public(mbedtls_dhm_context *ctx, int x_size,
252252
unsigned char *output, size_t olen,
253-
int (*f_rng)(void *, unsigned char *, size_t),
253+
mbedtls_f_rng_t *f_rng,
254254
void *p_rng);
255255

256256
/**
@@ -281,7 +281,7 @@ int mbedtls_dhm_make_public(mbedtls_dhm_context *ctx, int x_size,
281281
*/
282282
int mbedtls_dhm_calc_secret(mbedtls_dhm_context *ctx,
283283
unsigned char *output, size_t output_size, size_t *olen,
284-
int (*f_rng)(void *, unsigned char *, size_t),
284+
mbedtls_f_rng_t *f_rng,
285285
void *p_rng);
286286

287287
/**

include/mbedtls/ecdh.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid);
189189
* \c MBEDTLS_MPI_XXX error code on failure.
190190
*/
191191
int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
192-
int (*f_rng)(void *, unsigned char *, size_t),
192+
mbedtls_f_rng_t *f_rng,
193193
void *p_rng);
194194

195195
/**
@@ -225,7 +225,7 @@ int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_
225225
*/
226226
int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,
227227
const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
228-
int (*f_rng)(void *, unsigned char *, size_t),
228+
mbedtls_f_rng_t *f_rng,
229229
void *p_rng);
230230

231231
/**
@@ -290,7 +290,7 @@ void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx);
290290
*/
291291
int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
292292
unsigned char *buf, size_t blen,
293-
int (*f_rng)(void *, unsigned char *, size_t),
293+
mbedtls_f_rng_t *f_rng,
294294
void *p_rng);
295295

296296
/**
@@ -372,7 +372,7 @@ int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
372372
*/
373373
int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
374374
unsigned char *buf, size_t blen,
375-
int (*f_rng)(void *, unsigned char *, size_t),
375+
mbedtls_f_rng_t *f_rng,
376376
void *p_rng);
377377

378378
/**
@@ -428,7 +428,7 @@ int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
428428
*/
429429
int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
430430
unsigned char *buf, size_t blen,
431-
int (*f_rng)(void *, unsigned char *, size_t),
431+
mbedtls_f_rng_t *f_rng,
432432
void *p_rng);
433433

434434
#if defined(MBEDTLS_ECP_RESTARTABLE)

include/mbedtls/ecdsa.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid);
150150
* buffer of length \p blen Bytes. It may be \c NULL if
151151
* \p blen is zero.
152152
* \param blen The length of \p buf in Bytes.
153-
* \param f_rng The RNG function. This must not be \c NULL.
153+
* \param f_rng The RNG function, used both to generate the ECDSA nonce
154+
* and for blinding. This must not be \c NULL.
154155
* \param p_rng The RNG context to be passed to \p f_rng. This may be
155156
* \c NULL if \p f_rng doesn't need a context parameter.
156157
*
@@ -160,7 +161,7 @@ int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid);
160161
*/
161162
int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
162163
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
163-
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
164+
mbedtls_f_rng_t *f_rng, void *p_rng);
164165

165166
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
166167
/**
@@ -207,7 +208,7 @@ int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
207208
mbedtls_mpi *s, const mbedtls_mpi *d,
208209
const unsigned char *buf, size_t blen,
209210
mbedtls_md_type_t md_alg,
210-
int (*f_rng_blind)(void *, unsigned char *, size_t),
211+
mbedtls_f_rng_t *f_rng_blind,
211212
void *p_rng_blind);
212213
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
213214

@@ -247,7 +248,8 @@ int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
247248
* buffer of length \p blen Bytes. It may be \c NULL if
248249
* \p blen is zero.
249250
* \param blen The length of \p buf in Bytes.
250-
* \param f_rng The RNG function. This must not be \c NULL.
251+
* \param f_rng The RNG function used to generate the ECDSA nonce.
252+
* This must not be \c NULL.
251253
* \param p_rng The RNG context to be passed to \p f_rng. This may be
252254
* \c NULL if \p f_rng doesn't need a context parameter.
253255
* \param f_rng_blind The RNG function used for blinding. This must not be
@@ -271,9 +273,9 @@ int mbedtls_ecdsa_sign_restartable(
271273
mbedtls_mpi *r, mbedtls_mpi *s,
272274
const mbedtls_mpi *d,
273275
const unsigned char *buf, size_t blen,
274-
int (*f_rng)(void *, unsigned char *, size_t),
276+
mbedtls_f_rng_t *f_rng,
275277
void *p_rng,
276-
int (*f_rng_blind)(void *, unsigned char *, size_t),
278+
mbedtls_f_rng_t *f_rng_blind,
277279
void *p_rng_blind,
278280
mbedtls_ecdsa_restart_ctx *rs_ctx);
279281

@@ -334,7 +336,7 @@ int mbedtls_ecdsa_sign_det_restartable(
334336
mbedtls_mpi *r, mbedtls_mpi *s,
335337
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
336338
mbedtls_md_type_t md_alg,
337-
int (*f_rng_blind)(void *, unsigned char *, size_t),
339+
mbedtls_f_rng_t *f_rng_blind,
338340
void *p_rng_blind,
339341
mbedtls_ecdsa_restart_ctx *rs_ctx);
340342

@@ -458,10 +460,10 @@ int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
458460
* \param sig_size The size of the \p sig buffer in bytes.
459461
* \param slen The address at which to store the actual length of
460462
* the signature written. Must not be \c NULL.
461-
* \param f_rng The RNG function. This must not be \c NULL if
462-
* #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
463-
* it is used only for blinding and may be set to \c NULL, but
464-
* doing so is DEPRECATED.
463+
* \param f_rng The RNG function. This is used for blinding.
464+
* If #MBEDTLS_ECDSA_DETERMINISTIC is unset, this is also
465+
* used to generate the ECDSA nonce.
466+
* This must not be \c NULL.
465467
* \param p_rng The RNG context to be passed to \p f_rng. This may be
466468
* \c NULL if \p f_rng is \c NULL or doesn't use a context.
467469
*
@@ -473,7 +475,7 @@ int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
473475
mbedtls_md_type_t md_alg,
474476
const unsigned char *hash, size_t hlen,
475477
unsigned char *sig, size_t sig_size, size_t *slen,
476-
int (*f_rng)(void *, unsigned char *, size_t),
478+
mbedtls_f_rng_t *f_rng,
477479
void *p_rng);
478480

479481
/**
@@ -501,9 +503,10 @@ int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
501503
* \param sig_size The size of the \p sig buffer in bytes.
502504
* \param slen The address at which to store the actual length of
503505
* the signature written. Must not be \c NULL.
504-
* \param f_rng The RNG function. This must not be \c NULL if
505-
* #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
506-
* it is unused and may be set to \c NULL.
506+
* \param f_rng The RNG function. This is used for blinding.
507+
* If #MBEDTLS_ECDSA_DETERMINISTIC is unset, this is also
508+
* used to generate the ECDSA nonce.
509+
* This must not be \c NULL.
507510
* \param p_rng The RNG context to be passed to \p f_rng. This may be
508511
* \c NULL if \p f_rng is \c NULL or doesn't use a context.
509512
* \param rs_ctx The restart context to use. This may be \c NULL to disable
@@ -520,7 +523,7 @@ int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
520523
mbedtls_md_type_t md_alg,
521524
const unsigned char *hash, size_t hlen,
522525
unsigned char *sig, size_t sig_size, size_t *slen,
523-
int (*f_rng)(void *, unsigned char *, size_t),
526+
mbedtls_f_rng_t *f_rng,
524527
void *p_rng,
525528
mbedtls_ecdsa_restart_ctx *rs_ctx);
526529

@@ -608,7 +611,7 @@ int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
608611
* \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
609612
*/
610613
int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
611-
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
614+
mbedtls_f_rng_t *f_rng, void *p_rng);
612615

613616
/**
614617
* \brief This function sets up an ECDSA context from an EC key pair.

include/mbedtls/ecjpake.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ int mbedtls_ecjpake_check(const mbedtls_ecjpake_context *ctx);
162162
*/
163163
int mbedtls_ecjpake_write_round_one(mbedtls_ecjpake_context *ctx,
164164
unsigned char *buf, size_t len, size_t *olen,
165-
int (*f_rng)(void *, unsigned char *, size_t),
165+
mbedtls_f_rng_t *f_rng,
166166
void *p_rng);
167167

168168
/**
@@ -203,7 +203,7 @@ int mbedtls_ecjpake_read_round_one(mbedtls_ecjpake_context *ctx,
203203
*/
204204
int mbedtls_ecjpake_write_round_two(mbedtls_ecjpake_context *ctx,
205205
unsigned char *buf, size_t len, size_t *olen,
206-
int (*f_rng)(void *, unsigned char *, size_t),
206+
mbedtls_f_rng_t *f_rng,
207207
void *p_rng);
208208

209209
/**
@@ -243,7 +243,7 @@ int mbedtls_ecjpake_read_round_two(mbedtls_ecjpake_context *ctx,
243243
*/
244244
int mbedtls_ecjpake_derive_secret(mbedtls_ecjpake_context *ctx,
245245
unsigned char *buf, size_t len, size_t *olen,
246-
int (*f_rng)(void *, unsigned char *, size_t),
246+
mbedtls_f_rng_t *f_rng,
247247
void *p_rng);
248248

249249
/**
@@ -266,7 +266,7 @@ int mbedtls_ecjpake_derive_secret(mbedtls_ecjpake_context *ctx,
266266
*/
267267
int mbedtls_ecjpake_write_shared_key(mbedtls_ecjpake_context *ctx,
268268
unsigned char *buf, size_t len, size_t *olen,
269-
int (*f_rng)(void *, unsigned char *, size_t),
269+
mbedtls_f_rng_t *f_rng,
270270
void *p_rng);
271271

272272
/**

include/mbedtls/ecp.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp,
966966
*/
967967
int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
968968
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
969-
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
969+
mbedtls_f_rng_t *f_rng, void *p_rng);
970970

971971
/**
972972
* \brief This function performs multiplication of a point by
@@ -1000,7 +1000,7 @@ int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
10001000
*/
10011001
int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
10021002
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1003-
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1003+
mbedtls_f_rng_t *f_rng, void *p_rng,
10041004
mbedtls_ecp_restart_ctx *rs_ctx);
10051005

10061006
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
@@ -1179,7 +1179,7 @@ int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
11791179
*/
11801180
int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
11811181
mbedtls_mpi *d,
1182-
int (*f_rng)(void *, unsigned char *, size_t),
1182+
mbedtls_f_rng_t *f_rng,
11831183
void *p_rng);
11841184

11851185
/**
@@ -1212,7 +1212,7 @@ int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
12121212
int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
12131213
const mbedtls_ecp_point *G,
12141214
mbedtls_mpi *d, mbedtls_ecp_point *Q,
1215-
int (*f_rng)(void *, unsigned char *, size_t),
1215+
mbedtls_f_rng_t *f_rng,
12161216
void *p_rng);
12171217

12181218
/**
@@ -1240,7 +1240,7 @@ int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
12401240
*/
12411241
int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d,
12421242
mbedtls_ecp_point *Q,
1243-
int (*f_rng)(void *, unsigned char *, size_t),
1243+
mbedtls_f_rng_t *f_rng,
12441244
void *p_rng);
12451245

12461246
/**
@@ -1257,7 +1257,7 @@ int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d,
12571257
* on failure.
12581258
*/
12591259
int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1260-
int (*f_rng)(void *, unsigned char *, size_t),
1260+
mbedtls_f_rng_t *f_rng,
12611261
void *p_rng);
12621262

12631263
/** \brief Set the public key in a key pair object.
@@ -1451,7 +1451,7 @@ int mbedtls_ecp_write_public_key(const mbedtls_ecp_keypair *key,
14511451
*/
14521452
int mbedtls_ecp_check_pub_priv(
14531453
const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
1454-
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
1454+
mbedtls_f_rng_t *f_rng, void *p_rng);
14551455

14561456
/** \brief Calculate the public key from a private key in a key pair.
14571457
*
@@ -1468,7 +1468,7 @@ int mbedtls_ecp_check_pub_priv(
14681468
*/
14691469
int mbedtls_ecp_keypair_calc_public(
14701470
mbedtls_ecp_keypair *key,
1471-
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
1471+
mbedtls_f_rng_t *f_rng, void *p_rng);
14721472

14731473
/** \brief Query the group that a key pair belongs to.
14741474
*

include/mbedtls/lms.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx);
364364
int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
365365
mbedtls_lms_algorithm_type_t type,
366366
mbedtls_lmots_algorithm_type_t otstype,
367-
int (*f_rng)(void *, unsigned char *, size_t),
367+
mbedtls_f_rng_t *f_rng,
368368
void *p_rng, const unsigned char *seed,
369369
size_t seed_size);
370370

@@ -427,7 +427,7 @@ int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
427427
* \return A non-zero error code on failure.
428428
*/
429429
int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
430-
int (*f_rng)(void *, unsigned char *, size_t),
430+
mbedtls_f_rng_t *f_rng,
431431
void *p_rng, const unsigned char *msg,
432432
unsigned int msg_size, unsigned char *sig, size_t sig_size,
433433
size_t *sig_len);

0 commit comments

Comments
 (0)