Skip to content

Commit 4515d10

Browse files
authored
Merge pull request #10039 from bjwtaylor/remove-rng-from-ssl
Remove RNG parameters from public SSL APIs
2 parents 94b9972 + 857144c commit 4515d10

File tree

8 files changed

+10
-31
lines changed

8 files changed

+10
-31
lines changed

include/mbedtls/ssl_cookie.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx);
5555
/**
5656
* \brief Setup cookie context (generate keys)
5757
*/
58-
int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
59-
int (*f_rng)(void *, unsigned char *, size_t),
60-
void *p_rng);
58+
int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx);
6159

6260
/**
6361
* \brief Set expiration delay for cookies

include/mbedtls/ssl_ticket.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ typedef struct mbedtls_ssl_ticket_context {
6868
uint32_t MBEDTLS_PRIVATE(ticket_lifetime); /*!< lifetime of tickets in seconds */
6969

7070
/** Callback for getting (pseudo-)random numbers */
71-
int(*MBEDTLS_PRIVATE(f_rng))(void *, unsigned char *, size_t);
72-
void *MBEDTLS_PRIVATE(p_rng); /*!< context for the RNG function */
7371

7472
#if defined(MBEDTLS_THREADING_C)
7573
mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex);
@@ -90,8 +88,6 @@ void mbedtls_ssl_ticket_init(mbedtls_ssl_ticket_context *ctx);
9088
* \brief Prepare context to be actually used
9189
*
9290
* \param ctx Context to be set up
93-
* \param f_rng RNG callback function (mandatory)
94-
* \param p_rng RNG callback context
9591
* \param alg AEAD cipher to use for ticket protection.
9692
* \param key_type Cryptographic key type to use.
9793
* \param key_bits Cryptographic key size to use in bits.
@@ -116,7 +112,6 @@ void mbedtls_ssl_ticket_init(mbedtls_ssl_ticket_context *ctx);
116112
* or a specific MBEDTLS_ERR_XXX error code
117113
*/
118114
int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
119-
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
120115
psa_algorithm_t alg, psa_key_type_t key_type, psa_key_bits_t key_bits,
121116
uint32_t lifetime);
122117

library/ssl_cookie.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,12 @@ void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
8181
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
8282
}
8383

84-
int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
85-
int (*f_rng)(void *, unsigned char *, size_t),
86-
void *p_rng)
84+
int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx)
8785
{
8886
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8987
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
9088
psa_algorithm_t alg;
9189

92-
(void) f_rng;
93-
(void) p_rng;
9490

9591
alg = mbedtls_md_psa_alg_from_type(COOKIE_MD);
9692
if (alg == 0) {

library/ssl_ticket.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ static int ssl_ticket_gen_key(mbedtls_ssl_ticket_context *ctx,
7575
*/
7676
key->lifetime = ctx->ticket_lifetime;
7777

78-
if ((ret = ctx->f_rng(ctx->p_rng, key->name, sizeof(key->name))) != 0) {
78+
if ((ret = psa_generate_random(key->name, sizeof(key->name))) != 0) {
7979
return ret;
8080
}
8181

82-
if ((ret = ctx->f_rng(ctx->p_rng, buf, sizeof(buf))) != 0) {
82+
if ((ret = psa_generate_random(buf, sizeof(buf))) != 0) {
8383
return ret;
8484
}
8585

@@ -185,7 +185,6 @@ int mbedtls_ssl_ticket_rotate(mbedtls_ssl_ticket_context *ctx,
185185
* Setup context for actual use
186186
*/
187187
int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
188-
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
189188
psa_algorithm_t alg, psa_key_type_t key_type, psa_key_bits_t key_bits,
190189
uint32_t lifetime)
191190
{
@@ -199,9 +198,6 @@ int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
199198
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
200199
}
201200

202-
ctx->f_rng = f_rng;
203-
ctx->p_rng = p_rng;
204-
205201
ctx->ticket_lifetime = lifetime;
206202

207203
ctx->keys[0].alg = alg;
@@ -254,7 +250,7 @@ int mbedtls_ssl_ticket_write(void *p_ticket,
254250

255251
*tlen = 0;
256252

257-
if (ctx == NULL || ctx->f_rng == NULL) {
253+
if (ctx == NULL) {
258254
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
259255
}
260256

@@ -278,7 +274,7 @@ int mbedtls_ssl_ticket_write(void *p_ticket,
278274

279275
memcpy(key_name, key->name, TICKET_KEY_NAME_BYTES);
280276

281-
if ((ret = ctx->f_rng(ctx->p_rng, iv, TICKET_IV_BYTES)) != 0) {
277+
if ((ret = psa_generate_random(iv, TICKET_IV_BYTES)) != 0) {
282278
goto cleanup;
283279
}
284280

@@ -355,7 +351,7 @@ int mbedtls_ssl_ticket_parse(void *p_ticket,
355351

356352
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
357353

358-
if (ctx == NULL || ctx->f_rng == NULL) {
354+
if (ctx == NULL) {
359355
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
360356
}
361357

programs/fuzz/fuzz_dtlsserver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
108108
}
109109
#endif
110110

111-
if (mbedtls_ssl_cookie_setup(&cookie_ctx, dummy_random, &ctr_drbg) != 0) {
111+
if (mbedtls_ssl_cookie_setup(&cookie_ctx) != 0) {
112112
goto exit;
113113
}
114114

programs/fuzz/fuzz_server.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
132132
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
133133
if (options & 0x4) {
134134
if (mbedtls_ssl_ticket_setup(&ticket_ctx, //context
135-
dummy_random, //f_rng
136-
&ctr_drbg, //p_rng
137135
PSA_ALG_GCM, //alg
138136
PSA_KEY_TYPE_AES, //key_type
139137
256, //key_bits

programs/ssl/dtls_server.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ int main(void)
216216
goto exit;
217217
}
218218

219-
if ((ret = mbedtls_ssl_cookie_setup(&cookie_ctx,
220-
mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
219+
if ((ret = mbedtls_ssl_cookie_setup(&cookie_ctx)) != 0) {
221220
printf(" failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret);
222221
goto exit;
223222
}

programs/ssl/ssl_server2.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,8 +2971,6 @@ int main(int argc, char *argv[])
29712971
#endif /* MBEDTLS_HAVE_TIME */
29722972
{
29732973
if ((ret = mbedtls_ssl_ticket_setup(&ticket_ctx,
2974-
rng_get,
2975-
&rng,
29762974
opt.ticket_alg,
29772975
opt.ticket_key_type,
29782976
opt.ticket_key_bits,
@@ -3014,8 +3012,7 @@ int main(int argc, char *argv[])
30143012
if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
30153013
#if defined(MBEDTLS_SSL_COOKIE_C)
30163014
if (opt.cookies > 0) {
3017-
if ((ret = mbedtls_ssl_cookie_setup(&cookie_ctx,
3018-
rng_get, &rng)) != 0) {
3015+
if ((ret = mbedtls_ssl_cookie_setup(&cookie_ctx)) != 0) {
30193016
mbedtls_printf(" failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret);
30203017
goto exit;
30213018
}

0 commit comments

Comments
 (0)