]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/common/ciphers/cipher_common_hw.c
Providers: move all digests
[thirdparty/openssl.git] / providers / common / ciphers / cipher_common_hw.c
CommitLineData
e1178600
SL
1/*
2 * Copyright 2019 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
706457b7 10#include "cipher_local.h"
e1178600 11
e1178600
SL
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 */
16int cipher_hw_generic_cbc(PROV_CIPHER_CTX *dat, unsigned char *out,
17 const unsigned char *in, size_t len)
18{
19 if (dat->stream.cbc)
20 (*dat->stream.cbc) (in, out, len, dat->ks, dat->iv, dat->enc);
21 else if (dat->enc)
22 CRYPTO_cbc128_encrypt(in, out, len, dat->ks, dat->iv, dat->block);
23 else
24 CRYPTO_cbc128_decrypt(in, out, len, dat->ks, dat->iv, dat->block);
25
26 return 1;
27}
28
29int cipher_hw_generic_ecb(PROV_CIPHER_CTX *dat, unsigned char *out,
30 const unsigned char *in, size_t len)
31{
32 size_t i, bl = dat->blocksize;
33
34 if (len < bl)
35 return 1;
36
37 for (i = 0, len -= bl; i <= len; i += bl)
38 (*dat->block) (in + i, out + i, dat->ks);
39
40 return 1;
41}
42
43int cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out,
44 const unsigned char *in, size_t len)
45{
46 int num = dat->num;
47
48 CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block);
49 dat->num = num;
50
51 return 1;
52}
53
54int cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out,
55 const unsigned char *in, size_t len)
56{
57 int num = dat->num;
58
59 CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
60 dat->block);
61 dat->num = num;
62
63 return 1;
64}
65
66int cipher_hw_generic_cfb8(PROV_CIPHER_CTX *dat, unsigned char *out,
67 const unsigned char *in, size_t len)
68{
69 int num = dat->num;
70
71 CRYPTO_cfb128_8_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
72 dat->block);
73 dat->num = num;
74
75 return 1;
76}
77
78int cipher_hw_generic_cfb1(PROV_CIPHER_CTX *dat, unsigned char *out,
79 const unsigned char *in, size_t len)
80{
81 int num = dat->num;
82
83 if ((dat->flags & EVP_CIPH_FLAG_LENGTH_BITS) != 0) {
84 CRYPTO_cfb128_1_encrypt(in, out, len, dat->ks, dat->iv, &num,
85 dat->enc, dat->block);
86 dat->num = num;
87 return 1;
88 }
89
90 while (len >= MAXBITCHUNK) {
91 CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, dat->ks,
92 dat->iv, &num, dat->enc, dat->block);
93 len -= MAXBITCHUNK;
94 out += MAXBITCHUNK;
95 in += MAXBITCHUNK;
96 }
97 if (len)
98 CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num,
99 dat->enc, dat->block);
100
101 dat->num = num;
102
103 return 1;
104}
105
106int cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out,
107 const unsigned char *in, size_t len)
108{
109 unsigned int num = dat->num;
110
111 if (dat->stream.ctr)
112 CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf,
113 &num, dat->stream.ctr);
114 else
115 CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf,
116 &num, dat->block);
117 dat->num = num;
118
119 return 1;
120}
121
122/*-
123 * The chunked cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
124 * Used if there is no special hardware implementations.
125 */
126
127int cipher_hw_chunked_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
128 const unsigned char *in, size_t inl)
129{
130 while (inl >= MAXCHUNK) {
131 cipher_hw_generic_cbc(ctx, out, in, MAXCHUNK);
132 inl -= MAXCHUNK;
133 in += MAXCHUNK;
134 out += MAXCHUNK;
135 }
136 if (inl > 0)
137 cipher_hw_generic_cbc(ctx, out, in, inl);
138 return 1;
139}
140
141int cipher_hw_chunked_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
142 const unsigned char *in, size_t inl)
143{
144 size_t chunk = MAXCHUNK;
145
146 if (inl < chunk)
147 chunk = inl;
148 while (inl > 0 && inl >= chunk) {
149 cipher_hw_generic_cfb8(ctx, out, in, inl);
150 inl -= chunk;
151 in += chunk;
152 out += chunk;
153 if (inl < chunk)
154 chunk = inl;
155 }
156 return 1;
157}
158
159int cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
160 const unsigned char *in, size_t inl)
161{
162 size_t chunk = MAXCHUNK;
163
164 if (inl < chunk)
165 chunk = inl;
166 while (inl > 0 && inl >= chunk) {
167 cipher_hw_generic_cfb128(ctx, out, in, inl);
168 inl -= chunk;
169 in += chunk;
170 out += chunk;
171 if (inl < chunk)
172 chunk = inl;
173 }
174 return 1;
175}
176
177int cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
178 const unsigned char *in, size_t inl)
179{
180 while (inl >= MAXCHUNK) {
181 cipher_hw_generic_ofb128(ctx, out, in, MAXCHUNK);
182 inl -= MAXCHUNK;
183 in += MAXCHUNK;
184 out += MAXCHUNK;
185 }
186 if (inl > 0)
187 cipher_hw_generic_ofb128(ctx, out, in, inl);
188 return 1;
189}