summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/modes/cts128.c
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
committercvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
commitdecf84ba5550c1656a7fdb51b5b81969590c3f03 (patch)
tree44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libcrypto/modes/cts128.c
parent7a8f138352aa4eb7b65ac4b1a5fe7630fbee1427 (diff)
downloadopenbsd-libressl-v2.1.5.tar.gz
openbsd-libressl-v2.1.5.tar.bz2
openbsd-libressl-v2.1.5.zip
This commit was manufactured by cvs2git to create branch 'OPENBSD_5_7'.libressl-v2.1.5
Diffstat (limited to 'src/lib/libcrypto/modes/cts128.c')
-rw-r--r--src/lib/libcrypto/modes/cts128.c285
1 files changed, 0 insertions, 285 deletions
diff --git a/src/lib/libcrypto/modes/cts128.c b/src/lib/libcrypto/modes/cts128.c
deleted file mode 100644
index 192dfb7c14..0000000000
--- a/src/lib/libcrypto/modes/cts128.c
+++ /dev/null
@@ -1,285 +0,0 @@
1/* $OpenBSD: cts128.c,v 1.4 2015/02/10 09:46:30 miod Exp $ */
2/* ====================================================================
3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
4 *
5 * Rights for redistribution and usage in source and binary
6 * forms are granted according to the OpenSSL license.
7 */
8
9#include <openssl/crypto.h>
10#include "modes_lcl.h"
11#include <string.h>
12
13#ifndef MODES_DEBUG
14# ifndef NDEBUG
15# define NDEBUG
16# endif
17#endif
18
19/*
20 * Trouble with Ciphertext Stealing, CTS, mode is that there is no
21 * common official specification, but couple of cipher/application
22 * specific ones: RFC2040 and RFC3962. Then there is 'Proposal to
23 * Extend CBC Mode By "Ciphertext Stealing"' at NIST site, which
24 * deviates from mentioned RFCs. Most notably it allows input to be
25 * of block length and it doesn't flip the order of the last two
26 * blocks. CTS is being discussed even in ECB context, but it's not
27 * adopted for any known application. This implementation provides
28 * two interfaces: one compliant with above mentioned RFCs and one
29 * compliant with the NIST proposal, both extending CBC mode.
30 */
31
32size_t CRYPTO_cts128_encrypt_block(const unsigned char *in, unsigned char *out,
33 size_t len, const void *key,
34 unsigned char ivec[16], block128_f block)
35{ size_t residue, n;
36
37 if (len <= 16) return 0;
38
39 if ((residue=len%16) == 0) residue = 16;
40
41 len -= residue;
42
43 CRYPTO_cbc128_encrypt(in,out,len,key,ivec,block);
44
45 in += len;
46 out += len;
47
48 for (n=0; n<residue; ++n)
49 ivec[n] ^= in[n];
50 (*block)(ivec,ivec,key);
51 memcpy(out,out-16,residue);
52 memcpy(out-16,ivec,16);
53
54 return len+residue;
55}
56
57size_t CRYPTO_nistcts128_encrypt_block(const unsigned char *in, unsigned char *out,
58 size_t len, const void *key,
59 unsigned char ivec[16], block128_f block)
60{ size_t residue, n;
61
62 if (len < 16) return 0;
63
64 residue=len%16;
65
66 len -= residue;
67
68 CRYPTO_cbc128_encrypt(in,out,len,key,ivec,block);
69
70 if (residue==0) return len;
71
72 in += len;
73 out += len;
74
75 for (n=0; n<residue; ++n)
76 ivec[n] ^= in[n];
77 (*block)(ivec,ivec,key);
78 memcpy(out-16+residue,ivec,16);
79
80 return len+residue;
81}
82
83size_t CRYPTO_cts128_encrypt(const unsigned char *in, unsigned char *out,
84 size_t len, const void *key,
85 unsigned char ivec[16], cbc128_f cbc)
86{ size_t residue;
87 union { size_t align; unsigned char c[16]; } tmp;
88
89 if (len <= 16) return 0;
90
91 if ((residue=len%16) == 0) residue = 16;
92
93 len -= residue;
94
95 (*cbc)(in,out,len,key,ivec,1);
96
97 in += len;
98 out += len;
99
100#if defined(CBC_HANDLES_TRUNCATED_IO)
101 memcpy(tmp.c,out-16,16);
102 (*cbc)(in,out-16,residue,key,ivec,1);
103 memcpy(out,tmp.c,residue);
104#else
105 memset(tmp.c,0,sizeof(tmp));
106 memcpy(tmp.c,in,residue);
107 memcpy(out,out-16,residue);
108 (*cbc)(tmp.c,out-16,16,key,ivec,1);
109#endif
110 return len+residue;
111}
112
113size_t CRYPTO_nistcts128_encrypt(const unsigned char *in, unsigned char *out,
114 size_t len, const void *key,
115 unsigned char ivec[16], cbc128_f cbc)
116{ size_t residue;
117 union { size_t align; unsigned char c[16]; } tmp;
118
119 if (len < 16) return 0;
120
121 residue=len%16;
122
123 len -= residue;
124
125 (*cbc)(in,out,len,key,ivec,1);
126
127 if (residue==0) return len;
128
129 in += len;
130 out += len;
131
132#if defined(CBC_HANDLES_TRUNCATED_IO)
133 (*cbc)(in,out-16+residue,residue,key,ivec,1);
134#else
135 memset(tmp.c,0,sizeof(tmp));
136 memcpy(tmp.c,in,residue);
137 (*cbc)(tmp.c,out-16+residue,16,key,ivec,1);
138#endif
139 return len+residue;
140}
141
142size_t CRYPTO_cts128_decrypt_block(const unsigned char *in, unsigned char *out,
143 size_t len, const void *key,
144 unsigned char ivec[16], block128_f block)
145{ size_t residue, n;
146 union { size_t align; unsigned char c[32]; } tmp;
147
148 if (len<=16) return 0;
149
150 if ((residue=len%16) == 0) residue = 16;
151
152 len -= 16+residue;
153
154 if (len) {
155 CRYPTO_cbc128_decrypt(in,out,len,key,ivec,block);
156 in += len;
157 out += len;
158 }
159
160 (*block)(in,tmp.c+16,key);
161
162 memcpy(tmp.c,tmp.c+16,16);
163 memcpy(tmp.c,in+16,residue);
164 (*block)(tmp.c,tmp.c,key);
165
166 for(n=0; n<16; ++n) {
167 unsigned char c = in[n];
168 out[n] = tmp.c[n] ^ ivec[n];
169 ivec[n] = c;
170 }
171 for(residue+=16; n<residue; ++n)
172 out[n] = tmp.c[n] ^ in[n];
173
174 return 16+len+residue;
175}
176
177size_t CRYPTO_nistcts128_decrypt_block(const unsigned char *in, unsigned char *out,
178 size_t len, const void *key,
179 unsigned char ivec[16], block128_f block)
180{ size_t residue, n;
181 union { size_t align; unsigned char c[32]; } tmp;
182
183 if (len<16) return 0;
184
185 residue=len%16;
186
187 if (residue==0) {
188 CRYPTO_cbc128_decrypt(in,out,len,key,ivec,block);
189 return len;
190 }
191
192 len -= 16+residue;
193
194 if (len) {
195 CRYPTO_cbc128_decrypt(in,out,len,key,ivec,block);
196 in += len;
197 out += len;
198 }
199
200 (*block)(in+residue,tmp.c+16,key);
201
202 memcpy(tmp.c,tmp.c+16,16);
203 memcpy(tmp.c,in,residue);
204 (*block)(tmp.c,tmp.c,key);
205
206 for(n=0; n<16; ++n) {
207 unsigned char c = in[n];
208 out[n] = tmp.c[n] ^ ivec[n];
209 ivec[n] = in[n+residue];
210 tmp.c[n] = c;
211 }
212 for(residue+=16; n<residue; ++n)
213 out[n] = tmp.c[n] ^ tmp.c[n-16];
214
215 return 16+len+residue;
216}
217
218size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out,
219 size_t len, const void *key,
220 unsigned char ivec[16], cbc128_f cbc)
221{ size_t residue;
222 union { size_t align; unsigned char c[32]; } tmp;
223
224 if (len<=16) return 0;
225
226 if ((residue=len%16) == 0) residue = 16;
227
228 len -= 16+residue;
229
230 if (len) {
231 (*cbc)(in,out,len,key,ivec,0);
232 in += len;
233 out += len;
234 }
235
236 memset(tmp.c,0,sizeof(tmp));
237 /* this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0] */
238 (*cbc)(in,tmp.c,16,key,tmp.c+16,0);
239
240 memcpy(tmp.c,in+16,residue);
241#if defined(CBC_HANDLES_TRUNCATED_IO)
242 (*cbc)(tmp.c,out,16+residue,key,ivec,0);
243#else
244 (*cbc)(tmp.c,tmp.c,32,key,ivec,0);
245 memcpy(out,tmp.c,16+residue);
246#endif
247 return 16+len+residue;
248}
249
250size_t CRYPTO_nistcts128_decrypt(const unsigned char *in, unsigned char *out,
251 size_t len, const void *key,
252 unsigned char ivec[16], cbc128_f cbc)
253{ size_t residue;
254 union { size_t align; unsigned char c[32]; } tmp;
255
256 if (len<16) return 0;
257
258 residue=len%16;
259
260 if (residue==0) {
261 (*cbc)(in,out,len,key,ivec,0);
262 return len;
263 }
264
265 len -= 16+residue;
266
267 if (len) {
268 (*cbc)(in,out,len,key,ivec,0);
269 in += len;
270 out += len;
271 }
272
273 memset(tmp.c,0,sizeof(tmp));
274 /* this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0] */
275 (*cbc)(in+residue,tmp.c,16,key,tmp.c+16,0);
276
277 memcpy(tmp.c,in,residue);
278#if defined(CBC_HANDLES_TRUNCATED_IO)
279 (*cbc)(tmp.c,out,16+residue,key,ivec,0);
280#else
281 (*cbc)(tmp.c,tmp.c,32,key,ivec,0);
282 memcpy(out,tmp.c,16+residue);
283#endif
284 return 16+len+residue;
285}