[prev in list] [next in list] [prev in thread] [next in thread]
List: openbsd-tech
Subject: [PATCH 5/6] crypto: RC2: convert to use new modes 64-bit helpers
From: Dmitry Baryshkov <dbaryshkov () gmail ! com>
Date: 2020-06-27 19:37:00
Message-ID: 20200627193701.139170-5-dbaryshkov () gmail ! com
[Download RAW message or body]
Convert RC2 cipher to use 64-bit modes helper functions.
Signed-off-by: Dmitry Baryshkov <dbaryshkov@gmail.com>
---
src/lib/libcrypto/rc2/rc2.h | 4 +-
src/lib/libcrypto/rc2/rc2_cbc.c | 111 +++++++++++--------------------
src/lib/libcrypto/rc2/rc2_locl.h | 7 ++
src/lib/libcrypto/rc2/rc2cfb64.c | 57 ++--------------
src/lib/libcrypto/rc2/rc2ofb64.c | 47 ++-----------
5 files changed, 55 insertions(+), 171 deletions(-)
diff --git a/src/lib/libcrypto/rc2/rc2.h b/src/lib/libcrypto/rc2/rc2.h
index 21511ff36ead..03df1433cc22 100644
--- a/src/lib/libcrypto/rc2/rc2.h
+++ b/src/lib/libcrypto/rc2/rc2.h
@@ -83,8 +83,8 @@ typedef struct rc2_key_st
void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data,int bits);
void RC2_ecb_encrypt(const unsigned char *in,unsigned char *out,RC2_KEY *key,
int enc);
-void RC2_encrypt(unsigned long *data,RC2_KEY *key);
-void RC2_decrypt(unsigned long *data,RC2_KEY *key);
+void RC2_encrypt(unsigned long *data,const RC2_KEY *key);
+void RC2_decrypt(unsigned long *data,const RC2_KEY *key);
void RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
RC2_KEY *ks, unsigned char *iv, int enc);
void RC2_cfb64_encrypt(const unsigned char *in, unsigned char *out,
diff --git a/src/lib/libcrypto/rc2/rc2_cbc.c b/src/lib/libcrypto/rc2/rc2_cbc.c
index a947f1d3c3a1..276f3b3b4d61 100644
--- a/src/lib/libcrypto/rc2/rc2_cbc.c
+++ b/src/lib/libcrypto/rc2/rc2_cbc.c
@@ -57,86 +57,22 @@
*/
#include <openssl/rc2.h>
+#include <openssl/modes.h>
#include "rc2_locl.h"
void RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
RC2_KEY *ks, unsigned char *iv, int encrypt)
- {
- unsigned long tin0,tin1;
- unsigned long tout0,tout1,xor0,xor1;
- long l=length;
- unsigned long tin[2];
-
+{
if (encrypt)
- {
- c2l(iv,tout0);
- c2l(iv,tout1);
- iv-=8;
- for (l-=8; l>=0; l-=8)
- {
- c2l(in,tin0);
- c2l(in,tin1);
- tin0^=tout0;
- tin1^=tout1;
- tin[0]=tin0;
- tin[1]=tin1;
- RC2_encrypt(tin,ks);
- tout0=tin[0]; l2c(tout0,out);
- tout1=tin[1]; l2c(tout1,out);
- }
- if (l != -8)
- {
- c2ln(in,tin0,tin1,l+8);
- tin0^=tout0;
- tin1^=tout1;
- tin[0]=tin0;
- tin[1]=tin1;
- RC2_encrypt(tin,ks);
- tout0=tin[0]; l2c(tout0,out);
- tout1=tin[1]; l2c(tout1,out);
- }
- l2c(tout0,iv);
- l2c(tout1,iv);
- }
+ CRYPTO_cbc64_encrypt(in, out, length, ks, iv, (block64_f)RC2_block_encrypt);
else
- {
- c2l(iv,xor0);
- c2l(iv,xor1);
- iv-=8;
- for (l-=8; l>=0; l-=8)
- {
- c2l(in,tin0); tin[0]=tin0;
- c2l(in,tin1); tin[1]=tin1;
- RC2_decrypt(tin,ks);
- tout0=tin[0]^xor0;
- tout1=tin[1]^xor1;
- l2c(tout0,out);
- l2c(tout1,out);
- xor0=tin0;
- xor1=tin1;
- }
- if (l != -8)
- {
- c2l(in,tin0); tin[0]=tin0;
- c2l(in,tin1); tin[1]=tin1;
- RC2_decrypt(tin,ks);
- tout0=tin[0]^xor0;
- tout1=tin[1]^xor1;
- l2cn(tout0,tout1,out,l+8);
- xor0=tin0;
- xor1=tin1;
- }
- l2c(xor0,iv);
- l2c(xor1,iv);
- }
- tin0=tin1=tout0=tout1=xor0=xor1=0;
- tin[0]=tin[1]=0;
- }
+ CRYPTO_cbc64_decrypt(in, out, length, ks, iv, (block64_f)RC2_block_decrypt);
+}
-void RC2_encrypt(unsigned long *d, RC2_KEY *key)
+void RC2_encrypt(unsigned long *d, const RC2_KEY *key)
{
int i,n;
- RC2_INT *p0,*p1;
+ const RC2_INT *p0,*p1;
RC2_INT x0,x1,x2,x3,t;
unsigned long l;
@@ -178,10 +114,10 @@ void RC2_encrypt(unsigned long *d, RC2_KEY *key)
d[1]=(unsigned long)(x2&0xffff)|((unsigned long)(x3&0xffff)<<16L);
}
-void RC2_decrypt(unsigned long *d, RC2_KEY *key)
+void RC2_decrypt(unsigned long *d, const RC2_KEY *key)
{
int i,n;
- RC2_INT *p0,*p1;
+ const RC2_INT *p0,*p1;
RC2_INT x0,x1,x2,x3,t;
unsigned long l;
@@ -224,3 +160,32 @@ void RC2_decrypt(unsigned long *d, RC2_KEY *key)
d[1]=(unsigned long)(x2&0xffff)|((unsigned long)(x3&0xffff)<<16L);
}
+void RC2_block_encrypt(const unsigned char in[8],
+ unsigned char out[8],
+ const RC2_KEY *key)
+{
+ const unsigned char *pin = in;
+ unsigned char *pout = out;
+ unsigned long ti[2];
+
+ n2l(pin, ti[0]);
+ n2l(pin, ti[1]);
+ RC2_encrypt(ti, key);
+ l2n(ti[0], pout);
+ l2n(ti[1], pout);
+}
+
+void RC2_block_decrypt(const unsigned char in[8],
+ unsigned char out[8],
+ const RC2_KEY *key)
+{
+ const unsigned char *pin = in;
+ unsigned char *pout = out;
+ unsigned long ti[2];
+
+ n2l(pin, ti[0]);
+ n2l(pin, ti[1]);
+ RC2_decrypt(ti, key);
+ l2n(ti[0], pout);
+ l2n(ti[1], pout);
+}
diff --git a/src/lib/libcrypto/rc2/rc2_locl.h b/src/lib/libcrypto/rc2/rc2_locl.h
index 73d8c68ca766..39db5593af2e 100644
--- a/src/lib/libcrypto/rc2/rc2_locl.h
+++ b/src/lib/libcrypto/rc2/rc2_locl.h
@@ -154,3 +154,10 @@
t=(x3+(x0& ~x2)+(x1&x2)+ *(p0++))&0xffff; \
x3=(t<<5)|(t>>11);
+void RC2_block_encrypt(const unsigned char in[8],
+ unsigned char out[8],
+ const RC2_KEY *key);
+
+void RC2_block_decrypt(const unsigned char in[8],
+ unsigned char out[8],
+ const RC2_KEY *key);
diff --git a/src/lib/libcrypto/rc2/rc2cfb64.c b/src/lib/libcrypto/rc2/rc2cfb64.c
index 95366444c4ef..a41758aa3b12 100644
--- a/src/lib/libcrypto/rc2/rc2cfb64.c
+++ b/src/lib/libcrypto/rc2/rc2cfb64.c
@@ -57,6 +57,7 @@
*/
#include <openssl/rc2.h>
+#include <openssl/modes.h>
#include "rc2_locl.h"
/* The input and output encrypted as though 64bit cfb mode is being
@@ -67,56 +68,6 @@
void RC2_cfb64_encrypt(const unsigned char *in, unsigned char *out,
long length, RC2_KEY *schedule, unsigned char *ivec,
int *num, int encrypt)
- {
- unsigned long v0,v1,t;
- int n= *num;
- long l=length;
- unsigned long ti[2];
- unsigned char *iv,c,cc;
-
- iv=(unsigned char *)ivec;
- if (encrypt)
- {
- while (l--)
- {
- if (n == 0)
- {
- c2l(iv,v0); ti[0]=v0;
- c2l(iv,v1); ti[1]=v1;
- RC2_encrypt((unsigned long *)ti,schedule);
- iv=(unsigned char *)ivec;
- t=ti[0]; l2c(t,iv);
- t=ti[1]; l2c(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)
- {
- c2l(iv,v0); ti[0]=v0;
- c2l(iv,v1); ti[1]=v1;
- RC2_encrypt((unsigned long *)ti,schedule);
- iv=(unsigned char *)ivec;
- t=ti[0]; l2c(t,iv);
- t=ti[1]; l2c(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) RC2_block_encrypt);
+}
diff --git a/src/lib/libcrypto/rc2/rc2ofb64.c b/src/lib/libcrypto/rc2/rc2ofb64.c
index c47b4137394c..7b9520869860 100644
--- a/src/lib/libcrypto/rc2/rc2ofb64.c
+++ b/src/lib/libcrypto/rc2/rc2ofb64.c
@@ -57,6 +57,7 @@
*/
#include <openssl/rc2.h>
+#include <openssl/modes.h>
#include "rc2_locl.h"
/* The input and output encrypted as though 64bit ofb mode is being
@@ -66,46 +67,6 @@
void RC2_ofb64_encrypt(const unsigned char *in, unsigned char *out,
long length, RC2_KEY *schedule, unsigned char *ivec,
int *num)
- {
- unsigned long v0,v1,t;
- int n= *num;
- long l=length;
- unsigned char d[8];
- char *dp;
- unsigned long ti[2];
- unsigned char *iv;
- int save=0;
-
- iv=(unsigned char *)ivec;
- c2l(iv,v0);
- c2l(iv,v1);
- ti[0]=v0;
- ti[1]=v1;
- dp=(char *)d;
- l2c(v0,dp);
- l2c(v1,dp);
- while (l--)
- {
- if (n == 0)
- {
- RC2_encrypt((unsigned long *)ti,schedule);
- dp=(char *)d;
- t=ti[0]; l2c(t,dp);
- t=ti[1]; l2c(t,dp);
- save++;
- }
- *(out++)= *(in++)^d[n];
- n=(n+1)&0x07;
- }
- if (save)
- {
- v0=ti[0];
- v1=ti[1];
- iv=(unsigned char *)ivec;
- l2c(v0,iv);
- l2c(v1,iv);
- }
- t=v0=v1=ti[0]=ti[1]=0;
- *num=n;
- }
-
+{
+ CRYPTO_ofb64_encrypt(in, out, length, schedule, ivec, num, (block64_f) RC2_block_encrypt);
+}
--
2.27.0
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic