Debian Codegraph @

src/raw_ofb.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_ofb)
35
36 #include "block_base.h"
37
38 #define ERR_OFB_IV_LEN ((3 << 16) | 1)
39
40 #define MAX_BLOCK_LEN 16
41
42 typedef struct {
43 BlockBase *cipher;
44
45 /** How many bytes at the beginning of the key stream
46 * have already been used.
47 */
48 size_t usedKeyStream;
49
50 uint8_t keyStream[MAX_BLOCK_LEN];
51 } OfbModeState;
52
53 EXPORT_SYM int OFB_start_operation(BlockBase *cipher,
54 const uint8_t iv[],
55 size_t iv_len,
56 OfbModeState **pResult)
57 {
58 if ((NULL == cipher) || (NULL == iv) || (NULL == pResult))
59 return ERR_NULL;
60
61 if (cipher->block_len > MAX_BLOCK_LEN)
62 return ERR_BLOCK_SIZE;
63
64 if (cipher->block_len != iv_len)
65 return ERR_OFB_IV_LEN;
66
67 *pResult = calloc(1, sizeof(OfbModeState));
68 if (NULL == *pResult)
69 return ERR_MEMORY;
70
71 (*pResult)->cipher = cipher;
72 (*pResult)->usedKeyStream = cipher->block_len;
73 memcpy((*pResult)->keyStream, iv, iv_len);
74
75 return 0;
76 }
77
78 EXPORT_SYM int OFB_encrypt(OfbModeState *ofbState,
79 const uint8_t *in,
80 uint8_t *out,
81 size_t data_len)
82 {
83 size_t block_len;
84 uint8_t oldKeyStream[MAX_BLOCK_LEN];
85
86 if ((NULL == ofbState) || (NULL == in) || (NULL == out))
87 return ERR_NULL;
88
89 block_len = ofbState->cipher->block_len;
90 if (block_len > MAX_BLOCK_LEN)
91 return ERR_BLOCK_SIZE;
92
93 while (data_len > 0) {
94 size_t i;
95 size_t keyStreamToUse;
96
97 if (ofbState->usedKeyStream == block_len) {
98 int result;
99
100 memcpy(oldKeyStream, ofbState->keyStream, block_len);
101 result = ofbState->cipher->encrypt(ofbState->cipher,
102 oldKeyStream,
103 ofbState->keyStream,
104 block_len);
105 if (0 != result)
106 return result;
107
108 ofbState->usedKeyStream = 0;
109 }
110
111 keyStreamToUse = MIN(data_len, block_len - ofbState->usedKeyStream);
112 for (i=0; i<keyStreamToUse; i++)
113 *out++ = *in++ ^ ofbState->keyStream[i + ofbState->usedKeyStream];
114
115 data_len -= keyStreamToUse;
116 ofbState->usedKeyStream += keyStreamToUse;
117 }
118
119 return 0;
120 }
121
122 EXPORT_SYM int OFB_decrypt(OfbModeState *ofbState,
123 const uint8_t *in,
124 uint8_t *out,
125 size_t data_len)
126 {
127 return OFB_encrypt(ofbState, in, out, data_len);
128 }
129
130 EXPORT_SYM int OFB_stop_operation(OfbModeState *state)
131 {
132 if (NULL == state)
133 return ERR_NULL;
134 state->cipher->destructor(state->cipher);
135 free(state);
136 return 0;
137 }