Skip to content

Commit 8bf5613

Browse files
CTR_DRBG: Don't use functions before they're defined
Move the definitions of mbedtls_ctr_drbg_seed_entropy_len() and mbedtls_ctr_drbg_seed() to after they are used. This makes the code easier to read and to maintain.
1 parent 8f7921e commit 8bf5613

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

library/ctr_drbg.c

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -62,70 +62,6 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
6262
#endif
6363
}
6464

65-
/*
66-
* Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
67-
* NIST tests to succeed (which require known length fixed entropy)
68-
*/
69-
/* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2)
70-
* mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy,
71-
* custom, len, entropy_len)
72-
* implements
73-
* CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
74-
* security_strength) -> initial_working_state
75-
* with inputs
76-
* custom[:len] = nonce || personalization_string
77-
* where entropy_input comes from f_entropy for entropy_len bytes
78-
* and with outputs
79-
* ctx = initial_working_state
80-
*/
81-
int mbedtls_ctr_drbg_seed_entropy_len(
82-
mbedtls_ctr_drbg_context *ctx,
83-
int (*f_entropy)(void *, unsigned char *, size_t),
84-
void *p_entropy,
85-
const unsigned char *custom,
86-
size_t len,
87-
size_t entropy_len )
88-
{
89-
int ret;
90-
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
91-
92-
memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
93-
94-
mbedtls_aes_init( &ctx->aes_ctx );
95-
96-
ctx->f_entropy = f_entropy;
97-
ctx->p_entropy = p_entropy;
98-
99-
ctx->entropy_len = entropy_len;
100-
ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
101-
102-
/*
103-
* Initialize with an empty key
104-
*/
105-
if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, key,
106-
MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
107-
{
108-
return( ret );
109-
}
110-
111-
if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
112-
{
113-
return( ret );
114-
}
115-
return( 0 );
116-
}
117-
118-
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
119-
int (*f_entropy)(void *, unsigned char *, size_t),
120-
void *p_entropy,
121-
const unsigned char *custom,
122-
size_t len )
123-
{
124-
return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy,
125-
custom, len,
126-
MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
127-
}
128-
12965
void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx )
13066
{
13167
if( ctx == NULL )
@@ -445,6 +381,70 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
445381
return( ret );
446382
}
447383

384+
/*
385+
* Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
386+
* NIST tests to succeed (which require known length fixed entropy)
387+
*/
388+
/* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2)
389+
* mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy,
390+
* custom, len, entropy_len)
391+
* implements
392+
* CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
393+
* security_strength) -> initial_working_state
394+
* with inputs
395+
* custom[:len] = nonce || personalization_string
396+
* where entropy_input comes from f_entropy for entropy_len bytes
397+
* and with outputs
398+
* ctx = initial_working_state
399+
*/
400+
int mbedtls_ctr_drbg_seed_entropy_len(
401+
mbedtls_ctr_drbg_context *ctx,
402+
int (*f_entropy)(void *, unsigned char *, size_t),
403+
void *p_entropy,
404+
const unsigned char *custom,
405+
size_t len,
406+
size_t entropy_len )
407+
{
408+
int ret;
409+
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
410+
411+
memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
412+
413+
mbedtls_aes_init( &ctx->aes_ctx );
414+
415+
ctx->f_entropy = f_entropy;
416+
ctx->p_entropy = p_entropy;
417+
418+
ctx->entropy_len = entropy_len;
419+
ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
420+
421+
/*
422+
* Initialize with an empty key
423+
*/
424+
if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, key,
425+
MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
426+
{
427+
return( ret );
428+
}
429+
430+
if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
431+
{
432+
return( ret );
433+
}
434+
return( 0 );
435+
}
436+
437+
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
438+
int (*f_entropy)(void *, unsigned char *, size_t),
439+
void *p_entropy,
440+
const unsigned char *custom,
441+
size_t len )
442+
{
443+
return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy,
444+
custom, len,
445+
MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
446+
}
447+
448448
/* CTR_DRBG_Generate with derivation function (SP 800-90A §10.2.1.5.2)
449449
* mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, additional, add_len)
450450
* implements

0 commit comments

Comments
 (0)