Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / ctr.c
1 /* ctr.c
2  *
3  * Cipher counter mode.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2005 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 "ctr.h"
35
36 #include "macros.h"
37 #include "memxor.h"
38 #include "nettle-internal.h"
39
40 #define NBLOCKS 4
41
42 void
43 ctr_crypt(void *ctx, nettle_crypt_func *f,
44           unsigned block_size, uint8_t *ctr,
45           unsigned length, uint8_t *dst,
46           const uint8_t *src)
47 {
48   if (src != dst)
49     {
50       if (length == block_size)
51         {
52           f(ctx, block_size, dst, ctr);
53           INCREMENT(block_size, ctr);
54           memxor(dst, src, block_size);
55         }
56       else
57         {
58           unsigned left;
59           uint8_t *p;     
60
61           for (p = dst, left = length;
62                left >= block_size;
63                left -= block_size, p += block_size)
64             {
65               memcpy (p, ctr, block_size);
66               INCREMENT(block_size, ctr);
67             }
68
69           f(ctx, length - left, dst, dst);
70           memxor(dst, src, length - left);
71
72           if (left)
73             {
74               TMP_DECL(buffer, uint8_t, NETTLE_MAX_CIPHER_BLOCK_SIZE);
75               TMP_ALLOC(buffer, block_size);
76
77               f(ctx, block_size, buffer, ctr);
78               INCREMENT(block_size, ctr);
79               memxor3(dst + length - left, src + length - left, buffer, left);
80             }
81         }
82     }
83   else
84     {
85       if (length > block_size)
86         {
87           TMP_DECL(buffer, uint8_t, NBLOCKS * NETTLE_MAX_CIPHER_BLOCK_SIZE);
88           unsigned chunk = NBLOCKS * block_size;
89
90           TMP_ALLOC(buffer, chunk);
91
92           for (; length >= chunk;
93                length -= chunk, src += chunk, dst += chunk)
94             {
95               unsigned n;
96               uint8_t *p;         
97               for (n = 0, p = buffer; n < NBLOCKS; n++, p += block_size)
98                 {
99                   memcpy (p, ctr, block_size);
100                   INCREMENT(block_size, ctr);
101                 }
102               f(ctx, chunk, buffer, buffer);
103               memxor(dst, buffer, chunk);
104             }
105
106           if (length > 0)
107             {
108               /* Final, possibly partial, blocks */
109               for (chunk = 0; chunk < length; chunk += block_size)
110                 {
111                   memcpy (buffer + chunk, ctr, block_size);
112                   INCREMENT(block_size, ctr);
113                 }
114               f(ctx, chunk, buffer, buffer);
115               memxor3(dst, src, buffer, length);
116             }
117         }
118       else if (length > 0)
119         {
120           TMP_DECL(buffer, uint8_t, NETTLE_MAX_CIPHER_BLOCK_SIZE);
121           TMP_ALLOC(buffer, block_size);
122
123           f(ctx, block_size, buffer, ctr);
124           INCREMENT(block_size, ctr);
125           memxor3(dst, src, buffer, length);
126         }
127     }
128 }