1/* ====================================================================
2 * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in
13 *    the documentation and/or other materials provided with the
14 *    distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 *    software must display the following acknowledgment:
18 *    "This product includes software developed by the OpenSSL Project
19 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20 *
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 *    endorse or promote products derived from this software without
23 *    prior written permission. For written permission, please contact
24 *    openssl-core@openssl.org.
25 *
26 * 5. Products derived from this software may not be called "OpenSSL"
27 *    nor may "OpenSSL" appear in their names without prior written
28 *    permission of the OpenSSL Project.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 *    acknowledgment:
32 *    "This product includes software developed by the OpenSSL Project
33 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ==================================================================== */
48
49#include <openssl/modes.h>
50
51#include <assert.h>
52
53#include "internal.h"
54
55
56void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
57                           const void *key, uint8_t ivec[16], int *num, int enc,
58                           block128_f block) {
59  unsigned int n;
60  size_t l = 0;
61
62  assert(in && out && key && ivec && num);
63  assert((16 % sizeof(size_t)) == 0);
64
65  n = *num;
66
67  if (enc) {
68    while (n && len) {
69      *(out++) = ivec[n] ^= *(in++);
70      --len;
71      n = (n + 1) % 16;
72    }
73    if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
74      while (l < len) {
75        if (n == 0) {
76          (*block)(ivec, ivec, key);
77        }
78        out[l] = ivec[n] ^= in[l];
79        ++l;
80        n = (n + 1) % 16;
81      }
82      *num = n;
83      return;
84    }
85    while (len >= 16) {
86      (*block)(ivec, ivec, key);
87      for (; n < 16; n += sizeof(size_t)) {
88        *(size_t *)(out + n) = *(size_t *)(ivec + n) ^= *(size_t *)(in + n);
89      }
90      len -= 16;
91      out += 16;
92      in += 16;
93      n = 0;
94    }
95    if (len) {
96      (*block)(ivec, ivec, key);
97      while (len--) {
98        out[n] = ivec[n] ^= in[n];
99        ++n;
100      }
101    }
102    *num = n;
103    return;
104  } else {
105    while (n && len) {
106      uint8_t c;
107      *(out++) = ivec[n] ^ (c = *(in++));
108      ivec[n] = c;
109      --len;
110      n = (n + 1) % 16;
111    }
112    if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
113      while (l < len) {
114        unsigned char c;
115        if (n == 0) {
116          (*block)(ivec, ivec, key);
117        }
118        out[l] = ivec[n] ^ (c = in[l]);
119        ivec[n] = c;
120        ++l;
121        n = (n + 1) % 16;
122      }
123      *num = n;
124      return;
125    }
126    while (len >= 16) {
127      (*block)(ivec, ivec, key);
128      for (; n < 16; n += sizeof(size_t)) {
129        size_t t = *(size_t *)(in + n);
130        *(size_t *)(out + n) = *(size_t *)(ivec + n) ^ t;
131        *(size_t *)(ivec + n) = t;
132      }
133      len -= 16;
134      out += 16;
135      in += 16;
136      n = 0;
137    }
138    if (len) {
139      (*block)(ivec, ivec, key);
140      while (len--) {
141        uint8_t c;
142        out[n] = ivec[n] ^ (c = in[n]);
143        ivec[n] = c;
144        ++n;
145      }
146    }
147    *num = n;
148    return;
149  }
150}
151
152
153/* This expects a single block of size nbits for both in and out. Note that
154   it corrupts any extra bits in the last byte of out */
155static void cfbr_encrypt_block(const uint8_t *in, uint8_t *out, unsigned nbits,
156                               const void *key, uint8_t ivec[16], int enc,
157                               block128_f block) {
158  int n, rem, num;
159  uint8_t ovec[16 * 2 + 1]; /* +1 because we dererefence (but don't use) one
160                               byte off the end */
161
162  if (nbits <= 0 || nbits > 128) {
163    return;
164  }
165
166  /* fill in the first half of the new IV with the current IV */
167  memcpy(ovec, ivec, 16);
168  /* construct the new IV */
169  (*block)(ivec, ivec, key);
170  num = (nbits + 7) / 8;
171  if (enc) {
172    /* encrypt the input */
173    for (n = 0; n < num; ++n) {
174      out[n] = (ovec[16 + n] = in[n] ^ ivec[n]);
175    }
176  } else {
177    /* decrypt the input */
178    for (n = 0; n < num; ++n) {
179      out[n] = (ovec[16 + n] = in[n]) ^ ivec[n];
180    }
181  }
182  /* shift ovec left... */
183  rem = nbits % 8;
184  num = nbits / 8;
185  if (rem == 0) {
186    memcpy(ivec, ovec + num, 16);
187  } else {
188    for (n = 0; n < 16; ++n) {
189      ivec[n] = ovec[n + num] << rem | ovec[n + num + 1] >> (8 - rem);
190    }
191  }
192
193  /* it is not necessary to cleanse ovec, since the IV is not secret */
194}
195
196/* N.B. This expects the input to be packed, MS bit first */
197void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
198                             const void *key, uint8_t ivec[16], int *num,
199                             int enc, block128_f block) {
200  size_t n;
201  uint8_t c[1], d[1];
202
203  assert(in && out && key && ivec && num);
204  assert(*num == 0);
205
206  for (n = 0; n < bits; ++n) {
207    c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
208    cfbr_encrypt_block(c, d, 1, key, ivec, enc, block);
209    out[n / 8] = (out[n / 8] & ~(1 << (unsigned int)(7 - n % 8))) |
210                 ((d[0] & 0x80) >> (unsigned int)(n % 8));
211  }
212}
213
214void CRYPTO_cfb128_8_encrypt(const unsigned char *in, unsigned char *out,
215                             size_t length, const void *key,
216                             unsigned char ivec[16], int *num, int enc,
217                             block128_f block) {
218  size_t n;
219
220  assert(in && out && key && ivec && num);
221  assert(*num == 0);
222
223  for (n = 0; n < length; ++n) {
224    cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block);
225  }
226}
227
228