Coverage Report

Created: 2022-12-08 06:10

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