[PATCH 1/6] modes: add functions implementing common code for 64-bit ciphers

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 1/6] modes: add functions implementing common code for 64-bit ciphers

Dmitry Baryshkov
64-bit ciphers are old, but it would be good to use common code for
their implementations.

Signed-off-by: Dmitry Baryshkov <[hidden email]>
---
 src/lib/libcrypto/modes/cbc64.c | 202 ++++++++++++++++++++++++++++++++
 src/lib/libcrypto/modes/cfb64.c | 169 ++++++++++++++++++++++++++
 src/lib/libcrypto/modes/ctr64.c | 174 +++++++++++++++++++++++++++
 src/lib/libcrypto/modes/modes.h |  26 ++++
 src/lib/libcrypto/modes/ofb64.c | 119 +++++++++++++++++++
 5 files changed, 690 insertions(+)
 create mode 100644 src/lib/libcrypto/modes/cbc64.c
 create mode 100644 src/lib/libcrypto/modes/cfb64.c
 create mode 100644 src/lib/libcrypto/modes/ctr64.c
 create mode 100644 src/lib/libcrypto/modes/ofb64.c

diff --git a/src/lib/libcrypto/modes/cbc64.c b/src/lib/libcrypto/modes/cbc64.c
new file mode 100644
index 000000000000..ec65ac5d3468
--- /dev/null
+++ b/src/lib/libcrypto/modes/cbc64.c
@@ -0,0 +1,202 @@
+/* $OpenBSD: cbc64.c,v 1.4 2015/02/10 09:46:30 miod Exp $ */
+/* ====================================================================
+ * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    [hidden email].
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ */
+
+#include <openssl/crypto.h>
+#include "modes_lcl.h"
+#include <string.h>
+
+#ifndef MODES_DEBUG
+# ifndef NDEBUG
+#  define NDEBUG
+# endif
+#endif
+
+#undef STRICT_ALIGNMENT
+#ifdef __STRICT_ALIGNMENT
+#define STRICT_ALIGNMENT 1
+#else
+#define STRICT_ALIGNMENT 0
+#endif
+
+void CRYPTO_cbc64_encrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], block64_f block)
+{
+ size_t n;
+ const unsigned char *iv = ivec;
+
+#if !defined(OPENSSL_SMALL_FOOTPRINT)
+ if (STRICT_ALIGNMENT &&
+    ((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0) {
+ while (len>=8) {
+ for(n=0; n<8; ++n)
+ out[n] = in[n] ^ iv[n];
+ (*block)(out, out, key);
+ iv = out;
+ len -= 8;
+ in  += 8;
+ out += 8;
+ }
+ } else {
+ while (len>=8) {
+ for(n=0; n<8; n+=sizeof(size_t))
+ *(size_t*)(out+n) =
+ *(size_t*)(in+n) ^ *(size_t*)(iv+n);
+ (*block)(out, out, key);
+ iv = out;
+ len -= 8;
+ in  += 8;
+ out += 8;
+ }
+ }
+#endif
+ while (len) {
+ for(n=0; n<8 && n<len; ++n)
+ out[n] = in[n] ^ iv[n];
+ for(; n<8; ++n)
+ out[n] = iv[n];
+ (*block)(out, out, key);
+ iv = out;
+ if (len<=8) break;
+ len -= 8;
+ in  += 8;
+ out += 8;
+ }
+ memcpy(ivec,iv,8);
+}
+
+void CRYPTO_cbc64_decrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], block64_f block)
+{
+ size_t n;
+ union { size_t t[8/sizeof(size_t)]; unsigned char c[8]; } tmp;
+
+#if !defined(OPENSSL_SMALL_FOOTPRINT)
+ if (in != out) {
+ const unsigned char *iv = ivec;
+
+ if (STRICT_ALIGNMENT &&
+    ((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0) {
+ while (len>=8) {
+ (*block)(in, out, key);
+ for(n=0; n<8; ++n)
+ out[n] ^= iv[n];
+ iv = in;
+ len -= 8;
+ in  += 8;
+ out += 8;
+ }
+ } else if (8%sizeof(size_t) == 0) { /* always true */
+ while (len>=8) {
+ size_t *out_t=(size_t *)out, *iv_t=(size_t *)iv;
+
+ (*block)(in, out, key);
+ for(n=0; n<8/sizeof(size_t); n++)
+ out_t[n] ^= iv_t[n];
+ iv = in;
+ len -= 8;
+ in  += 8;
+ out += 8;
+ }
+ }
+ memcpy(ivec,iv,8);
+ } else {
+ if (STRICT_ALIGNMENT &&
+    ((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0) {
+ unsigned char c;
+ while (len>=8) {
+ (*block)(in, tmp.c, key);
+ for(n=0; n<8; ++n) {
+ c = in[n];
+ out[n] = tmp.c[n] ^ ivec[n];
+ ivec[n] = c;
+ }
+ len -= 8;
+ in  += 8;
+ out += 8;
+ }
+ } else if (8%sizeof(size_t) == 0) { /* always true */
+ while (len>=8) {
+ size_t c, *out_t=(size_t *)out, *ivec_t=(size_t *)ivec;
+ const size_t *in_t=(const size_t *)in;
+
+ (*block)(in, tmp.c, key);
+ for(n=0; n<8/sizeof(size_t); n++) {
+ c = in_t[n];
+ out_t[n] = tmp.t[n] ^ ivec_t[n];
+ ivec_t[n] = c;
+ }
+ len -= 8;
+ in  += 8;
+ out += 8;
+ }
+ }
+ }
+#endif
+ while (len) {
+ unsigned char c;
+ (*block)(in, tmp.c, key);
+ for(n=0; n<8 && n<len; ++n) {
+ c = in[n];
+ out[n] = tmp.c[n] ^ ivec[n];
+ ivec[n] = c;
+ }
+ if (len<=8) {
+ for (; n<8; ++n)
+ ivec[n] = in[n];
+ break;
+ }
+ len -= 8;
+ in  += 8;
+ out += 8;
+ }
+}
diff --git a/src/lib/libcrypto/modes/cfb64.c b/src/lib/libcrypto/modes/cfb64.c
new file mode 100644
index 000000000000..f335fa39c4ea
--- /dev/null
+++ b/src/lib/libcrypto/modes/cfb64.c
@@ -0,0 +1,169 @@
+/* $OpenBSD: cfb64.c,v 1.4 2015/02/10 09:46:30 miod Exp $ */
+/* ====================================================================
+ * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    [hidden email].
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ */
+
+#include <openssl/crypto.h>
+#include "modes_lcl.h"
+#include <string.h>
+
+#ifndef MODES_DEBUG
+# ifndef NDEBUG
+#  define NDEBUG
+# endif
+#endif
+
+/* The input and output encrypted as though 64bit cfb mode is being
+ * used.  The extra state information to record how much of the
+ * 64bit block we have used is contained in *num;
+ */
+void CRYPTO_cfb64_encrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], int *num,
+ int enc, block64_f block)
+{
+    unsigned int n;
+    size_t l = 0;
+
+    n = *num;
+
+    if (enc) {
+#if !defined(OPENSSL_SMALL_FOOTPRINT)
+ if (8%sizeof(size_t) == 0) do { /* always true actually */
+ while (n && len) {
+ *(out++) = ivec[n] ^= *(in++);
+ --len;
+ n = (n+1) % 8;
+ }
+#ifdef __STRICT_ALIGNMENT
+ if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
+ break;
+#endif
+ while (len>=8) {
+ (*block)(ivec, ivec, key);
+ for (; n<8; n+=sizeof(size_t)) {
+ *(size_t*)(out+n) =
+ *(size_t*)(ivec+n) ^= *(size_t*)(in+n);
+ }
+ len -= 8;
+ out += 8;
+ in  += 8;
+ n = 0;
+ }
+ if (len) {
+ (*block)(ivec, ivec, key);
+ while (len--) {
+ out[n] = ivec[n] ^= in[n];
+ ++n;
+ }
+ }
+ *num = n;
+ return;
+ } while (0);
+ /* the rest would be commonly eliminated by x86* compiler */
+#endif
+ while (l<len) {
+ if (n == 0) {
+ (*block)(ivec, ivec, key);
+ }
+ out[l] = ivec[n] ^= in[l];
+ ++l;
+ n = (n+1) % 8;
+ }
+ *num = n;
+    } else {
+#if !defined(OPENSSL_SMALL_FOOTPRINT)
+ if (8%sizeof(size_t) == 0) do { /* always true actually */
+ while (n && len) {
+ unsigned char c;
+ *(out++) = ivec[n] ^ (c = *(in++)); ivec[n] = c;
+ --len;
+ n = (n+1) % 8;
+ }
+#ifdef __STRICT_ALIGNMENT
+ if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
+ break;
+#endif
+ while (len>=8) {
+ (*block)(ivec, ivec, key);
+ for (; n<8; n+=sizeof(size_t)) {
+ size_t t = *(size_t*)(in+n);
+ *(size_t*)(out+n) = *(size_t*)(ivec+n) ^ t;
+ *(size_t*)(ivec+n) = t;
+ }
+ len -= 8;
+ out += 8;
+ in  += 8;
+ n = 0;
+ }
+ if (len) {
+ (*block)(ivec, ivec, key);
+ while (len--) {
+ unsigned char c;
+ out[n] = ivec[n] ^ (c = in[n]); ivec[n] = c;
+ ++n;
+ }
+ }
+ *num = n;
+ return;
+ } while (0);
+ /* the rest would be commonly eliminated by x86* compiler */
+#endif
+ while (l<len) {
+ unsigned char c;
+ if (n == 0) {
+ (*block)(ivec, ivec, key);
+ }
+ out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
+ ++l;
+ n = (n+1) % 8;
+ }
+ *num=n;
+    }
+}
diff --git a/src/lib/libcrypto/modes/ctr64.c b/src/lib/libcrypto/modes/ctr64.c
new file mode 100644
index 000000000000..e1743cb91193
--- /dev/null
+++ b/src/lib/libcrypto/modes/ctr64.c
@@ -0,0 +1,174 @@
+/* $OpenBSD: ctr64.c,v 1.7 2017/08/13 17:46:24 bcook Exp $ */
+/* ====================================================================
+ * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    [hidden email].
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ */
+
+#include <openssl/crypto.h>
+#include "modes_lcl.h"
+#include <string.h>
+
+#ifndef MODES_DEBUG
+# ifndef NDEBUG
+#  define NDEBUG
+# endif
+#endif
+#include <assert.h>
+
+/* NOTE: the IV/counter CTR mode is big-endian.  The code itself
+ * is endian-neutral. */
+
+/* increment counter (64-bit int) by 1 */
+static void ctr64_inc(unsigned char *counter) {
+ u32 n=8;
+ u8  c;
+
+ do {
+ --n;
+ c = counter[n];
+ ++c;
+ counter[n] = c;
+ if (c) return;
+ } while (n);
+}
+
+#if !defined(OPENSSL_SMALL_FOOTPRINT)
+static void
+ctr64_inc_aligned(unsigned char *counter)
+{
+#if BYTE_ORDER == LITTLE_ENDIAN
+ ctr64_inc(counter);
+#else
+ size_t *data, c, n;
+ data = (size_t *)counter;
+ n = 8 / sizeof(size_t);
+ do {
+ --n;
+ c = data[n];
+ ++c;
+ data[n] = c;
+ if (c)
+ return;
+ } while (n);
+#endif
+}
+#endif
+
+/* The input encrypted as though 64bit counter mode is being
+ * used.  The extra state information to record how much of the
+ * 64bit 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 CRYPTO_ctr64_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 CRYPTO_ctr64_encrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], unsigned char ecount_buf[8],
+ unsigned int *num, block64_f block)
+{
+ unsigned int n;
+ size_t l=0;
+
+ assert(*num < 8);
+
+ n = *num;
+
+#if !defined(OPENSSL_SMALL_FOOTPRINT)
+ if (8%sizeof(size_t) == 0) do { /* always true actually */
+ while (n && len) {
+ *(out++) = *(in++) ^ ecount_buf[n];
+ --len;
+ n = (n+1) % 8;
+ }
+
+#ifdef __STRICT_ALIGNMENT
+ if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
+ break;
+#endif
+ while (len>=8) {
+ (*block)(ivec, ecount_buf, key);
+ ctr64_inc_aligned(ivec);
+ for (; n<8; n+=sizeof(size_t))
+ *(size_t *)(out+n) =
+ *(size_t *)(in+n) ^ *(size_t *)(ecount_buf+n);
+ len -= 8;
+ out += 8;
+ in  += 8;
+ n = 0;
+ }
+ if (len) {
+ (*block)(ivec, ecount_buf, key);
+ ctr64_inc_aligned(ivec);
+ while (len--) {
+ out[n] = in[n] ^ ecount_buf[n];
+ ++n;
+ }
+ }
+ *num = n;
+ return;
+ } while(0);
+ /* the rest would be commonly eliminated by x86* compiler */
+#endif
+ while (l<len) {
+ if (n==0) {
+ (*block)(ivec, ecount_buf, key);
+ ctr64_inc(ivec);
+ }
+ out[l] = in[l] ^ ecount_buf[n];
+ ++l;
+ n = (n+1) % 8;
+ }
+
+ *num=n;
+}
diff --git a/src/lib/libcrypto/modes/modes.h b/src/lib/libcrypto/modes/modes.h
index 67ec7518d621..2344e944ea31 100644
--- a/src/lib/libcrypto/modes/modes.h
+++ b/src/lib/libcrypto/modes/modes.h
@@ -139,6 +139,32 @@ typedef struct xts128_context XTS128_CONTEXT;
 int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
  const unsigned char *inp, unsigned char *out, size_t len, int enc);
 
