mirror of
https://github.com/nxp-imx/linux-imx.git
synced 2025-09-03 02:16:09 +02:00

CAAM's Black Key mechanism is intended for protection of user keys against bus snooping. This automatically encapsulates and decapsulates cryptographic keys ''on-the-fly'' in an encrypted data structure called a Black Key. Before a value is copied from a Key Register to memory, CAAM will automatically encrypt the key as a Black Key (encrypted key) using the current value in the JDKEKR or TDKEKR as the encryption key. CAAM's built-in Blob Protocol provides a method for protecting user-defined data across system power cycles. CAAM protects data in a data structure called a Blob, which provides both confidentiality and integrity protection. The data to be protected is encrypted so that it can be safely placed into non-volatile storage before the SoC is powered down. This patch includes the support to generate a black key from random or from a plaintext. Also one can encapsulate it into a blob or decapsulate a black key from a blob. The key and blob generation descriptors are exported into a separate file, such that they could be shared with other interfaces (qi, qi2). This feature has support only for black keys, encapsulated in black blobs in General Memory. In caamkeyblob_test.c file is a test that validates the above operations: create a black key from plaintext or from random, encapsulate and decapsulate a blob and compare the obtained black key. This test is configured as a kernel module. Signed-off-by: Franck LENORMAND <franck.lenormand@nxp.com> Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com> Reviewed-by: Horia Geantă <horia.geanta@nxp.com> (cherry picked from commit84287c5d3b
) Squashed fixes:9c24012e6b
("MLK-24496 crypto: caam - fix blob encapsulation/decapsulation")cd078fac33
("MLK-24517-1 crypto: caam - removed unnecessary validation of black key for blob decapsulation")8888926c54
("MLK-24517-2 crypto: caam - removed unnecessary validation of black key for blob encapsulation")e4b484ce2d
("MLK-24497 crypto: caam - update job descriptor with inline commands") Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com> Reviewed-by: Horia Geantă <horia.geanta@nxp.com> Squashed LF commit (rebase-v5.10-rc2/crypto/caam): 035f5933cc45 ("crypto: caam: change kzfree to kfree_sensitive") Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
102 lines
3.0 KiB
C
102 lines
3.0 KiB
C
/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
|
|
/*
|
|
* Shared descriptors for CAAM black key and blob
|
|
*
|
|
* Copyright 2018-2020 NXP
|
|
*/
|
|
|
|
#ifndef _CAAMKEYBLOB_DESC_H_
|
|
#define _CAAMKEYBLOB_DESC_H_
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include "jr.h"
|
|
#include "regs.h"
|
|
#include "desc.h"
|
|
|
|
#include "compat.h"
|
|
#include "tag_object.h"
|
|
#include "desc_constr.h"
|
|
|
|
/* Defines for secure memory and general memory blobs */
|
|
#define DATA_GENMEM 0
|
|
#define DATA_SECMEM 1
|
|
|
|
/* Encrypted key */
|
|
#define BLACK_KEY 1
|
|
|
|
/* Define key encryption/covering options */
|
|
#define KEY_COVER_ECB 0 /* cover key in AES-ECB */
|
|
#define KEY_COVER_CCM 1 /* cover key with AES-CCM */
|
|
|
|
/* Define the trust in the key, to select either JDKEK or TDKEK */
|
|
#define UNTRUSTED_KEY 0
|
|
#define TRUSTED_KEY 1
|
|
|
|
/* Define space required for BKEK + MAC tag storage in any blob */
|
|
#define BLOB_OVERHEAD (32 + 16)
|
|
|
|
#define PAD_16_BYTE(_key_size) (roundup(_key_size, 16))
|
|
#define PAD_8_BYTE(_key_size) (roundup(_key_size, 8))
|
|
|
|
/*
|
|
* ECB-Black Key will be padded with zeros to make it a
|
|
* multiple of 16 bytes long before it is encrypted,
|
|
* and the resulting Black Key will be this length.
|
|
*/
|
|
#define ECB_BLACK_KEY_SIZE(_key_size) (PAD_16_BYTE(_key_size))
|
|
|
|
/*
|
|
* CCM-Black Key will always be at least 12 bytes longer,
|
|
* since the encapsulation uses a 6-byte nonce and adds
|
|
* a 6-byte ICV. But first, the key is padded as necessary so
|
|
* that CCM-Black Key is a multiple of 8 bytes long.
|
|
*/
|
|
#define NONCE_SIZE 6
|
|
#define ICV_SIZE 6
|
|
#define CCM_OVERHEAD (NONCE_SIZE + ICV_SIZE)
|
|
#define CCM_BLACK_KEY_SIZE(_key_size) (PAD_8_BYTE(_key_size) \
|
|
+ CCM_OVERHEAD)
|
|
|
|
static inline int secret_size_in_ccm_black_key(int key_size)
|
|
{
|
|
return ((key_size >= CCM_OVERHEAD) ? key_size - CCM_OVERHEAD : 0);
|
|
}
|
|
|
|
#define SECRET_SIZE_IN_CCM_BLACK_KEY(_key_size) \
|
|
secret_size_in_ccm_black_key(_key_size)
|
|
|
|
/* A red key is not encrypted so its size is the same */
|
|
#define RED_KEY_SIZE(_key_size) (_key_size)
|
|
|
|
/*
|
|
* Based on memory type, the key modifier length
|
|
* can be either 8-byte or 16-byte.
|
|
*/
|
|
#define KEYMOD_SIZE_SM 8
|
|
#define KEYMOD_SIZE_GM 16
|
|
|
|
/* Create job descriptor to cover key */
|
|
int cnstr_desc_black_key(u32 **desc, char *key, size_t key_len,
|
|
dma_addr_t black_key, size_t black_key_len,
|
|
u8 key_enc, u8 trusted_key);
|
|
|
|
/* Create job descriptor to generate a random key and cover it */
|
|
int cnstr_desc_random_black_key(u32 **desc, size_t key_len,
|
|
dma_addr_t black_key, size_t black_key_len,
|
|
u8 key_enc, u8 trusted_key);
|
|
|
|
/* Encapsulate data in a blob */
|
|
int cnstr_desc_blob_encap(u32 **desc, dma_addr_t black_key,
|
|
size_t black_key_len, u8 color, u8 key_enc,
|
|
u8 trusted_key, u8 mem_type, const void *key_mod,
|
|
size_t key_mod_len, dma_addr_t blob, size_t blob_len);
|
|
|
|
/* Decapsulate data from a blob */
|
|
int cnstr_desc_blob_decap(u32 **desc, dma_addr_t blob, size_t blob_len,
|
|
const void *key_mod, size_t key_mod_len,
|
|
dma_addr_t black_key, size_t black_key_len,
|
|
u8 keycolor, u8 key_enc, u8 trusted_key, u8 mem_type);
|
|
|
|
#endif /* _CAAMKEYBLOB_DESC_H_ */
|