aboutsummaryrefslogtreecommitdiff
path: root/C/Sha512.h
blob: 1f3a4d1e4b890281082b18f8adde9aa85c01f802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Sha512.h -- SHA-512 Hash
: Igor Pavlov : Public domain */

#ifndef ZIP7_INC_SHA512_H
#define ZIP7_INC_SHA512_H

#include "7zTypes.h"

EXTERN_C_BEGIN

#define SHA512_NUM_BLOCK_WORDS  16
#define SHA512_NUM_DIGEST_WORDS  8

#define SHA512_BLOCK_SIZE   (SHA512_NUM_BLOCK_WORDS * 8)
#define SHA512_DIGEST_SIZE  (SHA512_NUM_DIGEST_WORDS * 8)
#define SHA512_224_DIGEST_SIZE  (224 / 8)
#define SHA512_256_DIGEST_SIZE  (256 / 8)
#define SHA512_384_DIGEST_SIZE  (384 / 8)

typedef void (Z7_FASTCALL *SHA512_FUNC_UPDATE_BLOCKS)(UInt64 state[8], const Byte *data, size_t numBlocks);

/*
  if (the system supports different SHA512 code implementations)
  {
    (CSha512::func_UpdateBlocks) will be used
    (CSha512::func_UpdateBlocks) can be set by
       Sha512_Init()        - to default (fastest)
       Sha512_SetFunction() - to any algo
  }
  else
  {
    (CSha512::func_UpdateBlocks) is ignored.
  }
*/

typedef struct
{
  union
  {
    struct
    {
      SHA512_FUNC_UPDATE_BLOCKS func_UpdateBlocks;
      UInt64 count;
    } vars;
    UInt64 _pad_64bit[8];
    void *_pad_align_ptr[2];
  } v;
  UInt64 state[SHA512_NUM_DIGEST_WORDS];
  
  Byte buffer[SHA512_BLOCK_SIZE];
} CSha512;


#define SHA512_ALGO_DEFAULT 0
#define SHA512_ALGO_SW      1
#define SHA512_ALGO_HW      2

/*
Sha512_SetFunction()
return:
  0 - (algo) value is not supported, and func_UpdateBlocks was not changed
  1 - func_UpdateBlocks was set according (algo) value.
*/

BoolInt Sha512_SetFunction(CSha512 *p, unsigned algo);
// we support only these (digestSize) values: 224/8, 256/8, 384/8, 512/8
void Sha512_InitState(CSha512 *p, unsigned digestSize);
void Sha512_Init(CSha512 *p, unsigned digestSize);
void Sha512_Update(CSha512 *p, const Byte *data, size_t size);
void Sha512_Final(CSha512 *p, Byte *digest, unsigned digestSize);




// void Z7_FASTCALL Sha512_UpdateBlocks(UInt64 state[8], const Byte *data, size_t numBlocks);

/*
call Sha512Prepare() once at program start.
It prepares all supported implementations, and detects the fastest implementation.
*/

void Sha512Prepare(void);

EXTERN_C_END

#endif