1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2019, Linaro Limited 4 */ 5 6 #ifndef __CRYPTO_CRYPTO_IMPL_H 7 #define __CRYPTO_CRYPTO_IMPL_H 8 9 #include <tee_api_types.h> 10 11 /* 12 * The crypto context used by the crypto_hash_*() functions is defined by 13 * struct crypto_hash_ctx. 14 */ 15 struct crypto_hash_ctx { 16 const struct crypto_hash_ops *ops; 17 }; 18 19 struct crypto_hash_ops { 20 TEE_Result (*init)(struct crypto_hash_ctx *ctx); 21 TEE_Result (*update)(struct crypto_hash_ctx *ctx, const uint8_t *data, 22 size_t len); 23 TEE_Result (*final)(struct crypto_hash_ctx *ctx, uint8_t *digest, 24 size_t len); 25 void (*free_ctx)(struct crypto_hash_ctx *ctx); 26 void (*copy_state)(struct crypto_hash_ctx *dst_ctx, 27 struct crypto_hash_ctx *src_ctx); 28 }; 29 30 #define CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(name, type) \ 31 static inline TEE_Result \ 32 crypto_##name##_alloc_ctx(struct crypto_##type##_ctx **ctx __unused) \ 33 { return TEE_ERROR_NOT_IMPLEMENTED; } 34 35 #if defined(CFG_CRYPTO_MD5) 36 TEE_Result crypto_md5_alloc_ctx(struct crypto_hash_ctx **ctx); 37 #else 38 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(md5, hash) 39 #endif 40 41 #if defined(CFG_CRYPTO_SHA1) 42 TEE_Result crypto_sha1_alloc_ctx(struct crypto_hash_ctx **ctx); 43 #else 44 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha1, hash) 45 #endif 46 47 #if defined(CFG_CRYPTO_SHA224) 48 TEE_Result crypto_sha224_alloc_ctx(struct crypto_hash_ctx **ctx); 49 #else 50 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha224, hash) 51 #endif 52 53 #if defined(CFG_CRYPTO_SHA256) 54 TEE_Result crypto_sha256_alloc_ctx(struct crypto_hash_ctx **ctx); 55 #else 56 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha256, hash) 57 #endif 58 59 #if defined(CFG_CRYPTO_SHA384) 60 TEE_Result crypto_sha384_alloc_ctx(struct crypto_hash_ctx **ctx); 61 #else 62 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha384, hash) 63 #endif 64 65 #if defined(CFG_CRYPTO_SHA512) 66 TEE_Result crypto_sha512_alloc_ctx(struct crypto_hash_ctx **ctx); 67 #else 68 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha512, hash) 69 #endif 70 71 /* 72 * The crypto context used by the crypto_mac_*() functions is defined by 73 * struct crypto_mac_ctx. 74 */ 75 struct crypto_mac_ctx { 76 const struct crypto_mac_ops *ops; 77 }; 78 79 struct crypto_mac_ops { 80 TEE_Result (*init)(struct crypto_mac_ctx *ctx, const uint8_t *key, 81 size_t len); 82 TEE_Result (*update)(struct crypto_mac_ctx *ctx, const uint8_t *data, 83 size_t len); 84 TEE_Result (*final)(struct crypto_mac_ctx *ctx, uint8_t *digest, 85 size_t len); 86 void (*free_ctx)(struct crypto_mac_ctx *ctx); 87 void (*copy_state)(struct crypto_mac_ctx *dst_ctx, 88 struct crypto_mac_ctx *src_ctx); 89 }; 90 91 #if defined(CFG_CRYPTO_HMAC) 92 TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx); 93 TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx); 94 TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx); 95 TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx); 96 TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx); 97 TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx); 98 #else 99 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_md5, mac) 100 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha1, mac) 101 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha224, mac) 102 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha256, mac) 103 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha384, mac) 104 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha512, mac) 105 #endif 106 107 #if defined(CFG_CRYPTO_CBC_MAC) 108 TEE_Result crypto_aes_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx); 109 TEE_Result crypto_aes_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx); 110 TEE_Result crypto_des_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx); 111 TEE_Result crypto_des_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx); 112 TEE_Result crypto_des3_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx); 113 TEE_Result crypto_des3_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx); 114 #else 115 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc_mac_nopad, mac) 116 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc_mac_pkcs5, mac) 117 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc_mac_nopad, mac) 118 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc_mac_pkcs5, mac) 119 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc_mac_nopad, mac) 120 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc_mac_pkcs5, mac) 121 #endif 122 123 #if defined(CFG_CRYPTO_CMAC) 124 TEE_Result crypto_aes_cmac_alloc_ctx(struct crypto_mac_ctx **ctx); 125 #else 126 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cmac, mac) 127 #endif 128 129 /* 130 * The crypto context used by the crypto_cipher_*() functions is defined by 131 * struct crypto_cipher_ctx. 132 */ 133 struct crypto_cipher_ctx { 134 const struct crypto_cipher_ops *ops; 135 }; 136 137 struct crypto_cipher_ops { 138 TEE_Result (*init)(struct crypto_cipher_ctx *ctx, 139 TEE_OperationMode mode, 140 const uint8_t *key1, size_t key1_len, 141 const uint8_t *key2, size_t key2_len, 142 const uint8_t *iv, size_t iv_len); 143 TEE_Result (*update)(struct crypto_cipher_ctx *ctx, bool last_block, 144 const uint8_t *data, size_t len, uint8_t *dst); 145 void (*final)(struct crypto_cipher_ctx *ctx); 146 147 void (*free_ctx)(struct crypto_cipher_ctx *ctx); 148 void (*copy_state)(struct crypto_cipher_ctx *dst_ctx, 149 struct crypto_cipher_ctx *src_ctx); 150 }; 151 152 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_ECB) 153 TEE_Result crypto_aes_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx); 154 #else 155 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_ecb, cipher) 156 #endif 157 158 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CBC) 159 TEE_Result crypto_aes_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx); 160 #else 161 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc, cipher) 162 #endif 163 164 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CTR) 165 TEE_Result crypto_aes_ctr_alloc_ctx(struct crypto_cipher_ctx **ctx); 166 #else 167 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_ctr, cipher) 168 #endif 169 170 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CTS) 171 TEE_Result crypto_aes_cts_alloc_ctx(struct crypto_cipher_ctx **ctx); 172 #else 173 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cts, cipher) 174 #endif 175 176 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_XTS) 177 TEE_Result crypto_aes_xts_alloc_ctx(struct crypto_cipher_ctx **ctx); 178 #else 179 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_xts, cipher) 180 #endif 181 182 #if defined(CFG_CRYPTO_DES) && defined(CFG_CRYPTO_ECB) 183 TEE_Result crypto_des_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx); 184 TEE_Result crypto_des3_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx); 185 #else 186 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_ecb, cipher) 187 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_ecb, cipher) 188 #endif 189 190 #if defined(CFG_CRYPTO_DES) && defined(CFG_CRYPTO_CBC) 191 TEE_Result crypto_des_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx); 192 TEE_Result crypto_des3_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx); 193 #else 194 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc, cipher) 195 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc, cipher) 196 #endif 197 198 /* 199 * The crypto context used by the crypto_authen_*() functions below is 200 * defined by struct crypto_authenc_ctx. 201 */ 202 struct crypto_authenc_ctx { 203 const struct crypto_authenc_ops *ops; 204 }; 205 206 struct crypto_authenc_ops { 207 TEE_Result (*init)(struct crypto_authenc_ctx *ctx, 208 TEE_OperationMode mode, 209 const uint8_t *key, size_t key_len, 210 const uint8_t *nonce, size_t nonce_len, 211 size_t tag_len, size_t aad_len, 212 size_t payload_len); 213 TEE_Result (*update_aad)(struct crypto_authenc_ctx *ctx, 214 const uint8_t *data, size_t len); 215 TEE_Result (*update_payload)(struct crypto_authenc_ctx *ctx, 216 TEE_OperationMode mode, 217 const uint8_t *src_data, size_t len, 218 uint8_t *dst_data); 219 TEE_Result (*enc_final)(struct crypto_authenc_ctx *ctx, 220 const uint8_t *src_data, size_t len, 221 uint8_t *dst_data, uint8_t *dst_tag, 222 size_t *dst_tag_len); 223 TEE_Result (*dec_final)(struct crypto_authenc_ctx *ctx, 224 const uint8_t *src_data, size_t len, 225 uint8_t *dst_data, const uint8_t *tag, 226 size_t tag_len); 227 void (*final)(struct crypto_authenc_ctx *ctx); 228 void (*free_ctx)(struct crypto_authenc_ctx *ctx); 229 void (*copy_state)(struct crypto_authenc_ctx *dst_ctx, 230 struct crypto_authenc_ctx *src_ctx); 231 }; 232 233 TEE_Result crypto_aes_ccm_alloc_ctx(struct crypto_authenc_ctx **ctx); 234 TEE_Result crypto_aes_gcm_alloc_ctx(struct crypto_authenc_ctx **ctx); 235 236 #ifdef CFG_CRYPTO_DRV_HASH 237 TEE_Result drvcrypt_hash_alloc_ctx(struct crypto_hash_ctx **ctx, uint32_t algo); 238 #else 239 static inline TEE_Result 240 drvcrypt_hash_alloc_ctx(struct crypto_hash_ctx **ctx __unused, 241 uint32_t algo __unused) 242 { 243 return TEE_ERROR_NOT_IMPLEMENTED; 244 } 245 #endif 246 #endif /*__CRYPTO_CRYPTO_IMPL_H*/ 247