Debian Codegraph @

cipher/cipher-ofb.c

raw
1 /* cipher-ofb.c - Generic OFB 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_ofb_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 unsigned int burn, nburn;
43
44 if (outbuflen < inbuflen)
45 return GPG_ERR_BUFFER_TOO_SHORT;
46
47 if ( inbuflen <= c->unused )
48 {
49 /* Short enough to be encoded by the remaining XOR mask. */
50 /* XOR the input with the IV */
51 ivp = c->u_iv.iv + blocksize - c->unused;
52 buf_xor(outbuf, ivp, inbuf, inbuflen);
53 c->unused -= inbuflen;
54 return 0;
55 }
56
57 burn = 0;
58
59 if( c->unused )
60 {
61 inbuflen -= c->unused;
62 ivp = c->u_iv.iv + blocksize - c->unused;
63 buf_xor(outbuf, ivp, inbuf, c->unused);
64 outbuf += c->unused;
65 inbuf += c->unused;
66 c->unused = 0;
67 }
68
69 /* Now we can process complete blocks. */
70 if (c->bulk.ofb_enc)
71 {
72 size_t nblocks = inbuflen >> blocksize_shift;
73 c->bulk.ofb_enc (&c->context.c, c->u_iv.iv, outbuf, inbuf, nblocks);
74 outbuf += nblocks << blocksize_shift;
75 inbuf += nblocks << blocksize_shift;
76 inbuflen -= nblocks << blocksize_shift;
77 }
78 else
79 {
80 while ( inbuflen >= blocksize )
81 {
82 /* Encrypt the IV (and save the current one). */
83 nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv );
84 burn = nburn > burn ? nburn : burn;
85 cipher_block_xor(outbuf, c->u_iv.iv, inbuf, blocksize);
86 outbuf += blocksize;
87 inbuf += blocksize;
88 inbuflen -= blocksize;
89 }
90 }
91
92 if ( inbuflen )
93 { /* process the remaining bytes */
94 nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv );
95 burn = nburn > burn ? nburn : burn;
96 c->unused = blocksize;
97 c->unused -= inbuflen;
98 buf_xor(outbuf, c->u_iv.iv, inbuf, inbuflen);
99 outbuf += inbuflen;
100 inbuf += inbuflen;
101 inbuflen = 0;
102 }
103
104 if (burn > 0)
105 _gcry_burn_stack (burn + 4 * sizeof(void *));
106
107 return 0;
108 }