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 #endif /*__CRYPTO_CRYPTO_IMPL_H*/ 129