Engage crypto/modes.
authorAndy Polyakov <appro@openssl.org>
Tue, 23 Dec 2008 11:33:01 +0000 (11:33 +0000)
committerAndy Polyakov <appro@openssl.org>
Tue, 23 Dec 2008 11:33:01 +0000 (11:33 +0000)
15 files changed:
Makefile.org
crypto/aes/Makefile
crypto/aes/aes_cbc.c
crypto/aes/aes_cfb.c
crypto/aes/aes_ctr.c
crypto/aes/aes_ofb.c
crypto/camellia/Makefile
crypto/camellia/cmll_cbc.c
crypto/camellia/cmll_cfb.c
crypto/camellia/cmll_ctr.c
crypto/camellia/cmll_ofb.c
crypto/seed/Makefile
crypto/seed/seed_cbc.c
crypto/seed/seed_cfb.c
crypto/seed/seed_ofb.c

index 97e8e7289094c8231a7d1e776cb309b20b6ba1c4..695a4742c2bad5d37c6f2a9d68a61cc227aa4056 100644 (file)
@@ -115,7 +115,7 @@ SHLIBDIRS= crypto ssl
 SDIRS=  \
        objects \
        md2 md4 md5 sha mdc2 hmac ripemd whrlpool \
-       des aes rc2 rc4 rc5 idea bf cast camellia seed \
+       des aes rc2 rc4 rc5 idea bf cast camellia seed modes \
        bn ec rsa dsa ecdsa dh ecdh dso engine \
        buffer bio stack lhash rand err \
        evp asn1 pem x509 x509v3 conf txt_db pkcs7 pkcs12 comp ocsp ui krb5 \
index 0ebd4a2642b594313d3fe3abdb931608b52a79d8..c501a43a8f6b703df54bdb5a4ce246686883822c 100644 (file)
@@ -100,18 +100,14 @@ clean:
 
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
-aes_cbc.o: ../../include/openssl/aes.h ../../include/openssl/e_os2.h
-aes_cbc.o: ../../include/openssl/opensslconf.h aes_cbc.c aes_locl.h
-aes_cfb.o: ../../e_os.h ../../include/openssl/aes.h
-aes_cfb.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
-aes_cfb.o: aes_cfb.c aes_locl.h
+aes_cbc.o: ../../include/openssl/aes.h ../../include/openssl/modes.h
+aes_cbc.o: ../../include/openssl/opensslconf.h aes_cbc.c
+aes_cfb.o: ../../include/openssl/aes.h ../../include/openssl/modes.h
+aes_cfb.o: ../../include/openssl/opensslconf.h aes_cfb.c
 aes_core.o: ../../include/openssl/aes.h ../../include/openssl/e_os2.h
 aes_core.o: ../../include/openssl/opensslconf.h aes_core.c aes_locl.h
-aes_ctr.o: ../../include/openssl/aes.h ../../include/openssl/crypto.h
-aes_ctr.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
-aes_ctr.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
-aes_ctr.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
-aes_ctr.o: ../../include/openssl/symhacks.h aes_ctr.c aes_locl.h
+aes_ctr.o: ../../include/openssl/aes.h ../../include/openssl/modes.h
+aes_ctr.o: ../../include/openssl/opensslconf.h aes_ctr.c
 aes_ecb.o: ../../include/openssl/aes.h ../../include/openssl/e_os2.h
 aes_ecb.o: ../../include/openssl/opensslconf.h aes_ecb.c aes_locl.h
 aes_ige.o: ../../e_os.h ../../include/openssl/aes.h ../../include/openssl/bio.h
@@ -124,8 +120,8 @@ aes_ige.o: ../../include/openssl/symhacks.h ../cryptlib.h aes_ige.c aes_locl.h
 aes_misc.o: ../../include/openssl/aes.h ../../include/openssl/e_os2.h
 aes_misc.o: ../../include/openssl/opensslconf.h
 aes_misc.o: ../../include/openssl/opensslv.h aes_locl.h aes_misc.c
-aes_ofb.o: ../../include/openssl/aes.h ../../include/openssl/e_os2.h
-aes_ofb.o: ../../include/openssl/opensslconf.h aes_locl.h aes_ofb.c
+aes_ofb.o: ../../include/openssl/aes.h ../../include/openssl/modes.h
+aes_ofb.o: ../../include/openssl/opensslconf.h aes_ofb.c
 aes_wrap.o: ../../e_os.h ../../include/openssl/aes.h
 aes_wrap.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
 aes_wrap.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
index 5ce614570fd2ea026006d0b1986412a218ba49ef..2ae0f4a365d982da8de3d9bc52593c33e74edb87 100644 (file)
  *
  */
 
-#ifndef AES_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-#include <assert.h>
-
 #include <openssl/aes.h>
