@@ -61,6 +61,11 @@ static int osrandom_rand_status(void) {
61
61
return 1 ;
62
62
}
63
63
}
64
+
65
+ static const char * osurandom_get_implementation (void ) {
66
+ return "CryptGenRandom" ;
67
+ }
68
+
64
69
#endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_CRYPTGENRANDOM */
65
70
66
71
/****************************************************************************
@@ -89,12 +94,16 @@ static int osrandom_finish(ENGINE *e) {
89
94
static int osrandom_rand_status (void ) {
90
95
return 1 ;
91
96
}
92
- #endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_GETRANDOM */
97
+
98
+ static const char * osurandom_get_implementation (void ) {
99
+ return "CCRandomGenerateBytes" ;
100
+ }
101
+ #endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_CC_RANDOM */
93
102
94
103
/****************************************************************************
95
104
* BSD getentropy
96
105
*/
97
- #if CRYPTOGRAPHY_OSRANDOM_ENGINE == CRYPTOGRAPHY_OSRANDOM_ENGINE_GETRANDOM
106
+ #if CRYPTOGRAPHY_OSRANDOM_ENGINE == CRYPTOGRAPHY_OSRANDOM_ENGINE_GETENTROPY
98
107
static const char * Cryptography_osrandom_engine_name = "osrandom_engine getentropy()" ;
99
108
100
109
static int osrandom_init (ENGINE * e ) {
@@ -109,7 +118,7 @@ static int osrandom_rand_bytes(unsigned char *buffer, int size) {
109
118
len = size > 256 : 256 : size ;
110
119
res = getentropy (buffer , len );
111
120
if (res < 0 ) {
112
- CRYPTOGRAPHY_OSRANDOM_put_error (
121
+ CRYPTOGRAPHY_OSRANDOM_put_error (as
113
122
"osrandom_engine.py:getentropy()" );
114
123
return 0 ;
115
124
}
@@ -126,7 +135,11 @@ static int osrandom_finish(ENGINE *e) {
126
135
static int osrandom_rand_status (void ) {
127
136
return 1 ;
128
137
}
129
- #endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_GETRANDOM */
138
+
139
+ static const char * osurandom_get_implementation (void ) {
140
+ return "getentropy" ;
141
+ }
142
+ #endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_GETENTROPY */
130
143
131
144
/****************************************************************************
132
145
* /dev/urandom helpers for all non-BSD Unix platforms
@@ -233,13 +246,13 @@ static void dev_urandom_close(void) {
233
246
}
234
247
}
235
248
}
236
- #endif
249
+ #endif /* CRYPTOGRAPHY_OSRANDOM_NEEDS_DEV_URANDOM */
237
250
238
251
/****************************************************************************
239
252
* Linux getrandom engine with fallback to dev_urandom
240
253
*/
241
254
242
- #if CRYPTOGRAPHY_OSRANDOM_ENGINE == CRYPTOGRAPHY_OSRANDOM_ENGINE_GETENTROPY
255
+ #if CRYPTOGRAPHY_OSRANDOM_ENGINE == CRYPTOGRAPHY_OSRANDOM_ENGINE_GETRANDOM
243
256
static const char * Cryptography_osrandom_engine_name = "osrandom_engine getrandom()" ;
244
257
245
258
static int getrandom_works = -1 ;
@@ -305,7 +318,14 @@ static int osrandom_rand_status(void) {
305
318
}
306
319
return 1 ;
307
320
}
308
- #endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_GETENTROPY */
321
+
322
+ static const char * osurandom_get_implementation (void ) {
323
+ if (getrandom_works == 1 ) {
324
+ return "getrandom" ;
325
+ }
326
+ return "/dev/urandom" ;
327
+ }
328
+ #endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_GETRANDOM */
309
329
310
330
/****************************************************************************
311
331
* dev_urandom engine for all remaining platforms
@@ -338,8 +358,15 @@ static int osrandom_rand_status(void) {
338
358
return 1 ;
339
359
}
340
360
361
+ static const char * osurandom_get_implementation (void ) {
362
+ return "/dev/urandom" ;
363
+ }
341
364
#endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_DEV_URANDOM */
342
365
366
+ /****************************************************************************
367
+ * ENGINE boiler plate
368
+ */
369
+
343
370
/* This replicates the behavior of the OpenSSL FIPS RNG, which returns a
344
371
-1 in the event that there is an error when calling RAND_pseudo_bytes. */
345
372
static int osrandom_pseudo_rand_bytes (unsigned char * buffer , int size ) {
@@ -360,6 +387,39 @@ static RAND_METHOD osrandom_rand = {
360
387
osrandom_rand_status ,
361
388
};
362
389
390
+ static const ENGINE_CMD_DEFN osrandom_cmd_defns [] = {
391
+ {CRYPTOGRAPHY_OSRANDOM_GET_IMPLEMENTATION ,
392
+ "get_implementation" ,
393
+ "Get CPRNG implementation." ,
394
+ ENGINE_CMD_FLAG_NO_INPUT },
395
+ {0 , NULL , NULL , 0 }
396
+ };
397
+
398
+ static int osrandom_ctrl (ENGINE * e , int cmd , long i , void * p , void (* f ) (void )) {
399
+ const char * name ;
400
+ size_t len ;
401
+
402
+ switch (cmd ) {
403
+ case CRYPTOGRAPHY_OSRANDOM_GET_IMPLEMENTATION :
404
+ name = osurandom_get_implementation ();
405
+ len = strlen (name );
406
+ if ((p == NULL ) && (i == 0 )) {
407
+ /* return required buffer len */
408
+ return len ;
409
+ }
410
+ if ((p == NULL ) || ((size_t )i <= len )) {
411
+ /* no buffer or buffer too small */
412
+ ENGINEerr (ENGINE_F_ENGINE_CTRL , ENGINE_R_INVALID_ARGUMENT );
413
+ return 0 ;
414
+ }
415
+ strncpy ((char * )p , name , len );
416
+ return len ;
417
+ default :
418
+ ENGINEerr (ENGINE_F_ENGINE_CTRL , ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED );
419
+ return 0 ;
420
+ }
421
+ }
422
+
363
423
/* Returns 1 if successfully added, 2 if engine has previously been added,
364
424
and 0 for error. */
365
425
int Cryptography_add_osrandom_engine (void ) {
@@ -380,7 +440,9 @@ int Cryptography_add_osrandom_engine(void) {
380
440
!ENGINE_set_name (e , Cryptography_osrandom_engine_name ) ||
381
441
!ENGINE_set_RAND (e , & osrandom_rand ) ||
382
442
!ENGINE_set_init_function (e , osrandom_init ) ||
383
- !ENGINE_set_finish_function (e , osrandom_finish )) {
443
+ !ENGINE_set_finish_function (e , osrandom_finish ) ||
444
+ !ENGINE_set_cmd_defns (e , osrandom_cmd_defns ) ||
445
+ !ENGINE_set_ctrl_function (e , osrandom_ctrl )) {
384
446
ENGINE_free (e );
385
447
return 0 ;
386
448
}
0 commit comments