1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* Copyright (c) 2000, 2002 Virtual Unlimited B.V.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*!\file blockmode.c
* \brief Blockcipher operation modes.
* \todo Additional modes, such as CFB and OFB.
* \author Bob Deblier <bob.deblier@pandora.be>
* \ingroup BC_m
*/
#include "system.h"
#include "blockmode.h"
#include "mp.h"
#include "debug.h"
/*!\addtogroup BC_m
* \{
*/
int blockEncryptECB(const blockCipher* bc, blockCipherParam* bp, uint32_t* dst, const uint32_t* src, int nblocks)
{
register const int blockwords = bc->blocksize >> 2;
while (nblocks > 0)
{
/*@-noeffectuncon@*/
(void) bc->encrypt(bp, dst, src);
/*@=noeffectuncon@*/
dst += blockwords;
src += blockwords;
nblocks--;
}
return 0;
}
int blockDecryptECB(const blockCipher* bc, blockCipherParam* bp, uint32_t* dst, const uint32_t* src, int nblocks)
{
register const int blockwords = bc->blocksize >> 2;
while (nblocks > 0)
{
/*@-noeffectuncon@*/
(void) bc->decrypt(bp, dst, src);
/*@=noeffectuncon@*/
dst += blockwords;
src += blockwords;
nblocks--;
}
return 0;
}
int blockEncryptCBC(const blockCipher* bc, blockCipherParam* bp, uint32_t* dst, const uint32_t* src, int nblocks)
{
register int blockwords = bc->blocksize >> 2;
register uint32_t* fdback = bc->getfb(bp);
if (nblocks > 0)
{
register int i;
for (i = 0; i < blockwords; i++)
dst[i] = src[i] ^ fdback[i];
/*@-noeffectuncon@*/
(void) bc->encrypt(bp, dst, dst);
/*@=noeffectuncon@*/
dst += blockwords;
src += blockwords;
nblocks--;
/*@-usedef@*/ /* LCL: dst is initialized. */
while (nblocks > 0)
{
for (i = 0; i < blockwords; i++)
dst[i] = src[i] ^ dst[i-blockwords];
/*@-noeffectuncon@*/
(void) bc->encrypt(bp, dst, dst);
/*@=noeffectuncon@*/
dst += blockwords;
src += blockwords;
nblocks--;
}
for (i = 0; i < blockwords; i++)
fdback[i] = dst[i-blockwords];
/*@=usedef@*/
}
return 0;
}
int blockDecryptCBC(const blockCipher* bc, blockCipherParam* bp, uint32_t* dst, const uint32_t* src, int nblocks)
{
/* assumes that every blockcipher's blocksize is a multiple of 32 bits */
register int blockwords = bc->blocksize >> 2;
register uint32_t* fdback = bc->getfb(bp);
register uint32_t* buf = (uint32_t*) malloc(blockwords * sizeof(*buf));
if (buf)
{
while (nblocks > 0)
{
register uint32_t tmp;
register int i;
/*@-noeffectuncon@*/
(void) bc->decrypt(bp, buf, src);
/*@=noeffectuncon@*/
for (i = 0; i < blockwords; i++)
{
tmp = src[i];
/*@-usedef@*/ /* LCL: buf is initialized. */
dst[i] = buf[i] ^ fdback[i];
/*@-usedef@*/
fdback[i] = tmp;
}
dst += blockwords;
src += blockwords;
nblocks--;
}
free(buf);
return 0;
}
return -1;
}
#ifdef DYING
int blockEncrypt(const blockCipher* bc, blockCipherParam* bp, cipherMode mode, int blocks, uint32_t* dst, const uint32_t* src)
{
if (bc->mode)
{
register const blockMode* bm = bc->mode+mode;
if (bm)
{
register const blockModeEncrypt be = bm->encrypt;
if (be)
return be(bp, blocks, dst, src);
}
}
return -1;
}
int blockDecrypt(const blockCipher* bc, blockCipherParam* bp, cipherMode mode, int blocks, uint32_t* dst, const uint32_t* src)
{
if (bc->mode)
{
register const blockMode* bm = bc->mode+mode;
if (bm)
{
register const blockModeEncrypt bd = bm->decrypt;
if (bd)
return bd(bp, blocks, dst, src);
}
}
return -1;
}
#endif
/*!\}
*/
|