+typedef void (*block64_f)(const unsigned char in[8],
+ unsigned char out[8],
+ const void *key);
+
+void CRYPTO_cbc64_encrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], block64_f block);
+void CRYPTO_cbc64_decrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], block64_f block);
+
+void CRYPTO_ctr64_encrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], unsigned char ecount_buf[8],
+ unsigned int *num, block64_f block);
+
+void CRYPTO_ofb64_encrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], int *num,
+ block64_f block);
+
+void CRYPTO_cfb64_encrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], int *num,
+ int enc, block64_f block);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/lib/libcrypto/modes/ofb64.c b/src/lib/libcrypto/modes/ofb64.c
new file mode 100644
index 000000000000..8368811cecbf
--- /dev/null
+++ b/src/lib/libcrypto/modes/ofb64.c
@@ -0,0 +1,119 @@
+/* $OpenBSD: ofb64.c,v 1.4 2015/02/10 09:46:30 miod Exp $ */
+/* ====================================================================
+ * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    [hidden email].
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ */
+
+#include <openssl/crypto.h>
+#include "modes_lcl.h"
+#include <string.h>
+
+#ifndef MODES_DEBUG
+# ifndef NDEBUG
+#  define NDEBUG
+# endif
+#endif
+
+/* The input and output encrypted as though 64bit ofb mode is being
+ * used.  The extra state information to record how much of the
+ * 64bit block we have used is contained in *num;
+ */
+void CRYPTO_ofb64_encrypt(const unsigned char *in, unsigned char *out,
+ size_t len, const void *key,
+ unsigned char ivec[8], int *num,
+ block64_f block)
+{
+ unsigned int n;
+ size_t l=0;
+
+ n = *num;
+
+#if !defined(OPENSSL_SMALL_FOOTPRINT)
+ if (8%sizeof(size_t) == 0) do { /* always true actually */
+ while (n && len) {
+ *(out++) = *(in++) ^ ivec[n];
+ --len;
+ n = (n+1) % 8;
+ }
+#ifdef __STRICT_ALIGNMENT
+ if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
+ break;
+#endif
+ while (len>=8) {
+ (*block)(ivec, ivec, key);
+ for (; n<8; n+=sizeof(size_t))
+ *(size_t*)(out+n) =
+ *(size_t*)(in+n) ^ *(size_t*)(ivec+n);
+ len -= 8;
+ out += 8;
+ in  += 8;
+ n = 0;
+ }
+ if (len) {
+ (*block)(ivec, ivec, key);
+ while (len--) {
+ out[n] = in[n] ^ ivec[n];
+ ++n;
+ }
+ }
+ *num = n;
+ return;
+ } while(0);
+ /* the rest would be commonly eliminated by x86* compiler */
+#endif
+ while (l<len) {
+ if (n==0) {
+ (*block)(ivec, ivec, key);
+ }
+ out[l] = in[l] ^ ivec[n];
+ ++l;
+ n = (n+1) % 8;
+ }
+
+ *num=n;
+}
--
2.27.0