-#include "aes_locl.h"
+#include <openssl/modes.h>
 
 void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
                     size_t len, const AES_KEY *key,
                     unsigned char *ivec, const int enc) {
 
-       size_t n;
-       unsigned char tmp[AES_BLOCK_SIZE];
-       const unsigned char *iv = ivec;
-
-       assert(in && out && key && ivec);
-       assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc));
-
-       if (AES_ENCRYPT == enc) {
-               while (len >= AES_BLOCK_SIZE) {
-                       for(n=0; n < AES_BLOCK_SIZE; ++n)
-                               out[n] = in[n] ^ iv[n];
-                       AES_encrypt(out, out, key);
-                       iv = out;
-                       len -= AES_BLOCK_SIZE;
-                       in += AES_BLOCK_SIZE;
-                       out += AES_BLOCK_SIZE;
-               }
-               if (len) {
-                       for(n=0; n < len; ++n)
-                               out[n] = in[n] ^ iv[n];
-                       for(n=len; n < AES_BLOCK_SIZE; ++n)
-                               out[n] = iv[n];
-                       AES_encrypt(out, out, key);
-                       iv = out;
-               }
-               memcpy(ivec,iv,AES_BLOCK_SIZE);
-       } else if (in != out) {
-               while (len >= AES_BLOCK_SIZE) {
-                       AES_decrypt(in, out, key);
-                       for(n=0; n < AES_BLOCK_SIZE; ++n)
-                               out[n] ^= iv[n];
-                       iv = in;
-                       len -= AES_BLOCK_SIZE;
-                       in  += AES_BLOCK_SIZE;
-                       out += AES_BLOCK_SIZE;
-               }
-               if (len) {
-                       AES_decrypt(in,tmp,key);
-                       for(n=0; n < len; ++n)
-                               out[n] = tmp[n] ^ iv[n];
-                       iv = in;
-               }
-               memcpy(ivec,iv,AES_BLOCK_SIZE);
-       } else {
-               while (len >= AES_BLOCK_SIZE) {
-                       memcpy(tmp, in, AES_BLOCK_SIZE);
-                       AES_decrypt(in, out, key);
-                       for(n=0; n < AES_BLOCK_SIZE; ++n)
-                               out[n] ^= ivec[n];
-                       memcpy(ivec, tmp, AES_BLOCK_SIZE);
-                       len -= AES_BLOCK_SIZE;
-                       in += AES_BLOCK_SIZE;
-                       out += AES_BLOCK_SIZE;
-               }
-               if (len) {
-                       memcpy(tmp, in, AES_BLOCK_SIZE);
-                       AES_decrypt(tmp, out, key);
-                       for(n=0; n < len; ++n)
-                               out[n] ^= ivec[n];
-                       for(n=len; n < AES_BLOCK_SIZE; ++n)
-                               out[n] = tmp[n];
-                       memcpy(ivec, tmp, AES_BLOCK_SIZE);
-               }
-       }
+       if (enc)
+               CRYPTO_cbc128_encrypt(in,out,len,key,ivec,(block_f)AES_encrypt);
+       else
+               CRYPTO_cbc128_decrypt(in,out,len,key,ivec,(block_f)AES_decrypt);
 }
index b085316941344eba3c122927f502e9bbd2e2865d..0c6d058ce729d975a2037a7f5da839ada8853089 100644 (file)
  *
  */
 
-#ifndef AES_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-#include <assert.h>
-
 #include <openssl/aes.h>
-#include "aes_locl.h"
-#include "e_os.h"
-
-#define STRICT_ALIGNMENT
-#if defined(__i386) || defined(__i386__) || \
-    defined(__x86_64) || defined(__x86_64__) || \
-    defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64)
-#  undef STRICT_ALIGNMENT
-#endif
+#include <openssl/modes.h>
 
 /* The input and output encrypted as though 128bit cfb mode is being
  * used.  The extra state information to record how much of the
@@ -76,177 +61,21 @@ void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out,
        size_t length, const AES_KEY *key,
        unsigned char *ivec, int *num, const int enc) {
 
-    unsigned int n;
-    size_t l = 0;
-
-    assert(in && out && key && ivec && num);
-
-    n = *num;
-
-#if !defined(OPENSSL_SMALL_FOOTPRINT)
-    if (AES_BLOCK_SIZE%sizeof(size_t) == 0) {  /* always true actually */
-       if (enc) {
-               if (n) {
-                       while (length) {
-                               *(out++) = ivec[n] ^= *(in++);
-                               length--;
-                               if(!(n = (n + 1) % AES_BLOCK_SIZE))
-                                       break;
-                       }
-               }
-#if defined(STRICT_ALIGNMENT)
-               if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
-                       goto enc_unaligned;
-#endif
-               while ((l + AES_BLOCK_SIZE) <= length) {
-                       unsigned int i;
-                       AES_encrypt(ivec, ivec, key);
-                       for (i=0;i<AES_BLOCK_SIZE;i+=sizeof(size_t)) {
-                               *(size_t*)(out+l+i) =
-                               *(size_t*)(ivec+i) ^= *(size_t*)(in+l+i);
-                       }
-                       l += AES_BLOCK_SIZE;
-               }
-
-               if (l < length) {
-                       AES_encrypt(ivec, ivec, key);
-                       do {    out[l] = ivec[n] ^= in[l];
-                               l++; n++;
-                       } while (l < length);
-               }
-       } else {
-               if (n) {
-                       while (length) {
-                               unsigned char c;
-                               *(out++) = ivec[n] ^ (c = *(in++)); ivec[n] = c;
-                               length--;
-                               if(!(n = (n + 1) % AES_BLOCK_SIZE))
-                                       break;
-                       }
-               }
-#if defined(STRICT_ALIGNMENT)
-               if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
-                       goto dec_unaligned;
-#endif
-               while (l + AES_BLOCK_SIZE <= length) {
-                       unsigned int i;
-                       AES_encrypt(ivec, ivec, key);
-                       for (i=0;i<AES_BLOCK_SIZE;i+=sizeof(size_t)) {
-                               size_t t = *(size_t*)(in+l+i);
-                               *(size_t*)(out+l+i) = *(size_t*)(ivec+i) ^ t;
-                               *(size_t*)(ivec+i) = t;
-                       }
-                       l += AES_BLOCK_SIZE;
-               }
-
-               if (l < length) {
-                       AES_encrypt(ivec, ivec, key);
-                       do {    unsigned char c;
-                               out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
-                               l++; n++;
-                       } while (l < length);
-               }
-       }
-       *num = n;
-       return;
-    }
-#endif
-
-    /* this code would be commonly eliminated by x86* compiler */
-       if (enc) {
-#if defined(STRICT_ALIGNMENT) && !defined(OPENSSL_SMALL_FOOTPRINT)
-           enc_unaligned:
-#endif
-               while (l<length) {
-                       if (n == 0) {
-                               AES_encrypt(ivec, ivec, key);
-                       }
-                       out[l] = ivec[n] ^= in[l];
-                       l++;
-                       n = (n+1) % AES_BLOCK_SIZE;
-               }
-       } else {
-#if defined(STRICT_ALIGNMENT) && !defined(OPENSSL_SMALL_FOOTPRINT)
-           dec_unaligned:
-#endif
-               while (l<length) {
-                       unsigned char c;
-                       if (n == 0) {
-                               AES_encrypt(ivec, ivec, key);
-                       }
-                       out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
-                       l++;
-                       n = (n+1) % AES_BLOCK_SIZE;
-               }
-       }
-
-       *num=n;
+       CRYPTO_cfb128_encrypt(in,out,length,key,ivec,num,enc,(block128_f)AES_encrypt);
 }
 
