]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
crypto: Add return value to DES and AES encrypt/decrypt
authorJouni Malinen <j@w1.fi>
Tue, 28 Feb 2017 08:57:43 +0000 (10:57 +0200)
committerJouni Malinen <j@w1.fi>
Tue, 28 Feb 2017 09:23:54 +0000 (11:23 +0200)
These operations may fail with some crypto wrappers, so allow the
functions to report their results to the caller.

Signed-off-by: Jouni Malinen <j@w1.fi>
src/crypto/aes-internal-dec.c
src/crypto/aes-internal-enc.c
src/crypto/aes.h
src/crypto/crypto.h
src/crypto/crypto_gnutls.c
src/crypto/crypto_libtomcrypt.c
src/crypto/crypto_none.c
src/crypto/crypto_openssl.c
src/crypto/des-internal.c

index 720c7036e4e7b9776f9361aa9e2fa5ae3397459f..74822959490466a32872710a803a405d43e76f63 100644 (file)
@@ -147,10 +147,12 @@ d##3 = TD0(s##3) ^ TD1(s##2) ^ TD2(s##1) ^ TD3(s##0) ^ rk[4 * i + 3]
        PUTU32(pt + 12, s3);
 }
 
-void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
+
+int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
 {
        u32 *rk = ctx;
        rijndaelDecrypt(ctx, rk[AES_PRIV_NR_POS], crypt, plain);
+       return 0;
 }
 
 
index f3c61b8508f3ce39a330bc3be5f7e71ca1d7ec15..9fdb4f35cb9b794a03f14bac5a98d9d764eec32e 100644 (file)
@@ -112,10 +112,11 @@ void * aes_encrypt_init(const u8 *key, size_t len)
 }
 
 
-void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
+int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
 {
        u32 *rk = ctx;
        rijndaelEncrypt(ctx, rk[AES_PRIV_NR_POS], plain, crypt);
+       return 0;
 }
 
 
index 2de59e04efa5f0cfbe16b8054249beadae8922fe..8ab3de2ee83f390c0abc1cb3805f9352b03fb7e7 100644 (file)
 #define AES_BLOCK_SIZE 16
 
 void * aes_encrypt_init(const u8 *key, size_t len);
-void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
+int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
 void aes_encrypt_deinit(void *ctx);
 void * aes_decrypt_init(const u8 *key, size_t len);
-void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
+int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
 void aes_decrypt_deinit(void *ctx);
 
 #endif /* AES_H */
index bdc3ba6f37e0dd3d05731f5f13e3c3131dfdb6a4..eb3c33bbee1a6def12685b9e0b3403e7c264ad86 100644 (file)
@@ -106,8 +106,9 @@ int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  * @clear: 8 octets (in)
  * @key: 7 octets (in) (no parity bits included)
  * @cypher: 8 octets (out)
+ * Returns: 0 on success, -1 on failure
  */
-void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
+int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher);
 
 /**
  * aes_encrypt_init - Initialize AES for encryption
@@ -122,8 +123,9 @@ void * aes_encrypt_init(const u8 *key, size_t len);
  * @ctx: Context pointer from aes_encrypt_init()
  * @plain: Plaintext data to be encrypted (16 bytes)
  * @crypt: Buffer for the encrypted data (16 bytes)
+ * Returns: 0 on success, -1 on failure
  */
-void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
+int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
 
 /**
  * aes_encrypt_deinit - Deinitialize AES encryption
@@ -144,8 +146,9 @@ void * aes_decrypt_init(const u8 *key, size_t len);
  * @ctx: Context pointer from aes_encrypt_init()
  * @crypt: Encrypted data (16 bytes)
  * @plain: Buffer for the decrypted data (16 bytes)
+ * Returns: 0 on success, -1 on failure
  */
