[API-NEXT,PATCHv5,4/7] linux-generic: crypto: implement crypto capabilities and random kind

Message ID 1480900520-21989-4-git-send-email-bill.fischofer@linaro.org
State New
Headers show

Commit Message

Bill Fischofer Dec. 5, 2016, 1:15 a.m.
Add random kind related values to odp_crypto_capability() and implement
this selector field in odp_random_data(). Since odp-linux uses the
OpenSSL RAND_bytes() API, the highest kind of random data supported by
this implementation is ODP_RANDOM_CRYPTO.

Signed-off-by: Bill Fischofer <bill.fischofer@linaro.org>

---
 platform/linux-generic/odp_crypto.c | 47 +++++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 5 deletions(-)

-- 
2.7.4

Patch

diff --git a/platform/linux-generic/odp_crypto.c b/platform/linux-generic/odp_crypto.c
index 7e686ff..8d1eea8 100644
--- a/platform/linux-generic/odp_crypto.c
+++ b/platform/linux-generic/odp_crypto.c
@@ -4,6 +4,7 @@ 
  * SPDX-License-Identifier:     BSD-3-Clause
  */
 
+#include <odp_posix_extensions.h>
 #include <odp/api/crypto.h>
 #include <odp_internal.h>
 #include <odp/api/atomic.h>
@@ -19,6 +20,7 @@ 
 #include <odp_packet_internal.h>
 
 #include <string.h>
+#include <stdlib.h>
 
 #include <openssl/des.h>
 #include <openssl/rand.h>
@@ -620,6 +622,8 @@  int odp_crypto_capability(odp_crypto_capability_t *capa)
 
 	capa->max_sessions = MAX_SESSIONS;
 
+	capa->max_random_kind = ODP_RANDOM_CRYPTO;
+
 	return 0;
 }
 
@@ -877,12 +881,45 @@  int odp_crypto_term_global(void)
 	return rc;
 }
 
-int32_t
-odp_random_data(uint8_t *buf, int32_t len, odp_bool_t use_entropy ODP_UNUSED)
+int odp_random_data(uint8_t *buf, uint32_t len, odp_random_kind_t kind)
 {
-	int32_t rc;
-	rc = RAND_bytes(buf, len);
-	return (1 == rc) ? len /*success*/: -1 /*failure*/;
+	int rc;
+
+	switch (kind) {
+	case ODP_RANDOM_BASIC:
+		RAND_pseudo_bytes(buf, len);
+		return len;
+
+	case ODP_RANDOM_CRYPTO:
+		rc = RAND_bytes(buf, len);
+		return (1 == rc) ? (int)len /*success*/: -1 /*failure*/;
+
+	case ODP_RANDOM_TRUE:
+	default:
+		return -1;
+	}
+}
+
+int odp_random_seeded_data(uint8_t *buf, uint32_t len,
+			   odp_random_kind_t kind, uint32_t *seed)
+{
+	union {
+		uint32_t rand_word;
+		uint8_t rand_byte[4];
+	} u;
+	uint32_t i = 0, j;
+
+	if (kind != ODP_RANDOM_BASIC)
+		return -1;
+
+	while (i < len) {
+		u.rand_word = rand_r(seed);
+
+		for (j = 0; j < 4 && i < len; j++, i++)
+			*buf++ = u.rand_byte[j];
+	}
+
+	return len;
 }
 
 odp_crypto_compl_t odp_crypto_compl_from_event(odp_event_t ev)