Convert Blowfish cipher to use 64-bit modes helper functions. Signed-off-by: Dmitry Baryshkov <dbarysh...@gmail.com> --- src/lib/libcrypto/Makefile | 2 +- src/lib/libcrypto/bf/bf_cbc.c | 83 ++--------------------- src/lib/libcrypto/bf/bf_cfb64.c | 57 ++-------------- src/lib/libcrypto/bf/bf_enc.c | 114 ++++++++------------------------ src/lib/libcrypto/bf/bf_locl.h | 8 +++ src/lib/libcrypto/bf/bf_ofb64.c | 47 ++----------- 6 files changed, 51 insertions(+), 260 deletions(-)
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile index 9207b93f321d..291af21965bf 100644 --- a/src/lib/libcrypto/Makefile +++ b/src/lib/libcrypto/Makefile @@ -65,7 +65,7 @@ SRCS+= evp_asn1.c asn_pack.c p5_pbe.c p5_pbev2.c p8_pkey.c asn_moid.c SRCS+= a_time_tm.c # bf/ -SRCS+= bf_skey.c bf_ecb.c bf_cfb64.c bf_ofb64.c +SRCS+= bf_skey.c bf_ecb.c bf_cfb64.c bf_ofb64.c bf_cbc.c # bio/ SRCS+= bio_lib.c bio_cb.c bio_err.c bio_meth.c diff --git a/src/lib/libcrypto/bf/bf_cbc.c b/src/lib/libcrypto/bf/bf_cbc.c index 6f45f9ae4c35..a9d3cf6d5541 100644 --- a/src/lib/libcrypto/bf/bf_cbc.c +++ b/src/lib/libcrypto/bf/bf_cbc.c @@ -57,87 +57,14 @@ */ #include <openssl/blowfish.h> +#include <openssl/modes.h> #include "bf_locl.h" void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, const BF_KEY *schedule, unsigned char *ivec, int encrypt) - { - BF_LONG tin0,tin1; - BF_LONG tout0,tout1,xor0,xor1; - long l=length; - BF_LONG tin[2]; - +{ if (encrypt) - { - n2l(ivec,tout0); - n2l(ivec,tout1); - ivec-=8; - for (l-=8; l>=0; l-=8) - { - n2l(in,tin0); - n2l(in,tin1); - tin0^=tout0; - tin1^=tout1; - tin[0]=tin0; - tin[1]=tin1; - BF_encrypt(tin,schedule); - tout0=tin[0]; - tout1=tin[1]; - l2n(tout0,out); - l2n(tout1,out); - } - if (l != -8) - { - n2ln(in,tin0,tin1,l+8); - tin0^=tout0; - tin1^=tout1; - tin[0]=tin0; - tin[1]=tin1; - BF_encrypt(tin,schedule); - tout0=tin[0]; - tout1=tin[1]; - l2n(tout0,out); - l2n(tout1,out); - } - l2n(tout0,ivec); - l2n(tout1,ivec); - } + CRYPTO_cbc64_encrypt(in, out, length, schedule, ivec, (block64_f)BF_block_encrypt); else - { - n2l(ivec,xor0); - n2l(ivec,xor1); - ivec-=8; - for (l-=8; l>=0; l-=8) - { - n2l(in,tin0); - n2l(in,tin1); - tin[0]=tin0; - tin[1]=tin1; - BF_decrypt(tin,schedule); - tout0=tin[0]^xor0; - tout1=tin[1]^xor1; - l2n(tout0,out); - l2n(tout1,out); - xor0=tin0; - xor1=tin1; - } - if (l != -8) - { - n2l(in,tin0); - n2l(in,tin1); - tin[0]=tin0; - tin[1]=tin1; - BF_decrypt(tin,schedule); - tout0=tin[0]^xor0; - tout1=tin[1]^xor1; - l2nn(tout0,tout1,out,l+8); - xor0=tin0; - xor1=tin1; - } - l2n(xor0,ivec); - l2n(xor1,ivec); - } - tin0=tin1=tout0=tout1=xor0=xor1=0; - tin[0]=tin[1]=0; - } - + CRYPTO_cbc64_decrypt(in, out, length, schedule, ivec, (block64_f)BF_block_decrypt); +} diff --git a/src/lib/libcrypto/bf/bf_cfb64.c b/src/lib/libcrypto/bf/bf_cfb64.c index 6cc0bb999bd3..463080cb230f 100644 --- a/src/lib/libcrypto/bf/bf_cfb64.c +++ b/src/lib/libcrypto/bf/bf_cfb64.c @@ -57,6 +57,7 @@ */ #include <openssl/blowfish.h> +#include <openssl/modes.h> #include "bf_locl.h" /* The input and output encrypted as though 64bit cfb mode is being @@ -66,56 +67,6 @@ void BF_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, const BF_KEY *schedule, unsigned char *ivec, int *num, int encrypt) - { - BF_LONG v0,v1,t; - int n= *num; - long l=length; - BF_LONG ti[2]; - unsigned char *iv,c,cc; - - iv=(unsigned char *)ivec; - if (encrypt) - { - while (l--) - { - if (n == 0) - { - n2l(iv,v0); ti[0]=v0; - n2l(iv,v1); ti[1]=v1; - BF_encrypt((BF_LONG *)ti,schedule); - iv=(unsigned char *)ivec; - t=ti[0]; l2n(t,iv); - t=ti[1]; l2n(t,iv); - iv=(unsigned char *)ivec; - } - c= *(in++)^iv[n]; - *(out++)=c; - iv[n]=c; - n=(n+1)&0x07; - } - } - else - { - while (l--) - { - if (n == 0) - { - n2l(iv,v0); ti[0]=v0; - n2l(iv,v1); ti[1]=v1; - BF_encrypt((BF_LONG *)ti,schedule); - iv=(unsigned char *)ivec; - t=ti[0]; l2n(t,iv); - t=ti[1]; l2n(t,iv); - iv=(unsigned char *)ivec; - } - cc= *(in++); - c=iv[n]; - iv[n]=cc; - *(out++)=c^cc; - n=(n+1)&0x07; - } - } - v0=v1=ti[0]=ti[1]=t=c=cc=0; - *num=n; - } - +{ + CRYPTO_cfb64_encrypt(in, out, length, schedule, ivec, num, encrypt, (block64_f)BF_block_encrypt); +} diff --git a/src/lib/libcrypto/bf/bf_enc.c b/src/lib/libcrypto/bf/bf_enc.c index 2cf1c860630c..ada85e38739c 100644 --- a/src/lib/libcrypto/bf/bf_enc.c +++ b/src/lib/libcrypto/bf/bf_enc.c @@ -144,8 +144,6 @@ void BF_encrypt(BF_LONG *data, const BF_KEY *key) #endif } -#ifndef BF_DEFAULT_OPTIONS - void BF_decrypt(BF_LONG *data, const BF_KEY *key) { #ifndef BF_PTR2 @@ -221,86 +219,32 @@ void BF_decrypt(BF_LONG *data, const BF_KEY *key) #endif } -void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, - const BF_KEY *schedule, unsigned char *ivec, int encrypt) - { - BF_LONG tin0,tin1; - BF_LONG tout0,tout1,xor0,xor1; - long l=length; - BF_LONG tin[2]; - - if (encrypt) - { - n2l(ivec,tout0); - n2l(ivec,tout1); - ivec-=8; - for (l-=8; l>=0; l-=8) - { - n2l(in,tin0); - n2l(in,tin1); - tin0^=tout0; - tin1^=tout1; - tin[0]=tin0; - tin[1]=tin1; - BF_encrypt(tin,schedule); - tout0=tin[0]; - tout1=tin[1]; - l2n(tout0,out); - l2n(tout1,out); - } - if (l != -8) - { - n2ln(in,tin0,tin1,l+8); - tin0^=tout0; - tin1^=tout1; - tin[0]=tin0; - tin[1]=tin1; - BF_encrypt(tin,schedule); - tout0=tin[0]; - tout1=tin[1]; - l2n(tout0,out); - l2n(tout1,out); - } - l2n(tout0,ivec); - l2n(tout1,ivec); - } - else - { - n2l(ivec,xor0); - n2l(ivec,xor1); - ivec-=8; - for (l-=8; l>=0; l-=8) - { - n2l(in,tin0); - n2l(in,tin1); - tin[0]=tin0; - tin[1]=tin1; - BF_decrypt(tin,schedule); - tout0=tin[0]^xor0; - tout1=tin[1]^xor1; - l2n(tout0,out); - l2n(tout1,out); - xor0=tin0; - xor1=tin1; - } - if (l != -8) - { - n2l(in,tin0); - n2l(in,tin1); - tin[0]=tin0; - tin[1]=tin1; - BF_decrypt(tin,schedule); - tout0=tin[0]^xor0; - tout1=tin[1]^xor1; - l2nn(tout0,tout1,out,l+8); - xor0=tin0; - xor1=tin1; - } - l2n(xor0,ivec); - l2n(xor1,ivec); - } - tin0=tin1=tout0=tout1=xor0=xor1=0; - tin[0]=tin[1]=0; - } - -#endif +void BF_block_encrypt(const unsigned char in[8], + unsigned char out[8], + const BF_KEY *schedule) +{ + const unsigned char *pin = in; + unsigned char *pout = out; + BF_LONG ti[2]; + + n2l(pin, ti[0]); + n2l(pin, ti[1]); + BF_encrypt(ti, schedule); + l2n(ti[0], pout); + l2n(ti[1], pout); +} + +void BF_block_decrypt(const unsigned char in[8], + unsigned char out[8], + const BF_KEY *schedule) +{ + const unsigned char *pin = in; + unsigned char *pout = out; + BF_LONG ti[2]; + + n2l(pin, ti[0]); + n2l(pin, ti[1]); + BF_decrypt(ti, schedule); + l2n(ti[0], pout); + l2n(ti[1], pout); +} diff --git a/src/lib/libcrypto/bf/bf_locl.h b/src/lib/libcrypto/bf/bf_locl.h index 0b663622d825..4d2a450e6199 100644 --- a/src/lib/libcrypto/bf/bf_locl.h +++ b/src/lib/libcrypto/bf/bf_locl.h @@ -216,4 +216,12 @@ ) #endif +void BF_block_encrypt(const unsigned char in[8], + unsigned char out[8], + const BF_KEY *schedule); + +void BF_block_decrypt(const unsigned char in[8], + unsigned char out[8], + const BF_KEY *schedule); + #endif diff --git a/src/lib/libcrypto/bf/bf_ofb64.c b/src/lib/libcrypto/bf/bf_ofb64.c index 9e33162aab3c..3dbfd8d9ad1c 100644 --- a/src/lib/libcrypto/bf/bf_ofb64.c +++ b/src/lib/libcrypto/bf/bf_ofb64.c @@ -57,6 +57,7 @@ */ #include <openssl/blowfish.h> +#include <openssl/modes.h> #include "bf_locl.h" /* The input and output encrypted as though 64bit ofb mode is being @@ -65,46 +66,6 @@ */ void BF_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length, const BF_KEY *schedule, unsigned char *ivec, int *num) - { - BF_LONG v0,v1,t; - int n= *num; - long l=length; - unsigned char d[8]; - char *dp; - BF_LONG ti[2]; - unsigned char *iv; - int save=0; - - iv=(unsigned char *)ivec; - n2l(iv,v0); - n2l(iv,v1); - ti[0]=v0; - ti[1]=v1; - dp=(char *)d; - l2n(v0,dp); - l2n(v1,dp); - while (l--) - { - if (n == 0) - { - BF_encrypt((BF_LONG *)ti,schedule); - dp=(char *)d; - t=ti[0]; l2n(t,dp); - t=ti[1]; l2n(t,dp); - save++; - } - *(out++)= *(in++)^d[n]; - n=(n+1)&0x07; - } - if (save) - { - v0=ti[0]; - v1=ti[1]; - iv=(unsigned char *)ivec; - l2n(v0,iv); - l2n(v1,iv); - } - t=v0=v1=ti[0]=ti[1]=0; - *num=n; - } - +{ + CRYPTO_ofb64_encrypt(in, out, length, schedule, ivec, num, (block64_f) BF_block_encrypt); +} -- 2.27.0