Because Rijndael is more known as AES, use crypto/aes instead of
[openssl.git] / crypto / evp / e_aes.c
index 3465da5..9d91533 100644 (file)
  *
  */
 
+#ifndef OPENSSL_NO_AES
 #include <openssl/evp.h>
 #include <openssl/err.h>
 #include <string.h>
 #include <assert.h>
+#include <openssl/aes.h>
+#include "evp_locl.h"
 
-static int aes_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                                        const unsigned char *iv, int enc);
-static int aes_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
-                               const unsigned char *in, unsigned int inl);
-static int aes_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
-                               const unsigned char *in, unsigned int inl);
 
-#define IMPLEMENT_AES_CIPHER(name, ciph_func, keylen, ivlen, mode) \
-static const EVP_CIPHER name##_cipher_st = \
-       { \
-       NID_##name, \
-       16,keylen,ivlen, \
-       mode, \
-       aes_init, \
-       ciph_func, \
-       NULL, \
-       sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+ \
-               sizeof((((EVP_CIPHER_CTX *)NULL)->c.rijndael)), \
-       EVP_CIPHER_set_asn1_iv, \
-       EVP_CIPHER_get_asn1_iv, \
-       NULL, \
-       NULL \
-       }; \
-const EVP_CIPHER * EVP_##name(void) \
-       { \
-       return &name##_cipher_st; \
-       }
+typedef struct
+       {
+       AES_KEY ks;
+       } EVP_AES_KEY;
 
-IMPLEMENT_AES_CIPHER(aes_128_ecb, aes_ecb, 16, 0, EVP_CIPH_ECB_MODE)
-IMPLEMENT_AES_CIPHER(aes_192_ecb, aes_ecb, 24, 0, EVP_CIPH_ECB_MODE)
-IMPLEMENT_AES_CIPHER(aes_256_ecb, aes_ecb, 32, 0, EVP_CIPH_ECB_MODE)
+#define data(ctx)      EVP_C_DATA(EVP_AES_KEY,ctx)
 
-IMPLEMENT_AES_CIPHER(aes_128_cbc, aes_cbc, 16, 16, EVP_CIPH_CBC_MODE)
-IMPLEMENT_AES_CIPHER(aes_192_cbc, aes_cbc, 24, 24, EVP_CIPH_CBC_MODE)
-IMPLEMENT_AES_CIPHER(aes_256_cbc, aes_cbc, 32, 32, EVP_CIPH_CBC_MODE)
+#define IMPLEMENT_BLOCK_CIPHER_def_ecb_cbc(cname, ksched, cprefix, kstruct, \
+                         nid, block_size, key_len, iv_len, flags, \
+                         init_key, cleanup, set_asn1, get_asn1, ctrl) \
+BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
+BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
+BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
+                    init_key, cleanup, set_asn1, get_asn1, ctrl) \
+BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, 0, flags, \
+                    init_key, cleanup, set_asn1, get_asn1, ctrl)
 
-static int aes_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
-                  const unsigned char *iv, int enc)
-       {
-       RIJNDAEL_KEY *k=&ctx->c.rijndael;
-       if (enc) 
-               k->rounds = rijndaelKeySetupEnc(k->rd_key, key, ctx->key_len * 8);
-       else
-               k->rounds = rijndaelKeySetupDec(k->rd_key, key, ctx->key_len * 8);
+IMPLEMENT_BLOCK_CIPHER_def_ecb_cbc(aes_128, ks, AES, EVP_AES_KEY,
+                                  NID_aes_128, 16, 16, 16,
+                                  0, aes_init_key, NULL, 
+                                  EVP_CIPHER_set_asn1_iv,
+                                  EVP_CIPHER_get_asn1_iv,
+                                  NULL)
+IMPLEMENT_BLOCK_CIPHER_def_ecb_cbc(aes_192, ks, AES, EVP_AES_KEY,
+                                  NID_aes_192, 16, 24, 16,
+                                  0, aes_init_key, NULL, 
+                                  EVP_CIPHER_set_asn1_iv,
+                                  EVP_CIPHER_get_asn1_iv,
+                                  NULL)
+IMPLEMENT_BLOCK_CIPHER_def_ecb_cbc(aes_256, ks, AES, EVP_AES_KEY,
+                                  NID_aes_256, 16, 32, 16,
+                                  0, aes_init_key, NULL, 
+                                  EVP_CIPHER_set_asn1_iv,
+                                  EVP_CIPHER_get_asn1_iv,
+                                  NULL)
 
-       return 1;
-       }
+static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+                  const unsigned char *iv, int enc) {
 
-static int aes_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
-                        const unsigned char *in, unsigned int inl)
-       {
-       RIJNDAEL_KEY *k=&ctx->c.rijndael;
-       while(inl > 0)
-               {
-               if(ctx->encrypt)
-                       rijndaelEncrypt(k->rd_key,k->rounds, in, out);
-               else
-                       rijndaelDecrypt(k->rd_key,k->rounds, in, out);
-               inl-=16;
-               in+=16;
-               out+=16;
-               }
-       assert(inl == 0);
-
-       return 1;
-       }
+       if (enc) 
+               AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
+       else
+               AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
 
-static int aes_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
-                        const unsigned char *in, unsigned int inl)
-       {
-       int n;
-       unsigned char tmp[16];
-       RIJNDAEL_KEY *k=&ctx->c.rijndael;
-       while(inl > 0)
-               {
-               if(ctx->encrypt)
-                       {
-                       for(n=0 ; n < 16 ; n++)
-                               tmp[n] = in[n] ^ ctx->iv[n];
-                       rijndaelEncrypt(k->rd_key,k->rounds, tmp, out);
-                       memcpy(ctx->iv,out,16);
-                       }
-               else
-                       {
-                       memcpy(tmp, in, 16);
-                       rijndaelDecrypt(k->rd_key,k->rounds, in, out);
-                       for(n=0 ; n < 16 ; n++)
-                               out[n] ^= ctx->iv[n];
-                       memcpy(ctx->iv,tmp,16);
-                       }
-               inl-=16;
-               in+=16;
-               out+=16;
-               }
-       assert(inl == 0);
        return 1;
-       }
+}
 
+#endif