Add a return value to aead_t.encrypt()
authorMartin Willi <martin@revosec.ch>
Thu, 5 Jul 2012 15:10:17 +0000 (17:10 +0200)
committerMartin Willi <martin@revosec.ch>
Mon, 16 Jul 2012 12:53:32 +0000 (14:53 +0200)
scripts/crypt_burn.c
src/libcharon/encoding/payloads/encryption_payload.c
src/libcharon/sa/ikev1/keymat_v1.c
src/libstrongswan/crypto/aead.c
src/libstrongswan/crypto/aead.h
src/libstrongswan/crypto/crypto_tester.c
src/libstrongswan/plugins/ccm/ccm_aead.c
src/libstrongswan/plugins/gcm/gcm_aead.c

index 3f7ad5f..3b05144 100644 (file)
@@ -70,10 +70,14 @@ int main(int argc, char *argv[])
                }
                while (TRUE)
                {
-                       aead->encrypt(aead,
+                       if (!aead->encrypt(aead,
                                chunk_create(buffer, sizeof(buffer) - aead->get_icv_size(aead)),
                                chunk_from_thing(assoc),
-                               chunk_create(iv, aead->get_iv_size(aead)), NULL);
+                               chunk_create(iv, aead->get_iv_size(aead)), NULL))
+                       {
+                               fprintf(stderr, "aead encryption failed!\n");
+                               return 1;
+                       }
                        if (!aead->decrypt(aead, chunk_create(buffer, sizeof(buffer)),
                                chunk_from_thing(assoc),
                                chunk_create(iv, aead->get_iv_size(aead)), NULL))
index 096079a..842e516 100644 (file)
@@ -367,7 +367,11 @@ METHOD(encryption_payload_t, encrypt, bool,
        DBG3(DBG_ENC, "padding %B", &padding);
        DBG3(DBG_ENC, "assoc %B", &assoc);
 
-       this->aead->encrypt(this->aead, crypt, assoc, iv, NULL);
+       if (!this->aead->encrypt(this->aead, crypt, assoc, iv, NULL))
+       {
+               free(assoc.ptr);
+               return FALSE;
+       }
 
        DBG3(DBG_ENC, "encrypted %B", &crypt);
        DBG3(DBG_ENC, "ICV %B", &icv);
@@ -410,7 +414,11 @@ METHOD(encryption_payload_t, encrypt_v1, bool,
        DBG3(DBG_ENC, "plain %B", &plain);
        DBG3(DBG_ENC, "padding %B", &padding);
 
-       this->aead->encrypt(this->aead, this->encrypted, chunk_empty, iv, NULL);
+       if (!this->aead->encrypt(this->aead, this->encrypted, chunk_empty, iv, NULL))
+       {
+               chunk_free(&iv);
+               return FALSE;
+       }
        chunk_free(&iv);
 
        DBG3(DBG_ENC, "encrypted %B", &this->encrypted);
index 000c73a..9b566b1 100644 (file)
@@ -163,11 +163,12 @@ typedef struct {
 } private_aead_t;
 
 
-METHOD(aead_t, encrypt, void,
+METHOD(aead_t, encrypt, bool,
        private_aead_t *this, chunk_t plain, chunk_t assoc, chunk_t iv,
        chunk_t *encrypted)
 {
        this->crypter->encrypt(this->crypter, plain, iv, encrypted);
+       return TRUE;
 }
 
 METHOD(aead_t, decrypt, bool,
index 51cb059..417c0b5 100644 (file)
@@ -40,7 +40,7 @@ struct private_aead_t {
        signer_t *signer;
 };
 
-METHOD(aead_t, encrypt, void,
+METHOD(aead_t, encrypt, bool,
        private_aead_t *this, chunk_t plain, chunk_t assoc, chunk_t iv,
        chunk_t *encrypted)
 {
@@ -60,6 +60,7 @@ METHOD(aead_t, encrypt, void,
                this->crypter->encrypt(this->crypter, plain, iv, NULL);
                this->signer->get_signature(this->signer, plain, plain.ptr + plain.len);
        }
+       return TRUE;
 }
 
 METHOD(aead_t, decrypt, bool,
index 3f6abb4..8536110 100644 (file)
@@ -45,8 +45,10 @@ struct aead_t {
         * @param assoc                 associated data to sign
         * @param iv                    initialization vector
         * @param encrypted             allocated encryption result
+        * @return                              TRUE if successfully encrypted
         */
-       void (*encrypt)(aead_t *this, chunk_t plain, chunk_t assoc, chunk_t iv,
+       __attribute__((warn_unused_result))
+       bool (*encrypt)(aead_t *this, chunk_t plain, chunk_t assoc, chunk_t iv,
                                        chunk_t *encrypted);
 
        /**
index 9619570..7ee1a7a 100644 (file)
@@ -317,12 +317,16 @@ static u_int bench_aead(private_crypto_tester_t *this,
                start_timing(&start);
                while (end_timing(&start) < this->bench_time)
                {
-                       aead->encrypt(aead, buf, chunk_from_thing(assoc),
-                                                 chunk_from_thing(iv), NULL);
-                       runs += 2;
-                       aead->decrypt(aead, chunk_create(buf.ptr, buf.len + icv),
-                                                 chunk_from_thing(assoc), chunk_from_thing(iv), NULL);
-                       runs += 2;
+                       if (aead->encrypt(aead, buf, chunk_from_thing(assoc),
+                                               chunk_from_thing(iv), NULL))
+                       {
+                               runs += 2;
+                       }
+                       if (aead->decrypt(aead, chunk_create(buf.ptr, buf.len + icv),
+                                               chunk_from_thing(assoc), chunk_from_thing(iv), NULL))
+                       {
+                               runs += 2;
+                       }
                }
                free(buf.ptr);
                aead->destroy(aead);
@@ -377,7 +381,10 @@ METHOD(crypto_tester_t, test_aead, bool,
 
                /* allocated encryption */
                plain = chunk_create(vector->plain, vector->len);
-               aead->encrypt(aead, plain, assoc, iv, &cipher);
+               if (!aead->encrypt(aead, plain, assoc, iv, &cipher))
+               {
+                       failed = TRUE;
+               }
                if (!memeq(vector->cipher, cipher.ptr, cipher.len))
                {
                        failed = TRUE;
@@ -405,7 +412,10 @@ METHOD(crypto_tester_t, test_aead, bool,
                }
                plain.ptr = realloc(plain.ptr, plain.len + icv);
                /* inline encryption */
-               aead->encrypt(aead, plain, assoc, iv, NULL);
+               if (!aead->encrypt(aead, plain, assoc, iv, NULL))
+               {
+                       failed = TRUE;
+               }
                if (!memeq(vector->cipher, plain.ptr, plain.len + icv))
                {
                        failed = TRUE;
index 0d2a56a..f03440f 100644 (file)
@@ -240,7 +240,7 @@ static bool verify_icv(private_ccm_aead_t *this, chunk_t plain, chunk_t assoc,
        return memeq(buf, icv, this->icv_size);
 }
 
-METHOD(aead_t, encrypt, void,
+METHOD(aead_t, encrypt, bool,
        private_ccm_aead_t *this, chunk_t plain, chunk_t assoc, chunk_t iv,
        chunk_t *encrypted)
 {
@@ -255,6 +255,7 @@ METHOD(aead_t, encrypt, void,
                create_icv(this, plain, assoc, iv, plain.ptr + plain.len);
                crypt_data(this, iv, plain, plain);
        }
+       return TRUE;
 }
 
 METHOD(aead_t, decrypt, bool,
index 0d7d91d..8552f5c 100644 (file)
@@ -267,7 +267,7 @@ static bool verify_icv(private_gcm_aead_t *this, chunk_t assoc, chunk_t crypt,
        return memeq(tmp, icv, this->icv_size);
 }
 
-METHOD(aead_t, encrypt, void,
+METHOD(aead_t, encrypt, bool,
        private_gcm_aead_t *this, chunk_t plain, chunk_t assoc, chunk_t iv,
        chunk_t *encrypted)
 {
@@ -288,6 +288,7 @@ METHOD(aead_t, encrypt, void,
                crypt(this, j, plain, plain);
                create_icv(this, assoc, plain, j, plain.ptr + plain.len);
        }
+       return TRUE;
 }
 
 METHOD(aead_t, decrypt, bool,