-/* This expects a single block of size nbits for both in and out. Note that
-   it corrupts any extra bits in the last byte of out */
-void AES_cfbr_encrypt_block(const unsigned char *in,unsigned char *out,
-                           const int nbits,const AES_KEY *key,
-                           unsigned char *ivec,const int enc)
-    {
-    int n,rem,num;
-    unsigned char ovec[AES_BLOCK_SIZE*2 + 1];  /* +1 because we dererefence (but don't use) one byte off the end */
-
-    if (nbits<=0 || nbits>128) return;
-
-       /* fill in the first half of the new IV with the current IV */
-       memcpy(ovec,ivec,AES_BLOCK_SIZE);
-       /* construct the new IV */
-       AES_encrypt(ivec,ivec,key);
-       num = (nbits+7)/8;
-       if (enc)        /* encrypt the input */
-           for(n=0 ; n < num ; ++n)
-               out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n] ^ ivec[n]);
-       else            /* decrypt the input */
-           for(n=0 ; n < num ; ++n)
-               out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n]) ^ ivec[n];
-       /* shift ovec left... */
-       rem = nbits%8;
-       num = nbits/8;
-       if(rem==0)
-           memcpy(ivec,ovec+num,AES_BLOCK_SIZE);
-       else
-           for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
-               ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);
-
-    /* it is not necessary to cleanse ovec, since the IV is not secret */
-    }
-
 /* N.B. This expects the input to be packed, MS bit first */
 void AES_cfb1_encrypt(const unsigned char *in, unsigned char *out,
                      size_t length, const AES_KEY *key,
                      unsigned char *ivec, int *num, const int enc)
     {
-    size_t n;
-    unsigned char c[1],d[1];
-
-    assert(in && out && key && ivec && num);
-    assert(*num == 0);
-
-    memset(out,0,(length+7)/8);
-    for(n=0 ; n < length ; ++n)
-       {
-       c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
-       AES_cfbr_encrypt_block(c,d,1,key,ivec,enc);
-       out[n/8]=(out[n/8]&~(1 << (7-n%8)))|((d[0]&0x80) >> (n%8));
-       }
+    CRYPTO_cfb128_1_encrypt(in,out,length,key,ivec,num,enc,(block128_f)AES_encrypt);
     }
 
 void AES_cfb8_encrypt(const unsigned char *in, unsigned char *out,
                      size_t length, const AES_KEY *key,
                      unsigned char *ivec, int *num, const int enc)
     {
-    size_t n;
-
-    assert(in && out && key && ivec && num);
-    assert(*num == 0);
-
-    for(n=0 ; n < length ; ++n)
-       AES_cfbr_encrypt_block(&in[n],&out[n],8,key,ivec,enc);
+    CRYPTO_cfb128_8_encrypt(in,out,length,key,ivec,num,enc,(block128_f)AES_encrypt);
     }
 
index 9b793a9e235e36735fbdbfa5b976912939ecf5b4..7c9d165d8adbf35a6054e42032eb342164ca0a71 100644 (file)
  *
  */
 
-#ifndef AES_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-
 #include <openssl/aes.h>
