diff options
author | jsing <> | 2023-07-08 07:25:43 +0000 |
---|---|---|
committer | jsing <> | 2023-07-08 07:25:43 +0000 |
commit | 903dc2a64c29e2f25074485b7e91d9844296b1f3 (patch) | |
tree | 62f8b433574e4f04b2602a55200cbb3ef37b48f0 /src/lib/libcrypto/cast/c_enc.c | |
parent | 605061cd51d735c05fdfe92ab4ec68dd715c2deb (diff) | |
download | openbsd-903dc2a64c29e2f25074485b7e91d9844296b1f3.tar.gz openbsd-903dc2a64c29e2f25074485b7e91d9844296b1f3.tar.bz2 openbsd-903dc2a64c29e2f25074485b7e91d9844296b1f3.zip |
Hit cast with the style(9) stick.
Diffstat (limited to 'src/lib/libcrypto/cast/c_enc.c')
-rw-r--r-- | src/lib/libcrypto/cast/c_enc.c | 272 |
1 files changed, 133 insertions, 139 deletions
diff --git a/src/lib/libcrypto/cast/c_enc.c b/src/lib/libcrypto/cast/c_enc.c index cc28991bd2..70cad75f31 100644 --- a/src/lib/libcrypto/cast/c_enc.c +++ b/src/lib/libcrypto/cast/c_enc.c | |||
@@ -1,25 +1,25 @@ | |||
1 | /* $OpenBSD: c_enc.c,v 1.8 2022/11/26 16:08:51 tb Exp $ */ | 1 | /* $OpenBSD: c_enc.c,v 1.9 2023/07/08 07:25:43 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 | * |
5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
8 | * | 8 | * |
9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | 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 | 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 | 13 | * included with this distribution is covered by the same copyright terms |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | * | 15 | * |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
18 | * If this package is used in a product, Eric Young should be given attribution | 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. | 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 | 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. | 21 | * in documentation (online or textual) provided with the package. |
22 | * | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
25 | * are met: | 25 | * are met: |
@@ -34,10 +34,10 @@ | |||
34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 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: | 38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | * | 40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -49,7 +49,7 @@ | |||
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 |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
@@ -60,151 +60,145 @@ | |||
60 | #include "cast_local.h" | 60 | #include "cast_local.h" |
61 | 61 | ||
62 | #ifndef OPENBSD_CAST_ASM | 62 | #ifndef OPENBSD_CAST_ASM |
63 | void CAST_encrypt(CAST_LONG *data, const CAST_KEY *key) | 63 | void |
64 | { | 64 | CAST_encrypt(CAST_LONG *data, const CAST_KEY *key) |
65 | CAST_LONG l,r,t; | 65 | { |
66 | CAST_LONG l, r, t; | ||
66 | const CAST_LONG *k; | 67 | const CAST_LONG *k; |
67 | 68 | ||
68 | k= &(key->data[0]); | 69 | k = &(key->data[0]); |
69 | l=data[0]; | 70 | l = data[0]; |
70 | r=data[1]; | 71 | r = data[1]; |
71 | |||
72 | E_CAST( 0,k,l,r,+,^,-); | ||
73 | E_CAST( 1,k,r,l,^,-,+); | ||
74 | E_CAST( 2,k,l,r,-,+,^); | ||
75 | E_CAST( 3,k,r,l,+,^,-); | ||
76 | E_CAST( 4,k,l,r,^,-,+); | ||
77 | E_CAST( 5,k,r,l,-,+,^); | ||
78 | E_CAST( 6,k,l,r,+,^,-); | ||
79 | E_CAST( 7,k,r,l,^,-,+); | ||
80 | E_CAST( 8,k,l,r,-,+,^); | ||
81 | E_CAST( 9,k,r,l,+,^,-); | ||
82 | E_CAST(10,k,l,r,^,-,+); | ||
83 | E_CAST(11,k,r,l,-,+,^); | ||
84 | if(!key->short_key) | ||
85 | { | ||
86 | E_CAST(12,k,l,r,+,^,-); | ||
87 | E_CAST(13,k,r,l,^,-,+); | ||
88 | E_CAST(14,k,l,r,-,+,^); | ||
89 | E_CAST(15,k,r,l,+,^,-); | ||
90 | } | ||
91 | 72 | ||
92 | data[1]=l&0xffffffffL; | 73 | E_CAST( 0, k,l, r,+,^, -); |
93 | data[0]=r&0xffffffffL; | 74 | E_CAST( 1, k,r, l,^, -,+); |
75 | E_CAST( 2, k,l, r, -,+,^); | ||
76 | E_CAST( 3, k,r, l,+,^, -); | ||
77 | E_CAST( 4, k,l, r,^, -,+); | ||
78 | E_CAST( 5, k,r, l, -,+,^); | ||
79 | E_CAST( 6, k,l, r,+,^, -); | ||
80 | E_CAST( 7, k,r, l,^, -,+); | ||
81 | E_CAST( 8, k,l, r, -,+,^); | ||
82 | E_CAST( 9, k,r, l,+,^, -); | ||
83 | E_CAST(10, k,l, r,^, -,+); | ||
84 | E_CAST(11, k,r, l, -,+,^); | ||
85 | if (!key->short_key) { | ||
86 | E_CAST(12, k,l, r,+,^, -); | ||
87 | E_CAST(13, k,r, l,^, -,+); | ||
88 | E_CAST(14, k,l, r, -,+,^); | ||
89 | E_CAST(15, k,r, l,+,^, -); | ||
94 | } | 90 | } |
95 | 91 | ||
96 | void CAST_decrypt(CAST_LONG *data, const CAST_KEY *key) | 92 | data[1] = l&0xffffffffL; |
97 | { | 93 | data[0] = r&0xffffffffL; |
98 | CAST_LONG l,r,t; | 94 | } |
99 | const CAST_LONG *k; | ||
100 | 95 | ||
101 | k= &(key->data[0]); | 96 | void |
102 | l=data[0]; | 97 | CAST_decrypt(CAST_LONG *data, const CAST_KEY *key) |
103 | r=data[1]; | 98 | { |
99 | CAST_LONG l, r, t; | ||
100 | const CAST_LONG *k; | ||
104 | 101 | ||
105 | if(!key->short_key) | 102 | k = &(key->data[0]); |
106 | { | 103 | l = data[0]; |
107 | E_CAST(15,k,l,r,+,^,-); | 104 | r = data[1]; |
108 | E_CAST(14,k,r,l,-,+,^); | ||
109 | E_CAST(13,k,l,r,^,-,+); | ||
110 | E_CAST(12,k,r,l,+,^,-); | ||
111 | } | ||
112 | E_CAST(11,k,l,r,-,+,^); | ||
113 | E_CAST(10,k,r,l,^,-,+); | ||
114 | E_CAST( 9,k,l,r,+,^,-); | ||
115 | E_CAST( 8,k,r,l,-,+,^); | ||
116 | E_CAST( 7,k,l,r,^,-,+); | ||
117 | E_CAST( 6,k,r,l,+,^,-); | ||
118 | E_CAST( 5,k,l,r,-,+,^); | ||
119 | E_CAST( 4,k,r,l,^,-,+); | ||
120 | E_CAST( 3,k,l,r,+,^,-); | ||
121 | E_CAST( 2,k,r,l,-,+,^); | ||
122 | E_CAST( 1,k,l,r,^,-,+); | ||
123 | E_CAST( 0,k,r,l,+,^,-); | ||
124 | 105 | ||
125 | data[1]=l&0xffffffffL; | 106 | if (!key->short_key) { |
126 | data[0]=r&0xffffffffL; | 107 | E_CAST(15, k,l, r,+,^, -); |
108 | E_CAST(14, k,r, l, -,+,^); | ||
109 | E_CAST(13, k,l, r,^, -,+); | ||
110 | E_CAST(12, k,r, l,+,^, -); | ||
127 | } | 111 | } |
112 | E_CAST(11, k,l, r, -,+,^); | ||
113 | E_CAST(10, k,r, l,^, -,+); | ||
114 | E_CAST( 9, k,l, r,+,^, -); | ||
115 | E_CAST( 8, k,r, l, -,+,^); | ||
116 | E_CAST( 7, k,l, r,^, -,+); | ||
117 | E_CAST( 6, k,r, l,+,^, -); | ||
118 | E_CAST( 5, k,l, r, -,+,^); | ||
119 | E_CAST( 4, k,r, l,^, -,+); | ||
120 | E_CAST( 3, k,l, r,+,^, -); | ||
121 | E_CAST( 2, k,r, l, -,+,^); | ||
122 | E_CAST( 1, k,l, r,^, -,+); | ||
123 | E_CAST( 0, k,r, l,+,^, -); | ||
124 | |||
125 | data[1] = l&0xffffffffL; | ||
126 | data[0] = r&0xffffffffL; | ||
127 | } | ||
128 | #endif | 128 | #endif |
129 | 129 | ||
130 | void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, | 130 | void |
131 | const CAST_KEY *ks, unsigned char *iv, int enc) | 131 | CAST_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, |
132 | { | 132 | const CAST_KEY *ks, unsigned char *iv, int enc) |
133 | CAST_LONG tin0,tin1; | 133 | { |
134 | CAST_LONG tout0,tout1,xor0,xor1; | 134 | CAST_LONG tin0, tin1; |
135 | long l=length; | 135 | CAST_LONG tout0, tout1, xor0, xor1; |
136 | long l = length; | ||
136 | CAST_LONG tin[2]; | 137 | CAST_LONG tin[2]; |
137 | 138 | ||
138 | if (enc) | 139 | if (enc) { |
139 | { | 140 | n2l(iv, tout0); |
140 | n2l(iv,tout0); | 141 | n2l(iv, tout1); |
141 | n2l(iv,tout1); | 142 | iv -= 8; |
142 | iv-=8; | 143 | for (l -= 8; l >= 0; l -= 8) { |
143 | for (l-=8; l>=0; l-=8) | 144 | n2l(in, tin0); |
144 | { | 145 | n2l(in, tin1); |
145 | n2l(in,tin0); | 146 | tin0 ^= tout0; |
146 | n2l(in,tin1); | 147 | tin1 ^= tout1; |
147 | tin0^=tout0; | 148 | tin[0] = tin0; |
148 | tin1^=tout1; | 149 | tin[1] = tin1; |
149 | tin[0]=tin0; | 150 | CAST_encrypt(tin, ks); |
150 | tin[1]=tin1; | 151 | tout0 = tin[0]; |
151 | CAST_encrypt(tin,ks); | 152 | tout1 = tin[1]; |
152 | tout0=tin[0]; | 153 | l2n(tout0, out); |
153 | tout1=tin[1]; | 154 | l2n(tout1, out); |
154 | l2n(tout0,out); | 155 | } |
155 | l2n(tout1,out); | 156 | if (l != -8) { |
156 | } | 157 | n2ln(in, tin0, tin1, l + 8); |
157 | if (l != -8) | 158 | tin0 ^= tout0; |
158 | { | 159 | tin1 ^= tout1; |
159 | n2ln(in,tin0,tin1,l+8); | 160 | tin[0] = tin0; |
160 | tin0^=tout0; | 161 | tin[1] = tin1; |
161 | tin1^=tout1; | 162 | CAST_encrypt(tin, ks); |
162 | tin[0]=tin0; | 163 | tout0 = tin[0]; |
163 | tin[1]=tin1; | 164 | tout1 = tin[1]; |
164 | CAST_encrypt(tin,ks); | 165 | l2n(tout0, out); |
165 | tout0=tin[0]; | 166 | l2n(tout1, out); |
166 | tout1=tin[1]; | 167 | } |
167 | l2n(tout0,out); | 168 | l2n(tout0, iv); |
168 | l2n(tout1,out); | 169 | l2n(tout1, iv); |
169 | } | 170 | } else { |
170 | l2n(tout0,iv); | 171 | n2l(iv, xor0); |
171 | l2n(tout1,iv); | 172 | n2l(iv, xor1); |
173 | iv -= 8; | ||
174 | for (l -= 8; l >= 0; l -= 8) { | ||
175 | n2l(in, tin0); | ||
176 | n2l(in, tin1); | ||
177 | tin[0] = tin0; | ||
178 | tin[1] = tin1; | ||
179 | CAST_decrypt(tin, ks); | ||
180 | tout0 = tin[0]^xor0; | ||
181 | tout1 = tin[1]^xor1; | ||
182 | l2n(tout0, out); | ||
183 | l2n(tout1, out); | ||
184 | xor0 = tin0; | ||
185 | xor1 = tin1; | ||
172 | } | 186 | } |
173 | else | 187 | if (l != -8) { |
174 | { | 188 | n2l(in, tin0); |
175 | n2l(iv,xor0); | 189 | n2l(in, tin1); |
176 | n2l(iv,xor1); | 190 | tin[0] = tin0; |
177 | iv-=8; | 191 | tin[1] = tin1; |
178 | for (l-=8; l>=0; l-=8) | 192 | CAST_decrypt(tin, ks); |
179 | { | 193 | tout0 = tin[0]^xor0; |
180 | n2l(in,tin0); | 194 | tout1 = tin[1]^xor1; |
181 | n2l(in,tin1); | 195 | l2nn(tout0, tout1, out, l + 8); |
182 | tin[0]=tin0; | 196 | xor0 = tin0; |
183 | tin[1]=tin1; | 197 | xor1 = tin1; |
184 | CAST_decrypt(tin,ks); | ||
185 | tout0=tin[0]^xor0; | ||
186 | tout1=tin[1]^xor1; | ||
187 | l2n(tout0,out); | ||
188 | l2n(tout1,out); | ||
189 | xor0=tin0; | ||
190 | xor1=tin1; | ||
191 | } | ||
192 | if (l != -8) | ||
193 | { | ||
194 | n2l(in,tin0); | ||
195 | n2l(in,tin1); | ||
196 | tin[0]=tin0; | ||
197 | tin[1]=tin1; | ||
198 | CAST_decrypt(tin,ks); | ||
199 | tout0=tin[0]^xor0; | ||
200 | tout1=tin[1]^xor1; | ||
201 | l2nn(tout0,tout1,out,l+8); | ||
202 | xor0=tin0; | ||
203 | xor1=tin1; | ||
204 | } | ||
205 | l2n(xor0,iv); | ||
206 | l2n(xor1,iv); | ||
207 | } | 198 | } |
208 | tin0=tin1=tout0=tout1=xor0=xor1=0; | 199 | l2n(xor0, iv); |
209 | tin[0]=tin[1]=0; | 200 | l2n(xor1, iv); |
210 | } | 201 | } |
202 | tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; | ||
203 | tin[0] = tin[1] = 0; | ||
204 | } | ||