RNGs' get_bytes and allocate_bytes return boolean
authorTobias Brunner <tobias@strongswan.org>
Mon, 25 Jun 2012 11:22:54 +0000 (13:22 +0200)
committerMartin Willi <martin@revosec.ch>
Mon, 16 Jul 2012 12:53:34 +0000 (14:53 +0200)
src/libstrongswan/crypto/rngs/rng.h
src/libstrongswan/plugins/gcrypt/gcrypt_rng.c
src/libstrongswan/plugins/openssl/openssl_rng.c
src/libstrongswan/plugins/padlock/padlock_rng.c
src/libstrongswan/plugins/pkcs11/pkcs11_rng.c
src/libstrongswan/plugins/random/random_rng.c

index 36ef52b..48ca52d 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2012 Tobias Brunner
  * Copyright (C) 2008 Martin Willi
  * Hochschule fuer Technik Rapperswil
  *
@@ -53,16 +54,20 @@ struct rng_t {
         *
         * @param len           number of bytes to get
         * @param buffer        pointer where the generated bytes will be written
+        * @return                      TRUE if bytes successfully written
         */
-       void (*get_bytes) (rng_t *this, size_t len, u_int8_t *buffer);
+       __attribute__((warn_unused_result))
+       bool (*get_bytes) (rng_t *this, size_t len, u_int8_t *buffer);
 
        /**
         * Generates random bytes and allocate space for them.
         *
         * @param len           number of bytes to get
         * @param chunk         chunk which will hold generated bytes
+        * @return                      TRUE if allocation succeeded
         */
-       void (*allocate_bytes) (rng_t *this, size_t len, chunk_t *chunk);
+       __attribute__((warn_unused_result))
+       bool (*allocate_bytes) (rng_t *this, size_t len, chunk_t *chunk);
 
        /**
         * Destroys a rng object.
index d29755d..dc34a8d 100644 (file)
@@ -35,7 +35,7 @@ struct private_gcrypt_rng_t {
        rng_quality_t quality;
 };
 
-METHOD(rng_t, get_bytes, void,
+METHOD(rng_t, get_bytes, bool,
        private_gcrypt_rng_t *this, size_t bytes, u_int8_t *buffer)
 {
        switch (this->quality)
@@ -50,13 +50,15 @@ METHOD(rng_t, get_bytes, void,
                        gcry_randomize(buffer, bytes, GCRY_VERY_STRONG_RANDOM);
                        break;
        }
+       return TRUE;
 }
 
-METHOD(rng_t, allocate_bytes, void,
+METHOD(rng_t, allocate_bytes, bool,
        private_gcrypt_rng_t *this, size_t bytes, chunk_t *chunk)
 {
        *chunk = chunk_alloc(bytes);
        get_bytes(this, chunk->len, chunk->ptr);
+       return TRUE;
 }
 
 METHOD(rng_t, destroy, void,
index abb4622..c83244f 100644 (file)
@@ -44,10 +44,10 @@ struct private_openssl_rng_t {
        rng_quality_t quality;
 };
 
-METHOD(rng_t, get_bytes, void,
+METHOD(rng_t, get_bytes, bool,
        private_openssl_rng_t *this, size_t bytes, u_int8_t *buffer)
 {
-       u_int32_t ret=0;
+       u_int32_t ret;
 
        if (this->quality == RNG_STRONG)
        {
@@ -57,18 +57,19 @@ METHOD(rng_t, get_bytes, void,
        {
                ret = RAND_pseudo_bytes((char*)buffer, bytes);
        }
-
-       if (ret == 0)
-       {
-               DBG1(DBG_LIB, "getting randomness from openssl failed.");
-       }
+       return ret != 0;
 }
 
-METHOD(rng_t, allocate_bytes, void,
+METHOD(rng_t, allocate_bytes, bool,
        private_openssl_rng_t *this, size_t bytes, chunk_t *chunk)
 {
        *chunk = chunk_alloc(bytes);
-       get_bytes(this, chunk->len, chunk->ptr);
+       if (!get_bytes(this, chunk->len, chunk->ptr))
+       {
+               chunk_free(chunk);
+               return FALSE;
+       }
+       return TRUE;
 }
 
 METHOD(rng_t, destroy, void,
index 3d805df..517914a 100644 (file)
@@ -69,7 +69,7 @@ static void rng(char *buf, int len, int quality)
        }
 }
 
-METHOD(rng_t, allocate_bytes, void,
+METHOD(rng_t, allocate_bytes, bool,
        private_padlock_rng_t *this, size_t bytes, chunk_t *chunk)
 {
        chunk->len = bytes;
@@ -77,9 +77,10 @@ METHOD(rng_t, allocate_bytes, void,
        chunk->ptr = malloc(bytes + 7);
 
        rng(chunk->ptr, chunk->len, this->quality);
+       return TRUE;
 }
 
-METHOD(rng_t, get_bytes, void,
+METHOD(rng_t, get_bytes, bool,
        private_padlock_rng_t *this, size_t bytes, u_int8_t *buffer)
 {
        chunk_t chunk;
@@ -88,6 +89,7 @@ METHOD(rng_t, get_bytes, void,
        allocate_bytes(this, bytes, &chunk);
        memcpy(buffer, chunk.ptr, bytes);
        chunk_clear(&chunk);
+       return TRUE;
 }
 
 METHOD(rng_t, destroy, void,
index 45cf0b7..20e4b6f 100644 (file)
@@ -43,7 +43,7 @@ struct private_pkcs11_rng_t {
 
 };
 
-METHOD(rng_t, get_bytes, void,
+METHOD(rng_t, get_bytes, bool,
        private_pkcs11_rng_t *this, size_t bytes, u_int8_t *buffer)
 {
        CK_RV rv;
@@ -51,15 +51,21 @@ METHOD(rng_t, get_bytes, void,
        if (rv != CKR_OK)
        {
                DBG1(DBG_CFG, "C_GenerateRandom() failed: %N", ck_rv_names, rv);
-               abort();
+               return FALSE;
        }
+       return TRUE;
 }
 
-METHOD(rng_t, allocate_bytes, void,
+METHOD(rng_t, allocate_bytes, bool,
        private_pkcs11_rng_t *this, size_t bytes, chunk_t *chunk)
 {
        *chunk = chunk_alloc(bytes);
-       get_bytes(this, chunk->len, chunk->ptr);
+       if (!get_bytes(this, chunk->len, chunk->ptr))
+       {
+               chunk_clear(chunk);
+               return FALSE;
+       }
+       return TRUE;
 }
 
 METHOD(rng_t, destroy, void,
index 42eddbb..52cfc08 100644 (file)
@@ -40,7 +40,7 @@ struct private_random_rng_t {
        int fd;
 };
 
-METHOD(rng_t, get_bytes, void,
+METHOD(rng_t, get_bytes, bool,
        private_random_rng_t *this, size_t bytes, u_int8_t *buffer)
 {
        size_t done;
@@ -59,13 +59,15 @@ METHOD(rng_t, get_bytes, void,
                }
                done += got;
        }
+       return TRUE;
 }
 
-METHOD(rng_t, allocate_bytes, void,
+METHOD(rng_t, allocate_bytes, bool,
        private_random_rng_t *this, size_t bytes, chunk_t *chunk)
 {
        *chunk = chunk_alloc(bytes);
        get_bytes(this, chunk->len, chunk->ptr);
+       return TRUE;
 }
 
 METHOD(rng_t, destroy, void,