/src/openssl/providers/implementations/ciphers/ciphercommon_hw.c
Line | Count | Source |
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 | 0 | { |
19 | 0 | if (dat->stream.cbc) |
20 | 0 | (*dat->stream.cbc)(in, out, len, dat->ks, dat->iv, dat->enc); |
21 | 0 | else if (dat->enc) |
22 | 0 | 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 | 0 | return 1; |
27 | 0 | } |
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 | } else { |
40 | 0 | for (i = 0, len -= bl; i <= len; i += bl) |
41 | 0 | (*dat->block)(in + i, out + i, dat->ks); |
42 | 0 | } |
43 | |
|
44 | 0 | return 1; |
45 | 0 | } |
46 | | |
47 | | int ossl_cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out, |
48 | | const unsigned char *in, size_t len) |
49 | 0 | { |
50 | 0 | int num = dat->num; |
51 | |
|
52 | 0 | CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block); |
53 | 0 | dat->num = num; |
54 | |
|
55 | 0 | return 1; |
56 | 0 | } |
57 | | |
58 | | int ossl_cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out, |
59 | | const unsigned char *in, size_t len) |
60 | 0 | { |
61 | 0 | int num = dat->num; |
62 | |
|
63 | 0 | CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc, |
64 | 0 | dat->block); |
65 | 0 | dat->num = num; |
66 | |
|
67 | 0 | return 1; |
68 | 0 | } |
69 | | |
70 | | int ossl_cipher_hw_generic_cfb8(PROV_CIPHER_CTX *dat, unsigned char *out, |
71 | | const unsigned char *in, size_t len) |
72 | 0 | { |
73 | 0 | int num = dat->num; |
74 | |
|
75 | 0 | CRYPTO_cfb128_8_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc, |
76 | 0 | dat->block); |
77 | 0 | dat->num = num; |
78 | |
|
79 | 0 | return 1; |
80 | 0 | } |
81 | | |
82 | | int ossl_cipher_hw_generic_cfb1(PROV_CIPHER_CTX *dat, unsigned char *out, |
83 | | const unsigned char *in, size_t len) |
84 | 0 | { |
85 | 0 | int num = dat->num; |
86 | |
|
87 | 0 | if (dat->use_bits) { |
88 | 0 | CRYPTO_cfb128_1_encrypt(in, out, len, dat->ks, dat->iv, &num, |
89 | 0 | dat->enc, dat->block); |
90 | 0 | dat->num = num; |
91 | 0 | return 1; |
92 | 0 | } |
93 | | |
94 | 0 | while (len >= MAXBITCHUNK) { |
95 | 0 | CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, dat->ks, |
96 | 0 | dat->iv, &num, dat->enc, dat->block); |
97 | 0 | len -= MAXBITCHUNK; |
98 | 0 | out += MAXBITCHUNK; |
99 | 0 | in += MAXBITCHUNK; |
100 | 0 | } |
101 | 0 | if (len) |
102 | 0 | CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num, |
103 | 0 | dat->enc, dat->block); |
104 | |
|
105 | 0 | dat->num = num; |
106 | |
|
107 | 0 | return 1; |
108 | 0 | } |
109 | | |
110 | | int ossl_cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out, |
111 | | const unsigned char *in, size_t len) |
112 | 0 | { |
113 | 0 | unsigned int num = dat->num; |
114 | |
|
115 | 0 | if (dat->stream.ctr) |
116 | 0 | CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf, |
117 | 0 | &num, dat->stream.ctr); |
118 | 0 | else |
119 | 0 | CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf, |
120 | 0 | &num, dat->block); |
121 | 0 | dat->num = num; |
122 | |
|
123 | 0 | return 1; |
124 | 0 | } |
125 | | |
126 | | /*- |
127 | | * The chunked cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr. |
128 | | * Used if there is no special hardware implementations. |
129 | | */ |
130 | | |
131 | | int ossl_cipher_hw_chunked_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out, |
132 | | const unsigned char *in, size_t inl) |
133 | 0 | { |
134 | 0 | while (inl >= MAXCHUNK) { |
135 | 0 | ossl_cipher_hw_generic_cbc(ctx, out, in, MAXCHUNK); |
136 | 0 | inl -= MAXCHUNK; |
137 | 0 | in += MAXCHUNK; |
138 | 0 | out += MAXCHUNK; |
139 | 0 | } |
140 | 0 | if (inl > 0) |
141 | 0 | ossl_cipher_hw_generic_cbc(ctx, out, in, inl); |
142 | 0 | return 1; |
143 | 0 | } |
144 | | |
145 | | int ossl_cipher_hw_chunked_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out, |
146 | | const unsigned char *in, size_t inl) |
147 | 0 | { |
148 | 0 | size_t chunk = MAXCHUNK; |
149 | |
|
150 | 0 | if (inl < chunk) |
151 | 0 | chunk = inl; |
152 | 0 | while (inl > 0 && inl >= chunk) { |
153 | 0 | ossl_cipher_hw_generic_cfb8(ctx, out, in, inl); |
154 | 0 | inl -= chunk; |
155 | 0 | in += chunk; |
156 | 0 | out += chunk; |
157 | 0 | if (inl < chunk) |
158 | 0 | chunk = inl; |
159 | 0 | } |
160 | 0 | return 1; |
161 | 0 | } |
162 | | |
163 | | int ossl_cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out, |
164 | | const unsigned char *in, size_t inl) |
165 | 0 | { |
166 | 0 | size_t chunk = MAXCHUNK; |
167 | |
|
168 | 0 | if (inl < chunk) |
169 | 0 | chunk = inl; |
170 | 0 | while (inl > 0 && inl >= chunk) { |
171 | 0 | ossl_cipher_hw_generic_cfb128(ctx, out, in, inl); |
172 | 0 | inl -= chunk; |
173 | 0 | in += chunk; |
174 | 0 | out += chunk; |
175 | 0 | if (inl < chunk) |
176 | 0 | chunk = inl; |
177 | 0 | } |
178 | 0 | return 1; |
179 | 0 | } |
180 | | |
181 | | int ossl_cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out, |
182 | | const unsigned char *in, size_t inl) |
183 | 0 | { |
184 | 0 | while (inl >= MAXCHUNK) { |
185 | 0 | ossl_cipher_hw_generic_ofb128(ctx, out, in, MAXCHUNK); |
186 | 0 | inl -= MAXCHUNK; |
187 | 0 | in += MAXCHUNK; |
188 | 0 | out += MAXCHUNK; |
189 | 0 | } |
190 | 0 | if (inl > 0) |
191 | 0 | ossl_cipher_hw_generic_ofb128(ctx, out, in, inl); |
192 | 0 | return 1; |
193 | 0 | } |