Reply | Threaded
Open this post in threaded view
|

[PATCH 2/6] crypto: bf: convert to use new modes 64-bit helpers

Dmitry Baryshkov
Convert Blowfish cipher to use 64-bit modes helper functions.

Signed-off-by: Dmitry Baryshkov <[hidden email]>
---
 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

Reply | Threaded
Open this post in threaded view
|

[PATCH 3/6] crypto: cast: convert to use new modes 64-bit helpers

Dmitry Baryshkov
In reply to this post by Dmitry Baryshkov
Convert CAST cipher to use 64-bit modes helper functions.

Signed-off-by: Dmitry Baryshkov <[hidden email]>
---
 src/lib/libcrypto/Makefile        |   2 +-
 src/lib/libcrypto/cast/c_cbc.c    |  75 +++++++++++++++++++++
 src/lib/libcrypto/cast/c_cfb64.c  |  56 ++--------------
 src/lib/libcrypto/cast/c_enc.c    | 108 ++++++++----------------------
 src/lib/libcrypto/cast/c_ofb64.c  |  46 ++-----------
 src/lib/libcrypto/cast/cast_lcl.h |   8 +++
 6 files changed, 120 insertions(+), 175 deletions(-)
 create mode 100644 src/lib/libcrypto/cast/c_cbc.c

diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile
index 291af21965bf..2e20904ab840 100644
--- a/src/lib/libcrypto/Makefile
+++ b/src/lib/libcrypto/Makefile
@@ -89,7 +89,7 @@ SRCS+= buffer.c buf_err.c buf_str.c
 SRCS+= cmll_cfb.c cmll_ctr.c cmll_ecb.c cmll_ofb.c
 
 # cast/
-SRCS+= c_skey.c c_ecb.c c_enc.c c_cfb64.c c_ofb64.c
+SRCS+= c_skey.c c_ecb.c c_enc.c c_cfb64.c c_ofb64.c c_cbc.c
 
 # chacha/
 SRCS+= chacha.c
diff --git a/src/lib/libcrypto/cast/c_cbc.c b/src/lib/libcrypto/cast/c_cbc.c
new file mode 100644
index 000000000000..1dc32ad8ca54
--- /dev/null
+++ b/src/lib/libcrypto/cast/c_cbc.c
@@ -0,0 +1,75 @@
+/* $OpenBSD: c_cbc.c,v 1.5 2014/10/28 07:35:58 jsg Exp $ */
+/* Copyright (C) 1995-1998 Eric Young ([hidden email])
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young ([hidden email]).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to.  The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson ([hidden email]).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *    "This product includes cryptographic software written by
+ *     Eric Young ([hidden email])"
+ *    The word 'cryptographic' can be left out if the rouines from the library
+ *    being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ *    the apps directory (application code) you must include an acknowledgement:
+ *    "This product includes software written by Tim Hudson ([hidden email])"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed.  i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+#include <openssl/cast.h>
+#include <openssl/modes.h>
+#include "cast_lcl.h"
+
+/* The input and output encrypted as though 64bit cbc mode is being
+ * used.
+ */
+
+void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out,
+ long length, const CAST_KEY *schedule, unsigned char *ivec,
+ int enc)
+{
+ if (enc)
+ CRYPTO_cbc64_encrypt(in, out, length, schedule, ivec, (block64_f)CAST_block_encrypt);
+ else
+ CRYPTO_cbc64_decrypt(in, out, length, schedule, ivec, (block64_f)CAST_block_decrypt);
+}
diff --git a/src/lib/libcrypto/cast/c_cfb64.c b/src/lib/libcrypto/cast/c_cfb64.c
index 726d19e0053d..036c3354f83e 100644
--- a/src/lib/libcrypto/cast/c_cfb64.c
+++ b/src/lib/libcrypto/cast/c_cfb64.c
@@ -57,6 +57,7 @@
  */
 
 #include <openssl/cast.h>
