Debian Codegraph @

src/raw_cbc.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_cbc)
35
36 #include "block_base.h"
37
38 #define ERR_CBC_IV_LEN ((1 << 16) | 1)
39
40 #define MAX_BLOCK_LEN 16
41
42 typedef struct {
43 BlockBase *cipher;
44 uint8_t iv[MAX_BLOCK_LEN];
45 } CbcModeState;
46
47 EXPORT_SYM int CBC_start_operation(BlockBase *cipher,
48 const uint8_t iv[],
49 size_t iv_len,
50 CbcModeState **pResult)
51 {
52 if ((NULL == cipher) || (NULL == iv) || (NULL == pResult))
53 return ERR_NULL;
54
55 if (cipher->block_len > MAX_BLOCK_LEN)
56 return ERR_BLOCK_SIZE;
57
58 if (cipher->block_len != iv_len)
59 return ERR_CBC_IV_LEN;
60
61 *pResult = calloc(1, sizeof(CbcModeState));
62 if (NULL == *pResult) {
63 return ERR_MEMORY;
64 }
65
66 (*pResult)->cipher = cipher;
67 memcpy((*pResult)->iv, iv, iv_len);
68
69 return 0;
70 }
71
72 EXPORT_SYM int CBC_encrypt(CbcModeState *cbcState,
73 const uint8_t *in,
74 uint8_t *out,
75 size_t data_len)
76 {
77 uint8_t pt[MAX_BLOCK_LEN],
78 iv[MAX_BLOCK_LEN];
79 size_t block_len;
80
81 if ((NULL == cbcState) || (NULL == in) || (NULL == out))
82 return ERR_NULL;
83
84 block_len = cbcState->cipher->block_len;
85 if (block_len > MAX_BLOCK_LEN)
86 return ERR_BLOCK_SIZE;
87
88 memcpy(iv, cbcState->iv, block_len);
89 while (data_len >= block_len) {
90 unsigned i;
91 int result;
92
93 for (i=0; i<block_len; i++)
94 pt[i] = in[i] ^ iv[i];
95
96 result = cbcState->cipher->encrypt(cbcState->cipher, pt, out, block_len);
97 if (result)
98 return result;
99
100 memcpy(iv, out, block_len);
101
102 data_len -= block_len;
103 in += block_len;
104 out += block_len;
105 }
106 memcpy(cbcState->iv, iv, block_len);
107
108 if (data_len > 0)
109 return ERR_NOT_ENOUGH_DATA;
110
111 return 0;
112 }
113
114 EXPORT_SYM int CBC_decrypt(CbcModeState *cbcState,
115 const uint8_t *in,
116 uint8_t *out,
117 size_t data_len)
118 {
119 uint8_t pt[MAX_BLOCK_LEN],
120 iv[MAX_BLOCK_LEN];
121 size_t block_len;
122
123 if ((NULL == cbcState) || (NULL == in) || (NULL == out))
124 return ERR_NULL;
125
126 block_len = cbcState->cipher->block_len;
127 if (block_len > MAX_BLOCK_LEN)
128 return ERR_BLOCK_SIZE;
129
130 memcpy(iv, cbcState->iv, block_len);
131 while (data_len >= block_len) {
132 unsigned i;
133 int result;
134
135 result = cbcState->cipher->decrypt(cbcState->cipher, in, pt, block_len);
136 if (result)
137 return result;
138
139 for (i=0; i<block_len; i++)
140 pt[i] = pt[i] ^ iv[i];
141
142 memcpy(iv, in, block_len);
143 memcpy(out, pt, block_len);
144
145 data_len -= block_len;
146 in += block_len;
147 out += block_len;
148 }
149 memcpy(cbcState->iv, iv, block_len);
150
151 if (data_len > 0)
152 return ERR_NOT_ENOUGH_DATA;
153
154 return 0;
155 }
156
157
158 EXPORT_SYM int CBC_stop_operation(CbcModeState *state)
159 {
160 if (NULL == state)
161 return ERR_NULL;
162
163 state->cipher->destructor(state->cipher);
164 free(state);
165 return 0;
166 }