-#include <openssl/crypto.h>
-#include "aes_locl.h"
-
-/* NOTE: the IV/counter CTR mode is big-endian.  The rest of the AES code
- * is endian-neutral. */
-
-/* increment counter (128-bit int) by 1 */
-static void AES_ctr128_inc(unsigned char *counter) {
-       unsigned long c;
-
-       /* Grab bottom dword of counter and increment */
-       c = GETU32(counter + 12);
-       c++;    c &= 0xFFFFFFFF;
-       PUTU32(counter + 12, c);
-
-       /* if no overflow, we're done */
-       if (c)
-               return;
-
-       /* Grab 1st dword of counter and increment */
-       c = GETU32(counter +  8);
-       c++;    c &= 0xFFFFFFFF;
-       PUTU32(counter +  8, c);
-
-       /* if no overflow, we're done */
-       if (c)
-               return;
-
-       /* Grab 2nd dword of counter and increment */
-       c = GETU32(counter +  4);
-       c++;    c &= 0xFFFFFFFF;
-       PUTU32(counter +  4, c);
-
-       /* if no overflow, we're done */
-       if (c)
-               return;
+#include <openssl/modes.h>
 
-       /* Grab top dword of counter and increment */
-       c = GETU32(counter +  0);
-       c++;    c &= 0xFFFFFFFF;
-       PUTU32(counter +  0, c);
-}
-
-/* The input encrypted as though 128bit counter mode is being
- * used.  The extra state information to record how much of the
- * 128bit block we have used is contained in *num, and the
- * encrypted counter is kept in ecount_buf.  Both *num and
- * ecount_buf must be initialised with zeros before the first
- * call to AES_ctr128_encrypt().
- *
- * This algorithm assumes that the counter is in the x lower bits
- * of the IV (ivec), and that the application has full control over
- * overflow and the rest of the IV.  This implementation takes NO
- * responsability for checking that the counter doesn't overflow
- * into the rest of the IV when incremented.
- */
 void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out,
                        size_t length, const AES_KEY *key,
                        unsigned char ivec[AES_BLOCK_SIZE],
                        unsigned char ecount_buf[AES_BLOCK_SIZE],
                        unsigned int *num) {
-       unsigned int n;
-
-       OPENSSL_assert(in && out && key && ecount_buf && num);
-       OPENSSL_assert(*num < AES_BLOCK_SIZE);
-
-       n = *num;
-
-       while (length--) {
-               if (n == 0) {
-                       AES_encrypt(ivec, ecount_buf, key);
-                       AES_ctr128_inc(ivec);
-               }
-               *(out++) = *(in++) ^ ecount_buf[n];
-               n = (n+1) % AES_BLOCK_SIZE;
-       }
-
-       *num=n;
+       CRYPTO_ctr128_encrypt(in,out,length,key,ivec,ecount_buf,num,(block128_f)AES_encrypt);
 }
index b71c6f16f8f49cb65283cc26c6ec3451f5843ae2..50bf0b83250478e8759f523b652cdbb86828abe6 100644 (file)
  *
  */
 
-#ifndef AES_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-#include <assert.h>
-
 #include <openssl/aes.h>
-#include "aes_locl.h"
-
-#define STRICT_ALIGNMENT
-#if defined(__i386) || defined(__i386__) || \
-    defined(__x86_64) || defined(__x86_64__) || \
-    defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64)
-#  undef STRICT_ALIGNMENT
-#endif
+#include <openssl/modes.h>
 
-/* The input and output encrypted as though 128bit ofb mode is being
- * used.  The extra state information to record how much of the
- * 128bit block we have used is contained in *num;
- */
 void AES_ofb128_encrypt(const unsigned char *in, unsigned char *out,
        size_t length, const AES_KEY *key,
-       unsigned char *ivec, int *num) {
-
-       unsigned int n;
-       size_t l=0;
-
-       assert(in && out && key && ivec && num);
-
-       n = *num;
-
-#if !defined(OPENSSL_SMALL_FOOTPRINT)
-       if (AES_BLOCK_SIZE%sizeof(size_t) == 0) do { /* always true actually */
-               if (n) {
-                       while (length) {
-                               *(out++) = ivec[n] ^ *(in++);
-                               length--;
-                               if(!(n = (n + 1) % AES_BLOCK_SIZE))
-                                       break;
-                       }
-               }
-#if defined(STRICT_ALIGNMENT)
-               if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
-                       break;
-#endif
-               while ((l + AES_BLOCK_SIZE) <= length) {
-                       unsigned int i;
-                       AES_encrypt(ivec, ivec, key);
-                       for (i=0;i<AES_BLOCK_SIZE;i+=sizeof(size_t)) {
-                               *(size_t*)(out+l+i) =
-                               *(size_t*)(ivec+i) ^ *(size_t*)(in+l+i);
-                       }
-                       l += AES_BLOCK_SIZE;
-               }
-
-               if (l < length) {
-                       AES_encrypt(ivec, ivec, key);
-                       do {    out[l] = ivec[n] ^ in[l];
-                               l++; n++;
-                       } while (l < length);
-               }
-               *num = n;
-               return;
-       } while(0);
-#endif
-
-       /* this code would be commonly eliminated by x86* compiler */
-       while (l<length) {
-               if (n == 0) {
-                       AES_encrypt(ivec, ivec, key);
-               }
-               out[l] = ivec[n] ^ in[l];
-               l++;
-               n = (n+1) % AES_BLOCK_SIZE;
-       }
-
-       *num=n;
+       unsigned char *ivec, int *num)
+{
+       CRYPTO_ofb128_encrypt(in,out,length,key,ivec,num,(block128_f)AES_encrypt);
 }
index 76331ff07acf4b7e9837caea4e0815f57e721cdd..ff5fe4a01dba4da9aa6697cf3b1c0d61147b3355 100644 (file)
@@ -88,20 +88,16 @@ clean:
 
 camellia.o: ../../include/openssl/opensslconf.h camellia.c camellia.h
 camellia.o: cmll_locl.h