+#include <openssl/modes.h>
 #include "cast_lcl.h"
 
 /* The input and output encrypted as though 64bit cfb mode is being
@@ -67,55 +68,6 @@
 void CAST_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  long length, const CAST_KEY *schedule, unsigned char *ivec,
  int *num, int enc)
- {
- CAST_LONG v0,v1,t;
- int n= *num;
- long l=length;
- CAST_LONG ti[2];
- unsigned char *iv,c,cc;
-
- iv=ivec;
- if (enc)
- {
- while (l--)
- {
- if (n == 0)
- {
- n2l(iv,v0); ti[0]=v0;
- n2l(iv,v1); ti[1]=v1;
- CAST_encrypt((CAST_LONG *)ti,schedule);
- iv=ivec;
- t=ti[0]; l2n(t,iv);
- t=ti[1]; l2n(t,iv);
- iv=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;
- CAST_encrypt((CAST_LONG *)ti,schedule);
- iv=ivec;
- t=ti[0]; l2n(t,iv);
- t=ti[1]; l2n(t,iv);
- iv=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, enc, (block64_f) CAST_block_encrypt);
+}
diff --git a/src/lib/libcrypto/cast/c_enc.c b/src/lib/libcrypto/cast/c_enc.c
index 5999a590317f..57417f8aabf2 100644
--- a/src/lib/libcrypto/cast/c_enc.c
+++ b/src/lib/libcrypto/cast/c_enc.c
@@ -127,84 +127,32 @@ void CAST_decrypt(CAST_LONG *data, const CAST_KEY *key)
  }
 #endif
 
-void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
-     const CAST_KEY *ks, unsigned char *iv, int enc)
- {
- CAST_LONG tin0,tin1;
- CAST_LONG tout0,tout1,xor0,xor1;
- long l=length;
- CAST_LONG tin[2];
+void CAST_block_encrypt(const unsigned char in[8],
+ unsigned char out[8],
+ const CAST_KEY *key)
+{
+ const unsigned char *pin = in;
+ unsigned char *pout = out;
+ CAST_LONG ti[2];
 
- if (enc)
- {
- n2l(iv,tout0);
- n2l(iv,tout1);
- iv-=8;
- for (l-=8; l>=0; l-=8)
- {
- n2l(in,tin0);
- n2l(in,tin1);
- tin0^=tout0;
- tin1^=tout1;
- tin[0]=tin0;
- tin[1]=tin1;
- CAST_encrypt(tin,ks);
- 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;
- CAST_encrypt(tin,ks);
- tout0=tin[0];
- tout1=tin[1];
- l2n(tout0,out);
- l2n(tout1,out);
- }
- l2n(tout0,iv);
- l2n(tout1,iv);
- }
- else
- {
- n2l(iv,xor0);
- n2l(iv,xor1);
- iv-=8;
- for (l-=8; l>=0; l-=8)
- {
- n2l(in,tin0);
- n2l(in,tin1);
- tin[0]=tin0;
- tin[1]=tin1;
- CAST_decrypt(tin,ks);
- 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;
- CAST_decrypt(tin,ks);
- tout0=tin[0]^xor0;
- tout1=tin[1]^xor1;
- l2nn(tout0,tout1,out,l+8);
- xor0=tin0;
- xor1=tin1;
- }
- l2n(xor0,iv);
- l2n(xor1,iv);
- }
- tin0=tin1=tout0=tout1=xor0=xor1=0;
- tin[0]=tin[1]=0;
- }
+ n2l(pin, ti[0]);
+ n2l(pin, ti[1]);
+ CAST_encrypt(ti, key);
+ l2n(ti[0], pout);
+ l2n(ti[1], pout);
+}
+
+void CAST_block_decrypt(const unsigned char in[8],
+ unsigned char out[8],
+ const CAST_KEY *key)
+{
+ const unsigned char *pin = in;
+ unsigned char *pout = out;
+ CAST_LONG ti[2];
+
+ n2l(pin, ti[0]);
+ n2l(pin, ti[1]);
+ CAST_decrypt(ti, key);
+ l2n(ti[0], pout);
+ l2n(ti[1], pout);
+}
diff --git a/src/lib/libcrypto/cast/c_ofb64.c b/src/lib/libcrypto/cast/c_ofb64.c
index 611425a66822..526b1f097283 100644
--- a/src/lib/libcrypto/cast/c_ofb64.c
+++ b/src/lib/libcrypto/cast/c_ofb64.c
@@ -57,6 +57,7 @@
  */
 
 #include <openssl/cast.h>
