summaryrefslogtreecommitdiffstats
path: root/crypto/rand
diff options
context:
space:
mode:
authorDr. Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>2020-01-26 22:18:23 +0100
committerDr. Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>2020-02-07 11:38:02 +0100
commit30a9d5d1a72149c4eb2b8e5aa83f509344c80232 (patch)
treec8ae072771a45fa23ad49b0cc0a3ffe740623392 /crypto/rand
parentb03de7a9207645c72e22627b10709f15eed211bf (diff)
RAND_DRBG: add a callback data for entropy and nonce callbacks
The callback data allows passing context specific data from the application of the DRBG to to the entropy callbacks. This a rather specialized feature which is useful for implementing known answer tests (KATs) or deterministic signatures (RFC6979), which require passing a specified entropy and nonce for instantiating the DRBG. Reviewed-by: Shane Lontis <shane.lontis@oracle.com> (Merged from https://github.com/openssl/openssl/pull/10950)
Diffstat (limited to 'crypto/rand')
-rw-r--r--crypto/rand/drbg_lib.c28
-rw-r--r--crypto/rand/rand_local.h2
2 files changed, 30 insertions, 0 deletions
diff --git a/crypto/rand/drbg_lib.c b/crypto/rand/drbg_lib.c
index a695a5f7dd..029cc6e77c 100644
--- a/crypto/rand/drbg_lib.c
+++ b/crypto/rand/drbg_lib.c
@@ -305,6 +305,34 @@ void rand_drbg_cleanup_nonce(RAND_DRBG *drbg,
}
/*
+ * Set the |drbg|'s callback data pointer for the entropy and nonce callbacks
+ *
+ * The ownership of the context data remains with the caller,
+ * i.e., it is the caller's responsibility to keep it available as long
+ * as it is need by the callbacks and free it after use.
+ *
+ * Setting the callback data is allowed only if the drbg has not been
+ * initialized yet. Otherwise, the operation will fail.
+ *
+ * Returns 1 on success, 0 on failure.
+ */
+int RAND_DRBG_set_callback_data(RAND_DRBG *drbg, void *data)
+{
+ if (drbg->state != DRBG_UNINITIALISED
+ || drbg->parent != NULL)
+ return 0;
+
+ drbg->callback_data = data;
+ return 1;
+}
+
+/* Retrieve the callback data pointer */
+void *RAND_DRBG_get_callback_data(RAND_DRBG *drbg)
+{
+ return drbg->callback_data;
+}
+
+/*
* Set/initialize |drbg| to be of type |type|, with optional |flags|.
*
* If |type| and |flags| are zero, use the defaults
diff --git a/crypto/rand/rand_local.h b/crypto/rand/rand_local.h
index c0ba3bad03..ce16892531 100644
--- a/crypto/rand/rand_local.h
+++ b/crypto/rand/rand_local.h
@@ -328,6 +328,8 @@ struct rand_drbg_st {
RAND_DRBG_cleanup_entropy_fn cleanup_entropy;
RAND_DRBG_get_nonce_fn get_nonce;
RAND_DRBG_cleanup_nonce_fn cleanup_nonce;
+
+ void *callback_data;
};
/* The global RAND method, and the global buffer and DRBG instance. */