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