diff options
| author | jsing <> | 2024-03-29 05:23:50 +0000 |
|---|---|---|
| committer | jsing <> | 2024-03-29 05:23:50 +0000 |
| commit | 1134df9b839cdc8a2d4b319397c0e460f131ca4e (patch) | |
| tree | e69cbc2f8f21a1890978c7a675bb7d0034d7bcc5 /src | |
| parent | 55e5787cb0bc4e62cca6c092e03eb10b2f12b700 (diff) | |
| download | openbsd-1134df9b839cdc8a2d4b319397c0e460f131ca4e.tar.gz openbsd-1134df9b839cdc8a2d4b319397c0e460f131ca4e.tar.bz2 openbsd-1134df9b839cdc8a2d4b319397c0e460f131ca4e.zip | |
Consolidate idea into a single C file.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/Makefile | 8 | ||||
| -rw-r--r-- | src/lib/libcrypto/idea/i_cfb64.c | 124 | ||||
| -rw-r--r-- | src/lib/libcrypto/idea/i_ecb.c | 80 | ||||
| -rw-r--r-- | src/lib/libcrypto/idea/i_ofb64.c | 111 | ||||
| -rw-r--r-- | src/lib/libcrypto/idea/idea.c (renamed from src/lib/libcrypto/idea/i_skey.c) | 321 |
5 files changed, 287 insertions, 357 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile index b763757ebf..49322bc5bb 100644 --- a/src/lib/libcrypto/Makefile +++ b/src/lib/libcrypto/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.187 2024/03/29 02:41:49 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.188 2024/03/29 05:23:50 jsing Exp $ |
| 2 | 2 | ||
| 3 | LIB= crypto | 3 | LIB= crypto |
| 4 | LIBREBUILD=y | 4 | LIBREBUILD=y |
| @@ -393,11 +393,7 @@ SRCS+= hm_pmeth.c | |||
| 393 | SRCS+= hmac.c | 393 | SRCS+= hmac.c |
| 394 | 394 | ||
| 395 | # idea/ | 395 | # idea/ |
| 396 | SRCS+= i_cbc.c | 396 | SRCS+= idea.c |
| 397 | SRCS+= i_cfb64.c | ||
| 398 | SRCS+= i_ecb.c | ||
| 399 | SRCS+= i_ofb64.c | ||
| 400 | SRCS+= i_skey.c | ||
| 401 | 397 | ||
| 402 | # kdf/ | 398 | # kdf/ |
| 403 | SRCS+= hkdf_evp.c | 399 | SRCS+= hkdf_evp.c |
diff --git a/src/lib/libcrypto/idea/i_cfb64.c b/src/lib/libcrypto/idea/i_cfb64.c deleted file mode 100644 index a1a543f619..0000000000 --- a/src/lib/libcrypto/idea/i_cfb64.c +++ /dev/null | |||
| @@ -1,124 +0,0 @@ | |||
| 1 | /* $OpenBSD: i_cfb64.c,v 1.6 2023/07/08 10:44:00 beck Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <openssl/idea.h> | ||
| 60 | #include "idea_local.h" | ||
| 61 | |||
| 62 | /* The input and output encrypted as though 64bit cfb mode is being | ||
| 63 | * used. The extra state information to record how much of the | ||
| 64 | * 64bit block we have used is contained in *num; | ||
| 65 | */ | ||
| 66 | |||
| 67 | void | ||
| 68 | idea_cfb64_encrypt(const unsigned char *in, unsigned char *out, | ||
| 69 | long length, IDEA_KEY_SCHEDULE *schedule, | ||
| 70 | unsigned char *ivec, int *num, int encrypt) | ||
| 71 | { | ||
| 72 | unsigned long v0, v1, t; | ||
| 73 | int n = *num; | ||
| 74 | long l = length; | ||
| 75 | unsigned long ti[2]; | ||
| 76 | unsigned char *iv, c, cc; | ||
| 77 | |||
| 78 | iv = (unsigned char *)ivec; | ||
| 79 | if (encrypt) { | ||
| 80 | while (l--) { | ||
| 81 | if (n == 0) { | ||
| 82 | n2l(iv, v0); | ||
| 83 | ti[0] = v0; | ||
| 84 | n2l(iv, v1); | ||
| 85 | ti[1] = v1; | ||
| 86 | idea_encrypt((unsigned long *)ti, schedule); | ||
| 87 | iv = (unsigned char *)ivec; | ||
| 88 | t = ti[0]; | ||
| 89 | l2n(t, iv); | ||
| 90 | t = ti[1]; | ||
| 91 | l2n(t, iv); | ||
| 92 | iv = (unsigned char *)ivec; | ||
| 93 | } | ||
| 94 | c = *(in++) ^ iv[n]; | ||
| 95 | *(out++) = c; | ||
| 96 | iv[n] = c; | ||
| 97 | n = (n + 1) & 0x07; | ||
| 98 | } | ||
| 99 | } else { | ||
| 100 | while (l--) { | ||
| 101 | if (n == 0) { | ||
| 102 | n2l(iv, v0); | ||
| 103 | ti[0] = v0; | ||
| 104 | n2l(iv, v1); | ||
| 105 | ti[1] = v1; | ||
| 106 | idea_encrypt((unsigned long *)ti, schedule); | ||
| 107 | iv = (unsigned char *)ivec; | ||
| 108 | t = ti[0]; | ||
| 109 | l2n(t, iv); | ||
| 110 | t = ti[1]; | ||
| 111 | l2n(t, iv); | ||
| 112 | iv = (unsigned char *)ivec; | ||
| 113 | } | ||
| 114 | cc = *(in++); | ||
| 115 | c = iv[n]; | ||
| 116 | iv[n] = cc; | ||
| 117 | *(out++) = c ^ cc; | ||
| 118 | n = (n + 1) & 0x07; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | v0 = v1 = ti[0] = ti[1] = t = c = cc = 0; | ||
| 122 | *num = n; | ||
| 123 | } | ||
| 124 | LCRYPTO_ALIAS(idea_cfb64_encrypt); | ||
diff --git a/src/lib/libcrypto/idea/i_ecb.c b/src/lib/libcrypto/idea/i_ecb.c deleted file mode 100644 index 9f7db232b1..0000000000 --- a/src/lib/libcrypto/idea/i_ecb.c +++ /dev/null | |||
| @@ -1,80 +0,0 @@ | |||
| 1 | /* $OpenBSD: i_ecb.c,v 1.7 2023/07/28 10:35:14 tb Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <openssl/idea.h> | ||
| 60 | #include "idea_local.h" | ||
| 61 | #include <openssl/opensslv.h> | ||
| 62 | |||
| 63 | void | ||
| 64 | idea_ecb_encrypt(const unsigned char *in, unsigned char *out, | ||
| 65 | IDEA_KEY_SCHEDULE *ks) | ||
| 66 | { | ||
| 67 | unsigned long l0, l1, d[2]; | ||
| 68 | |||
| 69 | n2l(in, l0); | ||
| 70 | d[0] = l0; | ||
| 71 | n2l(in, l1); | ||
| 72 | d[1] = l1; | ||
| 73 | idea_encrypt(d, ks); | ||
| 74 | l0 = d[0]; | ||
| 75 | l2n(l0, out); | ||
| 76 | l1 = d[1]; | ||
| 77 | l2n(l1, out); | ||
| 78 | l0 = l1 = d[0] = d[1] = 0; | ||
| 79 | } | ||
| 80 | LCRYPTO_ALIAS(idea_ecb_encrypt); | ||
diff --git a/src/lib/libcrypto/idea/i_ofb64.c b/src/lib/libcrypto/idea/i_ofb64.c deleted file mode 100644 index 5af394ef70..0000000000 --- a/src/lib/libcrypto/idea/i_ofb64.c +++ /dev/null | |||
| @@ -1,111 +0,0 @@ | |||
| 1 | /* $OpenBSD: i_ofb64.c,v 1.6 2023/07/08 10:44:00 beck Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <openssl/idea.h> | ||
| 60 | #include "idea_local.h" | ||
| 61 | |||
| 62 | /* The input and output encrypted as though 64bit ofb mode is being | ||
| 63 | * used. The extra state information to record how much of the | ||
| 64 | * 64bit block we have used is contained in *num; | ||
| 65 | */ | ||
| 66 | void | ||
| 67 | idea_ofb64_encrypt(const unsigned char *in, unsigned char *out, | ||
| 68 | long length, IDEA_KEY_SCHEDULE *schedule, | ||
| 69 | unsigned char *ivec, int *num) | ||
| 70 | { | ||
| 71 | unsigned long v0, v1, t; | ||
| 72 | int n = *num; | ||
| 73 | long l = length; | ||
| 74 | unsigned char d[8]; | ||
| 75 | char *dp; | ||
| 76 | unsigned long ti[2]; | ||
| 77 | unsigned char *iv; | ||
| 78 | int save = 0; | ||
| 79 | |||
| 80 | iv = (unsigned char *)ivec; | ||
| 81 | n2l(iv, v0); | ||
| 82 | n2l(iv, v1); | ||
| 83 | ti[0] = v0; | ||
| 84 | ti[1] = v1; | ||
| 85 | dp = (char *)d; | ||
| 86 | l2n(v0, dp); | ||
| 87 | l2n(v1, dp); | ||
| 88 | while (l--) { | ||
| 89 | if (n == 0) { | ||
| 90 | idea_encrypt((unsigned long *)ti, schedule); | ||
| 91 | dp = (char *)d; | ||
| 92 | t = ti[0]; | ||
| 93 | l2n(t, dp); | ||
| 94 | t = ti[1]; | ||
| 95 | l2n(t, dp); | ||
| 96 | save++; | ||
| 97 | } | ||
| 98 | *(out++) = *(in++) ^ d[n]; | ||
| 99 | n = (n + 1) & 0x07; | ||
| 100 | } | ||
| 101 | if (save) { | ||
| 102 | v0 = ti[0]; | ||
| 103 | v1 = ti[1]; | ||
| 104 | iv = (unsigned char *)ivec; | ||
| 105 | l2n(v0, iv); | ||
| 106 | l2n(v1, iv); | ||
| 107 | } | ||
| 108 | t = v0 = v1 = ti[0] = ti[1] = 0; | ||
| 109 | *num = n; | ||
| 110 | } | ||
| 111 | LCRYPTO_ALIAS(idea_ofb64_encrypt); | ||
diff --git a/src/lib/libcrypto/idea/i_skey.c b/src/lib/libcrypto/idea/idea.c index ad349bba57..809283b3c9 100644 --- a/src/lib/libcrypto/idea/i_skey.c +++ b/src/lib/libcrypto/idea/idea.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: i_skey.c,v 1.7 2023/07/08 10:44:00 beck Exp $ */ | 1 | /* $OpenBSD: idea.c,v 1.1 2024/03/29 05:23:50 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -47,7 +47,7 @@ | |||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
| 52 | * | 52 | * |
| 53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
| @@ -56,11 +56,292 @@ | |||
| 56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
| 57 | */ | 57 | */ |
| 58 | 58 | ||
| 59 | #include <openssl/crypto.h> | ||
| 60 | #include <openssl/idea.h> | 59 | #include <openssl/idea.h> |
| 61 | #include "idea_local.h" | 60 | #include "idea_local.h" |
| 62 | 61 | ||
| 63 | static IDEA_INT inverse(unsigned int xin); | 62 | void |
| 63 | idea_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, | ||
| 64 | IDEA_KEY_SCHEDULE *ks, unsigned char *iv, int encrypt) | ||
| 65 | { | ||
| 66 | unsigned long tin0, tin1; | ||
| 67 | unsigned long tout0, tout1, xor0, xor1; | ||
| 68 | long l = length; | ||
| 69 | unsigned long tin[2]; | ||
| 70 | |||
| 71 | if (encrypt) { | ||
| 72 | n2l(iv, tout0); | ||
| 73 | n2l(iv, tout1); | ||
| 74 | iv -= 8; | ||
| 75 | for (l -= 8; l >= 0; l -= 8) | ||
| 76 | { | ||
| 77 | n2l(in, tin0); | ||
| 78 | n2l(in, tin1); | ||
| 79 | tin0 ^= tout0; | ||
| 80 | tin1 ^= tout1; | ||
| 81 | tin[0] = tin0; | ||
| 82 | tin[1] = tin1; | ||
| 83 | idea_encrypt(tin, ks); | ||
| 84 | tout0 = tin[0]; | ||
| 85 | l2n(tout0, out); | ||
| 86 | tout1 = tin[1]; | ||
| 87 | l2n(tout1, out); | ||
| 88 | } | ||
| 89 | if (l != -8) { | ||
| 90 | n2ln(in, tin0, tin1, l + 8); | ||
| 91 | tin0 ^= tout0; | ||
| 92 | tin1 ^= tout1; | ||
| 93 | tin[0] = tin0; | ||
| 94 | tin[1] = tin1; | ||
| 95 | idea_encrypt(tin, ks); | ||
| 96 | tout0 = tin[0]; | ||
| 97 | l2n(tout0, out); | ||
| 98 | tout1 = tin[1]; | ||
| 99 | l2n(tout1, out); | ||
| 100 | } | ||
| 101 | l2n(tout0, iv); | ||
| 102 | l2n(tout1, iv); | ||
| 103 | } else { | ||
| 104 | n2l(iv, xor0); | ||
| 105 | n2l(iv, xor1); | ||
| 106 | iv -= 8; | ||
| 107 | for (l -= 8; l >= 0; l -= 8) | ||
| 108 | { | ||
| 109 | n2l(in, tin0); | ||
| 110 | tin[0] = tin0; | ||
| 111 | n2l(in, tin1); | ||
| 112 | tin[1] = tin1; | ||
| 113 | idea_encrypt(tin, ks); | ||
| 114 | tout0 = tin[0] ^ xor0; | ||
| 115 | tout1 = tin[1] ^ xor1; | ||
| 116 | l2n(tout0, out); | ||
| 117 | l2n(tout1, out); | ||
| 118 | xor0 = tin0; | ||
| 119 | xor1 = tin1; | ||
| 120 | } | ||
| 121 | if (l != -8) { | ||
| 122 | n2l(in, tin0); | ||
| 123 | tin[0] = tin0; | ||
| 124 | n2l(in, tin1); | ||
| 125 | tin[1] = tin1; | ||
| 126 | idea_encrypt(tin, ks); | ||
| 127 | tout0 = tin[0] ^ xor0; | ||
| 128 | tout1 = tin[1] ^ xor1; | ||
| 129 | l2nn(tout0, tout1, out, l + 8); | ||
| 130 | xor0 = tin0; | ||
| 131 | xor1 = tin1; | ||
| 132 | } | ||
| 133 | l2n(xor0, iv); | ||
| 134 | l2n(xor1, iv); | ||
| 135 | } | ||
| 136 | tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; | ||
| 137 | tin[0] = tin[1] = 0; | ||
| 138 | } | ||
| 139 | LCRYPTO_ALIAS(idea_cbc_encrypt); | ||
| 140 | |||
| 141 | void | ||
| 142 | idea_encrypt(unsigned long *d, IDEA_KEY_SCHEDULE *key) | ||
| 143 | { | ||
| 144 | IDEA_INT *p; | ||
| 145 | unsigned long x1, x2, x3, x4, t0, t1, ul; | ||
| 146 | |||
| 147 | x2 = d[0]; | ||
| 148 | x1 = (x2 >> 16); | ||
| 149 | x4 = d[1]; | ||
| 150 | x3 = (x4 >> 16); | ||
| 151 | |||
| 152 | p = &(key->data[0][0]); | ||
| 153 | |||
| 154 | E_IDEA(0); | ||
| 155 | E_IDEA(1); | ||
| 156 | E_IDEA(2); | ||
| 157 | E_IDEA(3); | ||
| 158 | E_IDEA(4); | ||
| 159 | E_IDEA(5); | ||
| 160 | E_IDEA(6); | ||
| 161 | E_IDEA(7); | ||
| 162 | |||
| 163 | x1 &= 0xffff; | ||
| 164 | idea_mul(x1, x1, *p, ul); | ||
| 165 | p++; | ||
| 166 | |||
| 167 | t0 = x3 + *(p++); | ||
| 168 | t1 = x2 + *(p++); | ||
| 169 | |||
| 170 | x4 &= 0xffff; | ||
| 171 | idea_mul(x4, x4, *p, ul); | ||
| 172 | |||
| 173 | d[0] = (t0 & 0xffff)|((x1 & 0xffff) << 16); | ||
| 174 | d[1] = (x4 & 0xffff)|((t1 & 0xffff) << 16); | ||
| 175 | } | ||
| 176 | LCRYPTO_ALIAS(idea_encrypt); | ||
| 177 | |||
| 178 | /* The input and output encrypted as though 64bit cfb mode is being | ||
| 179 | * used. The extra state information to record how much of the | ||
| 180 | * 64bit block we have used is contained in *num; | ||
| 181 | */ | ||
| 182 | |||
| 183 | void | ||
| 184 | idea_cfb64_encrypt(const unsigned char *in, unsigned char *out, | ||
| 185 | long length, IDEA_KEY_SCHEDULE *schedule, | ||
| 186 | unsigned char *ivec, int *num, int encrypt) | ||
| 187 | { | ||
| 188 | unsigned long v0, v1, t; | ||
| 189 | int n = *num; | ||
| 190 | long l = length; | ||
| 191 | unsigned long ti[2]; | ||
| 192 | unsigned char *iv, c, cc; | ||
| 193 | |||
| 194 | iv = (unsigned char *)ivec; | ||
| 195 | if (encrypt) { | ||
| 196 | while (l--) { | ||
| 197 | if (n == 0) { | ||
| 198 | n2l(iv, v0); | ||
| 199 | ti[0] = v0; | ||
| 200 | n2l(iv, v1); | ||
| 201 | ti[1] = v1; | ||
| 202 | idea_encrypt((unsigned long *)ti, schedule); | ||
| 203 | iv = (unsigned char *)ivec; | ||
| 204 | t = ti[0]; | ||
| 205 | l2n(t, iv); | ||
| 206 | t = ti[1]; | ||
| 207 | l2n(t, iv); | ||
| 208 | iv = (unsigned char *)ivec; | ||
| 209 | } | ||
| 210 | c = *(in++) ^ iv[n]; | ||
| 211 | *(out++) = c; | ||
| 212 | iv[n] = c; | ||
| 213 | n = (n + 1) & 0x07; | ||
| 214 | } | ||
| 215 | } else { | ||
| 216 | while (l--) { | ||
| 217 | if (n == 0) { | ||
| 218 | n2l(iv, v0); | ||
| 219 | ti[0] = v0; | ||
| 220 | n2l(iv, v1); | ||
| 221 | ti[1] = v1; | ||
| 222 | idea_encrypt((unsigned long *)ti, schedule); | ||
| 223 | iv = (unsigned char *)ivec; | ||
| 224 | t = ti[0]; | ||
| 225 | l2n(t, iv); | ||
| 226 | t = ti[1]; | ||
| 227 | l2n(t, iv); | ||
| 228 | iv = (unsigned char *)ivec; | ||
| 229 | } | ||
| 230 | cc = *(in++); | ||
| 231 | c = iv[n]; | ||
| 232 | iv[n] = cc; | ||
| 233 | *(out++) = c ^ cc; | ||
| 234 | n = (n + 1) & 0x07; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | v0 = v1 = ti[0] = ti[1] = t = c = cc = 0; | ||
| 238 | *num = n; | ||
| 239 | } | ||
| 240 | LCRYPTO_ALIAS(idea_cfb64_encrypt); | ||
| 241 | |||
| 242 | void | ||
| 243 | idea_ecb_encrypt(const unsigned char *in, unsigned char *out, | ||
| 244 | IDEA_KEY_SCHEDULE *ks) | ||
| 245 | { | ||
| 246 | unsigned long l0, l1, d[2]; | ||
| 247 | |||
| 248 | n2l(in, l0); | ||
| 249 | d[0] = l0; | ||
| 250 | n2l(in, l1); | ||
| 251 | d[1] = l1; | ||
| 252 | idea_encrypt(d, ks); | ||
| 253 | l0 = d[0]; | ||
| 254 | l2n(l0, out); | ||
| 255 | l1 = d[1]; | ||
| 256 | l2n(l1, out); | ||
| 257 | l0 = l1 = d[0] = d[1] = 0; | ||
| 258 | } | ||
| 259 | LCRYPTO_ALIAS(idea_ecb_encrypt); | ||
| 260 | |||
| 261 | /* | ||
| 262 | * The input and output encrypted as though 64bit ofb mode is being | ||
| 263 | * used. The extra state information to record how much of the | ||
| 264 | * 64bit block we have used is contained in *num; | ||
| 265 | */ | ||
| 266 | void | ||
| 267 | idea_ofb64_encrypt(const unsigned char *in, unsigned char *out, | ||
| 268 | long length, IDEA_KEY_SCHEDULE *schedule, | ||
| 269 | unsigned char *ivec, int *num) | ||
| 270 | { | ||
| 271 | unsigned long v0, v1, t; | ||
| 272 | int n = *num; | ||
| 273 | long l = length; | ||
| 274 | unsigned char d[8]; | ||
| 275 | char *dp; | ||
| 276 | unsigned long ti[2]; | ||
| 277 | unsigned char *iv; | ||
| 278 | int save = 0; | ||
| 279 | |||
| 280 | iv = (unsigned char *)ivec; | ||
| 281 | n2l(iv, v0); | ||
| 282 | n2l(iv, v1); | ||
| 283 | ti[0] = v0; | ||
| 284 | ti[1] = v1; | ||
| 285 | dp = (char *)d; | ||
| 286 | l2n(v0, dp); | ||
| 287 | l2n(v1, dp); | ||
| 288 | while (l--) { | ||
| 289 | if (n == 0) { | ||
| 290 | idea_encrypt((unsigned long *)ti, schedule); | ||
| 291 | dp = (char *)d; | ||
| 292 | t = ti[0]; | ||
| 293 | l2n(t, dp); | ||
| 294 | t = ti[1]; | ||
| 295 | l2n(t, dp); | ||
| 296 | save++; | ||
| 297 | } | ||
| 298 | *(out++) = *(in++) ^ d[n]; | ||
| 299 | n = (n + 1) & 0x07; | ||
| 300 | } | ||
| 301 | if (save) { | ||
| 302 | v0 = ti[0]; | ||
| 303 | v1 = ti[1]; | ||
| 304 | iv = (unsigned char *)ivec; | ||
| 305 | l2n(v0, iv); | ||
| 306 | l2n(v1, iv); | ||
| 307 | } | ||
| 308 | t = v0 = v1 = ti[0] = ti[1] = 0; | ||
| 309 | *num = n; | ||
| 310 | } | ||
| 311 | LCRYPTO_ALIAS(idea_ofb64_encrypt); | ||
| 312 | |||
| 313 | /* taken directly from the 'paper' I'll have a look at it later */ | ||
| 314 | static IDEA_INT | ||
| 315 | inverse(unsigned int xin) | ||
| 316 | { | ||
| 317 | long n1, n2, q, r, b1, b2, t; | ||
| 318 | |||
| 319 | if (xin == 0) | ||
| 320 | b2 = 0; | ||
| 321 | else { | ||
| 322 | n1 = 0x10001; | ||
| 323 | n2 = xin; | ||
| 324 | b2 = 1; | ||
| 325 | b1 = 0; | ||
| 326 | |||
| 327 | do { | ||
| 328 | r = (n1 % n2); | ||
| 329 | q = (n1 - r)/n2; | ||
| 330 | if (r == 0) { | ||
| 331 | if (b2 < 0) | ||
| 332 | b2 = 0x10001 + b2; | ||
| 333 | } else { | ||
| 334 | n1 = n2; | ||
| 335 | n2 = r; | ||
| 336 | t = b2; | ||
| 337 | b2 = b1 - q*b2; | ||
| 338 | b1 = t; | ||
| 339 | } | ||
| 340 | } while (r != 0); | ||
| 341 | } | ||
| 342 | return ((IDEA_INT)b2); | ||
| 343 | } | ||
| 344 | |||
| 64 | void | 345 | void |
| 65 | idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks) | 346 | idea_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks) |
| 66 | { | 347 | { |
| @@ -135,35 +416,3 @@ idea_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk) | |||
| 135 | tp[50] = t; | 416 | tp[50] = t; |
| 136 | } | 417 | } |
| 137 | LCRYPTO_ALIAS(idea_set_decrypt_key); | 418 | LCRYPTO_ALIAS(idea_set_decrypt_key); |
| 138 | |||
| 139 | /* taken directly from the 'paper' I'll have a look at it later */ | ||
| 140 | static IDEA_INT | ||
| 141 | inverse(unsigned int xin) | ||
| 142 | { | ||
| 143 | long n1, n2, q, r, b1, b2, t; | ||
| 144 | |||
| 145 | if (xin == 0) | ||
| 146 | b2 = 0; | ||
| 147 | else { | ||
| 148 | n1 = 0x10001; | ||
| 149 | n2 = xin; | ||
| 150 | b2 = 1; | ||
| 151 | b1 = 0; | ||
| 152 | |||
| 153 | do { | ||
| 154 | r = (n1 % n2); | ||
| 155 | q = (n1 - r)/n2; | ||
| 156 | if (r == 0) { | ||
| 157 | if (b2 < 0) | ||
| 158 | b2 = 0x10001 + b2; | ||
| 159 | } else { | ||
| 160 | n1 = n2; | ||
| 161 | n2 = r; | ||
| 162 | t = b2; | ||
| 163 | b2 = b1 - q*b2; | ||
| 164 | b1 = t; | ||
| 165 | } | ||
| 166 | } while (r != 0); | ||
| 167 | } | ||
| 168 | return ((IDEA_INT)b2); | ||
| 169 | } | ||
