Typo.
[shishi.git] / crypto / cbc-cts.c
blob4adca042f91902140aaa99dbb317938e9dca7c8e
1 /* cbc-cts.c
3 * Cipher block chaining mode, with cipher text stealing.
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2003 Simon Josefsson
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * MA 02111-1307, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "cbc-cts.h"
36 #include "memxor.h"
38 void
39 cbc_cts_encrypt (void *ctx,
40 void (*f) (void *ctx,
41 unsigned length,
42 uint8_t * dst,
43 const uint8_t * src),
44 unsigned block_size, uint8_t * iv,
45 unsigned length, uint8_t * dst, const uint8_t * src)
47 unsigned nblocks = length / block_size;
48 unsigned restbytes = (length % block_size) == 0 ?
49 block_size : length % block_size;
51 for (; nblocks; nblocks--, src += block_size, dst += block_size)
53 memxor (iv, src, block_size);
54 f (ctx, block_size, dst, iv);
55 memcpy (iv, dst, block_size);
58 if (length > block_size)
60 memcpy (dst, dst - block_size, restbytes);
61 dst -= block_size;
62 memcpy (dst, src, restbytes);
63 memset (dst + restbytes, 0, block_size - restbytes);
64 memxor (iv, dst, block_size);
65 f (ctx, block_size, dst, iv);
69 void
70 cbc_cts_decrypt (void *ctx,
71 void (*f) (void *ctx,
72 unsigned length,
73 uint8_t * dst,
74 const uint8_t * src),
75 unsigned block_size, uint8_t * iv,
76 unsigned length, uint8_t * dst, const uint8_t * src)
78 unsigned nblocks = length / block_size;
79 unsigned restbytes = (length % block_size) == 0 ?
80 block_size : length % block_size;
81 uint8_t *tmpiv = alloca (block_size);
83 if (length > block_size)
85 nblocks--;
86 if ((length % block_size) == 0)
87 nblocks--;
90 for (; nblocks; nblocks--, src += block_size, dst += block_size)
92 memcpy (tmpiv, src, block_size);
93 f (ctx, block_size, dst, src);
94 memxor (dst, iv, block_size);
95 memcpy (iv, tmpiv, block_size);
98 if (length > block_size)
100 memcpy (iv, src + block_size, restbytes);
101 f (ctx, block_size, dst, src);
102 memxor (dst, iv, restbytes);
103 memcpy (dst + block_size, dst, restbytes);
104 memcpy (iv + restbytes, dst + restbytes, block_size - restbytes);
105 f (ctx, block_size, dst, iv);
106 memxor (dst, tmpiv, block_size);