Debian Codegraph @

src/raw_cfb.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2014, Legrandin <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include "common.h"
33
34 FAKE_INIT(raw_cfb)
35
36 #include "block_base.h"
37
38 #define ERR_CFB_IV_LEN ((2 << 16) | 1)
39 #define ERR_CFB_INVALID_SEGMENT ((2 << 16) | 2)
40
41 typedef struct {
42 BlockBase *cipher;
43
44 size_t segment_len;
45
46 /** How many bytes at the beginning of the key stream
47 * have already been used.
48 */
49 size_t usedKeyStream;
50
51 uint8_t *keyStream;
52 uint8_t *next_iv;
53 } CfbModeState;
54
55 EXPORT_SYM int CFB_start_operation(BlockBase *cipher,
56 const uint8_t iv[],
57 size_t iv_len,
58 size_t segment_len, /* In bytes */
59 CfbModeState **pResult)
60 {
61 CfbModeState *state;
62
63 if ((NULL == cipher) || (NULL == iv) || (NULL == pResult)) {
64 return ERR_NULL;
65 }
66
67 if (cipher->block_len != iv_len) {
68 return ERR_CFB_IV_LEN;
69 }
70
71 if ((segment_len == 0) || (segment_len > cipher->block_len)) {
72 return ERR_CFB_INVALID_SEGMENT;
73 }
74
75 state = *pResult = calloc(1, sizeof(CfbModeState));
76 if (NULL == *pResult) {
77 return ERR_MEMORY;
78 }
79
80 state->next_iv = calloc(1, cipher->block_len);
81 if (NULL == state->next_iv) {
82 free(state);
83 return ERR_MEMORY;
84 }
85
86 state->keyStream = calloc(1, cipher->block_len);
87 if (NULL == state->keyStream) {
88 free(state->next_iv);
89 free(state);
90 return ERR_MEMORY;
91 }
92
93 state->cipher = cipher;
94 state->segment_len = segment_len;
95 state->usedKeyStream = 0;
96 memcpy(state->next_iv, iv + segment_len, iv_len - segment_len);
97 return cipher->encrypt(state->cipher, iv, state->keyStream, iv_len);
98 }
99
100 enum Direction { DirEncrypt, DirDecrypt };
101
102 static int CFB_transcrypt(CfbModeState *cfbState,
103 const uint8_t *in,
104 uint8_t *out,
105 size_t data_len,
106 enum Direction direction)
107 {
108 uint8_t *next_iv;
109 size_t block_len;
110 size_t segment_len;
111
112 if ((NULL == cfbState) || (NULL == in) || (NULL == out))
113 return ERR_NULL;
114
115 block_len = cfbState->cipher->block_len;
116 segment_len = cfbState->segment_len;
117 next_iv = cfbState->next_iv;
118
119 assert(cfbState->usedKeyStream <= segment_len);
120 assert((direction == DirEncrypt) || (direction == DirDecrypt));
121
122 while (data_len > 0) {
123 unsigned i;
124 size_t keyStreamToUse;
125 uint8_t *segment, *keyStream;
126
127 if (cfbState->usedKeyStream == segment_len) {
128 int result;
129
130 result = cfbState->cipher->encrypt(cfbState->cipher,
131 next_iv,
132 cfbState->keyStream,
133 block_len);
134 if (0 != result)
135 return result;
136
137 /* The next input to the cipher is:
138 * - the old input shifted left by the segment length
139 * - filled on the right with the enough ciphertext
140 *
141 * If the segment length equals the block length,
142 * the next input is simply the ciphertext block.
143 *
144 * If the segment length equals 1, only the leftmost
145 * byte of the ciphertext block is used.
146 */
147 memmove(next_iv, next_iv + segment_len, block_len - segment_len);
148
149 /*
150 * The rest of the next input is copied when enough ciphertext is
151 * available (we need segment_len bytes).
152 */
153 cfbState->usedKeyStream = 0;
154 }
155
156 keyStream = cfbState->keyStream + cfbState->usedKeyStream;
157 keyStreamToUse = MIN(segment_len - cfbState->usedKeyStream, data_len);
158
159 segment = next_iv + (block_len - (segment_len - cfbState->usedKeyStream));
160
161 if (direction == DirDecrypt) {
162 memcpy(segment, in, keyStreamToUse);
163 }
164
165 for (i=0; i<keyStreamToUse; i++) {
166 *out++ = *keyStream++ ^ *in++;
167 }
168
169 if (direction == DirEncrypt) {
170 memcpy(segment, out - keyStreamToUse, keyStreamToUse);
171 }
172
173 data_len -= keyStreamToUse;
174 cfbState->usedKeyStream += keyStreamToUse;
175 }
176
177 return 0;
178 }
179
180 EXPORT_SYM int CFB_encrypt(CfbModeState *cfbState,
181 const uint8_t *in,
182 uint8_t *out,
183 size_t data_len)
184 {
185 return CFB_transcrypt(cfbState, in, out, data_len, DirEncrypt);
186 }
187
188 EXPORT_SYM int CFB_decrypt(CfbModeState *cfbState,
189 const uint8_t *in,
190 uint8_t *out,
191 size_t data_len)
192 {
193 return CFB_transcrypt(cfbState, in, out, data_len, DirDecrypt);
194 }
195
196 EXPORT_SYM int CFB_stop_operation(CfbModeState *state)
197 {
198 if (NULL == state)
199 return ERR_NULL;
200
201 state->cipher->destructor(state->cipher);
202 free(state->next_iv);
203 free(state->keyStream);
204 free(state);
205 return 0;
206 }