diff options
author | Andy Polyakov <appro@openssl.org> | 2008-12-23 11:33:01 +0000 |
---|---|---|
committer | Andy Polyakov <appro@openssl.org> | 2008-12-23 11:33:01 +0000 |
commit | 5d48a66a6a4bc27b54d32721f7183077489c2e5f (patch) | |
tree | 27b158f856e0636bf349375c047a38b1fc97ee7c /crypto/camellia | |
parent | 63fc7f848d4e047c3bd0f4a1c7e843191b2e9f0a (diff) |
Engage crypto/modes.
Diffstat (limited to 'crypto/camellia')
-rw-r--r-- | crypto/camellia/Makefile | 20 | ||||
-rw-r--r-- | crypto/camellia/cmll_cbc.c | 90 | ||||
-rw-r--r-- | crypto/camellia/cmll_cfb.c | 103 | ||||
-rw-r--r-- | crypto/camellia/cmll_ctr.c | 83 | ||||
-rw-r--r-- | crypto/camellia/cmll_ofb.c | 25 |
5 files changed, 22 insertions, 299 deletions
diff --git a/crypto/camellia/Makefile b/crypto/camellia/Makefile index 76331ff07a..ff5fe4a01d 100644 --- a/crypto/camellia/Makefile +++ b/crypto/camellia/Makefile @@ -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 diff --git a/crypto/camellia/cmll_cbc.c b/crypto/camellia/cmll_cbc.c index 421c33127b..400a248850 100644 --- a/crypto/camellia/cmll_cbc.c +++ b/crypto/camellia/cmll_cbc.c @@ -49,94 +49,16 @@ * */ -#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); } diff --git a/crypto/camellia/cmll_cfb.c b/crypto/camellia/cmll_cfb.c index 8eed2be9ea..3d81b51d3f 100644 --- a/crypto/camellia/cmll_cfb.c +++ b/crypto/camellia/cmll_cfb.c @@ -105,17 +105,8 @@ * [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); } diff --git a/crypto/camellia/cmll_ctr.c b/crypto/camellia/cmll_ctr.c index a6cdc66957..014e621a34 100644 --- a/crypto/camellia/cmll_ctr.c +++ b/crypto/camellia/cmll_ctr.c @@ -49,70 +49,9 @@ * */ -#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); } diff --git a/crypto/camellia/cmll_ofb.c b/crypto/camellia/cmll_ofb.c index 1646fc0ce7..a482befc74 100644 --- a/crypto/camellia/cmll_ofb.c +++ b/crypto/camellia/cmll_ofb.c @@ -105,14 +105,8 @@ * [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 @@ -121,20 +115,5 @@ 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); } |