-cmll_cbc.o: ../../include/openssl/camellia.h
-cmll_cbc.o: ../../include/openssl/opensslconf.h cmll_cbc.c cmll_locl.h
-cmll_cfb.o: ../../e_os.h ../../include/openssl/camellia.h
-cmll_cfb.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
-cmll_cfb.o: cmll_cfb.c cmll_locl.h
-cmll_ctr.o: ../../include/openssl/camellia.h ../../include/openssl/crypto.h
-cmll_ctr.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
-cmll_ctr.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
-cmll_ctr.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
-cmll_ctr.o: ../../include/openssl/symhacks.h cmll_ctr.c cmll_locl.h
+cmll_cbc.o: ../../include/openssl/camellia.h ../../include/openssl/modes.h
+cmll_cbc.o: ../../include/openssl/opensslconf.h cmll_cbc.c
+cmll_cfb.o: ../../include/openssl/camellia.h ../../include/openssl/modes.h
+cmll_cfb.o: ../../include/openssl/opensslconf.h cmll_cfb.c
+cmll_ctr.o: ../../include/openssl/camellia.h ../../include/openssl/modes.h
+cmll_ctr.o: ../../include/openssl/opensslconf.h cmll_ctr.c
 cmll_ecb.o: ../../include/openssl/camellia.h
 cmll_ecb.o: ../../include/openssl/opensslconf.h cmll_ecb.c cmll_locl.h
 cmll_misc.o: ../../include/openssl/camellia.h
 cmll_misc.o: ../../include/openssl/opensslconf.h
 cmll_misc.o: ../../include/openssl/opensslv.h cmll_locl.h cmll_misc.c
-cmll_ofb.o: ../../include/openssl/camellia.h
-cmll_ofb.o: ../../include/openssl/opensslconf.h cmll_locl.h cmll_ofb.c
+cmll_ofb.o: ../../include/openssl/camellia.h ../../include/openssl/modes.h
+cmll_ofb.o: ../../include/openssl/opensslconf.h cmll_ofb.c
index 421c33127bd574d638b3a47f53050bf7187170b3..400a248850b7adc4f4c7a98ffacd61fb611990d5 100644 (file)
  *
  */
 
-#ifndef CAMELLIA_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-#include <assert.h>
-#include <string.h>
-
 #include <openssl/camellia.h>
-#include "cmll_locl.h"
+#include <openssl/modes.h>
+
 void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
        size_t len, const CAMELLIA_KEY *key,
        unsigned char *ivec, const int enc) 
        {
 
-       size_t n;
-       unsigned char tmp[CAMELLIA_BLOCK_SIZE];
-       const unsigned char *iv = ivec;
-
-       assert(in && out && key && ivec);
-       assert((CAMELLIA_ENCRYPT == enc)||(CAMELLIA_DECRYPT == enc));
-
-       if (CAMELLIA_ENCRYPT == enc) 
-               {
-               while (len >= CAMELLIA_BLOCK_SIZE) 
-                       {
-                       for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
-                               out[n] = in[n] ^ iv[n];
-                       Camellia_encrypt(out, out, key);
-                       iv = out;
-                       len -= CAMELLIA_BLOCK_SIZE;
-                       in += CAMELLIA_BLOCK_SIZE;
-                       out += CAMELLIA_BLOCK_SIZE;
-                       }
-               if (len) 
-                       {
-                       for(n=0; n < len; ++n)
-                               out[n] = in[n] ^ iv[n];
-                       for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
-                               out[n] = iv[n];
-                       Camellia_encrypt(out, out, key);
-                       iv = out;
-                       }
-               memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
-               } 
-       else if (in != out) 
-               {
-               while (len >= CAMELLIA_BLOCK_SIZE) 
-                       {
-                       Camellia_decrypt(in, out, key);
-                       for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
-                               out[n] ^= iv[n];
-                       iv = in;
-                       len -= CAMELLIA_BLOCK_SIZE;
-                       in  += CAMELLIA_BLOCK_SIZE;
-                       out += CAMELLIA_BLOCK_SIZE;
-                       }
-               if (len) 
-                       {
-                       Camellia_decrypt(in,tmp,key);
-                       for(n=0; n < len; ++n)
-                               out[n] = tmp[n] ^ iv[n];
-                       iv = in;
-                       }
-               memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
-               } 
-       else 
-               {
-               while (len >= CAMELLIA_BLOCK_SIZE) 
-                       {
-                       memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
-                       Camellia_decrypt(in, out, key);
-                       for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
-                               out[n] ^= ivec[n];
-                       memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
-                       len -= CAMELLIA_BLOCK_SIZE;
-                       in += CAMELLIA_BLOCK_SIZE;
-                       out += CAMELLIA_BLOCK_SIZE;
-                       }
-               if (len)
-                       {
-                       memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
-                       Camellia_decrypt(tmp, out, key);
-                       for(n=0; n < len; ++n)
-                               out[n] ^= ivec[n];
-                       for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
-                               out[n] = tmp[n];
-                       memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
-                       }
-               }
+       if (enc)
+               CRYPTO_cbc128_encrypt(in,out,len,key,ivec,(block_f)Camellia_encrypt);
+       else
+               CRYPTO_cbc128_decrypt(in,out,len,key,ivec,(block_f)Camellia_decrypt);
        }
index 8eed2be9ea3696fa3d62ad16656bf602e0dece17..3d81b51d3f42792d15f289cb5f75fba639353614 100644 (file)
  * [including the GNU Public Licence.]
  */
 
-#ifndef CAMELLIA_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-#include <assert.h>
-#include <string.h>
-
 #include <openssl/camellia.h>
