Engage GHASH for PowerISA 2.0.7.
[openssl.git] / crypto / evp / e_aes.c
index b4864fe2346c3fb1aa19512f29110ecf76c9a164..8ab434943a6cd6faaeeec508f0b01a3aed0a2c17 100644 (file)
@@ -1,5 +1,5 @@
 /* ====================================================================
- * Copyright (c) 2001-2011 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 2001-2014 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -154,9 +154,18 @@ void AES_xts_decrypt(const char *inp,char *out,size_t len,
                        const unsigned char iv[16]);
 #endif
 
-#if    defined(VPAES_ASM) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
-extern unsigned int OPENSSL_ppccap_P;
-#define        VPAES_CAPABLE   (OPENSSL_ppccap_P&(1<<1))
+#if    defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
+# include "ppc_arch.h"
+# ifdef VPAES_ASM
+#  define VPAES_CAPABLE        (OPENSSL_ppccap_P & PPC_ALTIVEC)
+# endif
+# define HWAES_CAPABLE (OPENSSL_ppccap_P & PPC_CRYPTO207)
+# define HWAES_set_encrypt_key aes_p8_set_encrypt_key
+# define HWAES_set_decrypt_key aes_p8_set_decrypt_key
+# define HWAES_encrypt aes_p8_encrypt
+# define HWAES_decrypt aes_p8_decrypt
+# define HWAES_cbc_encrypt aes_p8_cbc_encrypt
+# define HWAES_ctr32_encrypt_blocks aes_p8_ctr32_encrypt_blocks
 #endif
 
 #if    defined(AES_ASM) && !defined(I386_ONLY) &&      (  \
@@ -900,13 +909,38 @@ const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
 
 #endif
 
-#if defined(AES_ASM) && defined(BSAES_ASM) && (defined(__arm__) || defined(__arm))
+#if defined(OPENSSL_CPUID_OBJ) && (defined(__arm__) || defined(__arm) || defined(__aarch64__))
 #include "arm_arch.h"
 #if __ARM_ARCH__>=7
-#define BSAES_CAPABLE  (OPENSSL_armcap_P & ARMV7_NEON)
+# if defined(BSAES_ASM)
+#  define BSAES_CAPABLE        (OPENSSL_armcap_P & ARMV7_NEON)
+# endif
+# define HWAES_CAPABLE (OPENSSL_armcap_P & ARMV8_AES)
+# define HWAES_set_encrypt_key aes_v8_set_encrypt_key
+# define HWAES_set_decrypt_key aes_v8_set_decrypt_key
+# define HWAES_encrypt aes_v8_encrypt
+# define HWAES_decrypt aes_v8_decrypt
+# define HWAES_cbc_encrypt aes_v8_cbc_encrypt
+# define HWAES_ctr32_encrypt_blocks aes_v8_ctr32_encrypt_blocks
 #endif
 #endif
 
+#if defined(HWAES_CAPABLE)
+int HWAES_set_encrypt_key(const unsigned char *userKey, const int bits,
+       AES_KEY *key);
+int HWAES_set_decrypt_key(const unsigned char *userKey, const int bits,
+       AES_KEY *key);
+void HWAES_encrypt(const unsigned char *in, unsigned char *out,
+       const AES_KEY *key);
+void HWAES_decrypt(const unsigned char *in, unsigned char *out,
+       const AES_KEY *key);
+void HWAES_cbc_encrypt(const unsigned char *in, unsigned char *out,
+       size_t length, const AES_KEY *key,
+       unsigned char *ivec, const int enc);
+void HWAES_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
+       size_t len, const AES_KEY *key, const unsigned char ivec[16]);
+#endif
+
 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags)            \
        BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)     \
        BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1)      \
@@ -925,6 +959,19 @@ static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
        mode = ctx->cipher->flags & EVP_CIPH_MODE;
        if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
            && !enc)
+#ifdef HWAES_CAPABLE
+           if (HWAES_CAPABLE)
+               {
+               ret = HWAES_set_decrypt_key(key,ctx->key_len*8,&dat->ks.ks);
+               dat->block      = (block128_f)HWAES_decrypt;
+               dat->stream.cbc = NULL;
+#ifdef HWAES_cbc_encrypt
+               if (mode==EVP_CIPH_CBC_MODE)
+                   dat->stream.cbc = (cbc128_f)HWAES_cbc_encrypt;
+#endif
+               }
+           else
+#endif
 #ifdef BSAES_CAPABLE
            if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE)
                {
@@ -953,6 +1000,26 @@ static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                                        NULL;
                }
        else
+#ifdef HWAES_CAPABLE
+           if (HWAES_CAPABLE)
+               {
+               ret = HWAES_set_encrypt_key(key,ctx->key_len*8,&dat->ks.ks);
+               dat->block      = (block128_f)HWAES_encrypt;
+               dat->stream.cbc = NULL;
+#ifdef HWAES_cbc_encrypt
+               if (mode==EVP_CIPH_CBC_MODE)
+                   dat->stream.cbc = (cbc128_f)HWAES_cbc_encrypt;
+               else
+#endif
+#ifdef HWAES_ctr32_encrypt_blocks
+               if (mode==EVP_CIPH_CTR_MODE)
+                   dat->stream.ctr = (ctr128_f)HWAES_ctr32_encrypt_blocks;
+               else
+#endif
+               (void)0;        /* terminate potentially open 'else' */
+               }
+           else
+#endif
 #ifdef BSAES_CAPABLE
            if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE)
                {
@@ -1004,7 +1071,7 @@ static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
        else if (ctx->encrypt)
                CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
        else
-               CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
+               CRYPTO_cbc128_decrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
 
        return 1;
 }
