blob: 6201d2321ad093f16058b4f0035ecf80e70924a4 [file] [log] [blame]
Pankaj Gupta95c7eee2020-12-09 14:02:39 +05301/*
Pankaj Gupta7834b462021-03-25 15:15:52 +05302 * Copyright 2017-2021 NXP
Pankaj Gupta95c7eee2020-12-09 14:02:39 +05303 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __HASH_H__
9#define __HASH_H__
10
11#include <stdbool.h>
Raghu Krishnamurthy5821a542024-09-24 07:11:29 -070012#include <common/sha_common_macros.h>
Pankaj Gupta95c7eee2020-12-09 14:02:39 +053013
14/* List of hash algorithms */
15enum hash_algo {
16 SHA1 = 0,
17 SHA256
18};
19
Pankaj Gupta95c7eee2020-12-09 14:02:39 +053020/*
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
37struct 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 */
66struct 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
77int hash_init(enum hash_algo algo, void **ctx);
78int hash_update(enum hash_algo algo, void *context, void *data_ptr,
79 unsigned int data_len);
80int hash_final(enum hash_algo algo, void *context, void *hash_ptr,
81 unsigned int hash_len);
82
83#endif