+#include <openssl/modes.h>
 #include "cast_lcl.h"
 
 /* The input and output encrypted as though 64bit ofb mode is being
@@ -66,45 +67,6 @@
 void CAST_ofb64_encrypt(const unsigned char *in, unsigned char *out,
  long length, const CAST_KEY *schedule, unsigned char *ivec,
  int *num)
- {
- CAST_LONG v0,v1,t;
- int n= *num;
- long l=length;
- unsigned char d[8];
- char *dp;
- CAST_LONG ti[2];
- unsigned char *iv;
- int save=0;
-
- iv=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)
- {
- CAST_encrypt((CAST_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=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) CAST_block_encrypt);
+}
diff --git a/src/lib/libcrypto/cast/cast_lcl.h b/src/lib/libcrypto/cast/cast_lcl.h
index 100ff1f2d2c8..fe4b83898428 100644
--- a/src/lib/libcrypto/cast/cast_lcl.h
+++ b/src/lib/libcrypto/cast/cast_lcl.h
@@ -214,3 +214,11 @@ extern const CAST_LONG CAST_S_table4[256];
 extern const CAST_LONG CAST_S_table5[256];
 extern const CAST_LONG CAST_S_table6[256];
 extern const CAST_LONG CAST_S_table7[256];
+
+void CAST_block_encrypt(const unsigned char in[8],
+ unsigned char out[8],
+ const CAST_KEY *key);
+
+void CAST_block_decrypt(const unsigned char in[8],
+ unsigned char out[8],
+ const CAST_KEY *key);
--
2.27.0

Reply | Threaded
Open this post in threaded view
|

[PATCH 4/6] crypto: IDEA: convert to use new modes 64-bit helpers

Dmitry Baryshkov
In reply to this post by Dmitry Baryshkov
Convert IDEA cipher to use 64-bit modes helper functions.

Signed-off-by: Dmitry Baryshkov <[hidden email]>
---
 src/lib/libcrypto/idea/i_cbc.c   | 74 +++-----------------------------
 src/lib/libcrypto/idea/i_cfb64.c | 57 ++----------------------
 src/lib/libcrypto/idea/i_ofb64.c | 47 ++------------------
 3 files changed, 13 insertions(+), 165 deletions(-)

diff --git a/src/lib/libcrypto/idea/i_cbc.c b/src/lib/libcrypto/idea/i_cbc.c
index 5bb9640c3403..556a4aa5cbf3 100644
--- a/src/lib/libcrypto/idea/i_cbc.c
+++ b/src/lib/libcrypto/idea/i_cbc.c
@@ -57,81 +57,17 @@
  */
 
 #include <openssl/idea.h>
+#include <openssl/modes.h>
 #include "idea_lcl.h"
 
 void idea_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
      IDEA_KEY_SCHEDULE *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)
- {
- n2l(iv,tout0);
- n2l(iv,tout1);
- iv-=8;
- for (l-=8; l>=0; l-=8)
- {
- n2l(in,tin0);
- n2l(in,tin1);
- tin0^=tout0;
- tin1^=tout1;
- tin[0]=tin0;
- tin[1]=tin1;
- idea_encrypt(tin,ks);
- tout0=tin[0]; l2n(tout0,out);
- tout1=tin[1]; l2n(tout1,out);
- }
- if (l != -8)
- {
- n2ln(in,tin0,tin1,l+8);
- tin0^=tout0;
- tin1^=tout1;
- tin[0]=tin0;
- tin[1]=tin1;
- idea_encrypt(tin,ks);
- tout0=tin[0]; l2n(tout0,out);
- tout1=tin[1]; l2n(tout1,out);
- }
- l2n(tout0,iv);
- l2n(tout1,iv);
- }
+ CRYPTO_cbc64_encrypt(in, out, length, ks, iv, (block64_f)idea_ecb_encrypt);
  else