@@ -1231,6 +1298,28 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
                /* Extra padding: tag appended to record */
                return EVP_GCM_TLS_TAG_LEN;
 
+       case EVP_CTRL_COPY:
+               {
+                       EVP_CIPHER_CTX *out = ptr;
+                       EVP_AES_GCM_CTX *gctx_out = out->cipher_data;
+                       if (gctx->gcm.key)
+                               {
+                               if (gctx->gcm.key != &gctx->ks)
+                                       return 0;
+                               gctx_out->gcm.key = &gctx_out->ks;
+                               }
+                       if (gctx->iv == c->iv)
+                               gctx_out->iv = out->iv;
+                       else
+                       {
+                               gctx_out->iv = OPENSSL_malloc(gctx->ivlen);
+                               if (!gctx_out->iv)
+                                       return 0;
+                               memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
+                       }
+                       return 1;
+               }
+
        default:
                return -1;
 
@@ -1245,6 +1334,21 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                return 1;
        if (key)
                { do {
+#ifdef HWAES_CAPABLE
+               if (HWAES_CAPABLE)
+                       {
+                       HWAES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks.ks);
+                       CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
+                                       (block128_f)HWAES_encrypt);
+#ifdef HWAES_ctr32_encrypt_blocks
+                       gctx->ctr = (ctr128_f)HWAES_ctr32_encrypt_blocks;
+#else
+                       gctx->ctr = NULL;
+#endif
+                       break;
+                       }
+               else
+#endif
 #ifdef BSAES_CAPABLE
                if (BSAES_CAPABLE)
                        {
@@ -1605,7 +1709,8 @@ static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
 
 #define CUSTOM_FLAGS   (EVP_CIPH_FLAG_DEFAULT_ASN1 \
                | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
-               | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
+               | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
+               | EVP_CIPH_CUSTOM_COPY)
 
 BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
                EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
@@ -1617,7 +1722,25 @@ BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
        {
        EVP_AES_XTS_CTX *xctx = c->cipher_data;
-       if (type != EVP_CTRL_INIT)
+       if (type == EVP_CTRL_COPY)
+               {
+               EVP_CIPHER_CTX *out = ptr;
+               EVP_AES_XTS_CTX *xctx_out = out->cipher_data;
+               if (xctx->xts.key1)
+                       {
+                       if (xctx->xts.key1 != &xctx->ks1)
+                               return 0;
+                       xctx_out->xts.key1 = &xctx_out->ks1;
+                       }
+               if (xctx->xts.key2)
+                       {
+                       if (xctx->xts.key2 != &xctx->ks2)
+                               return 0;
+                       xctx_out->xts.key2 = &xctx_out->ks2;
+                       }
+               return 1;
+               }
+       else if (type != EVP_CTRL_INIT)
                return -1;
        /* key1 and key2 are used as an indicator both key and IV are set */
        xctx->xts.key1 = NULL;
@@ -1640,6 +1763,29 @@ static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                xctx->stream = NULL;
 #endif
                /* key_len is two AES keys */
+#ifdef HWAES_CAPABLE
+               if (HWAES_CAPABLE)
+                       {
+                       if (enc)
+                           {
+                           HWAES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
+                           xctx->xts.block1 = (block128_f)HWAES_encrypt;
+                           }
+                       else
+                           {
+                           HWAES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
+                           xctx->xts.block1 = (block128_f)HWAES_decrypt;
+                           }
+
+                       HWAES_set_encrypt_key(key + ctx->key_len/2,
+                                                   ctx->key_len * 4, &xctx->ks2.ks);
+                       xctx->xts.block2 = (block128_f)HWAES_encrypt;
+
+                       xctx->xts.key1 = &xctx->ks1;
+                       break;
+                       }
+               else
+#endif
 #ifdef BSAES_CAPABLE
                if (BSAES_CAPABLE)
                        xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
@@ -1726,7 +1872,8 @@ static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
 #define aes_xts_cleanup NULL
 
 #define XTS_FLAGS      (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
-                        | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
+                        | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
+                        | EVP_CIPH_CUSTOM_COPY)
 
 BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
 BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
@@ -1776,6 +1923,19 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
                cctx->len_set = 0;
                return 1;
 
+       case EVP_CTRL_COPY:
+               {
+                       EVP_CIPHER_CTX *out = ptr;
+                       EVP_AES_CCM_CTX *cctx_out = out->cipher_data;
+                       if (cctx->ccm.key)
+                               {
+                               if (cctx->ccm.key != &cctx->ks)
+                                       return 0;
+                               cctx_out->ccm.key = &cctx_out->ks;
+                               }
+                       return 1;
+               }
+
        default:
                return -1;
 
@@ -1790,6 +1950,19 @@ static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                return 1;
        if (key) do
                {
+#ifdef HWAES_CAPABLE
+               if (HWAES_CAPABLE)
+                       {
+                       HWAES_set_encrypt_key(key,ctx->key_len*8,&cctx->ks.ks);
+
+                       CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
+                                       &cctx->ks, (block128_f)HWAES_encrypt);
+                       cctx->str = NULL;
+                       cctx->key_set = 1;
+                       break;
+                       }
+               else
+#endif
 #ifdef VPAES_CAPABLE
                if (VPAES_CAPABLE)
                        {
@@ -1913,7 +2086,7 @@ static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                }
        if (iv)
                {
-               memcpy(ctx->iv, iv, 8);
+               memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
                wctx->iv = ctx->iv;
                }
        return 1;
