/src/openssl32/providers/implementations/ciphers/ciphercommon_hw.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "prov/ciphercommon.h" |
11 | | |
12 | | /*- |
13 | | * The generic cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr. |
14 | | * Used if there is no special hardware implementations. |
15 | | */ |
16 | | int ossl_cipher_hw_generic_cbc(PROV_CIPHER_CTX *dat, unsigned char *out, |
17 | | const unsigned char *in, size_t len) |
18 | 11.1k | { |
19 | 11.1k | if (dat->stream.cbc) |
20 | 11.1k | (*dat->stream.cbc) (in, out, len, dat->ks, dat->iv, dat->enc); |
21 | 25 | else if (dat->enc) |
22 | 25 | CRYPTO_cbc128_encrypt(in, out, len, dat->ks, dat->iv, dat->block); |
23 | 0 | else |
24 | 0 | CRYPTO_cbc128_decrypt(in, out, len, dat->ks, dat->iv, dat->block); |
25 | | |
26 | 11.1k | return 1; |
27 | 11.1k | } |
28 | | |
29 | | int ossl_cipher_hw_generic_ecb(PROV_CIPHER_CTX *dat, unsigned char *out, |
30 | | const unsigned char *in, size_t len) |
31 | 0 | { |
32 | 0 | size_t i, bl = dat->blocksize; |
33 | |
|
34 | 0 | if (len < bl) |
35 | 0 | return 1; |
36 | | |
37 | 0 | if (dat->stream.ecb) { |
38 | 0 | (*dat->stream.ecb) (in, out, len, dat->ks, dat->enc); |
39 | 0 | } |
40 | 0 | else { |
41 | 0 | for (i = 0, len -= bl; i <= len; i += bl) |
42 | 0 | (*dat->block) (in + i, out + i, dat->ks); |
43 | 0 | } |
44 | |
|
45 | 0 | return 1; |
46 | 0 | } |
47 | | |
48 | | int ossl_cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out, |
49 | | const unsigned char *in, size_t len) |
50 | 0 | { |
51 | 0 | int num = dat->num; |
52 | |
|
53 | 0 | CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block); |
54 | 0 | dat->num = num; |
55 | |
|
56 | 0 | return 1; |
57 | 0 | } |
58 | | |
59 | | int ossl_cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out, |
60 | | const unsigned char *in, size_t len) |
61 | 0 | { |
62 | 0 | int num = dat->num; |
63 | |
|
64 | 0 | CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc, |
65 | 0 | dat->block); |
66 | 0 | dat->num = num; |
67 | |
|
68 | 0 | return 1; |
69 | 0 | } |
70 | | |
71 | | int ossl_cipher_hw_generic_cfb8(PROV_CIPHER_CTX *dat, unsigned char *out, |
72 | | const unsigned char *in, size_t len) |
73 | 0 | { |
74 | 0 | int num = dat->num; |
75 | |
|
76 | 0 | CRYPTO_cfb128_8_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc, |
77 | 0 | dat->block); |
78 | 0 | dat->num = num; |
79 | |
|
80 | 0 | return 1; |
81 | 0 | } |
82 | | |
83 | | int ossl_cipher_hw_generic_cfb1(PROV_CIPHER_CTX *dat, unsigned char *out, |
84 | | const unsigned char *in, size_t len) |
85 | 0 | { |
86 | 0 | int num = dat->num; |
87 | |
|
88 | 0 | if (dat->use_bits) { |
89 | 0 | CRYPTO_cfb128_1_encrypt(in, out, len, dat->ks, dat->iv, &num, |
90 | 0 | dat->enc, dat->block); |
91 | 0 | dat->num = num; |
92 | 0 | return 1; |
93 | 0 | } |
94 | | |
95 | 0 | while (len >= MAXBITCHUNK) { |
96 | 0 | CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, dat->ks, |
97 | 0 | dat->iv, &num, dat->enc, dat->block); |
98 | 0 | len -= MAXBITCHUNK; |
99 | 0 | out += MAXBITCHUNK; |
100 | 0 | in += MAXBITCHUNK; |
101 | 0 | } |
102 | 0 | if (len) |
103 | 0 | CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num, |
104 | 0 | dat->enc, dat->block); |
105 | |
|
106 | 0 | dat->num = num; |
107 | |
|
108 | 0 | return 1; |
109 | 0 | } |
110 | | |
111 | | int ossl_cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out, |
112 | | const unsigned char *in, size_t len) |
113 | 81.6k | { |
114 | 81.6k | unsigned int num = dat->num; |
115 | | |
116 | 81.6k | if (dat->stream.ctr) |
117 | 81.6k | CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf, |
118 | 81.6k | &num, dat->stream.ctr); |
119 | 0 | else |
120 | 0 | CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf, |
121 | 0 | &num, dat->block); |
122 | 81.6k | dat->num = num; |
123 | | |
124 | 81.6k | return 1; |
125 | 81.6k | } |
126 | | |
127 | | /*- |
128 | | * The chunked cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr. |
129 | | * Used if there is no special hardware implementations. |
130 | | */ |
131 | | |
132 | | int ossl_cipher_hw_chunked_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out, |
133 | | const unsigned char *in, size_t inl) |
134 | 23 | { |
135 | 23 | while (inl >= MAXCHUNK) { |
136 | 0 | ossl_cipher_hw_generic_cbc(ctx, out, in, MAXCHUNK); |
137 | 0 | inl -= MAXCHUNK; |
138 | 0 | in += MAXCHUNK; |
139 | 0 | out += MAXCHUNK; |
140 | 0 | } |
141 | 23 | if (inl > 0) |
142 | 23 | ossl_cipher_hw_generic_cbc(ctx, out, in, inl); |
143 | 23 | return 1; |
144 | 23 | } |
145 | | |
146 | | int ossl_cipher_hw_chunked_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out, |
147 | | const unsigned char *in, size_t inl) |
148 | 0 | { |
149 | 0 | size_t chunk = MAXCHUNK; |
150 | |
|
151 | 0 | if (inl < chunk) |
152 | 0 | chunk = inl; |
153 | 0 | while (inl > 0 && inl >= chunk) { |
154 | 0 | ossl_cipher_hw_generic_cfb8(ctx, out, in, inl); |
155 | 0 | inl -= chunk; |
156 | 0 | in += chunk; |
157 | 0 | out += chunk; |
158 | 0 | if (inl < chunk) |
159 | 0 | chunk = inl; |
160 | 0 | } |
161 | 0 | return 1; |
162 | 0 | } |
163 | | |
164 | | int ossl_cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out, |
165 | | const unsigned char *in, size_t inl) |
166 | 0 | { |
167 | 0 | size_t chunk = MAXCHUNK; |
168 | |
|
169 | 0 | if (inl < chunk) |
170 | 0 | chunk = inl; |
171 | 0 | while (inl > 0 && inl >= chunk) { |
172 | 0 | ossl_cipher_hw_generic_cfb128(ctx, out, in, inl); |
173 | 0 | inl -= chunk; |
174 | 0 | in += chunk; |
175 | 0 | out += chunk; |
176 | 0 | if (inl < chunk) |
177 | 0 | chunk = inl; |
178 | 0 | } |
179 | 0 | return 1; |
180 | 0 | } |
181 | | |
182 | | int ossl_cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out, |
183 | | const unsigned char *in, size_t inl) |
184 | 0 | { |
185 | 0 | while (inl >= MAXCHUNK) { |
186 | 0 | ossl_cipher_hw_generic_ofb128(ctx, out, in, MAXCHUNK); |
187 | 0 | inl -= MAXCHUNK; |
188 | 0 | in += MAXCHUNK; |
189 | 0 | out += MAXCHUNK; |
190 | 0 | } |
191 | 0 | if (inl > 0) |
192 | 0 | ossl_cipher_hw_generic_ofb128(ctx, out, in, inl); |
193 | 0 | return 1; |
194 | 0 | } |