Debian Codegraph @

cipher/cipher-cbc.c

raw
1 /* cipher-cbc.c - Generic CBC mode implementation
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 * 2005, 2007, 2008, 2009, 2011 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "g10lib.h"
28 #include "cipher.h"
29 #include "./cipher-internal.h"
30 #include "bufhelp.h"
31
32
33
34 static inline unsigned int
35 cbc_encrypt_inner(gcry_cipher_hd_t c, unsigned char *outbuf,
36 const unsigned char *inbuf, size_t nblocks, size_t blocksize,
37 int is_cbc_cmac)
38 {
39
40 unsigned int burn, nburn;
41 size_t n;
42
43 burn = 0;
44
45 if (c->bulk.cbc_enc)
46 {
47 c->bulk.cbc_enc (&c->context.c, c->u_iv.iv, outbuf, inbuf, nblocks,
48 is_cbc_cmac);
49 }
50 else
51 {
52 gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
53 unsigned char *ivp;
54
55 ivp = c->u_iv.iv;
56
57 for (n=0; n < nblocks; n++ )
58 {
59 cipher_block_xor (outbuf, inbuf, ivp, blocksize);
60 nburn = enc_fn ( &c->context.c, outbuf, outbuf );
61 burn = nburn > burn ? nburn : burn;
62 ivp = outbuf;
63 inbuf += blocksize;
64 if (!is_cbc_cmac)
65 outbuf += blocksize;
66 }
67
68 if (ivp != c->u_iv.iv)
69 cipher_block_cpy (c->u_iv.iv, ivp, blocksize);
70 }
71
72 return burn;
73 }
74
75
76 gcry_err_code_t
77 _gcry_cipher_cbc_encrypt (gcry_cipher_hd_t c,
78 unsigned char *outbuf, size_t outbuflen,
79 const unsigned char *inbuf, size_t inbuflen)
80 {
81 size_t blocksize_shift = _gcry_blocksize_shift(c);
82 size_t blocksize = 1 << blocksize_shift;
83 size_t blocksize_mask = blocksize - 1;
84 size_t nblocks = inbuflen >> blocksize_shift;
85 int is_cbc_cmac = !!(c->flags & GCRY_CIPHER_CBC_MAC);
86 unsigned int burn;
87
88 if (outbuflen < (is_cbc_cmac ? blocksize : inbuflen))
89 return GPG_ERR_BUFFER_TOO_SHORT;
90
91 if (inbuflen & blocksize_mask)
92 return GPG_ERR_INV_LENGTH;
93
94 burn = cbc_encrypt_inner(c, outbuf, inbuf, nblocks, blocksize, is_cbc_cmac);
95
96 if (burn > 0)
97 _gcry_burn_stack (burn + 4 * sizeof(void *));
98
99 return 0;
100 }
101
102
103 gcry_err_code_t
104 _gcry_cipher_cbc_cts_encrypt (gcry_cipher_hd_t c,
105 unsigned char *outbuf, size_t outbuflen,
106 const unsigned char *inbuf, size_t inbuflen)
107 {
108 size_t blocksize_shift = _gcry_blocksize_shift(c);
109 size_t blocksize = 1 << blocksize_shift;
110 size_t blocksize_mask = blocksize - 1;
111 gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
112 size_t nblocks = inbuflen >> blocksize_shift;
113 unsigned int burn, nburn;
114 unsigned char *ivp;
115 int i;
116
117 if (outbuflen < inbuflen)
118 return GPG_ERR_BUFFER_TOO_SHORT;
119
120 if ((inbuflen & blocksize_mask) && !(inbuflen > blocksize))
121 return GPG_ERR_INV_LENGTH;
122
123 burn = 0;
124
125 if (inbuflen > blocksize)
126 {
127 if ((inbuflen & blocksize_mask) == 0)
128 nblocks--;
129 }
130
131 burn = cbc_encrypt_inner(c, outbuf, inbuf, nblocks, blocksize, 0);
132 inbuf += nblocks << blocksize_shift;
133 outbuf += nblocks << blocksize_shift;
134
135 if (inbuflen > blocksize)
136 {
137 /* We have to be careful here, since outbuf might be equal to
138 inbuf. */
139 size_t restbytes;
140 unsigned char b;
141
142 if ((inbuflen & blocksize_mask) == 0)
143 restbytes = blocksize;
144 else
145 restbytes = inbuflen & blocksize_mask;
146
147 outbuf -= blocksize;
148 for (ivp = c->u_iv.iv, i = 0; i < restbytes; i++)
149 {
150 b = inbuf[i];
151 outbuf[blocksize + i] = outbuf[i];
152 outbuf[i] = b ^ *ivp++;
153 }
154 for (; i < blocksize; i++)
155 outbuf[i] = 0 ^ *ivp++;
156
157 nburn = enc_fn (&c->context.c, outbuf, outbuf);
158 burn = nburn > burn ? nburn : burn;
159 cipher_block_cpy (c->u_iv.iv, outbuf, blocksize);
160 }
161
162 if (burn > 0)
163 _gcry_burn_stack (burn + 4 * sizeof(void *));
164
165 return 0;
166 }
167
168
169 static inline unsigned int
170 cbc_decrypt_inner(gcry_cipher_hd_t c, unsigned char *outbuf,
171 const unsigned char *inbuf, size_t nblocks, size_t blocksize)
172 {
173 unsigned int burn, nburn;
174 size_t n;
175
176 burn = 0;
177
178 if (c->bulk.cbc_dec)
179 {
180 c->bulk.cbc_dec (&c->context.c, c->u_iv.iv, outbuf, inbuf, nblocks);
181 }
182 else
183 {
184 gcry_cipher_decrypt_t dec_fn = c->spec->decrypt;
185
186 for (n = 0; n < nblocks; n++)
187 {
188 /* Because outbuf and inbuf might be the same, we must not overwrite
189 the original ciphertext block. We use LASTIV as intermediate
190 storage here because it is not used otherwise. */
191 nburn = dec_fn ( &c->context.c, c->lastiv, inbuf );
192 burn = nburn > burn ? nburn : burn;
193 cipher_block_xor_n_copy_2 (outbuf, c->lastiv, c->u_iv.iv, inbuf,
194 blocksize);
195 inbuf += blocksize;
196 outbuf += blocksize;
197 }
198 }
199
200 return burn;
201 }
202
203
204 gcry_err_code_t
205 _gcry_cipher_cbc_decrypt (gcry_cipher_hd_t c,
206 unsigned char *outbuf, size_t outbuflen,
207 const unsigned char *inbuf, size_t inbuflen)
208 {
209 size_t blocksize_shift = _gcry_blocksize_shift(c);
210 size_t blocksize = 1 << blocksize_shift;
211 size_t blocksize_mask = blocksize - 1;
212 size_t nblocks = inbuflen >> blocksize_shift;
213 unsigned int burn;
214
215 if (outbuflen < inbuflen)
216 return GPG_ERR_BUFFER_TOO_SHORT;
217
218 if (inbuflen & blocksize_mask)
219 return GPG_ERR_INV_LENGTH;
220
221 burn = cbc_decrypt_inner(c, outbuf, inbuf, nblocks, blocksize);
222
223 if (burn > 0)
224 _gcry_burn_stack (burn + 4 * sizeof(void *));
225
226 return 0;
227 }
228
229
230 gcry_err_code_t
231 _gcry_cipher_cbc_cts_decrypt (gcry_cipher_hd_t c,
232 unsigned char *outbuf, size_t outbuflen,
233 const unsigned char *inbuf, size_t inbuflen)
234 {
235 size_t blocksize_shift = _gcry_blocksize_shift(c);
236 size_t blocksize = 1 << blocksize_shift;
237 size_t blocksize_mask = blocksize - 1;
238 gcry_cipher_decrypt_t dec_fn = c->spec->decrypt;
239 size_t nblocks = inbuflen >> blocksize_shift;
240 unsigned int burn, nburn;
241 int i;
242
243 if (outbuflen < inbuflen)
244 return GPG_ERR_BUFFER_TOO_SHORT;
245
246 if ((inbuflen & blocksize_mask) && !(inbuflen > blocksize))
247 return GPG_ERR_INV_LENGTH;
248
249 burn = 0;
250
251 if (inbuflen > blocksize)
252 {
253 nblocks--;
254 if ((inbuflen & blocksize_mask) == 0)
255 nblocks--;
256 cipher_block_cpy (c->lastiv, c->u_iv.iv, blocksize);
257 }
258
259 burn = cbc_decrypt_inner(c, outbuf, inbuf, nblocks, blocksize);
260 inbuf += nblocks << blocksize_shift;
261 outbuf += nblocks << blocksize_shift;
262
263 if (inbuflen > blocksize)
264 {
265 size_t restbytes;
266
267 if ((inbuflen & blocksize_mask) == 0)
268 restbytes = blocksize;
269 else
270 restbytes = inbuflen & blocksize_mask;
271
272 cipher_block_cpy (c->lastiv, c->u_iv.iv, blocksize ); /* Save Cn-2. */
273 buf_cpy (c->u_iv.iv, inbuf + blocksize, restbytes ); /* Save Cn. */
274
275 nburn = dec_fn ( &c->context.c, outbuf, inbuf );
276 burn = nburn > burn ? nburn : burn;
277 buf_xor(outbuf, outbuf, c->u_iv.iv, restbytes);
278
279 buf_cpy (outbuf + blocksize, outbuf, restbytes);
280 for(i=restbytes; i < blocksize; i++)
281 c->u_iv.iv[i] = outbuf[i];
282 nburn = dec_fn (&c->context.c, outbuf, c->u_iv.iv);
283 burn = nburn > burn ? nburn : burn;
284 cipher_block_xor(outbuf, outbuf, c->lastiv, blocksize);
285 /* c->lastiv is now really lastlastiv, does this matter? */
286 }
287
288 if (burn > 0)
289 _gcry_burn_stack (burn + 4 * sizeof(void *));
290
291 return 0;
292 }