| 1 |
/* cipher-cfb.c - Generic CFB mode implementation |
| 2 |
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 |
| 3 |
* 2005, 2007, 2008, 2009, 2011 Free Software Foundation, Inc. |
| 4 |
* |
| 5 |
* This file is part of Libgcrypt. |
| 6 |
* |
| 7 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as |
| 9 |
* published by the Free Software Foundation; either version 2.1 of |
| 10 |
* the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
#include "g10lib.h" |
| 28 |
#include "cipher.h" |
| 29 |
#include "bufhelp.h" |
| 30 |
#include "./cipher-internal.h" |
| 31 |
|
| 32 |
|
| 33 |
gcry_err_code_t |
| 34 |
_gcry_cipher_cfb_encrypt (gcry_cipher_hd_t c, |
| 35 |
unsigned char *outbuf, size_t outbuflen, |
| 36 |
const unsigned char *inbuf, size_t inbuflen) |
| 37 |
{ |
| 38 |
unsigned char *ivp; |
| 39 |
gcry_cipher_encrypt_t enc_fn = c->spec->encrypt; |
| 40 |
size_t blocksize_shift = _gcry_blocksize_shift(c); |
| 41 |
size_t blocksize = 1 << blocksize_shift; |
| 42 |
size_t blocksize_x_2 = blocksize + blocksize; |
| 43 |
unsigned int burn, nburn; |
| 44 |
|
| 45 |
if (outbuflen < inbuflen) |
| 46 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 47 |
|
| 48 |
if ( inbuflen <= c->unused ) |
| 49 |
{ |
| 50 |
/* Short enough to be encoded by the remaining XOR mask. */ |
| 51 |
/* XOR the input with the IV and store input into IV. */ |
| 52 |
ivp = c->u_iv.iv + blocksize - c->unused; |
| 53 |
buf_xor_2dst(outbuf, ivp, inbuf, inbuflen); |
| 54 |
c->unused -= inbuflen; |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
burn = 0; |
| 59 |
|
| 60 |
if ( c->unused ) |
| 61 |
{ |
| 62 |
/* XOR the input with the IV and store input into IV */ |
| 63 |
inbuflen -= c->unused; |
| 64 |
ivp = c->u_iv.iv + blocksize - c->unused; |
| 65 |
buf_xor_2dst(outbuf, ivp, inbuf, c->unused); |
| 66 |
outbuf += c->unused; |
| 67 |
inbuf += c->unused; |
| 68 |
c->unused = 0; |
| 69 |
} |
| 70 |
|
| 71 |
/* Now we can process complete blocks. We use a loop as long as we |
| 72 |
have at least 2 blocks and use conditions for the rest. This |
| 73 |
also allows using a bulk encryption function if available. */ |
| 74 |
if (inbuflen >= blocksize_x_2 && c->bulk.cfb_enc) |
| 75 |
{ |
| 76 |
size_t nblocks = inbuflen >> blocksize_shift; |
| 77 |
c->bulk.cfb_enc (&c->context.c, c->u_iv.iv, outbuf, inbuf, nblocks); |
| 78 |
outbuf += nblocks << blocksize_shift; |
| 79 |
inbuf += nblocks << blocksize_shift; |
| 80 |
inbuflen -= nblocks << blocksize_shift; |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
while ( inbuflen >= blocksize_x_2 ) |
| 85 |
{ |
| 86 |
/* Encrypt the IV. */ |
| 87 |
nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv ); |
| 88 |
burn = nburn > burn ? nburn : burn; |
| 89 |
/* XOR the input with the IV and store input into IV. */ |
| 90 |
cipher_block_xor_2dst(outbuf, c->u_iv.iv, inbuf, blocksize); |
| 91 |
outbuf += blocksize; |
| 92 |
inbuf += blocksize; |
| 93 |
inbuflen -= blocksize; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( inbuflen >= blocksize ) |
| 98 |
{ |
| 99 |
/* Save the current IV and then encrypt the IV. */ |
| 100 |
cipher_block_cpy( c->lastiv, c->u_iv.iv, blocksize ); |
| 101 |
nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv ); |
| 102 |
burn = nburn > burn ? nburn : burn; |
| 103 |
/* XOR the input with the IV and store input into IV */ |
| 104 |
cipher_block_xor_2dst(outbuf, c->u_iv.iv, inbuf, blocksize); |
| 105 |
outbuf += blocksize; |
| 106 |
inbuf += blocksize; |
| 107 |
inbuflen -= blocksize; |
| 108 |
} |
| 109 |
if ( inbuflen ) |
| 110 |
{ |
| 111 |
/* Save the current IV and then encrypt the IV. */ |
| 112 |
cipher_block_cpy( c->lastiv, c->u_iv.iv, blocksize ); |
| 113 |
nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv ); |
| 114 |
burn = nburn > burn ? nburn : burn; |
| 115 |
c->unused = blocksize; |
| 116 |
/* Apply the XOR. */ |
| 117 |
c->unused -= inbuflen; |
| 118 |
buf_xor_2dst(outbuf, c->u_iv.iv, inbuf, inbuflen); |
| 119 |
outbuf += inbuflen; |
| 120 |
inbuf += inbuflen; |
| 121 |
inbuflen = 0; |
| 122 |
} |
| 123 |
|
| 124 |
if (burn > 0) |
| 125 |
_gcry_burn_stack (burn + 4 * sizeof(void *)); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
gcry_err_code_t |
| 132 |
_gcry_cipher_cfb_decrypt (gcry_cipher_hd_t c, |
| 133 |
unsigned char *outbuf, size_t outbuflen, |
| 134 |
const unsigned char *inbuf, size_t inbuflen) |
| 135 |
{ |
| 136 |
unsigned char *ivp; |
| 137 |
gcry_cipher_encrypt_t enc_fn = c->spec->encrypt; |
| 138 |
size_t blocksize_shift = _gcry_blocksize_shift(c); |
| 139 |
size_t blocksize = 1 << blocksize_shift; |
| 140 |
size_t blocksize_x_2 = blocksize + blocksize; |
| 141 |
unsigned int burn, nburn; |
| 142 |
|
| 143 |
if (outbuflen < inbuflen) |
| 144 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 145 |
|
| 146 |
if (inbuflen <= c->unused) |
| 147 |
{ |
| 148 |
/* Short enough to be encoded by the remaining XOR mask. */ |
| 149 |
/* XOR the input with the IV and store input into IV. */ |
| 150 |
ivp = c->u_iv.iv + blocksize - c->unused; |
| 151 |
buf_xor_n_copy(outbuf, ivp, inbuf, inbuflen); |
| 152 |
c->unused -= inbuflen; |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
burn = 0; |
| 157 |
|
| 158 |
if (c->unused) |
| 159 |
{ |
| 160 |
/* XOR the input with the IV and store input into IV. */ |
| 161 |
inbuflen -= c->unused; |
| 162 |
ivp = c->u_iv.iv + blocksize - c->unused; |
| 163 |
buf_xor_n_copy(outbuf, ivp, inbuf, c->unused); |
| 164 |
outbuf += c->unused; |
| 165 |
inbuf += c->unused; |
| 166 |
c->unused = 0; |
| 167 |
} |
| 168 |
|
| 169 |
/* Now we can process complete blocks. We use a loop as long as we |
| 170 |
have at least 2 blocks and use conditions for the rest. This |
| 171 |
also allows using a bulk encryption function if available. */ |
| 172 |
if (inbuflen >= blocksize_x_2 && c->bulk.cfb_dec) |
| 173 |
{ |
| 174 |
size_t nblocks = inbuflen >> blocksize_shift; |
| 175 |
c->bulk.cfb_dec (&c->context.c, c->u_iv.iv, outbuf, inbuf, nblocks); |
| 176 |
outbuf += nblocks << blocksize_shift; |
| 177 |
inbuf += nblocks << blocksize_shift; |
| 178 |
inbuflen -= nblocks << blocksize_shift; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
while (inbuflen >= blocksize_x_2 ) |
| 183 |
{ |
| 184 |
/* Encrypt the IV. */ |
| 185 |
nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv ); |
| 186 |
burn = nburn > burn ? nburn : burn; |
| 187 |
/* XOR the input with the IV and store input into IV. */ |
| 188 |
cipher_block_xor_n_copy(outbuf, c->u_iv.iv, inbuf, blocksize); |
| 189 |
outbuf += blocksize; |
| 190 |
inbuf += blocksize; |
| 191 |
inbuflen -= blocksize; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if (inbuflen >= blocksize ) |
| 196 |
{ |
| 197 |
/* Save the current IV and then encrypt the IV. */ |
| 198 |
cipher_block_cpy ( c->lastiv, c->u_iv.iv, blocksize); |
| 199 |
nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv ); |
| 200 |
burn = nburn > burn ? nburn : burn; |
| 201 |
/* XOR the input with the IV and store input into IV */ |
| 202 |
cipher_block_xor_n_copy(outbuf, c->u_iv.iv, inbuf, blocksize); |
| 203 |
outbuf += blocksize; |
| 204 |
inbuf += blocksize; |
| 205 |
inbuflen -= blocksize; |
| 206 |
} |
| 207 |
|
| 208 |
if (inbuflen) |
| 209 |
{ |
| 210 |
/* Save the current IV and then encrypt the IV. */ |
| 211 |
cipher_block_cpy ( c->lastiv, c->u_iv.iv, blocksize ); |
| 212 |
nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv ); |
| 213 |
burn = nburn > burn ? nburn : burn; |
| 214 |
c->unused = blocksize; |
| 215 |
/* Apply the XOR. */ |
| 216 |
c->unused -= inbuflen; |
| 217 |
buf_xor_n_copy(outbuf, c->u_iv.iv, inbuf, inbuflen); |
| 218 |
outbuf += inbuflen; |
| 219 |
inbuf += inbuflen; |
| 220 |
inbuflen = 0; |
| 221 |
} |
| 222 |
|
| 223 |
if (burn > 0) |
| 224 |
_gcry_burn_stack (burn + 4 * sizeof(void *)); |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
gcry_err_code_t |
| 231 |
_gcry_cipher_cfb8_encrypt (gcry_cipher_hd_t c, |
| 232 |
unsigned char *outbuf, size_t outbuflen, |
| 233 |
const unsigned char *inbuf, size_t inbuflen) |
| 234 |
{ |
| 235 |
gcry_cipher_encrypt_t enc_fn = c->spec->encrypt; |
| 236 |
size_t blocksize = c->spec->blocksize; |
| 237 |
unsigned int burn, nburn; |
| 238 |
|
| 239 |
if (outbuflen < inbuflen) |
| 240 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 241 |
|
| 242 |
burn = 0; |
| 243 |
|
| 244 |
while ( inbuflen > 0) |
| 245 |
{ |
| 246 |
int i; |
| 247 |
|
| 248 |
/* Encrypt the IV. */ |
| 249 |
nburn = enc_fn ( &c->context.c, c->lastiv, c->u_iv.iv ); |
| 250 |
burn = nburn > burn ? nburn : burn; |
| 251 |
|
| 252 |
outbuf[0] = c->lastiv[0] ^ inbuf[0]; |
| 253 |
|
| 254 |
/* Bitshift iv by 8 bit to the left */ |
| 255 |
for (i = 0; i < blocksize-1; i++) |
| 256 |
c->u_iv.iv[i] = c->u_iv.iv[i+1]; |
| 257 |
|
| 258 |
/* append cipher text to iv */ |
| 259 |
c->u_iv.iv[blocksize-1] = outbuf[0]; |
| 260 |
|
| 261 |
outbuf += 1; |
| 262 |
inbuf += 1; |
| 263 |
inbuflen -= 1; |
| 264 |
} |
| 265 |
|
| 266 |
if (burn > 0) |
| 267 |
_gcry_burn_stack (burn + 4 * sizeof(void *)); |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
gcry_err_code_t |
| 274 |
_gcry_cipher_cfb8_decrypt (gcry_cipher_hd_t c, |
| 275 |
unsigned char *outbuf, size_t outbuflen, |
| 276 |
const unsigned char *inbuf, size_t inbuflen) |
| 277 |
{ |
| 278 |
gcry_cipher_encrypt_t enc_fn = c->spec->encrypt; |
| 279 |
size_t blocksize = c->spec->blocksize; |
| 280 |
unsigned int burn, nburn; |
| 281 |
unsigned char appendee; |
| 282 |
|
| 283 |
if (outbuflen < inbuflen) |
| 284 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 285 |
|
| 286 |
burn = 0; |
| 287 |
|
| 288 |
while (inbuflen > 0) |
| 289 |
{ |
| 290 |
int i; |
| 291 |
|
| 292 |
/* Encrypt the IV. */ |
| 293 |
nburn = enc_fn ( &c->context.c, c->lastiv, c->u_iv.iv ); |
| 294 |
burn = nburn > burn ? nburn : burn; |
| 295 |
|
| 296 |
/* inbuf might == outbuf, make sure we keep the value |
| 297 |
so we can append it later */ |
| 298 |
appendee = inbuf[0]; |
| 299 |
|
| 300 |
outbuf[0] = inbuf[0] ^ c->lastiv[0]; |
| 301 |
|
| 302 |
/* Bitshift iv by 8 bit to the left */ |
| 303 |
for (i = 0; i < blocksize-1; i++) |
| 304 |
c->u_iv.iv[i] = c->u_iv.iv[i+1]; |
| 305 |
|
| 306 |
c->u_iv.iv[blocksize-1] = appendee; |
| 307 |
|
| 308 |
outbuf += 1; |
| 309 |
inbuf += 1; |
| 310 |
inbuflen -= 1; |
| 311 |
} |
| 312 |
|
| 313 |
if (burn > 0) |
| 314 |
_gcry_burn_stack (burn + 4 * sizeof(void *)); |
| 315 |
|
| 316 |
return 0; |
| 317 |
} |