1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* Copyright (c) 2022-2024 HiSilicon Limited. */ 3 #ifndef __SEC_HASH_H__ 4 #define __SEC_HASH_H__ 5 6 #include <stdint.h> 7 #include <tee_api_types.h> 8 9 #define WORD_ALIGNMENT_MASK 0x3 10 #define HASH_MODE_OFFSET 28 11 #define WCRYPTO_DIGEST_HMAC 3 12 #define WCRYPTO_DIGEST_NORMAL 5 13 #define MAX_AUTH_LENGTH 16776704 14 #define HASH_MAC_LEN128 16 15 #define HASH_MAC_LEN160 20 16 #define HASH_MAC_LEN224 28 17 #define HASH_MAC_LEN256 32 18 #define HASH_MAC_LEN384 48 19 #define HASH_MAC_LEN512 64 20 #define SEC_DIGEST_MAX_KEY_SIZE 128 21 #define SEC_DIGEST_MAX_MAC_SIZE 64 22 #define SEC_DIGEST_MAX_IV_SIZE 64 23 #define SMALL_BUF_SIZE 0x1000 24 25 struct hashctx { 26 uint8_t key[SEC_DIGEST_MAX_KEY_SIZE]; 27 uint8_t iv[SEC_DIGEST_MAX_IV_SIZE]; 28 uint8_t out[SEC_DIGEST_MAX_MAC_SIZE]; 29 bool has_next; 30 uint8_t mode; 31 uint32_t algo; 32 uint32_t scene; 33 struct hisi_qp *qp; 34 uint8_t *in; 35 uint64_t in_dma; 36 size_t buf_len; 37 size_t in_len; 38 uint64_t out_dma; 39 size_t mac_len; 40 uint64_t key_dma; 41 size_t key_len; 42 uint64_t iv_dma; 43 size_t iv_len; 44 uint64_t long_data_len; 45 }; 46 47 /* 48 * Format the hash context to keep the reference to the 49 * operation driver 50 */ 51 struct crypto_hash { 52 struct crypto_hash_ctx hash_ctx; /* Crypto Hash API context */ 53 struct hashctx *ctx; /* Hash Context */ 54 }; 55 56 /* 57 * Format the hmac context to keep the reference to the 58 * operation driver 59 */ 60 struct crypto_hmac { 61 struct crypto_mac_ctx hmac_op; /* Crypto Hash API context */ 62 struct hashctx *ctx; /* Hash Context */ 63 }; 64 65 enum A_ALG { 66 A_ALG_SHA1 = 0x0, 67 A_ALG_SHA256 = 0x1, 68 A_ALG_MD5 = 0x2, 69 A_ALG_SHA224 = 0x3, 70 A_ALG_SHA384 = 0x4, 71 A_ALG_SHA512 = 0x5, 72 A_ALG_SHA512_224 = 0x6, 73 A_ALG_SHA512_256 = 0x7, 74 A_ALG_HMAC_SHA1 = 0x10, 75 A_ALG_HMAC_SHA256 = 0x11, 76 A_ALG_HMAC_MD5 = 0x12, 77 A_ALG_HMAC_SHA224 = 0x13, 78 A_ALG_HMAC_SHA384 = 0x14, 79 A_ALG_HMAC_SHA512 = 0x15, 80 A_ALG_HMAC_SHA512_224 = 0x16, 81 A_ALG_HMAC_SHA512_256 = 0x17, 82 A_ALG_AES_XCBC_MAC_96 = 0x20, 83 A_ALG_AES_XCBC_PRF_128 = 0x20, 84 A_ALG_AES_CMAC = 0x21, 85 A_ALG_AES_GMAC = 0x22, 86 A_ALG_SM3 = 0x25, 87 A_ALG_HMAC_SM3 = 0x26, 88 A_ALG_MAX 89 }; 90 91 enum { 92 AI_GEN_INNER = 0x0, 93 AI_GEN_IVIN_ADDR = 0x1, 94 AI_GEN_CAL_IV_ADDR = 0x2, 95 AI_GEN_TRNG = 0x3, 96 }; 97 98 enum { 99 AUTHPAD_PAD, 100 AUTHPAD_NOPAD, 101 }; 102 103 TEE_Result hisi_sec_hash_ctx_init(struct hashctx *hash_ctx, uint32_t algo); 104 TEE_Result hisi_sec_digest_ctx_init(struct hashctx *hash_ctx, 105 const uint8_t *key, size_t len); 106 TEE_Result hisi_sec_digest_do_update(struct hashctx *hashctx, 107 const uint8_t *data, size_t len); 108 TEE_Result hisi_sec_digest_do_final(struct hashctx *hashctx, uint8_t *digest, 109 size_t len); 110 void hisi_sec_digest_ctx_free(struct hashctx *hash_ctx); 111 void hisi_sec_digest_copy_state(struct hashctx *out_hash_ctx, 112 struct hashctx *in_hash_ctx); 113 114 #endif 115