Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/modes/cfb.c

raw
1 // Copyright (c) 2008 The OpenSSL Project. All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/type_check.h>
5
6 #include <assert.h>
7 #include <string.h>
8
9 #include "internal.h"
10
11
12 OPENSSL_STATIC_ASSERT(16 % sizeof(size_t) == 0,
13 cfb_block_cannot_be_divided_into_size_t)
14
15 void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
16 const AES_KEY *key, uint8_t ivec[16], unsigned *num,
17 int enc, block128_f block) {
18 assert(in && out && key && ivec && num);
19
20 unsigned n = *num;
21
22 if (enc) {
23 while (n && len) {
24 *(out++) = ivec[n] ^= *(in++);
25 --len;
26 n = (n + 1) % 16;
27 }
28 while (len >= 16) {
29 (*block)(ivec, ivec, key);
30 for (; n < 16; n += sizeof(crypto_word_t)) {
31 crypto_word_t tmp =
32 CRYPTO_load_word_le(ivec + n) ^ CRYPTO_load_word_le(in + n);
33 CRYPTO_store_word_le(ivec + n, tmp);
34 CRYPTO_store_word_le(out + n, tmp);
35 }
36 len -= 16;
37 out += 16;
38 in += 16;
39 n = 0;
40 }
41 if (len) {
42 (*block)(ivec, ivec, key);
43 while (len--) {
44 out[n] = ivec[n] ^= in[n];
45 ++n;
46 }
47 }
48 *num = n;
49 return;
50 } else {
51 while (n && len) {
52 uint8_t c;
53 *(out++) = ivec[n] ^ (c = *(in++));
54 ivec[n] = c;
55 --len;
56 n = (n + 1) % 16;
57 }
58 while (len >= 16) {
59 (*block)(ivec, ivec, key);
60 for (; n < 16; n += sizeof(crypto_word_t)) {
61 crypto_word_t t = CRYPTO_load_word_le(in + n);
62 CRYPTO_store_word_le(out + n, CRYPTO_load_word_le(ivec + n) ^ t);
63 CRYPTO_store_word_le(ivec + n, t);
64 }
65 len -= 16;
66 out += 16;
67 in += 16;
68 n = 0;
69 }
70 if (len) {
71 (*block)(ivec, ivec, key);
72 while (len--) {
73 uint8_t c;
74 out[n] = ivec[n] ^ (c = in[n]);
75 ivec[n] = c;
76 ++n;
77 }
78 }
79 *num = n;
80 return;
81 }
82 }
83
84
85 /* This expects a single block of size nbits for both in and out. Note that
86 it corrupts any extra bits in the last byte of out */
87 static void cfbr_encrypt_block(const uint8_t *in, uint8_t *out, unsigned nbits,
88 const AES_KEY *key, uint8_t ivec[16], int enc,
89 block128_f block) {
90 int n, rem, num;
91 uint8_t ovec[16 * 2 + 1]; /* +1 because we dererefence (but don't use) one
92 byte off the end */
93
94 if (nbits <= 0 || nbits > 128) {
95 return;
96 }
97
98 // fill in the first half of the new IV with the current IV
99 OPENSSL_memcpy(ovec, ivec, 16);
100 // construct the new IV
101 (*block)(ivec, ivec, key);
102 num = (nbits + 7) / 8;
103 if (enc) {
104 // encrypt the input
105 for (n = 0; n < num; ++n) {
106 out[n] = (ovec[16 + n] = in[n] ^ ivec[n]);
107 }
108 } else {
109 // decrypt the input
110 for (n = 0; n < num; ++n) {
111 out[n] = (ovec[16 + n] = in[n]) ^ ivec[n];
112 }
113 }
114 // shift ovec left...
115 rem = nbits % 8;
116 num = nbits / 8;
117 if (rem == 0) {
118 OPENSSL_memcpy(ivec, ovec + num, 16);
119 } else {
120 for (n = 0; n < 16; ++n) {
121 ivec[n] = ovec[n + num] << rem | ovec[n + num + 1] >> (8 - rem);
122 }
123 }
124
125 // it is not necessary to cleanse ovec, since the IV is not secret
126 }
127
128 // N.B. This expects the input to be packed, MS bit first
129 void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
130 const AES_KEY *key, uint8_t ivec[16],
131 unsigned *num, int enc, block128_f block) {
132 size_t n;
133 uint8_t c[1], d[1];
134
135 assert(in && out && key && ivec && num);
136 assert(*num == 0);
137
138 for (n = 0; n < bits; ++n) {
139 c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
140 cfbr_encrypt_block(c, d, 1, key, ivec, enc, block);
141 out[n / 8] = (out[n / 8] & ~(1 << (unsigned int)(7 - n % 8))) |
142 ((d[0] & 0x80) >> (unsigned int)(n % 8));
143 }
144 }
145
146 void CRYPTO_cfb128_8_encrypt(const unsigned char *in, unsigned char *out,
147 size_t length, const AES_KEY *key,
148 unsigned char ivec[16], unsigned *num, int enc,
149 block128_f block) {
150 size_t n;
151
152 assert(in && out && key && ivec && num);
153 assert(*num == 0);
154
155 for (n = 0; n < length; ++n) {
156 cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block);
157 }
158 }