1370b324cSopenharmony_ci/* Sha256.h -- SHA-256 Hash
2370b324cSopenharmony_ci2023-04-02 : Igor Pavlov : Public domain */
3370b324cSopenharmony_ci
4370b324cSopenharmony_ci#ifndef ZIP7_INC_SHA256_H
5370b324cSopenharmony_ci#define ZIP7_INC_SHA256_H
6370b324cSopenharmony_ci
7370b324cSopenharmony_ci#include "7zTypes.h"
8370b324cSopenharmony_ci
9370b324cSopenharmony_ciEXTERN_C_BEGIN
10370b324cSopenharmony_ci
11370b324cSopenharmony_ci#define SHA256_NUM_BLOCK_WORDS  16
12370b324cSopenharmony_ci#define SHA256_NUM_DIGEST_WORDS  8
13370b324cSopenharmony_ci
14370b324cSopenharmony_ci#define SHA256_BLOCK_SIZE   (SHA256_NUM_BLOCK_WORDS * 4)
15370b324cSopenharmony_ci#define SHA256_DIGEST_SIZE  (SHA256_NUM_DIGEST_WORDS * 4)
16370b324cSopenharmony_ci
17370b324cSopenharmony_citypedef void (Z7_FASTCALL *SHA256_FUNC_UPDATE_BLOCKS)(UInt32 state[8], const Byte *data, size_t numBlocks);
18370b324cSopenharmony_ci
19370b324cSopenharmony_ci/*
20370b324cSopenharmony_ci  if (the system supports different SHA256 code implementations)
21370b324cSopenharmony_ci  {
22370b324cSopenharmony_ci    (CSha256::func_UpdateBlocks) will be used
23370b324cSopenharmony_ci    (CSha256::func_UpdateBlocks) can be set by
24370b324cSopenharmony_ci       Sha256_Init()        - to default (fastest)
25370b324cSopenharmony_ci       Sha256_SetFunction() - to any algo
26370b324cSopenharmony_ci  }
27370b324cSopenharmony_ci  else
28370b324cSopenharmony_ci  {
29370b324cSopenharmony_ci    (CSha256::func_UpdateBlocks) is ignored.
30370b324cSopenharmony_ci  }
31370b324cSopenharmony_ci*/
32370b324cSopenharmony_ci
33370b324cSopenharmony_citypedef struct
34370b324cSopenharmony_ci{
35370b324cSopenharmony_ci  SHA256_FUNC_UPDATE_BLOCKS func_UpdateBlocks;
36370b324cSopenharmony_ci  UInt64 count;
37370b324cSopenharmony_ci  UInt64 _pad_2[2];
38370b324cSopenharmony_ci  UInt32 state[SHA256_NUM_DIGEST_WORDS];
39370b324cSopenharmony_ci
40370b324cSopenharmony_ci  Byte buffer[SHA256_BLOCK_SIZE];
41370b324cSopenharmony_ci} CSha256;
42370b324cSopenharmony_ci
43370b324cSopenharmony_ci
44370b324cSopenharmony_ci#define SHA256_ALGO_DEFAULT 0
45370b324cSopenharmony_ci#define SHA256_ALGO_SW      1
46370b324cSopenharmony_ci#define SHA256_ALGO_HW      2
47370b324cSopenharmony_ci
48370b324cSopenharmony_ci/*
49370b324cSopenharmony_ciSha256_SetFunction()
50370b324cSopenharmony_cireturn:
51370b324cSopenharmony_ci  0 - (algo) value is not supported, and func_UpdateBlocks was not changed
52370b324cSopenharmony_ci  1 - func_UpdateBlocks was set according (algo) value.
53370b324cSopenharmony_ci*/
54370b324cSopenharmony_ci
55370b324cSopenharmony_ciBoolInt Sha256_SetFunction(CSha256 *p, unsigned algo);
56370b324cSopenharmony_ci
57370b324cSopenharmony_civoid Sha256_InitState(CSha256 *p);
58370b324cSopenharmony_civoid Sha256_Init(CSha256 *p);
59370b324cSopenharmony_civoid Sha256_Update(CSha256 *p, const Byte *data, size_t size);
60370b324cSopenharmony_civoid Sha256_Final(CSha256 *p, Byte *digest);
61370b324cSopenharmony_ci
62370b324cSopenharmony_ci
63370b324cSopenharmony_ci
64370b324cSopenharmony_ci
65370b324cSopenharmony_ci// void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks);
66370b324cSopenharmony_ci
67370b324cSopenharmony_ci/*
68370b324cSopenharmony_cicall Sha256Prepare() once at program start.
69370b324cSopenharmony_ciIt prepares all supported implementations, and detects the fastest implementation.
70370b324cSopenharmony_ci*/
71370b324cSopenharmony_ci
72370b324cSopenharmony_civoid Sha256Prepare(void);
73370b324cSopenharmony_ci
74370b324cSopenharmony_ciEXTERN_C_END
75370b324cSopenharmony_ci
76370b324cSopenharmony_ci#endif
77