@@ -1924,23 +2097,62 @@ static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
        {
        EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
        size_t rv;
-       if (inlen % 8)
+       /* AES wrap with padding has IV length of 4, without padding 8 */
+       int pad = EVP_CIPHER_CTX_iv_length(ctx) == 4;
+       /* No final operation so always return zero length */
+       if (!in)
                return 0;
+       /* Input length must always be non-zero */
+       if (!inlen)
+               return -1;
+       /* If decrypting need at least 16 bytes and multiple of 8 */
+       if (!ctx->encrypt && (inlen < 16 || inlen & 0x7))
+               return -1;
+       /* If not padding input must be multiple of 8 */
+       if (!pad && inlen & 0x7)
+               return -1;
        if (!out)
                {
                if (ctx->encrypt)
+                       {
+                       /* If padding round up to multiple of 8 */
+                       if (pad)
+                               inlen = (inlen + 7)/8 * 8;
+                       /* 8 byte prefix */
                        return inlen + 8;
+                       }
                else
+                       {
+                       /* If not padding output will be exactly 8 bytes
+                        * smaller than input. If padding it will be at
+                        * least 8 bytes smaller but we don't know how
+                        * much.
+                        */
                        return inlen - 8;
+                       }
                }
-       if (!in)
-               return 0;
-       if (ctx->encrypt)
-               rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
+       if (pad)
+               {
+               if (ctx->encrypt)
+                       rv = CRYPTO_128_wrap_pad(&wctx->ks.ks, wctx->iv,
+                                               out, in, inlen,
                                                (block128_f)AES_encrypt);
+               else
+                       rv = CRYPTO_128_unwrap_pad(&wctx->ks.ks, wctx->iv,
+                                               out, in, inlen,
+                                               (block128_f)AES_decrypt);
+               }
        else
-               rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
+               {
+               if (ctx->encrypt)
+                       rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv,
+                                               out, in, inlen,
+                                               (block128_f)AES_encrypt);
+               else
+                       rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv,
+                                               out, in, inlen,
                                                (block128_f)AES_decrypt);
+               }
        return rv ? (int)rv : -1;
        }
 
@@ -1952,7 +2164,7 @@ static const EVP_CIPHER aes_128_wrap = {
        NID_id_aes128_wrap,
        8, 16, 8, WRAP_FLAGS,
        aes_wrap_init_key, aes_wrap_cipher,
-       NULL,   
+       NULL,
        sizeof(EVP_AES_WRAP_CTX),
        NULL,NULL,NULL,NULL };
 
@@ -1965,7 +2177,7 @@ static const EVP_CIPHER aes_192_wrap = {
        NID_id_aes192_wrap,
        8, 24, 8, WRAP_FLAGS,
        aes_wrap_init_key, aes_wrap_cipher,
-       NULL,   
+       NULL,
        sizeof(EVP_AES_WRAP_CTX),
        NULL,NULL,NULL,NULL };
 
@@ -1978,7 +2190,7 @@ static const EVP_CIPHER aes_256_wrap = {
        NID_id_aes256_wrap,
        8, 32, 8, WRAP_FLAGS,
        aes_wrap_init_key, aes_wrap_cipher,
-       NULL,   
+       NULL,
        sizeof(EVP_AES_WRAP_CTX),
        NULL,NULL,NULL,NULL };
 
@@ -1987,4 +2199,43 @@ const EVP_CIPHER *EVP_aes_256_wrap(void)
        return &aes_256_wrap;
        }
 
+static const EVP_CIPHER aes_128_wrap_pad = {
+       NID_id_aes128_wrap_pad,
+       8, 16, 4, WRAP_FLAGS,
+       aes_wrap_init_key, aes_wrap_cipher,
+       NULL,
+       sizeof(EVP_AES_WRAP_CTX),
+       NULL,NULL,NULL,NULL };
+
+const EVP_CIPHER *EVP_aes_128_wrap_pad(void)
+       {
+       return &aes_128_wrap_pad;
+       }
+
+static const EVP_CIPHER aes_192_wrap_pad = {
+       NID_id_aes192_wrap_pad,
+       8, 24, 4, WRAP_FLAGS,
+       aes_wrap_init_key, aes_wrap_cipher,
+       NULL,
+       sizeof(EVP_AES_WRAP_CTX),
+       NULL,NULL,NULL,NULL };
+
+const EVP_CIPHER *EVP_aes_192_wrap_pad(void)
+       {
+       return &aes_192_wrap_pad;
+       }
+
+static const EVP_CIPHER aes_256_wrap_pad = {
+       NID_id_aes256_wrap_pad,
+       8, 32, 4, WRAP_FLAGS,
+       aes_wrap_init_key, aes_wrap_cipher,
+       NULL,
+       sizeof(EVP_AES_WRAP_CTX),
+       NULL,NULL,NULL,NULL };
+
+const EVP_CIPHER *EVP_aes_256_wrap_pad(void)
+       {
+       return &aes_256_wrap_pad;
+       }
+
 #endif