1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright 2018-2020 NXP 4 * 5 * CAAM Cipher Local header. 6 */ 7 #ifndef __LOCAL_H__ 8 #define __LOCAL_H__ 9 10 #include <drvcrypt.h> 11 #include <drvcrypt_cipher.h> 12 13 /* 14 * Definition of the maximum number of CAAM Job descriptor entries 15 */ 16 #ifdef CFG_CAAM_64BIT 17 #define MAX_DESC_ENTRIES 22 18 #else 19 #define MAX_DESC_ENTRIES 16 20 #endif 21 22 /* 23 * Definition of flags tagging which key(s) is required 24 */ 25 #define NEED_KEY1 BIT(0) 26 #define NEED_KEY2 BIT(1) 27 #define NEED_IV BIT(2) 28 #define NEED_TWEAK BIT(3) 29 30 /* 31 * Cipher Algorithm definition 32 */ 33 struct cipheralg { 34 uint32_t type; /* Algo type for operation */ 35 uint8_t size_block; /* Computing block size */ 36 uint8_t size_ctx; /* CAAM Context Register size */ 37 uint8_t ctx_offset; /* CAAM Context Register offset */ 38 uint8_t require_key; /* Tag defining key(s) required */ 39 struct caamdefkey def_key; /* Key size accepted */ 40 41 TEE_Result (*update)(struct drvcrypt_cipher_update *dupdate); 42 }; 43 44 /* 45 * Full Cipher data SW context 46 */ 47 struct cipherdata { 48 uint32_t *descriptor; /* Job descriptor */ 49 bool encrypt; /* Encrypt direction */ 50 struct caambuf key1; /* First Key */ 51 struct caambuf key2; /* Second Key */ 52 struct caambuf tweak; /* XTS Tweak */ 53 struct caambuf ctx; /* CAAM Context Register */ 54 struct caamblock blockbuf; /* Temporary Block buffer */ 55 const struct cipheralg *alg; /* Reference to the algo constants */ 56 57 /* Additionnal Data for the MAC */ 58 unsigned int mode; /* MAC TEE_CHAIN_MODE* */ 59 size_t countdata; /* MAC Number of input data */ 60 }; 61 62 /* 63 * Cipher additionnal data block 64 */ 65 enum caam_cipher_block { 66 CIPHER_BLOCK_NONE = 0, 67 CIPHER_BLOCK_IN, 68 CIPHER_BLOCK_OUT, 69 CIPHER_BLOCK_BOTH, 70 }; 71 72 /* 73 * Update of the cipher operation of complete block except 74 * if last block. Last block can be partial block. 75 * 76 * @ctx Cipher context 77 * @savectx Save or not the context 78 * @keyid Id of the key to be used during operation 79 * @encrypt Encrypt or decrypt direction 80 * @src Source data to encrypt/decrypt 81 * @dst [out] Destination data encrypted/decrypted 82 * @blocks Additionnal data block to handle (input/output) 83 */ 84 enum caam_status caam_cipher_block(struct cipherdata *ctx, bool savectx, 85 uint8_t keyid, bool encrypt, 86 struct caambuf *src, struct caambuf *dst, 87 enum caam_cipher_block blocks); 88 89 /* 90 * Update of the cipher operation in xts mode. 91 * 92 * @dupdate Data update object 93 */ 94 TEE_Result caam_cipher_update_xts(struct drvcrypt_cipher_update *dupdate); 95 96 /* 97 * Initialization of the cipher operation 98 * 99 * @dinit Data initialization object 100 */ 101 TEE_Result caam_cipher_initialize(struct drvcrypt_cipher_init *dinit); 102 103 /* 104 * Free software context 105 * 106 * @ctx Caller context variable 107 */ 108 void caam_cipher_free(void *ctx); 109 110 /* 111 * Copy software Context 112 * 113 * @dst_ctx [out] Reference the context destination 114 * @src_ctx Reference the context source 115 */ 116 void caam_cipher_copy_state(void *dst_ctx, void *src_ctx); 117 118 #endif /* __LOCAL_H__ */ 119