1 /* 2 * Copyright 2017-2021 NXP 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 8 #ifndef __HASH_H__ 9 #define __HASH_H__ 10 11 #include <stdbool.h> 12 #include <common/sha_common_macros.h> 13 14 /* List of hash algorithms */ 15 enum hash_algo { 16 SHA1 = 0, 17 SHA256 18 }; 19 20 /* 21 * number of words in the digest - Digest is kept internally 22 * as 8 32-bit words 23 */ 24 #define _SHA256_DIGEST_LENGTH 8 25 26 /* 27 * block length - A block, treated as a sequence of 28 * 32-bit words 29 */ 30 #define SHA256_BLOCK_LENGTH 16 31 32 /* number of bytes in the block */ 33 #define SHA256_DATA_SIZE 64 34 35 #define MAX_SG 12 36 37 struct sg_entry { 38 #if defined(NXP_SEC_LE) 39 uint32_t addr_lo; /* Memory Address - lo */ 40 uint32_t addr_hi; /* Memory Address of start of buffer - hi */ 41 #else 42 uint32_t addr_hi; /* Memory Address of start of buffer - hi */ 43 uint32_t addr_lo; /* Memory Address - lo */ 44 #endif 45 46 uint32_t len_flag; /* Length of the data in the frame */ 47 #define SG_ENTRY_LENGTH_MASK 0x3FFFFFFF 48 #define SG_ENTRY_EXTENSION_BIT 0x80000000 49 #define SG_ENTRY_FINAL_BIT 0x40000000 50 uint32_t bpid_offset; 51 #define SG_ENTRY_BPID_MASK 0x00FF0000 52 #define SG_ENTRY_BPID_SHIFT 16 53 #define SG_ENTRY_OFFSET_MASK 0x00001FFF 54 #define SG_ENTRY_OFFSET_SHIFT 0 55 }; 56 57 /* 58 * SHA256-256 context 59 * contain the following fields 60 * State 61 * count low 62 * count high 63 * block data buffer 64 * index to the buffer 65 */ 66 struct hash_ctx { 67 struct sg_entry sg_tbl[MAX_SG]; 68 uint32_t hash_desc[64]; 69 uint8_t hash[SHA256_DIGEST_SIZE]; 70 uint32_t sg_num; 71 uint32_t len; 72 uint8_t *data; 73 enum hash_algo algo; 74 bool active; 75 }; 76 77 int hash_init(enum hash_algo algo, void **ctx); 78 int hash_update(enum hash_algo algo, void *context, void *data_ptr, 79 unsigned int data_len); 80 int hash_final(enum hash_algo algo, void *context, void *hash_ptr, 81 unsigned int hash_len); 82 83 #endif 84