- {
- n2l(iv,xor0);
- n2l(iv,xor1);
- iv-=8;
- for (l-=8; l>=0; l-=8)
- {
- n2l(in,tin0); tin[0]=tin0;
- n2l(in,tin1); tin[1]=tin1;
- idea_encrypt(tin,ks);
- tout0=tin[0]^xor0;
- tout1=tin[1]^xor1;
- l2n(tout0,out);
- l2n(tout1,out);
- xor0=tin0;
- xor1=tin1;
- }
- if (l != -8)
- {
- n2l(in,tin0); tin[0]=tin0;
- n2l(in,tin1); tin[1]=tin1;
- idea_encrypt(tin,ks);
- tout0=tin[0]^xor0;
- tout1=tin[1]^xor1;
- l2nn(tout0,tout1,out,l+8);
- xor0=tin0;
- xor1=tin1;
- }
- l2n(xor0,iv);
- l2n(xor1,iv);
- }
- tin0=tin1=tout0=tout1=xor0=xor1=0;
- tin[0]=tin[1]=0;
- }
+ CRYPTO_cbc64_decrypt(in, out, length, ks, iv, (block64_f)idea_ecb_encrypt);
+}
 
 void idea_encrypt(unsigned long *d, IDEA_KEY_SCHEDULE *key)
  {
diff --git a/src/lib/libcrypto/idea/i_cfb64.c b/src/lib/libcrypto/idea/i_cfb64.c
index b979aaef8669..a74b50d82309 100644
--- a/src/lib/libcrypto/idea/i_cfb64.c
+++ b/src/lib/libcrypto/idea/i_cfb64.c
@@ -57,6 +57,7 @@
  */
 
 #include <openssl/idea.h>
+#include <openssl/modes.h>
 #include "idea_lcl.h"
 
 /* The input and output encrypted as though 64bit cfb mode is being
@@ -67,56 +68,6 @@
 void idea_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  long length, IDEA_KEY_SCHEDULE *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)
- {
- n2l(iv,v0); ti[0]=v0;
- n2l(iv,v1); ti[1]=v1;
- idea_encrypt((unsigned 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;
- idea_encrypt((unsigned 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)idea_ecb_encrypt);
+}
diff --git a/src/lib/libcrypto/idea/i_ofb64.c b/src/lib/libcrypto/idea/i_ofb64.c
index 376dad9f6d91..9775bc211595 100644
--- a/src/lib/libcrypto/idea/i_ofb64.c
+++ b/src/lib/libcrypto/idea/i_ofb64.c
@@ -57,6 +57,7 @@
  */
 
 #include <openssl/idea.h>
+#include <openssl/modes.h>
 #include "idea_lcl.h"
 
 /* The input and output encrypted as though 64bit ofb mode is being
@@ -66,46 +67,6 @@
 void idea_ofb64_encrypt(const unsigned char *in, unsigned char *out,
  long length, IDEA_KEY_SCHEDULE *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;
- 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)
- {
- idea_encrypt((unsigned 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)idea_ecb_encrypt);
+}
--
2.27.0

Reply | Threaded
Open this post in threaded view
|

[PATCH 5/6] crypto: RC2: convert to use new modes 64-bit helpers

Dmitry Baryshkov
In reply to this post by Dmitry Baryshkov
Convert RC2 cipher to use 64-bit modes helper functions.

Signed-off-by: Dmitry Baryshkov <[hidden email]>
---
 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

Reply | Threaded
Open this post in threaded view
|

[PATCH 6/6] crypto: Gost 28147-89: convert to use new modes 64-bit helpers

Dmitry Baryshkov
In reply to this post by Dmitry Baryshkov
Convert Gost 28147-89 cipher to use 64-bit modes helper functions.

Signed-off-by: Dmitry Baryshkov <[hidden email]>
---
 src/lib/libcrypto/gost/gost2814789.c | 121 ++-------------------------
 1 file changed, 9 insertions(+), 112 deletions(-)

diff --git a/src/lib/libcrypto/gost/gost2814789.c b/src/lib/libcrypto/gost/gost2814789.c
index e285413ed460..bbd578ef7010 100644
--- a/src/lib/libcrypto/gost/gost2814789.c
+++ b/src/lib/libcrypto/gost/gost2814789.c
@@ -56,6 +56,7 @@
 #ifndef OPENSSL_NO_GOST
 #include <openssl/objects.h>
 #include <openssl/gost.h>
+#include <openssl/modes.h>
 
 #include "gost_locl.h"
 
@@ -181,15 +182,17 @@ Gost2814789_ecb_encrypt(const unsigned char *in, unsigned char *out,
 }
 
 static inline void
-Gost2814789_encrypt_mesh(unsigned char *iv, GOST2814789_KEY *key)
+Gost2814789_encrypt_mesh(const unsigned char *in, unsigned char *out, GOST2814789_KEY *key)
 {
  if (key->key_meshing && key->count == 1024) {
  Gost2814789_cryptopro_key_mesh(key);
- Gost2814789_encrypt(iv, iv, key);
- key->count = 0;
+ Gost2814789_encrypt(in, out, key);
+ Gost2814789_encrypt(out, out, key);
+ key->count = 8;
+ } else {
+ Gost2814789_encrypt(in, out, key);
+ key->count += 8;
  }
- Gost2814789_encrypt(iv, iv, key);
- key->count += 8;
 }
 
 static inline void
@@ -209,113 +212,7 @@ Gost2814789_cfb64_encrypt(const unsigned char *in, unsigned char *out,
     size_t len, GOST2814789_KEY *key, unsigned char *ivec, int *num,
     const int enc)
 {
- unsigned int n;
- size_t l = 0;
-
- n = *num;
-
- if (enc) {
-#if !defined(OPENSSL_SMALL_FOOTPRINT)
- if (8 % sizeof(size_t) == 0) do { /* always true actually */
- while (n && len) {
- *(out++) = ivec[n] ^= *(in++);
- --len;
- n = (n + 1) % 8;
- }
-#ifdef __STRICT_ALIGNMENT
- if (((size_t)in | (size_t)out | (size_t)ivec) %
-    sizeof(size_t) != 0)
- break;
-#endif
- while (len >= 8) {
- Gost2814789_encrypt_mesh(ivec, key);
- for (; n < 8; n += sizeof(size_t)) {
- *(size_t*)(out + n) =
- *(size_t*)(ivec + n) ^=
-    *(size_t*)(in + n);
- }
- len -= 8;
- out += 8;
- in  += 8;
- n = 0;
- }
- if (len) {
- Gost2814789_encrypt_mesh(ivec, key);
- while (len--) {
- out[n] = ivec[n] ^= in[n];
- ++n;
- }
- }
- *num = n;
- return;
- } while (0);
- /* the rest would be commonly eliminated by x86* compiler */
-#endif
- while (l<len) {
- if (n == 0) {
- Gost2814789_encrypt_mesh(ivec, key);
- }
- out[l] = ivec[n] ^= in[l];
- ++l;
- n = (n + 1) % 8;
- }
- *num = n;
- } else {
-#if !defined(OPENSSL_SMALL_FOOTPRINT)
- if (8 % sizeof(size_t) == 0) do { /* always true actually */
- while (n && len) {
- unsigned char c;
-
- *(out++) = ivec[n] ^ (c = *(in++));
- ivec[n] = c;
- --len;
- n = (n + 1) % 8;
- }
-#ifdef __STRICT_ALIGNMENT
- if (((size_t)in | (size_t)out | (size_t)ivec) %
-    sizeof(size_t) != 0)
- break;
-#endif
- while (len >= 8) {
- Gost2814789_encrypt_mesh(ivec, key);
- for (; n < 8; n += sizeof(size_t)) {
- size_t t = *(size_t*)(in + n);
- *(size_t*)(out + n) =
-    *(size_t*)(ivec + n) ^ t;
- *(size_t*)(ivec + n) = t;
- }
- len -= 8;
- out += 8;
- in  += 8;
- n = 0;
- }
- if (len) {
- Gost2814789_encrypt_mesh(ivec, key);
- while (len--) {
- unsigned char c;
-
- out[n] = ivec[n] ^ (c = in[n]);
- ivec[n] = c;
- ++n;
- }
- }
- *num = n;
- return;
- } while (0);
- /* the rest would be commonly eliminated by x86* compiler */
-#endif
- while (l < len) {
- unsigned char c;
-
- if (n == 0) {
- Gost2814789_encrypt_mesh(ivec, key);
- }
- out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
- ++l;
- n = (n + 1) % 8;
- }
- *num = n;
- }
+ CRYPTO_cfb64_encrypt(in, out, len, key, ivec, num, enc, (block64_f) Gost2814789_encrypt_mesh);
 }
 
 static inline void
--
2.27.0

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 3/6] crypto: cast: convert to use new modes 64-bit helpers

Joerg Sonnenberger-2
In reply to this post by Dmitry Baryshkov
On Sat, Jun 27, 2020 at 10:36:58PM +0300, Dmitry Baryshkov wrote:
> + * 3. All advertising materials mentioning features or use of this software
> + *    must display the following acknowledgement:
> + *    "This product includes cryptographic software written by
> + *     Eric Young ([hidden email])"
> + *    The word 'cryptographic' can be left out if the rouines from the library
> + *    being used are not cryptographic related :-).

Is the typo in routines necessary?

Joreg