-#include "cmll_locl.h"
-#include "e_os.h"
+#include <openssl/modes.h>
 
 
 /* The input and output encrypted as though 128bit cfb mode is being
@@ -128,75 +119,7 @@ void Camellia_cfb128_encrypt(const unsigned char *in, unsigned char *out,
        unsigned char *ivec, int *num, const int enc)
        {
 
-       unsigned int n;
-       unsigned char c;
-
-       assert(in && out && key && ivec && num);
-
-       n = *num;
-
-       if (enc) 
-               {
-               while (length--) 
-                       {
-                       if (n == 0) 
-                               {
-                               Camellia_encrypt(ivec, ivec, key);
-                               }
-                       ivec[n] = *(out++) = *(in++) ^ ivec[n];
-                       n = (n+1) % CAMELLIA_BLOCK_SIZE;
-                       }
-               } 
-       else 
-               {
-               while (length--) 
-                       {
-                       if (n == 0) 
-                               {
-                               Camellia_encrypt(ivec, ivec, key);
-                               }
-                       c = *(in);
-                       *(out++) = *(in++) ^ ivec[n];
-                       ivec[n] = c;
-                       n = (n+1) % CAMELLIA_BLOCK_SIZE;
-                       }
-               }
-
-       *num=n;
-       }
-
-/* This expects a single block of size nbits for both in and out. Note that
-   it corrupts any extra bits in the last byte of out */
-void Camellia_cfbr_encrypt_block(const unsigned char *in,unsigned char *out,
-       const int nbits,const CAMELLIA_KEY *key,
-       unsigned char *ivec,const int enc)
-       {
-       int n,rem,num;
-       unsigned char ovec[CAMELLIA_BLOCK_SIZE*2];
-
-       if (nbits<=0 || nbits>128) return;
-
-       /* fill in the first half of the new IV with the current IV */
-       memcpy(ovec,ivec,CAMELLIA_BLOCK_SIZE);
-       /* construct the new IV */
-       Camellia_encrypt(ivec,ivec,key);
-       num = (nbits+7)/8;
-       if (enc)        /* encrypt the input */
-               for(n=0 ; n < num ; ++n)
-                       out[n] = (ovec[CAMELLIA_BLOCK_SIZE+n] = in[n] ^ ivec[n]);
-       else            /* decrypt the input */
-               for(n=0 ; n < num ; ++n)
-                       out[n] = (ovec[CAMELLIA_BLOCK_SIZE+n] = in[n]) ^ ivec[n];
-       /* shift ovec left... */
-       rem = nbits%8;
-       num = nbits/8;
-       if(rem==0)
-               memcpy(ivec,ovec+num,CAMELLIA_BLOCK_SIZE);
-       else
-               for(n=0 ; n < CAMELLIA_BLOCK_SIZE ; ++n)
-                       ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);
-
-       /* it is not necessary to cleanse ovec, since the IV is not secret */
+       CRYPTO_cfb128_encrypt(in,out,length,key,ivec,num,enc,(block128_f)Camellia_encrypt);
        }
 
 /* N.B. This expects the input to be packed, MS bit first */
@@ -204,31 +127,13 @@ void Camellia_cfb1_encrypt(const unsigned char *in, unsigned char *out,
        size_t length, const CAMELLIA_KEY *key,
        unsigned char *ivec, int *num, const int enc)
        {
-       size_t n;
-       unsigned char c[1],d[1];
-
-       assert(in && out && key && ivec && num);
-       assert(*num == 0);
-
-       memset(out,0,(length+7)/8);
-       for(n=0 ; n < length ; ++n)
-               {
-               c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
-               Camellia_cfbr_encrypt_block(c,d,1,key,ivec,enc);
-               out[n/8]=(out[n/8]&~(1 << (7-n%8)))|((d[0]&0x80) >> (n%8));
-               }
+       CRYPTO_cfb128_1_encrypt(in,out,length,key,ivec,num,enc,(block128_f)Camellia_encrypt);
        }
 
 void Camellia_cfb8_encrypt(const unsigned char *in, unsigned char *out,
        size_t length, const CAMELLIA_KEY *key,
        unsigned char *ivec, int *num, const int enc)
        {
-       size_t n;
-
-       assert(in && out && key && ivec && num);
-       assert(*num == 0);
-
-       for(n=0 ; n < length ; ++n)
-               Camellia_cfbr_encrypt_block(&in[n],&out[n],8,key,ivec,enc);
+       CRYPTO_cfb128_8_encrypt(in,out,length,key,ivec,num,enc,(block128_f)Camellia_encrypt);
        }
 
index a6cdc6695793764f9f3f1693f5231979c1a1b4df..014e621a34bc6ecdd7efc5079a660e5b408729b5 100644 (file)
  *
  */
 
-#ifndef CAMELLIA_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-#include <assert.h>
-
 #include <openssl/camellia.h>
