1 From dfd6ba263dc25ea2a4bbc32448b24ca2b1fc40e8 Mon Sep 17 00:00:00 2001
2 From: Cristian Stoica <cristian.stoica@freescale.com>
3 Date: Thu, 29 Aug 2013 16:51:18 +0300
4 Subject: [PATCH 02/17] eng_cryptodev: add support for TLS algorithms offload
6 - aes-128-cbc-hmac-sha1
7 - aes-256-cbc-hmac-sha1
9 Requires TLS patches on cryptodev and TLS algorithm support in Linux
12 Change-Id: I43048caa348414daddd6c1a5cdc55e769ac1945f
13 Signed-off-by: Cristian Stoica <cristian.stoica@freescale.com>
14 Reviewed-on: http://git.am.freescale.net:8181/17223
16 crypto/engine/eng_cryptodev.c | 222 +++++++++++++++++++++++++++++++++++++++---
17 1 file changed, 211 insertions(+), 11 deletions(-)
19 diff --git a/crypto/engine/eng_cryptodev.c b/crypto/engine/eng_cryptodev.c
20 index 5a715ac..7588a28 100644
21 --- a/crypto/engine/eng_cryptodev.c
22 +++ b/crypto/engine/eng_cryptodev.c
23 @@ -72,6 +72,9 @@ ENGINE_load_cryptodev(void)
24 struct dev_crypto_state {
25 struct session_op d_sess;
28 + unsigned int aad_len;
31 #ifdef USE_CRYPTODEV_DIGESTS
32 char dummy_mac_key[HASH_MAX_LEN];
33 @@ -140,17 +143,20 @@ static struct {
39 - { CRYPTO_ARC4, NID_rc4, 0, 16, },
40 - { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
41 - { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
42 - { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
43 - { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, },
44 - { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, },
45 - { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
46 - { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
47 - { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
48 - { 0, NID_undef, 0, 0, },
49 + { CRYPTO_ARC4, NID_rc4, 0, 16, 0},
50 + { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, 0},
51 + { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, 0},
52 + { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, 0},
53 + { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, 0},
54 + { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, 0},
55 + { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, 0},
56 + { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, 0},
57 + { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, 0},
58 + { CRYPTO_TLS10_AES_CBC_HMAC_SHA1, NID_aes_128_cbc_hmac_sha1, 16, 16, 20},
59 + { CRYPTO_TLS10_AES_CBC_HMAC_SHA1, NID_aes_256_cbc_hmac_sha1, 16, 32, 20},
60 + { 0, NID_undef, 0, 0, 0},
63 #ifdef USE_CRYPTODEV_DIGESTS
64 @@ -250,13 +256,15 @@ get_cryptodev_ciphers(const int **cnids)
66 memset(&sess, 0, sizeof(sess));
67 sess.key = (caddr_t)"123456789abcdefghijklmno";
68 + sess.mackey = (caddr_t)"123456789ABCDEFGHIJKLMNO";
70 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
71 if (ciphers[i].nid == NID_undef)
73 sess.cipher = ciphers[i].id;
74 sess.keylen = ciphers[i].keylen;
76 + sess.mackeylen = ciphers[i].mackeylen;
78 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
79 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
80 nids[count++] = ciphers[i].nid;
81 @@ -414,6 +422,67 @@ cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
86 +static int cryptodev_aead_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
87 + const unsigned char *in, size_t len)
89 + struct crypt_auth_op cryp;
90 + struct dev_crypto_state *state = ctx->cipher_data;
91 + struct session_op *sess = &state->d_sess;
93 + unsigned char save_iv[EVP_MAX_IV_LENGTH];
95 + if (state->d_fd < 0)
99 + if ((len % ctx->cipher->block_size) != 0)
102 + memset(&cryp, 0, sizeof(cryp));
104 + /* TODO: make a seamless integration with cryptodev flags */
105 + switch (ctx->cipher->nid) {
106 + case NID_aes_128_cbc_hmac_sha1:
107 + case NID_aes_256_cbc_hmac_sha1:
108 + cryp.flags = COP_FLAG_AEAD_TLS_TYPE;
110 + cryp.ses = sess->ses;
111 + cryp.len = state->len;
112 + cryp.src = (caddr_t) in;
113 + cryp.dst = (caddr_t) out;
114 + cryp.auth_src = state->aad;
115 + cryp.auth_len = state->aad_len;
117 + cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
119 + if (ctx->cipher->iv_len) {
120 + cryp.iv = (caddr_t) ctx->iv;
121 + if (!ctx->encrypt) {
122 + iiv = in + len - ctx->cipher->iv_len;
123 + memcpy(save_iv, iiv, ctx->cipher->iv_len);
128 + if (ioctl(state->d_fd, CIOCAUTHCRYPT, &cryp) == -1) {
129 + /* XXX need better errror handling
130 + * this can fail for a number of different reasons.
135 + if (ctx->cipher->iv_len) {
137 + iiv = out + len - ctx->cipher->iv_len;
140 + memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
147 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
148 const unsigned char *iv, int enc)
149 @@ -452,6 +521,45 @@ cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
153 +/* Save the encryption key provided by upper layers.
155 + * This function is called by EVP_CipherInit_ex to initialize the algorithm's
156 + * extra data. We can't do much here because the mac key is not available.
157 + * The next call should/will be to cryptodev_cbc_hmac_sha1_ctrl with parameter
158 + * EVP_CTRL_AEAD_SET_MAC_KEY, to set the hmac key. There we call CIOCGSESSION
159 + * with both the crypto and hmac keys.
161 +static int cryptodev_init_aead_key(EVP_CIPHER_CTX *ctx,
162 + const unsigned char *key, const unsigned char *iv, int enc)
164 + struct dev_crypto_state *state = ctx->cipher_data;
165 + struct session_op *sess = &state->d_sess;
166 + int cipher = -1, i;
168 + for (i = 0; ciphers[i].id; i++)
169 + if (ctx->cipher->nid == ciphers[i].nid &&
170 + ctx->cipher->iv_len <= ciphers[i].ivmax &&
171 + ctx->key_len == ciphers[i].keylen) {
172 + cipher = ciphers[i].id;
176 + if (!ciphers[i].id) {
181 + memset(sess, 0, sizeof(struct session_op));
183 + sess->key = (caddr_t)key;
184 + sess->keylen = ctx->key_len;
185 + sess->cipher = cipher;
187 + /* for whatever reason, (1) means success */
193 * free anything we allocated earlier when initting a
194 * session, and close the session.
195 @@ -488,6 +596,63 @@ cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
199 +static int cryptodev_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
203 + case EVP_CTRL_AEAD_SET_MAC_KEY:
205 + /* TODO: what happens with hmac keys larger than 64 bytes? */
206 + struct dev_crypto_state *state = ctx->cipher_data;
207 + struct session_op *sess = &state->d_sess;
209 + if ((state->d_fd = get_dev_crypto()) < 0)
212 + /* the rest should have been set in cryptodev_init_aead_key */
213 + sess->mackey = ptr;
214 + sess->mackeylen = arg;
216 + if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
217 + put_dev_crypto(state->d_fd);
223 + case EVP_CTRL_AEAD_TLS1_AAD:
225 + /* ptr points to the associated data buffer of 13 bytes */
226 + struct dev_crypto_state *state = ctx->cipher_data;
227 + unsigned char *p = ptr;
228 + unsigned int cryptlen = p[arg - 2] << 8 | p[arg - 1];
229 + unsigned int maclen, padlen;
230 + unsigned int bs = ctx->cipher->block_size;
233 + state->aad_len = arg;
234 + state->len = cryptlen;
236 + /* TODO: this should be an extension of EVP_CIPHER struct */
237 + switch (ctx->cipher->nid) {
238 + case NID_aes_128_cbc_hmac_sha1:
239 + case NID_aes_256_cbc_hmac_sha1:
240 + maclen = SHA_DIGEST_LENGTH;
243 + /* space required for encryption (not only TLS padding) */
245 + if (ctx->encrypt) {
246 + cryptlen += maclen;
247 + padlen += bs - (cryptlen % bs);
257 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
258 * gets called when libcrypto requests a cipher NID.
259 @@ -600,6 +765,33 @@ const EVP_CIPHER cryptodev_aes_256_cbc = {
263 +const EVP_CIPHER cryptodev_aes_128_cbc_hmac_sha1 = {
264 + NID_aes_128_cbc_hmac_sha1,
266 + EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_AEAD_CIPHER,
267 + cryptodev_init_aead_key,
268 + cryptodev_aead_cipher,
270 + sizeof(struct dev_crypto_state),
271 + EVP_CIPHER_set_asn1_iv,
272 + EVP_CIPHER_get_asn1_iv,
273 + cryptodev_cbc_hmac_sha1_ctrl,
277 +const EVP_CIPHER cryptodev_aes_256_cbc_hmac_sha1 = {
278 + NID_aes_256_cbc_hmac_sha1,
280 + EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_AEAD_CIPHER,
281 + cryptodev_init_aead_key,
282 + cryptodev_aead_cipher,
284 + sizeof(struct dev_crypto_state),
285 + EVP_CIPHER_set_asn1_iv,
286 + EVP_CIPHER_get_asn1_iv,
287 + cryptodev_cbc_hmac_sha1_ctrl,
291 * Registered by the ENGINE when used to find out how to deal with
292 * a particular NID in the ENGINE. this says what we'll do at the
293 @@ -637,6 +829,12 @@ cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
294 case NID_aes_256_cbc:
295 *cipher = &cryptodev_aes_256_cbc;
297 + case NID_aes_128_cbc_hmac_sha1:
298 + *cipher = &cryptodev_aes_128_cbc_hmac_sha1;
300 + case NID_aes_256_cbc_hmac_sha1:
301 + *cipher = &cryptodev_aes_256_cbc_hmac_sha1;
306 @@ -1384,6 +1582,8 @@ ENGINE_load_cryptodev(void)
310 + EVP_add_cipher(&cryptodev_aes_128_cbc_hmac_sha1);
311 + EVP_add_cipher(&cryptodev_aes_256_cbc_hmac_sha1);
312 if (!ENGINE_set_id(engine, "cryptodev") ||
313 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
314 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||