1 From 808871b2943a386165614daf2e7f5fb5b33e9fd1 Mon Sep 17 00:00:00 2001
2 From: Udit Agarwal <udit.agarwal@nxp.com>
3 Date: Wed, 4 Jul 2018 09:51:59 +0530
4 Subject: [PATCH] security/keys/secure_key: Adds the secure key support based
7 Secure keys are derieved using CAAM crypto block.
9 Secure keys derieved are the random number symmetric keys from CAAM.
10 Blobs corresponding to the key are formed using CAAM. User space
11 will only be able to view the blob of the key.
13 Signed-off-by: Udit Agarwal <udit.agarwal@nxp.com>
14 Reviewed-by: Sahil Malhotra <sahil.malhotra@nxp.com>
16 Documentation/security/keys/secure-key.rst | 67 ++++
18 include/keys/secure-type.h | 33 ++
19 security/keys/Kconfig | 11 +
20 security/keys/Makefile | 5 +
21 security/keys/secure_key.c | 339 ++++++++++++++++
22 security/keys/securekey_desc.c | 608 +++++++++++++++++++++++++++++
23 security/keys/securekey_desc.h | 141 +++++++
24 8 files changed, 1215 insertions(+)
25 create mode 100644 Documentation/security/keys/secure-key.rst
26 create mode 100644 include/keys/secure-type.h
27 create mode 100644 security/keys/secure_key.c
28 create mode 100644 security/keys/securekey_desc.c
29 create mode 100644 security/keys/securekey_desc.h
32 +++ b/Documentation/security/keys/secure-key.rst
38 +Secure key is the new type added to kernel key ring service.
39 +Secure key is a symmetric type key of minimum length 32 bytes
40 +and with maximum possible length to be 128 bytes. It is produced
41 +in kernel using the CAAM crypto engine. Userspace can only see
42 +the blob for the corresponding key. All the blobs are displayed
43 +or loaded in hex ascii.
45 +Secure key can be created on platforms which supports CAAM
46 +hardware block. Secure key can also be used as a master key to
47 +create the encrypted keys along with the existing key types in
50 +Secure key uses CAAM hardware to generate the key and blobify its
51 +content for userspace. Generated blobs are tied up with the hardware
52 +secret key stored in CAAM, hence the same blob will not be able to
53 +de-blobify with the different secret key on another machine.
57 + keyctl add secure <name> "new <keylen>" <ring>
58 + keyctl load secure <name> "load <hex_blob>" <ring>
59 + keyctl print <key_id>
61 +"keyctl add secure" option will create the random data of the
62 +specified key len using CAAM and store it as a key in kernel.
63 +Key contents will be displayed as blobs to the user in hex ascii.
64 +User can input key len from 32 bytes to 128 bytes.
66 +"keyctl load secure" option will load the blob contents. In kernel,
67 +key will be deirved using input blob and CAAM, along with the secret
70 +"keyctl print" will return the hex string of the blob corresponding to
71 +key_id. Returned blob will be of key_len + 48 bytes. Extra 48 bytes are
72 +the header bytes added by the CAAM.
74 +Example of secure key usage::
76 +1. Create the secure key with name kmk-master of length 32 bytes::
78 + $ keyctl add secure kmk-master "new 32" @u
83 + 1030783626 --alswrv 0 65534 keyring: _uid_ses.0
84 + 695927745 --alswrv 0 65534 \_ keyring: _uid.0
85 + 46001928 --als-rv 0 0 \_ secure: kmk-master
87 +2. Print the blob contents for the kmk-master key::
89 + $ keyctl print 46001928
90 + d9743445b640f3d59c1670dddc0bc9c2
91 + 34fc9aab7dd05c965e6120025012f029b
92 + 07faa4776c4f6ed02899e35a135531e9a
93 + 6e5c2b51132f9d5aef28f68738e658296
94 + 3fe583177cfe50d2542b659a13039
96 + $ keyctl pipe 46001928 > secure_key.blob
98 +3. Load the blob in the user key ring::
100 + $ keyctl load secure kmk-master "load 'cat secure_key.blob'" @u
103 @@ -9074,6 +9074,17 @@ F: include/keys/trusted-type.h
104 F: security/keys/trusted.c
105 F: include/keys/trusted.h
108 +M: Udit Agarwal <udit.agarwal@nxp.com>
109 +R: Sahil Malhotra <sahil.malhotra@nxp.com>
110 +L: linux-security-module@vger.kernel.org
111 +L: keyrings@vger.kernel.org
113 +F: include/keys/secure-type.h
114 +F: security/keys/secure_key.c
115 +F: security/keys/securekey_desc.c
116 +F: security/keys/securekey_desc.h
119 M: David Howells <dhowells@redhat.com>
120 M: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
122 +++ b/include/keys/secure-type.h
124 +/* SPDX-License-Identifier: GPL-2.0 */
126 + * Copyright (C) 2018 NXP.
130 +#ifndef _KEYS_SECURE_TYPE_H
131 +#define _KEYS_SECURE_TYPE_H
133 +#include <linux/key.h>
134 +#include <linux/rcupdate.h>
136 +/* Minimum key size to be used is 32 bytes and maximum key size fixed
138 + * Blob size to be kept is Maximum key size + blob header added by CAAM.
141 +#define MIN_KEY_SIZE 32
142 +#define MAX_KEY_SIZE 128
143 +#define BLOB_HEADER_SIZE 48
145 +#define MAX_BLOB_SIZE (MAX_KEY_SIZE + BLOB_HEADER_SIZE)
147 +struct secure_key_payload {
148 + struct rcu_head rcu;
149 + unsigned int key_len;
150 + unsigned int blob_len;
151 + unsigned char key[MAX_KEY_SIZE + 1];
152 + unsigned char blob[MAX_BLOB_SIZE];
155 +extern struct key_type key_type_secure;
157 --- a/security/keys/Kconfig
158 +++ b/security/keys/Kconfig
159 @@ -90,6 +90,17 @@ config TRUSTED_KEYS
161 If you are unsure as to whether this is required, answer N.
164 + tristate "SECURE_KEYS"
165 + depends on KEYS && CRYPTO_DEV_FSL_CAAM && CRYPTO_DEV_FSL_CAAM_JR
167 + This option provide support for creating secure-type key and blobs
168 + in kernel. Secure keys are random number symmetric keys generated
169 + from CAAM. The CAAM creates the blobs for the random key.
170 + Userspace will only be able to see the blob.
172 + If you are unsure as to whether this is required, answer N.
174 config ENCRYPTED_KEYS
175 tristate "ENCRYPTED KEYS"
177 --- a/security/keys/Makefile
178 +++ b/security/keys/Makefile
179 @@ -29,4 +29,9 @@ obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += key
181 obj-$(CONFIG_BIG_KEYS) += big_key.o
182 obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
183 +CFLAGS_secure_key.o += -I$(obj)/../../drivers/crypto/caam/
184 +CFLAGS_securekey_desc.o += -I$(obj)/../../drivers/crypto/caam/
185 +obj-$(CONFIG_SECURE_KEYS) += securekey.o
186 +securekey-y := securekey_desc.o \
188 obj-$(CONFIG_ENCRYPTED_KEYS) += encrypted-keys/
190 +++ b/security/keys/secure_key.c
192 +// SPDX-License-Identifier: GPL-2.0
193 +/* Copyright (C) 2018 NXP
194 + * Secure key is generated using NXP CAAM hardware block. CAAM generates the
195 + * random number (used as a key) and creates its blob for the user.
198 +#include <linux/slab.h>
199 +#include <linux/parser.h>
200 +#include <linux/string.h>
201 +#include <linux/key-type.h>
202 +#include <linux/rcupdate.h>
203 +#include <keys/secure-type.h>
204 +#include <linux/completion.h>
206 +#include "securekey_desc.h"
208 +static const char hmac_alg[] = "hmac(sha1)";
209 +static const char hash_alg[] = "sha1";
211 +static struct crypto_shash *hashalg;
212 +static struct crypto_shash *hmacalg;
220 +static const match_table_t key_tokens = {
222 + {load_blob, "load"},
226 +static struct secure_key_payload *secure_payload_alloc(struct key *key)
228 + struct secure_key_payload *sec_key = NULL;
231 + ret = key_payload_reserve(key, sizeof(*sec_key));
235 + sec_key = kzalloc(sizeof(*sec_key), GFP_KERNEL);
244 + * parse_inputdata - parse the keyctl input data and fill in the
245 + * payload structure for key or its blob.
246 + * param[in]: data pointer to the data to be parsed for creating key.
247 + * param[in]: p pointer to secure key payload structure to fill parsed data
248 + * On success returns 0, otherwise -EINVAL.
250 +static int parse_inputdata(char *data, struct secure_key_payload *p)
252 + substring_t args[MAX_OPT_ARGS];
255 + int key_cmd = -EINVAL;
258 + c = strsep(&data, " \t");
264 + /* Get the keyctl command i.e. new_key or load_blob etc */
265 + key_cmd = match_token(c, key_tokens, args);
269 + /* first argument is key size */
270 + c = strsep(&data, " \t");
276 + ret = kstrtol(c, 10, &keylen);
277 + if (ret < 0 || keylen < MIN_KEY_SIZE ||
278 + keylen > MAX_KEY_SIZE) {
283 + p->key_len = keylen;
288 + /* first argument is blob data for CAAM*/
289 + c = strsep(&data, " \t");
295 + /* Blob_len = No of characters in blob/2 */
296 + p->blob_len = strlen(c) / 2;
297 + if (p->blob_len > MAX_BLOB_SIZE) {
302 + ret = hex2bin(p->blob, c, p->blob_len);
320 + * secure_instantiate - create a new secure type key.
321 + * Supports the operation to generate a new key. A random number
322 + * is generated from CAAM as key data and the corresponding red blob
323 + * is formed and stored as key_blob.
324 + * Also supports the operation to load the blob and key is derived using
325 + * that blob from CAAM.
326 + * On success, return 0. Otherwise return errno.
328 +static int secure_instantiate(struct key *key,
329 + struct key_preparsed_payload *prep)
331 + struct secure_key_payload *payload = NULL;
332 + size_t datalen = prep->datalen;
336 + enum sk_req_type sk_op_type;
337 + struct device *dev = NULL;
339 + if (datalen <= 0 || datalen > 32767 || !prep->data) {
344 + data = kmalloc(datalen + 1, GFP_KERNEL);
350 + memcpy(data, prep->data, datalen);
351 + data[datalen] = '\0';
353 + payload = secure_payload_alloc(key);
359 + /* Allocate caam job ring for operation to be performed from CAAM */
360 + dev = caam_jr_alloc();
362 + pr_info("caam_jr_alloc failed\n");
367 + key_cmd = parse_inputdata(data, payload);
376 + * Red blob decryption to be done for load operation
377 + * to derive the key.
379 + sk_op_type = sk_red_blob_dec;
380 + ret = key_deblob(payload, sk_op_type, dev);
382 + pr_info("secure_key: key_blob decap fail (%d)\n", ret);
387 + /* Get Random number from caam of the specified length */
388 + sk_op_type = sk_get_random;
389 + ret = caam_get_random(payload, sk_op_type, dev);
391 + pr_info("secure_key: get_random fail (%d)\n", ret);
395 + /* Generate red blob of key random bytes with CAAM */
396 + sk_op_type = sk_red_blob_enc;
397 + ret = key_blob(payload, sk_op_type, dev);
399 + pr_info("secure_key: key_blob encap fail (%d)\n", ret);
414 + rcu_assign_keypointer(key, payload);
422 + * secure_read - copy the blob data to userspace in hex.
423 + * param[in]: key pointer to key struct
424 + * param[in]: buffer pointer to user data for creating key
425 + * param[in]: buflen is the length of the buffer
426 + * On success, return to userspace the secure key data size.
428 +static long secure_read(const struct key *key, char __user *buffer,
431 + const struct secure_key_payload *p = NULL;
436 + p = dereference_key_locked(key);
440 + if (buffer && buflen >= 2 * p->blob_len) {
441 + ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
446 + for (i = 0; i < p->blob_len; i++)
447 + bufp = hex_byte_pack(bufp, p->blob[i]);
448 + if (copy_to_user(buffer, ascii_buf, 2 * p->blob_len) != 0) {
454 + return 2 * p->blob_len;
458 + * secure_destroy - clear and free the key's payload
460 +static void secure_destroy(struct key *key)
462 + kzfree(key->payload.data[0]);
465 +struct key_type key_type_secure = {
467 + .instantiate = secure_instantiate,
468 + .destroy = secure_destroy,
469 + .read = secure_read,
471 +EXPORT_SYMBOL_GPL(key_type_secure);
473 +static void secure_shash_release(void)
476 + crypto_free_shash(hashalg);
478 + crypto_free_shash(hmacalg);
481 +static int __init secure_shash_alloc(void)
485 + hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
486 + if (IS_ERR(hmacalg)) {
487 + pr_info("secure_key: could not allocate crypto %s\n",
489 + return PTR_ERR(hmacalg);
492 + hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
493 + if (IS_ERR(hashalg)) {
494 + pr_info("secure_key: could not allocate crypto %s\n",
496 + ret = PTR_ERR(hashalg);
503 + crypto_free_shash(hmacalg);
507 +static int __init init_secure_key(void)
511 + ret = secure_shash_alloc();
515 + ret = register_key_type(&key_type_secure);
517 + secure_shash_release();
521 +static void __exit cleanup_secure_key(void)
523 + secure_shash_release();
524 + unregister_key_type(&key_type_secure);
527 +late_initcall(init_secure_key);
528 +module_exit(cleanup_secure_key);
530 +MODULE_LICENSE("GPL");
532 +++ b/security/keys/securekey_desc.c
534 +// SPDX-License-Identifier: GPL-2.0
536 + * Copyright (C) 2018 NXP
540 +#include <keys/secure-type.h>
541 +#include "securekey_desc.h"
543 +/* key modifier for blob encapsulation & decapsulation descriptor */
544 +u8 key_modifier[] = "SECURE_KEY";
545 +u32 key_modifier_len = 10;
547 +void caam_sk_rng_desc(struct sk_req *skreq, struct sk_desc *skdesc)
549 + struct sk_fetch_rnd_data *fetch_rnd_data = NULL;
550 + struct random_desc *rnd_desc = NULL;
552 + u32 *desc = skreq->hwdesc;
554 + init_job_desc(desc, 0);
556 + fetch_rnd_data = &skreq->req_u.sk_fetch_rnd_data;
557 + rnd_desc = &skdesc->dma_u.random_descp;
558 + len = fetch_rnd_data->key_len;
560 + /* command 0x82500000 */
561 + append_cmd(desc, CMD_OPERATION | OP_TYPE_CLASS1_ALG |
562 + OP_ALG_ALGSEL_RNG);
563 + /* command 0x60340000 | len */
564 + append_cmd(desc, CMD_FIFO_STORE | FIFOST_TYPE_RNGSTORE | len);
565 + append_ptr(desc, rnd_desc->rnd_data);
568 +void caam_sk_redblob_encap_desc(struct sk_req *skreq, struct sk_desc *skdesc)
570 + struct redblob_encap_desc *red_blob_desc =
571 + &skdesc->dma_u.redblob_encapdesc;
572 + struct sk_red_blob_encap *red_blob_req =
573 + &skreq->req_u.sk_red_blob_encap;
574 + u32 *desc = skreq->hwdesc;
576 + init_job_desc(desc, 0);
578 + /* Load class 2 key with key modifier. */
579 + append_key_as_imm(desc, key_modifier, key_modifier_len,
580 + key_modifier_len, CLASS_2 | KEY_DEST_CLASS_REG);
582 + /* SEQ IN PTR Command. */
583 + append_seq_in_ptr(desc, red_blob_desc->in_data, red_blob_req->data_sz,
586 + /* SEQ OUT PTR Command. */
587 + append_seq_out_ptr(desc, red_blob_desc->redblob,
588 + red_blob_req->redblob_sz, 0);
590 + /* RedBlob encapsulation PROTOCOL Command. */
591 + append_operation(desc, OP_TYPE_ENCAP_PROTOCOL | OP_PCLID_BLOB);
594 +/* void caam_sk_redblob_decap_desc(struct sk_req *skreq, struct sk_desc *skdesc)
595 + * brief CAAM Descriptor creator from redblob to plaindata.
596 + * param[in] skreq Pointer to secure key request structure
597 + * param[in] skdesc Pointer to secure key descriptor structure
599 +void caam_sk_redblob_decap_desc(struct sk_req *skreq, struct sk_desc *skdesc)
601 + struct redblob_decap_desc *red_blob_desc =
602 + &skdesc->dma_u.redblob_decapdesc;
603 + struct sk_red_blob_decap *red_blob_req =
604 + &skreq->req_u.sk_red_blob_decap;
605 + u32 *desc = skreq->hwdesc;
607 + init_job_desc(desc, 0);
609 + /* Load class 2 key with key modifier. */
610 + append_key_as_imm(desc, key_modifier, key_modifier_len,
611 + key_modifier_len, CLASS_2 | KEY_DEST_CLASS_REG);
613 + /* SEQ IN PTR Command. */
614 + append_seq_in_ptr(desc, red_blob_desc->redblob,
615 + red_blob_req->redblob_sz, 0);
617 + /* SEQ OUT PTR Command. */
618 + append_seq_out_ptr(desc, red_blob_desc->out_data,
619 + red_blob_req->data_sz, 0);
621 + /* RedBlob decapsulation PROTOCOL Command. */
622 + append_operation(desc, OP_TYPE_DECAP_PROTOCOL | OP_PCLID_BLOB);
625 +/* int caam_sk_get_random_map(struct device *dev, struct sk_req *req,
626 + * struct sk_desc *skdesc)
627 + * brief DMA map the buffer virtual pointers to physical address.
628 + * param[in] dev Pointer to job ring device structure
629 + * param[in] req Pointer to secure key request structure
630 + * param[in] skdesc Pointer to secure key descriptor structure
631 + * return 0 on success, error value otherwise.
633 +int caam_sk_get_random_map(struct device *dev, struct sk_req *req,
634 + struct sk_desc *skdesc)
636 + struct sk_fetch_rnd_data *fetch_rnd_data;
637 + struct random_desc *rnd_desc;
639 + fetch_rnd_data = &req->req_u.sk_fetch_rnd_data;
640 + rnd_desc = &skdesc->dma_u.random_descp;
642 + rnd_desc->rnd_data = dma_map_single(dev, fetch_rnd_data->data,
643 + fetch_rnd_data->key_len, DMA_FROM_DEVICE);
645 + if (dma_mapping_error(dev, rnd_desc->rnd_data)) {
646 + dev_err(dev, "Unable to map memory\n");
647 + goto sk_random_map_fail;
655 +/* int caam_sk_redblob_encap_map(struct device *dev, struct sk_req *req,
656 + * struct sk_desc *skdesc)
657 + * brief DMA map the buffer virtual pointers to physical address.
658 + * param[in] dev Pointer to job ring device structure
659 + * param[in] req Pointer to secure key request structure
660 + * param[in] skdesc Pointer to secure key descriptor structure
661 + * return 0 on success, error value otherwise.
663 +int caam_sk_redblob_encap_map(struct device *dev, struct sk_req *req,
664 + struct sk_desc *skdesc)
666 + struct sk_red_blob_encap *red_blob_encap;
667 + struct redblob_encap_desc *red_blob_desc;
669 + red_blob_encap = &req->req_u.sk_red_blob_encap;
670 + red_blob_desc = &skdesc->dma_u.redblob_encapdesc;
672 + red_blob_desc->in_data = dma_map_single(dev, red_blob_encap->data,
673 + red_blob_encap->data_sz, DMA_TO_DEVICE);
674 + if (dma_mapping_error(dev, red_blob_desc->in_data)) {
675 + dev_err(dev, "Unable to map memory\n");
679 + red_blob_desc->redblob = dma_map_single(dev, red_blob_encap->redblob,
680 + red_blob_encap->redblob_sz, DMA_FROM_DEVICE);
681 + if (dma_mapping_error(dev, red_blob_desc->redblob)) {
682 + dev_err(dev, "Unable to map memory\n");
683 + goto sk_redblob_fail;
689 + dma_unmap_single(dev, red_blob_desc->in_data, red_blob_encap->data_sz,
695 +/* static int caam_sk_redblob_decap_map(struct device *dev,
696 + * struct sk_req *req,
697 + * struct sk_desc *skdesc)
698 + * brief DMA map the buffer virtual pointers to physical address.
699 + * param[in] dev Pointer to job ring device structure
700 + * param[in] req Pointer to secure key request structure
701 + * param[in] skdesc Pointer to secure key descriptor structure
702 + * return 0 on success, error value otherwise.
704 +int caam_sk_redblob_decap_map(struct device *dev, struct sk_req *req,
705 + struct sk_desc *skdesc)
707 + struct sk_red_blob_decap *red_blob_decap;
708 + struct redblob_decap_desc *red_blob_desc;
710 + red_blob_decap = &req->req_u.sk_red_blob_decap;
711 + red_blob_desc = &skdesc->dma_u.redblob_decapdesc;
713 + red_blob_desc->redblob = dma_map_single(dev, red_blob_decap->redblob,
714 + red_blob_decap->redblob_sz, DMA_TO_DEVICE);
715 + if (dma_mapping_error(dev, red_blob_desc->redblob)) {
716 + dev_err(dev, "Unable to map memory\n");
717 + goto sk_redblob_fail;
720 + red_blob_desc->out_data = dma_map_single(dev, red_blob_decap->data,
721 + red_blob_decap->data_sz, DMA_FROM_DEVICE);
722 + if (dma_mapping_error(dev, red_blob_desc->out_data)) {
723 + dev_err(dev, "Unable to map memory\n");
730 + dma_unmap_single(dev, red_blob_desc->redblob,
731 + red_blob_decap->redblob_sz, DMA_TO_DEVICE);
736 +/* @fn void securekey_unmap(struct device *dev,
737 + * struct sk_desc *skdesc, struct sk_req *req)
738 + * @brief DMA unmap the buffer pointers.
739 + * @param[in] dev Pointer to job ring device structure
740 + * @param[in] skdesc Pointer to secure key descriptor structure
741 + * @param[in] req Pointer to secure key request structure
743 +void securekey_unmap(struct device *dev,
744 + struct sk_desc *skdesc, struct sk_req *req)
747 + switch (req->type) {
748 + case sk_get_random:
750 + struct sk_fetch_rnd_data *fetch_rnd_data;
751 + struct random_desc *rnd_desc;
753 + fetch_rnd_data = &req->req_u.sk_fetch_rnd_data;
754 + rnd_desc = &skdesc->dma_u.random_descp;
756 + /* Unmap Descriptor buffer pointers. */
757 + dma_unmap_single(dev, rnd_desc->rnd_data,
758 + fetch_rnd_data->key_len,
762 + case sk_red_blob_enc:
764 + struct sk_red_blob_encap *red_blob_encap;
765 + struct redblob_encap_desc *red_blob_desc;
767 + red_blob_encap = &req->req_u.sk_red_blob_encap;
768 + red_blob_desc = &skdesc->dma_u.redblob_encapdesc;
770 + /* Unmap Descriptor buffer pointers. */
771 + dma_unmap_single(dev, red_blob_desc->in_data,
772 + red_blob_encap->data_sz,
775 + dma_unmap_single(dev, red_blob_desc->redblob,
776 + red_blob_encap->redblob_sz,
781 + case sk_red_blob_dec:
783 + struct sk_red_blob_decap *red_blob_decap;
784 + struct redblob_decap_desc *red_blob_desc;
786 + red_blob_decap = &req->req_u.sk_red_blob_decap;
787 + red_blob_desc = &skdesc->dma_u.redblob_decapdesc;
789 + /* Unmap Descriptor buffer pointers. */
790 + dma_unmap_single(dev, red_blob_desc->redblob,
791 + red_blob_decap->redblob_sz,
794 + dma_unmap_single(dev, red_blob_desc->out_data,
795 + red_blob_decap->data_sz,
801 + dev_err(dev, "Unable to find request type\n");
807 +/* int caam_securekey_desc_init(struct device *dev, struct sk_req *req)
808 + * brief CAAM Descriptor creator for secure key operations.
809 + * param[in] dev Pointer to job ring device structure
810 + * param[in] req Pointer to secure key request structure
811 + * return 0 on success, error value otherwise.
813 +int caam_securekey_desc_init(struct device *dev, struct sk_req *req)
815 + struct sk_desc *skdesc = NULL;
818 + switch (req->type) {
819 + case sk_get_random:
821 + skdesc = kmalloc(sizeof(*skdesc), GFP_DMA);
826 + skdesc->req_type = req->type;
828 + if (caam_sk_get_random_map(dev, req, skdesc)) {
829 + dev_err(dev, "caam get_random map fail\n");
833 + caam_sk_rng_desc(req, skdesc);
836 + case sk_red_blob_enc:
838 + skdesc = kmalloc(sizeof(*skdesc), GFP_DMA);
844 + skdesc->req_type = req->type;
846 + if (caam_sk_redblob_encap_map(dev, req, skdesc)) {
847 + dev_err(dev, "caam redblob_encap map fail\n");
852 + /* Descriptor function to create redblob from data. */
853 + caam_sk_redblob_encap_desc(req, skdesc);
857 + case sk_red_blob_dec:
859 + skdesc = kmalloc(sizeof(*skdesc), GFP_DMA);
865 + skdesc->req_type = req->type;
867 + if (caam_sk_redblob_decap_map(dev, req, skdesc)) {
868 + dev_err(dev, "caam redblob_decap map fail\n");
873 + /* Descriptor function to decap data from redblob. */
874 + caam_sk_redblob_decap_desc(req, skdesc);
878 + pr_debug("Unknown request type\n");
883 + req->desc_pointer = (void *)skdesc;
889 +/* static void caam_op_done (struct device *dev, u32 *desc, u32 ret,
891 + * brief callback function to be called when descriptor executed.
892 + * param[in] dev Pointer to device structure
893 + * param[in] desc descriptor pointer
894 + * param[in] ret return status of Job submitted
895 + * param[in] context void pointer
897 +static void caam_op_done(struct device *dev, u32 *desc, u32 ret,
900 + struct sk_req *req = context;
903 + dev_err(dev, "caam op done err: %x\n", ret);
904 + /* print the error source name. */
905 + caam_jr_strstatus(dev, ret);
907 + /* Call securekey_unmap function for unmapping the buffer pointers. */
908 + securekey_unmap(dev, req->desc_pointer, req);
911 + complete(&req->comp);
915 +/* static int sk_job_submit(struct device *jrdev, struct sk_req *req)
916 + * brief Enqueue a Job descriptor to Job ring and wait until SEC returns.
917 + * param[in] jrdev Pointer to job ring device structure
918 + * param[in] req Pointer to secure key request structure
919 + * return 0 on success, error value otherwise.
921 +static int sk_job_submit(struct device *jrdev, struct sk_req *req)
925 + init_completion(&req->comp);
927 + /* caam_jr_enqueue function for Enqueue a job descriptor */
928 + ret = caam_jr_enqueue(jrdev, req->hwdesc, caam_op_done, req);
930 + wait_for_completion_interruptible(&req->comp);
936 +/* caam_get_random(struct secure_key_payload *p, enum sk_req_type fetch_rnd,
937 + * struct device *dev)
938 + * Create the random number of the specified length using CAAM block
939 + * param[in]: out pointer to place the random bytes
940 + * param[in]: length for the random data bytes.
941 + * param[in]: dev Pointer to job ring device structure
942 + * If operation is successful return 0, otherwise error.
944 +int caam_get_random(struct secure_key_payload *p, enum sk_req_type fetch_rnd,
945 + struct device *dev)
947 + struct sk_fetch_rnd_data *fetch_rnd_data = NULL;
948 + struct sk_req *req = NULL;
952 + req = kmalloc(sizeof(struct sk_req), GFP_DMA);
958 + req->type = fetch_rnd;
959 + fetch_rnd_data = &(req->req_u.sk_fetch_rnd_data);
961 + /* initialise with key length */
962 + fetch_rnd_data->key_len = p->key_len;
964 + temp = kmalloc(fetch_rnd_data->key_len, GFP_DMA);
969 + fetch_rnd_data->data = temp;
971 + ret = caam_securekey_desc_init(dev, req);
974 + pr_info("caam_securekey_desc_init failed\n");
978 + ret = sk_job_submit(dev, req);
980 + /*Copy output to key buffer. */
981 + memcpy(p->key, fetch_rnd_data->data, p->key_len);
995 +EXPORT_SYMBOL(caam_get_random);
997 +/* key_deblob(struct secure_key_payload *p, enum sk_req_type decap_type
998 + * struct device *dev)
999 + * Deblobify the blob to get the key data and fill in secure key payload struct
1000 + * param[in] p pointer to the secure key payload
1001 + * param[in] decap_type operation to be done.
1002 + * param[in] dev dev Pointer to job ring device structure
1003 + * If operation is successful return 0, otherwise error.
1005 +int key_deblob(struct secure_key_payload *p, enum sk_req_type decap_type,
1006 + struct device *dev)
1008 + unsigned int blob_len;
1009 + struct sk_red_blob_decap *d_blob;
1010 + struct sk_req *req = NULL;
1011 + int total_sz = 0, *temp = NULL, ret = 0;
1013 + req = kmalloc(sizeof(struct sk_req), GFP_DMA);
1019 + d_blob = &(req->req_u.sk_red_blob_decap);
1020 + blob_len = p->blob_len;
1021 + req->type = decap_type;
1024 + * Red blob size is the blob_len filled in payload struct
1025 + * Data_sz i.e. key is the blob_len - blob header size
1028 + d_blob->redblob_sz = blob_len;
1029 + d_blob->data_sz = blob_len - (SK_BLOB_KEY_SZ + SK_BLOB_MAC_SZ);
1030 + total_sz = d_blob->data_sz + d_blob->redblob_sz;
1032 + temp = kmalloc(total_sz, GFP_DMA);
1038 + req->mem_pointer = temp;
1039 + d_blob->redblob = temp;
1040 + d_blob->data = d_blob->redblob + d_blob->redblob_sz;
1041 + memcpy(d_blob->redblob, p->blob, blob_len);
1043 + ret = caam_securekey_desc_init(dev, req);
1046 + pr_info("caam_securekey_desc_init: Failed\n");
1050 + ret = sk_job_submit(dev, req);
1052 + /*Copy output to key buffer. */
1053 + p->key_len = d_blob->data_sz;
1054 + memcpy(p->key, d_blob->data, p->key_len);
1066 +EXPORT_SYMBOL(key_deblob);
1068 +/* key_blob(struct secure_key_payload *p, enum sk_req_type encap_type,
1069 + * struct device *dev)
1070 + * To blobify the key data to get the blob. This blob can only be seen by
1072 + * param[in] p pointer to the secure key payload
1073 + * param[in] decap_type operation to be done.
1074 + * param[in] dev dev Pointer to job ring device structure
1075 + * If operation is successful return 0, otherwise error.
1077 +int key_blob(struct secure_key_payload *p, enum sk_req_type encap_type,
1078 + struct device *dev)
1080 + unsigned int key_len;
1081 + struct sk_red_blob_encap *k_blob;
1082 + struct sk_req *req = NULL;
1083 + int total_sz = 0, *temp = NULL, ret = 0;
1085 + req = kmalloc(sizeof(struct sk_req), GFP_DMA);
1091 + key_len = p->key_len;
1093 + req->type = encap_type;
1094 + k_blob = &(req->req_u.sk_red_blob_encap);
1097 + * Data_sz i.e. key len and the corresponding blob_len is
1098 + * key_len + BLOB header size.
1101 + k_blob->data_sz = key_len;
1102 + k_blob->redblob_sz = key_len + SK_BLOB_KEY_SZ + SK_BLOB_MAC_SZ;
1103 + total_sz = k_blob->data_sz + k_blob->redblob_sz;
1105 + temp = kmalloc(total_sz, GFP_DMA);
1111 + req->mem_pointer = temp;
1112 + k_blob->data = temp;
1114 + k_blob->redblob = k_blob->data + k_blob->data_sz;
1115 + memcpy(k_blob->data, p->key, key_len);
1117 + ret = caam_securekey_desc_init(dev, req);
1120 + pr_info("caam_securekey_desc_init failed\n");
1124 + ret = sk_job_submit(dev, req);
1126 + /*Copy output to key buffer. */
1127 + p->blob_len = k_blob->redblob_sz;
1128 + memcpy(p->blob, k_blob->redblob, p->blob_len);
1135 + kfree(req->mem_pointer);
1141 +EXPORT_SYMBOL(key_blob);
1143 +++ b/security/keys/securekey_desc.h
1145 +/* SPDX-License-Identifier: GPL-2.0 */
1147 + * Copyright 2018 NXP
1150 +#ifndef _SECUREKEY_DESC_H_
1151 +#define _SECUREKEY_DESC_H_
1153 +#include "compat.h"
1155 +#include "intern.h"
1157 +#include "desc_constr.h"
1162 +#define SK_BLOB_KEY_SZ 32 /* Blob key size. */
1163 +#define SK_BLOB_MAC_SZ 16 /* Blob MAC size. */
1166 + * brief defines different kinds of operations supported by this module.
1176 + * struct random_des
1177 + * param[out] rnd_data output buffer for random data.
1179 +struct random_desc {
1180 + dma_addr_t rnd_data;
1183 +/* struct redblob_encap_desc
1184 + * details Structure containing dma address for redblob encapsulation.
1185 + * param[in] in_data input data to redblob encap descriptor.
1186 + * param[out] redblob output buffer for redblob.
1188 +struct redblob_encap_desc {
1189 + dma_addr_t in_data;
1190 + dma_addr_t redblob;
1193 +/* struct redblob_decap_desc
1194 + * details Structure containing dma address for redblob decapsulation.
1195 + * param[in] redblob input buffer to redblob decap descriptor.
1196 + * param[out] out_data output data from redblob decap descriptor.
1198 +struct redblob_decap_desc {
1199 + dma_addr_t redblob;
1200 + dma_addr_t out_data;
1204 + * details Structure for securekey descriptor creation.
1205 + * param[in] req_type operation supported.
1206 + * param[in] dma_u union of struct for supported operation.
1211 + struct redblob_encap_desc redblob_encapdesc;
1212 + struct redblob_decap_desc redblob_decapdesc;
1213 + struct random_desc random_descp;
1217 +/* struct sk_fetch_rnd_data
1218 + * decriptor structure containing key length.
1220 +struct sk_fetch_rnd_data {
1225 +/* struct sk_red_blob_encap
1226 + * details Structure containing buffer pointers for redblob encapsulation.
1227 + * param[in] data Input data.
1228 + * param[in] data_sz size of Input data.
1229 + * param[out] redblob output buffer for redblob.
1230 + * param[in] redblob_sz size of redblob.
1232 +struct sk_red_blob_encap {
1236 + uint32_t redblob_sz;
1239 +/* struct sk_red_blob_decap
1240 + * details Structure containing buffer pointers for redblob decapsulation.
1241 + * param[in] redblob Input redblob.
1242 + * param[in] redblob_sz size of redblob.
1243 + * param[out] data output buffer for data.
1244 + * param[in] data_sz size of output data.
1246 +struct sk_red_blob_decap {
1248 + uint32_t redblob_sz;
1254 + * details Structure for securekey request creation.
1255 + * param[in] type operation supported.
1256 + * param[in] req_u union of struct for supported operation.
1257 + * param[out] ret return status of CAAM operation.
1258 + * param[in] mem_pointer memory pointer for allocated kernel memory.
1259 + * param[in] desc_pointer Pointer to securekey descriptor creation structure.
1260 + * param[in] comp struct completion object.
1261 + * param[in] hwdesc contains descriptor instructions.
1264 + enum sk_req_type type;
1267 + struct sk_red_blob_encap sk_red_blob_encap;
1268 + struct sk_red_blob_decap sk_red_blob_decap;
1269 + struct sk_fetch_rnd_data sk_fetch_rnd_data;
1272 + void *mem_pointer;
1273 + void *desc_pointer;
1274 + struct completion comp;
1275 + u32 hwdesc[MAX_CAAM_DESCSIZE];
1278 +int caam_get_random(struct secure_key_payload *p, enum sk_req_type fetch_rnd,
1279 + struct device *dev);
1280 +int key_blob(struct secure_key_payload *p, enum sk_req_type encap_type,
1281 + struct device *dev);
1282 +int key_deblob(struct secure_key_payload *p, enum sk_req_type decap_type,
1283 + struct device *dev);
1285 +#endif /*_SECUREKEY_DESC_H_*/