Fixed package group
[platform/upstream/nettle.git] / cbc.c
1 /* cbc.c
2  *
3  * Cipher block chaining mode.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2001 Niels Möller
9  *  
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.
14  * 
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.
19  * 
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., 51 Franklin Street, Fifth Floor, Boston,
23  * MA 02111-1301, USA.
24  */
25
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "cbc.h"
35
36 #include "memxor.h"
37 #include "nettle-internal.h"
38
39 void
40 cbc_encrypt(void *ctx, nettle_crypt_func *f,
41             unsigned block_size, uint8_t *iv,
42             unsigned length, uint8_t *dst,
43             const uint8_t *src)
44 {
45   assert(!(length % block_size));
46
47   for ( ; length; length -= block_size, src += block_size, dst += block_size)
48     {
49       memxor(iv, src, block_size);
50       f(ctx, block_size, dst, iv);
51       memcpy(iv, dst, block_size);
52     }
53 }
54
55 /* Don't allocate any more space than this on the stack */
56 #define CBC_BUFFER_LIMIT 512
57
58 void
59 cbc_decrypt(void *ctx, nettle_crypt_func *f,
60             unsigned block_size, uint8_t *iv,
61             unsigned length, uint8_t *dst,
62             const uint8_t *src)
63 {
64   assert(!(length % block_size));
65
66   if (!length)
67     return;
68
69   if (src != dst)
70     {
71       /* Decrypt in ECB mode */
72       f(ctx, length, dst, src);
73
74       /* XOR the cryptotext, shifted one block */
75       memxor(dst, iv, block_size);
76       memxor(dst + block_size, src, length - block_size);
77       memcpy(iv, src + length - block_size, block_size);
78     }
79
80   else
81     {
82       /* For in-place CBC, we decrypt into a temporary buffer of size
83        * at most CBC_BUFFER_LIMIT, and process that amount of data at
84        * a time. */
85       
86       /* NOTE: We assume that block_size <= CBC_BUFFER_LIMIT, and we
87          depend on memxor3 working from the end of the area, allowing
88          certain overlapping operands. */ 
89
90       TMP_DECL(buffer, uint8_t, CBC_BUFFER_LIMIT);
91       TMP_DECL(initial_iv, uint8_t, NETTLE_MAX_CIPHER_BLOCK_SIZE);
92
93       unsigned buffer_size;
94
95       if (length <= CBC_BUFFER_LIMIT)
96         buffer_size = length;
97       else
98         buffer_size
99           = CBC_BUFFER_LIMIT - (CBC_BUFFER_LIMIT % block_size);
100
101       TMP_ALLOC(buffer, buffer_size);
102       TMP_ALLOC(initial_iv, block_size);
103
104       for ( ; length > buffer_size;
105             length -= buffer_size, src += buffer_size, dst += buffer_size)
106         {
107           f(ctx, buffer_size, buffer, src);
108           memcpy(initial_iv, iv, block_size);
109           memcpy(iv, src + buffer_size - block_size, block_size);
110           memxor3(dst + block_size, buffer + block_size, src,
111                   buffer_size - block_size);
112           memxor3(dst, buffer, initial_iv, block_size);
113         }
114
115       f(ctx, length, buffer, src);
116       memcpy(initial_iv, iv, block_size);
117       /* Copies last block */
118       memcpy(iv, src + length - block_size, block_size);
119       /* Writes all but first block, reads all but last block. */
120       memxor3(dst + block_size, buffer + block_size, src,
121               length - block_size);
122       /* Writes first block. */
123       memxor3(dst, buffer, initial_iv, block_size);
124     }
125 }
126
127 #if 0
128 #include "twofish.h"
129 #include "aes.h"
130
131 static void foo(void)
132 {
133   struct CBC_CTX(struct twofish_ctx, TWOFISH_BLOCK_SIZE) ctx;
134   uint8_t src[TWOFISH_BLOCK_SIZE];
135   uint8_t dst[TWOFISH_BLOCK_SIZE];
136   
137   CBC_ENCRYPT(&ctx, twofish_encrypt, TWOFISH_BLOCK_SIZE, dst, src);
138
139   /* Should result in a warning */
140   CBC_ENCRYPT(&ctx, aes_encrypt, TWOFISH_BLOCK_SIZE, dst, src);
141   
142 }
143
144 static void foo2(void)
145 {
146   struct twofish_ctx ctx;
147   uint8_t iv[TWOFISH_BLOCK_SIZE];
148   uint8_t src[TWOFISH_BLOCK_SIZE];
149   uint8_t dst[TWOFISH_BLOCK_SIZE];
150   
151   CBC_ENCRYPT2(&ctx, twofish_encrypt, TWOFISH_BLOCK_SIZE, iv, TWOFISH_BLOCK_SIZE, dst, src);
152   /* Should result in a warning */
153   CBC_ENCRYPT2(&ctx, aes_encrypt, TWOFISH_BLOCK_SIZE, iv, TWOFISH_BLOCK_SIZE, dst, src);
154 }
155
156 #endif