-void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
+int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
 
 /**
  * aes_decrypt_deinit - Deinitialize AES decryption
index 0dfd54d22d47547801b45044e94eee942a446b30..31a580e658c64ede223864d717d898d37122a918 100644 (file)
@@ -30,7 +30,7 @@ int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 }
 
 
-void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
+int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 {
        gcry_cipher_hd_t hd;
        u8 pkey[8], next, tmp;
@@ -49,6 +49,7 @@ void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
        gcry_err_code(gcry_cipher_setkey(hd, pkey, 8));
        gcry_cipher_encrypt(hd, cypher, 8, clear, 8);
        gcry_cipher_close(hd);
+       return 0;
 }
 
 
@@ -107,10 +108,11 @@ void * aes_encrypt_init(const u8 *key, size_t len)
 }
 
 
-void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
+int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
 {
        gcry_cipher_hd_t hd = ctx;
        gcry_cipher_encrypt(hd, crypt, 16, plain, 16);
+       return 0;
 }
 
 
@@ -137,10 +139,11 @@ void * aes_decrypt_init(const u8 *key, size_t len)
 }
 
 
-void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
+int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
 {
        gcry_cipher_hd_t hd = ctx;
        gcry_cipher_decrypt(hd, plain, 16, crypt, 16);
+       return 0;
 }
 
 
index a55edd14e2d3cfdb7ab2db83d70025e850f3f689..b80ad576cc6ce193ab7ebd04edf00c58644d71d5 100644 (file)
@@ -35,7 +35,7 @@ int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 }
 
 
-void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
+int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 {
        u8 pkey[8], next, tmp;
        int i;
@@ -53,6 +53,7 @@ void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
        des_setup(pkey, 8, 0, &skey);
        des_ecb_encrypt(clear, cypher, &skey);
        des_done(&skey);
+       return 0;
 }
 
 
@@ -96,10 +97,10 @@ void * aes_encrypt_init(const u8 *key, size_t len)
 }
 
 
-void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
+int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
 {
        symmetric_key *skey = ctx;
-       aes_ecb_encrypt(plain, crypt, skey);
+       return aes_ecb_encrypt(plain, crypt, skey) == CRYPT_OK ? 0 : -1;
 }
 
 
@@ -125,10 +126,10 @@ void * aes_decrypt_init(const u8 *key, size_t len)
 }
 
 
-void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
+int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
 {
        symmetric_key *skey = ctx;
-       aes_ecb_encrypt(plain, (u8 *) crypt, skey);
+       return aes_ecb_encrypt(plain, (u8 *) crypt, skey) == CRYPT_OK ? 0 : -1;
 }
 
 
@@ -297,7 +298,7 @@ struct crypto_cipher {
 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
                                          const u8 *iv, const u8 *key,
                                          size_t key_len)
-{      
+{
        struct crypto_cipher *ctx;
        int idx, res, rc4 = 0;
 
index 011f3f35a05510a248177fcfecc92677483fd1c6..547919418af998f7d86f3f608951954342c2ec0e 100644 (file)
@@ -18,6 +18,7 @@ int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 }
 
 
-void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
+int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 {
+       return 0;
 }
index 5f7896c85e43d54f6e42eb3dacabd5f226a06e56..0492468812239ffb33211aa36648d63495283847 100644 (file)
@@ -161,7 +161,7 @@ int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 #endif /* CONFIG_FIPS */
 
 
-void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
+int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 {
        u8 pkey[8], next, tmp;
        int i;
@@ -179,6 +179,7 @@ void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
        DES_set_key((DES_cblock *) &pkey, &ks);
        DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
                        DES_ENCRYPT);
+       return 0;
 }
 
 
@@ -295,14 +296,16 @@ void * aes_encrypt_init(const u8 *key, size_t len)
 }
 
 
-void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
+int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
 {
        EVP_CIPHER_CTX *c = ctx;
        int clen = 16;
        if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
                wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
                           ERR_error_string(ERR_get_error(), NULL));
+               return -1;
        }
+       return 0;
 }
 
 
@@ -347,14 +350,16 @@ void * aes_decrypt_init(const u8 *key, size_t len)
 }
 
 
-void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
+int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
 {
        EVP_CIPHER_CTX *c = ctx;
        int plen = 16;
        if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
                wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
                           ERR_error_string(ERR_get_error(), NULL));
+               return -1;
        }
+       return 0;
 }
 
 
index ebd9952ea23d6d5d4a30bd3b54a6f960275ac44c..4ed6957802b0b49c077628468dbfaca01c7cb1c4 100644 (file)
@@ -396,7 +396,7 @@ static void desfunc(u32 *block, const u32 *keys)
 
 /* wpa_supplicant/hostapd specific wrapper */
 
-void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
+int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 {
        u8 pkey[8], next, tmp;
        int i;
@@ -421,6 +421,7 @@ void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 
        os_memset(pkey, 0, sizeof(pkey));
        os_memset(ek, 0, sizeof(ek));
+       return 0;
 }