Initialize Tizen 2.3
[external/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., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, 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 /* Requires that dst != src */
56 static void
57 cbc_decrypt_internal(void *ctx, nettle_crypt_func f,
58                      unsigned block_size, uint8_t *iv,
59                      unsigned length, uint8_t *dst,
60                      const uint8_t *src)
61 {
62   assert(length);
63   assert( !(length % block_size) );
64   assert(src != dst);
65   
66   /* Decrypt in ECB mode */
67   f(ctx, length, dst, src);
68
69   /* XOR the cryptotext, shifted one block */
70   memxor(dst, iv, block_size);
71   memxor(dst + block_size, src, length - block_size);
72   memcpy(iv, src + length - block_size, block_size);
73 }
74
75 /* Don't allocate any more space than this on the stack */
76 #define CBC_BUFFER_LIMIT 4096
77
78 void
79 cbc_decrypt(void *ctx, nettle_crypt_func f,
80             unsigned block_size, uint8_t *iv,
81             unsigned length, uint8_t *dst,
82             const uint8_t *src)
83 {
84   assert(!(length % block_size));
85
86   if (!length)
87     return;
88
89   if (src != dst)
90     cbc_decrypt_internal(ctx, f, block_size, iv,
91                          length, dst, src);
92   else
93     {
94       /* We need a copy of the ciphertext, so we can't ECB decrypt in
95        * place.
96        *
97        * If length is small, we allocate a complete copy of src on the
98        * stack. Otherwise, we allocate a block of size at most
99        * CBC_BUFFER_LIMIT, and process that amount of data at a
100        * time.
101        *
102        * NOTE: We assume that block_size <= CBC_BUFFER_LIMIT. */
103
104       unsigned buffer_size;
105
106       if (length <= CBC_BUFFER_LIMIT)
107         buffer_size = length;
108       else
109         buffer_size
110           = CBC_BUFFER_LIMIT - (CBC_BUFFER_LIMIT % block_size);
111
112       {
113         TMP_DECL(buffer, uint8_t, CBC_BUFFER_LIMIT);
114         TMP_ALLOC(buffer, buffer_size);
115
116         for ( ; length > buffer_size;
117               length -= buffer_size, dst += buffer_size, src += buffer_size)
118           {
119             memcpy(buffer, src, buffer_size);
120             cbc_decrypt_internal(ctx, f, block_size, iv,
121                                  buffer_size, dst, buffer);
122           }
123         /* Now, we have at most CBC_BUFFER_LIMIT octets left */
124         memcpy(buffer, src, length);
125         
126         cbc_decrypt_internal(ctx, f, block_size, iv,
127                              length, dst, buffer);
128       }
129     }
130 }
131
132 #if 0
133 #include "twofish.h"
134 #include "aes.h"
135
136 static void foo(void)
137 {
138   struct CBC_CTX(struct twofish_ctx, TWOFISH_BLOCK_SIZE) ctx;
139   uint8_t src[TWOFISH_BLOCK_SIZE];
140   uint8_t dst[TWOFISH_BLOCK_SIZE];
141   
142   CBC_ENCRYPT(&ctx, twofish_encrypt, TWOFISH_BLOCK_SIZE, dst, src);
143
144   /* Should result in a warning */
145   CBC_ENCRYPT(&ctx, aes_encrypt, TWOFISH_BLOCK_SIZE, dst, src);
146   
147 }
148
149 static void foo2(void)
150 {
151   struct twofish_ctx ctx;
152   uint8_t iv[TWOFISH_BLOCK_SIZE];
153   uint8_t src[TWOFISH_BLOCK_SIZE];
154   uint8_t dst[TWOFISH_BLOCK_SIZE];
155   
156   CBC_ENCRYPT2(&ctx, twofish_encrypt, TWOFISH_BLOCK_SIZE, iv, TWOFISH_BLOCK_SIZE, dst, src);
157   /* Should result in a warning */
158   CBC_ENCRYPT2(&ctx, aes_encrypt, TWOFISH_BLOCK_SIZE, iv, TWOFISH_BLOCK_SIZE, dst, src);
159 }
160
161 #endif