[PATCHv2,03/16] api: odp_crypto.h: odp_hw_random_get(): return number of bytes written

Message ID 1422610133-20756-4-git-send-email-ola.liljedahl@linaro.org
State New
Headers show

Commit Message

Ola Liljedahl Jan. 30, 2015, 9:28 a.m.
odp_hw_random_get() returns number of bytes written (on success), return <0
on failure. Output buffer size pass by value, not reference, as it is no
longer written to.
Updated some doxygen descriptions, particularly the @return/@retval
descriptions. No other changes of implementation necessary.

Signed-off-by: Ola Liljedahl <ola.liljedahl@linaro.org>
---
(This document/code contribution attached is provided under the terms of
agreement LES-LTM-21309)

 example/ipsec/odp_ipsec_cache.c                 |  6 ++++--
 platform/linux-generic/include/api/odp_crypto.h | 24 ++++++++++++++----------
 platform/linux-generic/odp_crypto.c             |  8 ++++----
 test/validation/crypto/odp_crypto_test_rng.c    |  6 ++----
 4 files changed, 24 insertions(+), 20 deletions(-)

Patch

diff --git a/example/ipsec/odp_ipsec_cache.c b/example/ipsec/odp_ipsec_cache.c
index 15180f2..332fb81 100644
--- a/example/ipsec/odp_ipsec_cache.c
+++ b/example/ipsec/odp_ipsec_cache.c
@@ -96,9 +96,11 @@  int create_ipsec_cache_entry(sa_db_entry_t *cipher_sa,
 
 	/* Generate an IV */
 	if (params.iv.length) {
-		size_t size = params.iv.length;
+		ssize_t size = params.iv.length;
 
-		odp_hw_random_get(params.iv.data, &size, 1);
+		ssize_t ret = odp_hw_random_get(params.iv.data, size, 1);
+		if (ret != size)
+			return -1;
 	}
 
 	/* Synchronous session create for now */
diff --git a/platform/linux-generic/include/api/odp_crypto.h b/platform/linux-generic/include/api/odp_crypto.h
index da6041c..f5a8801 100644
--- a/platform/linux-generic/include/api/odp_crypto.h
+++ b/platform/linux-generic/include/api/odp_crypto.h
@@ -18,6 +18,7 @@ 
 extern "C" {
 #endif
 
+#include <sys/types.h>
 #include <odp_std_types.h>
 #include <odp_event.h>
 #include <odp_pool.h>
@@ -218,7 +219,8 @@  typedef struct odp_crypto_compl_status {
  * @param session           Created session else ODP_CRYPTO_SESSION_INVALID
  * @param status            Failure code if unsuccessful
  *
- * @return 0 if successful else -1
+ * @retval 0 on success
+ * @retval <0 on failure
  */
 int
 odp_crypto_session_create(odp_crypto_session_params_t *params,
@@ -242,7 +244,8 @@  odp_crypto_session_create(odp_crypto_session_params_t *params,
  * @param posted            Pointer to return posted, TRUE for async operation
  * @param completion_event  Event by which the operation results are delivered.
  *
- * @return 0 if successful else -1
+ * @retval 0 on success
+ * @retval <0 on failure
  */
 int
 odp_crypto_operation(odp_crypto_op_params_t *params,
@@ -281,7 +284,7 @@  odp_crypto_get_operation_compl_status(odp_event_t completion_event,
  *
  * @param completion_event  Event containing operation results
  *
- * @return Packet structure where data now resides
+ * @return Handle for packet where data now resides
  */
 odp_packet_t
 odp_crypto_get_operation_compl_packet(odp_event_t completion_event);
@@ -297,18 +300,19 @@  void *
 odp_crypto_get_operation_compl_ctx(odp_event_t completion_event);
 
 /**
- * Generate random byte string
+ * Generate random byte data
  *
- * @param buf          Pointer to store result
- * @param len          Pointer to input length value as well as return value
- * @param use_entropy  Use entropy
+ * @param[out]    buf   Output buffer
+ * @param         bufsz Size of output buffer
+ * @param use_entropy   Use entropy
  *
  * @todo Define the implication of the use_entropy parameter
  *
- * @return 0 if succesful
+ * @retval Number of bytes written
+ * @retval <0 on failure
  */
-int
-odp_hw_random_get(uint8_t *buf, size_t *len, bool use_entropy);
+ssize_t
+odp_hw_random_get(uint8_t *buf, ssize_t bufsz, bool use_entropy);
 
 /**
  * @}
diff --git a/platform/linux-generic/odp_crypto.c b/platform/linux-generic/odp_crypto.c
index 4436a0c..b329756 100644
--- a/platform/linux-generic/odp_crypto.c
+++ b/platform/linux-generic/odp_crypto.c
@@ -428,12 +428,12 @@  odp_crypto_init_global(void)
 	return 0;
 }
 
-int
-odp_hw_random_get(uint8_t *buf, size_t *len, bool use_entropy ODP_UNUSED)
+ssize_t
+odp_hw_random_get(uint8_t *buf, ssize_t len, bool use_entropy ODP_UNUSED)
 {
 	int rc;
-	rc = RAND_bytes(buf, *len);
-	return ((1 == rc) ? 0 : -1);
+	rc = RAND_bytes(buf, len);
+	return (1 == rc) ? len /*success*/: -1 /*failure*/;
 }
 
 void
diff --git a/test/validation/crypto/odp_crypto_test_rng.c b/test/validation/crypto/odp_crypto_test_rng.c
index 458f908..a9de5b9 100644
--- a/test/validation/crypto/odp_crypto_test_rng.c
+++ b/test/validation/crypto/odp_crypto_test_rng.c
@@ -16,12 +16,10 @@ 
 static void rng_get_size(void)
 {
 	int ret;
-	size_t len = TDES_CBC_IV_LEN;
 	uint8_t buf[TDES_CBC_IV_LEN];
 
-	ret = odp_hw_random_get(buf, &len, false);
-	CU_ASSERT(!ret);
-	CU_ASSERT(len == TDES_CBC_IV_LEN);
+	ret = odp_hw_random_get(buf, sizeof(buf), false);
+	CU_ASSERT(ret == TDES_CBC_IV_LEN);
 }
 
 CU_TestInfo test_rng[] = {