-#include <openssl/crypto.h>
-#include "cmll_locl.h"
-
-/* NOTE: the IV/counter CTR mode is big-endian.  The rest of the Camellia code
- * is endian-neutral. */
-/* increment counter (128-bit int) by 1 */
-static void Camellia_ctr128_inc(unsigned char *counter) 
-       {
-       unsigned long c;
-
-       /* Grab bottom dword of counter and increment */
-       c = GETU32(counter + 12);
-       c++;    c &= 0xFFFFFFFF;
-       PUTU32(counter + 12, c);
-
-       /* if no overflow, we're done */
-       if (c)
-               return;
-
-       /* Grab 1st dword of counter and increment */
-       c = GETU32(counter +  8);
-       c++;    c &= 0xFFFFFFFF;
-       PUTU32(counter +  8, c);
-
-       /* if no overflow, we're done */
-       if (c)
-               return;
-
-       /* Grab 2nd dword of counter and increment */
-       c = GETU32(counter +  4);
-       c++;    c &= 0xFFFFFFFF;
-       PUTU32(counter +  4, c);
-
-       /* if no overflow, we're done */
-       if (c)
-               return;
+#include <openssl/modes.h>
 
-       /* Grab top dword of counter and increment */
-       c = GETU32(counter +  0);
-       c++;    c &= 0xFFFFFFFF;
-       PUTU32(counter +  0, c);
-       }
-
-/* The input encrypted as though 128bit counter mode is being
- * used.  The extra state information to record how much of the
- * 128bit block we have used is contained in *num, and the
- * encrypted counter is kept in ecount_buf.  Both *num and
- * ecount_buf must be initialised with zeros before the first
- * call to Camellia_ctr128_encrypt().
- *
- * This algorithm assumes that the counter is in the x lower bits
- * of the IV (ivec), and that the application has full control over
- * overflow and the rest of the IV.  This implementation takes NO
- * responsability for checking that the counter doesn't overflow
- * into the rest of the IV when incremented.
- */
 void Camellia_ctr128_encrypt(const unsigned char *in, unsigned char *out,
        size_t length, const CAMELLIA_KEY *key,
        unsigned char ivec[CAMELLIA_BLOCK_SIZE],
@@ -120,24 +59,6 @@ void Camellia_ctr128_encrypt(const unsigned char *in, unsigned char *out,
        unsigned int *num) 
        {
 
-       unsigned int n;
-
-       OPENSSL_assert(in && out && key && ecount_buf && num);
-       OPENSSL_assert(*num < CAMELLIA_BLOCK_SIZE);
-
-       n = *num;
-
-       while (length--) 
-               {
-               if (n == 0) 
-                       {
-                       Camellia_encrypt(ivec, ecount_buf, key);
-                       Camellia_ctr128_inc(ivec);
-                       }
-               *(out++) = *(in++) ^ ecount_buf[n];
-               n = (n+1) % CAMELLIA_BLOCK_SIZE;
-               }
-
-       *num=n;
+       CRYPTO_ctr128_encrypt(in,out,length,key,ivec,ecount_buf,num,(block128_f)Camellia_encrypt);
        }
 
index 1646fc0ce78a4262122aefc2fa5fb5a85e34d97d..a482befc74707df29eea9f43444bdde4a2c291ac 100644 (file)
  * [including the GNU Public Licence.]
  */
 
-#ifndef CAMELLIA_DEBUG
-# ifndef NDEBUG
-#  define NDEBUG
-# endif
-#endif
-#include <assert.h>
 #include <openssl/camellia.h>
-#include "cmll_locl.h"
+#include <openssl/modes.h>
 
 /* The input and output encrypted as though 128bit ofb mode is being
  * used.  The extra state information to record how much of the
 void Camellia_ofb128_encrypt(const unsigned char *in, unsigned char *out,
        size_t length, const CAMELLIA_KEY *key,
        unsigned char *ivec, int *num) {
-
-       unsigned int n;
-
-       assert(in && out && key && ivec && num);
-
-       n = *num;
-
-       while (length--) {
-               if (n == 0) {
-                       Camellia_encrypt(ivec, ivec, key);
-               }
-               *(out++) = *(in++) ^ ivec[n];
-               n = (n+1) % CAMELLIA_BLOCK_SIZE;
-       }
-
-       *num=n;
+       CRYPTO_ofb128_encrypt(in,out,length,key,ivec,num,(block128_f)Camellia_encrypt);
 }
index 4babe82f74f34234243c39e5456721ba9d5fa982..4bc55e4916491bdd3a0c80a9be27424a0091705f 100644 (file)
@@ -81,17 +81,17 @@ seed.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h
 seed.o: ../../include/openssl/seed.h ../../include/openssl/stack.h
 seed.o: ../../include/openssl/symhacks.h seed.c seed_locl.h
 seed_cbc.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
-seed_cbc.o: ../../include/openssl/opensslconf.h
+seed_cbc.o: ../../include/openssl/modes.h ../../include/openssl/opensslconf.h
 seed_cbc.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 seed_cbc.o: ../../include/openssl/safestack.h ../../include/openssl/seed.h
 seed_cbc.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
-seed_cbc.o: seed_cbc.c seed_locl.h
+seed_cbc.o: seed_cbc.c
 seed_cfb.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
-seed_cfb.o: ../../include/openssl/opensslconf.h
+seed_cfb.o: ../../include/openssl/modes.h ../../include/openssl/opensslconf.h
 seed_cfb.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 seed_cfb.o: ../../include/openssl/safestack.h ../../include/openssl/seed.h
 seed_cfb.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
-seed_cfb.o: seed_cfb.c seed_locl.h
+seed_cfb.o: seed_cfb.c
 seed_ecb.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
 seed_ecb.o: ../../include/openssl/opensslconf.h
 seed_ecb.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
@@ -99,8 +99,8 @@ seed_ecb.o: ../../include/openssl/safestack.h ../../include/openssl/seed.h
 seed_ecb.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
 seed_ecb.o: seed_ecb.c
 seed_ofb.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
-seed_ofb.o: ../../include/openssl/opensslconf.h
+seed_ofb.o: ../../include/openssl/modes.h ../../include/openssl/opensslconf.h
 seed_ofb.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 seed_ofb.o: ../../include/openssl/safestack.h ../../include/openssl/seed.h
 seed_ofb.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
-seed_ofb.o: seed_locl.h seed_ofb.c
+seed_ofb.o: seed_ofb.c
index 4f718ccb44e63f077ea0ee554c1bfcf17d042bc5..6c3f9b527af7d2bbf82d1b84e4b8b25049c49d01 100644 (file)
  *
  */
 
-#include "seed_locl.h"
-#include <string.h>
+#include <openssl/seed.h>
+#include <openssl/modes.h>
 
 void SEED_cbc_encrypt(const unsigned char *in, unsigned char *out,
                       size_t len, const SEED_KEY_SCHEDULE *ks,
                       unsigned char ivec[SEED_BLOCK_SIZE], int enc)
        {
-       size_t n;
-       unsigned char tmp[SEED_BLOCK_SIZE];
-       const unsigned char *iv = ivec;
-
        if (enc)
-               {
-               while (len >= SEED_BLOCK_SIZE)
-                       {
-                       for (n = 0; n < SEED_BLOCK_SIZE; ++n)
-                               out[n] = in[n] ^ iv[n];
-                       SEED_encrypt(out, out, ks);
-                       iv = out;
-                       len -= SEED_BLOCK_SIZE;
-                       in  += SEED_BLOCK_SIZE;
-                       out += SEED_BLOCK_SIZE;
-                       }
-               if (len)
-                       {
-                       for (n = 0; n < len; ++n)
-                               out[n] = in[n] ^ iv[n];
-                       for (n = len; n < SEED_BLOCK_SIZE; ++n)
-                               out[n] = iv[n];
-                       SEED_encrypt(out, out, ks);
-                       iv = out;
-                       }
-               memcpy(ivec, iv, SEED_BLOCK_SIZE);
-               }
-       else if (in != out) /* decrypt */
-               {
-               while (len >= SEED_BLOCK_SIZE)
-                       {
-                       SEED_decrypt(in, out, ks);
-                       for (n = 0; n < SEED_BLOCK_SIZE; ++n)
-                               out[n] ^= iv[n];
-                       iv = in;
-                       len -= SEED_BLOCK_SIZE;
-                       in  += SEED_BLOCK_SIZE;
-                       out += SEED_BLOCK_SIZE;
-                       }
-               if (len)
-                       {
-                       SEED_decrypt(in, tmp, ks);
-                       for (n = 0; n < len; ++n)
-                               out[n] = tmp[n] ^ iv[n];
-                       iv = in;
-                       }
-               memcpy(ivec, iv, SEED_BLOCK_SIZE);
-               }
-       else /* decrypt, overlap */
-               {
-               while (len >= SEED_BLOCK_SIZE)
-                       {
-                       memcpy(tmp, in, SEED_BLOCK_SIZE);
-                       SEED_decrypt(in, out, ks);
-                       for (n = 0; n < SEED_BLOCK_SIZE; ++n)
-                               out[n] ^= ivec[n];
-                       memcpy(ivec, tmp, SEED_BLOCK_SIZE);
-                       len -= SEED_BLOCK_SIZE;
-                       in  += SEED_BLOCK_SIZE;
-                       out += SEED_BLOCK_SIZE;
-                       }
-               if (len)
-                       {
-                       memcpy(tmp, in, SEED_BLOCK_SIZE);
-                       SEED_decrypt(tmp, tmp, ks);
-                       for (n = 0; n < len; ++n)
-                               out[n] = tmp[n] ^ ivec[n];
-                       memcpy(ivec, tmp, SEED_BLOCK_SIZE);
-                       }
-               }
+               CRYPTO_cbc128_encrypt(in,out,len,ks,ivec,(block128_f)SEED_encrypt);
+       else
+               CRYPTO_cbc128_decrypt(in,out,len,ks,ivec,(block128_f)SEED_decrypt);
        }
index 07d878a788891190f309991b7a9b1e15b58fadf5..694597dd06e17cde0857dc9042f638e7e33fc399 100644 (file)
  * [including the GNU Public Licence.]
  */
 
-#include "seed_locl.h"
-#include <string.h>
+#include <openssl/seed.h>
+#include <openssl/modes.h>
 
 void SEED_cfb128_encrypt(const unsigned char *in, unsigned char *out,
                          size_t len, const SEED_KEY_SCHEDULE *ks,
                          unsigned char ivec[SEED_BLOCK_SIZE], int *num, int enc)
        {
-       int n;
-       unsigned char c;
-
-       n = *num;
-
-       if (enc)
-               {
-               while (len--)
-                       {
-                       if (n == 0)
-                               SEED_encrypt(ivec, ivec, ks);
-                       ivec[n] = *(out++) = *(in++) ^ ivec[n];
-                       n = (n+1) % SEED_BLOCK_SIZE;
-                       }
-               }
-       else
-               {
-               while (len--)
-                       {
-                       if (n == 0)
-                               SEED_encrypt(ivec, ivec, ks);
-                       c = *(in);
-                       *(out++) = *(in++) ^ ivec[n];
-                       ivec[n] = c;
-                       n = (n+1) % SEED_BLOCK_SIZE;
-                       }
-               }
-
-       *num = n;
+       CRYPTO_cfb128_encrypt(in,out,len,ks,ivec,num,enc,(block128_f)SEED_encrypt);
        }
index e2f3f57a38cd75ad4f5e9623820193cace6b79ae..3c8ba33bb9f90caf7aa5c7ae38e2279d8ef8175d 100644 (file)
  * [including the GNU Public Licence.]
  */
 
-#include "seed_locl.h"
-#include <string.h>
+#include <openssl/seed.h>
+#include <openssl/modes.h>
 
 void SEED_ofb128_encrypt(const unsigned char *in, unsigned char *out,
                          size_t len, const SEED_KEY_SCHEDULE *ks,
                          unsigned char ivec[SEED_BLOCK_SIZE], int *num)
        {
-       int n;
-
-       n = *num;
-       
-       while (len--)
-               {
-               if (n == 0)
-                       SEED_encrypt(ivec, ivec, ks);
-               *(out++) = *(in++) ^ ivec[n];
-               n = (n+1) % SEED_BLOCK_SIZE;
-               }
-
-       *num = n;
+       CRYPTO_ofb128_encrypt(in,out,len,ks,ivec,num,(block128_f)SEED_encrypt);
        }