126
117
void Camellia_cfb128_encrypt(const unsigned char *in, unsigned char *out,
127
const unsigned long length, const CAMELLIA_KEY *key,
118
size_t length, const CAMELLIA_KEY *key,
128
119
unsigned char *ivec, int *num, const int enc)
132
unsigned long l = length;
135
assert(in && out && key && ivec && num);
145
Camellia_encrypt(ivec, ivec, key);
147
ivec[n] = *(out++) = *(in++) ^ ivec[n];
148
n = (n+1) % CAMELLIA_BLOCK_SIZE;
157
Camellia_encrypt(ivec, ivec, key);
160
*(out++) = *(in++) ^ ivec[n];
162
n = (n+1) % CAMELLIA_BLOCK_SIZE;
169
/* This expects a single block of size nbits for both in and out. Note that
170
it corrupts any extra bits in the last byte of out */
171
void Camellia_cfbr_encrypt_block(const unsigned char *in,unsigned char *out,
172
const int nbits,const CAMELLIA_KEY *key,
173
unsigned char *ivec,const int enc)
176
unsigned char ovec[CAMELLIA_BLOCK_SIZE*2];
178
if (nbits<=0 || nbits>128) return;
180
/* fill in the first half of the new IV with the current IV */
181
memcpy(ovec,ivec,CAMELLIA_BLOCK_SIZE);
182
/* construct the new IV */
183
Camellia_encrypt(ivec,ivec,key);
185
if (enc) /* encrypt the input */
186
for(n=0 ; n < num ; ++n)
187
out[n] = (ovec[CAMELLIA_BLOCK_SIZE+n] = in[n] ^ ivec[n]);
188
else /* decrypt the input */
189
for(n=0 ; n < num ; ++n)
190
out[n] = (ovec[CAMELLIA_BLOCK_SIZE+n] = in[n]) ^ ivec[n];
191
/* shift ovec left... */
195
memcpy(ivec,ovec+num,CAMELLIA_BLOCK_SIZE);
197
for(n=0 ; n < CAMELLIA_BLOCK_SIZE ; ++n)
198
ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);
200
/* it is not necessary to cleanse ovec, since the IV is not secret */
122
CRYPTO_cfb128_encrypt(in,out,length,key,ivec,num,enc,(block128_f)Camellia_encrypt);
203
125
/* N.B. This expects the input to be packed, MS bit first */
204
126
void Camellia_cfb1_encrypt(const unsigned char *in, unsigned char *out,
205
const unsigned long length, const CAMELLIA_KEY *key,
127
size_t length, const CAMELLIA_KEY *key,
206
128
unsigned char *ivec, int *num, const int enc)
209
unsigned char c[1],d[1];
211
assert(in && out && key && ivec && num);
214
memset(out,0,(length+7)/8);
215
for(n=0 ; n < length ; ++n)
217
c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
218
Camellia_cfbr_encrypt_block(c,d,1,key,ivec,enc);
219
out[n/8]=(out[n/8]&~(1 << (7-n%8)))|((d[0]&0x80) >> (n%8));
130
CRYPTO_cfb128_1_encrypt(in,out,length,key,ivec,num,enc,(block128_f)Camellia_encrypt);
223
133
void Camellia_cfb8_encrypt(const unsigned char *in, unsigned char *out,
224
const unsigned long length, const CAMELLIA_KEY *key,
134
size_t length, const CAMELLIA_KEY *key,
225
135
unsigned char *ivec, int *num, const int enc)
229
assert(in && out && key && ivec && num);
232
for(n=0 ; n < length ; ++n)
233
Camellia_cfbr_encrypt_block(&in[n],&out[n],8,key,ivec,enc);
137
CRYPTO_cfb128_8_encrypt(in,out,length,key,ivec,num,enc,(block128_f)Camellia_encrypt);