aboutsummaryrefslogtreecommitdiff
path: root/ext/ipp/sources/ippcp/src/pcphash.h
blob: a4650ecda65966880ee20affd512f0cdd11753b9 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*############################################################################
  # Copyright 1999-2018 Intel Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ############################################################################*/

/* 
// 
//  Purpose:
//     Cryptography Primitive.
//     Security Hash Standard
//     Internal Definitions and Internal Functions Prototypes
// 
// 
*/

#if !defined(_PCP_HASH_H)
#define _PCP_HASH_H


/* messge block size */
#define MBS_SHA1     (64)           /* SHA1 message block size (bytes) */
#define MBS_SHA256   (64)           /* SHA256 and SHA224               */
#define MBS_SHA224   (64)           /* SHA224                          */
#define MBS_SHA512   (128)          /* SHA512 and SHA384               */
#define MBS_SHA384   (128)          /* SHA384                          */
#define MBS_MD5      (64)           /* MD5                             */
#define MBS_SM3      (64)           /* SM3                             */
#define MBS_HASH_MAX (MBS_SHA512)   /* max message block size (bytes)  */

#define MAX_HASH_SIZE (IPP_SHA512_DIGEST_BITSIZE/8)   /* hash of the max len (bytes) */

/* size of processed message length representation (bytes) */
#define MLR_SHA1     (sizeof(Ipp64u))
#define MLR_SHA256   (sizeof(Ipp64u))
#define MLR_SHA224   (sizeof(Ipp64u))
#define MLR_SHA512   (sizeof(Ipp64u)*2)
#define MLR_SHA384   (sizeof(Ipp64u)*2)
#define MLR_MD5      (sizeof(Ipp64u))
#define MLR_SM3      (sizeof(Ipp64u))

/* hold some old definition for a purpose */
typedef Ipp32u DigestSHA1[5];   /* SHA1 digest   */
typedef Ipp32u DigestSHA224[7]; /* SHA224 digest */
typedef Ipp32u DigestSHA256[8]; /* SHA256 digest */
typedef Ipp64u DigestSHA384[6]; /* SHA384 digest */
typedef Ipp64u DigestSHA512[8]; /* SHA512 digest */
typedef Ipp32u DigestMD5[4];    /* MD5 digest */
typedef Ipp32u DigestSM3[8];    /* SM3 digest */

#define HASH_ALIGNMENT     ((int)(sizeof(void*)))
#define   SHA1_ALIGNMENT   HASH_ALIGNMENT
#define SHA224_ALIGNMENT   HASH_ALIGNMENT
#define SHA256_ALIGNMENT   HASH_ALIGNMENT
#define SHA384_ALIGNMENT   HASH_ALIGNMENT
#define SHA512_ALIGNMENT   HASH_ALIGNMENT
#define    MD5_ALIGNMENT   HASH_ALIGNMENT
#define    SM3_ALIGNMENT   HASH_ALIGNMENT


struct _cpSHA1 {
   IppCtxId    idCtx;      /* SHA1 identifier         */
   int         msgBuffIdx; /* buffer entry            */
   Ipp64u      msgLenLo;   /* message length (bytes)  */
   Ipp8u       msgBuffer[MBS_SHA1]; /* buffer         */
   DigestSHA1  msgHash;    /* intermediate hash       */
};

struct _cpSHA256 {
   IppCtxId     idCtx;        /* SHA224 identifier    */
   int          msgBuffIdx;   /* buffer entry         */
   Ipp64u       msgLenLo;     /* message length       */
   Ipp8u        msgBuffer[MBS_SHA256]; /* buffer      */
   DigestSHA256 msgHash;      /* intermediate hash    */
};

struct _cpSHA512 {
   IppCtxId     idCtx;        /* SHA384 identifier    */
   int          msgBuffIdx;   /* buffer entry         */
   Ipp64u       msgLenLo;     /* message length       */
   Ipp64u       msgLenHi;     /* message length       */
   Ipp8u        msgBuffer[MBS_SHA512]; /* buffer      */
   DigestSHA512 msgHash;      /* intermediate hash    */
};

struct _cpMD5 {
   IppCtxId     idCtx;        /* MD5 identifier       */
   int          msgBuffIdx;   /* buffer entry         */
   Ipp64u       msgLenLo;     /* message length       */
   Ipp8u        msgBuffer[MBS_MD5]; /* buffer         */
   DigestMD5    msgHash;      /* intermediate hash    */
};

struct _cpSM3 {
   IppCtxId     idCtx;        /* SM3    identifier    */
   int          msgBuffIdx;   /* buffer entry         */
   Ipp64u       msgLenLo;     /* message length       */
   Ipp8u        msgBuffer[MBS_SM3]; /* buffer         */
   DigestSM3    msgHash;      /* intermediate hash    */
};


/* hash alg attributes */
typedef struct _cpHashAttr {
   int         ivSize;        /* attr: length (bytes) of initial value cpHashIV */
   int         hashSize;      /* attr: length (bytes) of hash */
   int         msgBlkSize;    /* attr: length (bytes) of message block */
   int         msgLenRepSize; /* attr: length (bytes) in representation of processed message length */
   Ipp64u      msgLenMax[2];  /* attr: max message length (bytes) (low high) */
} cpHashAttr;

/* hash value */
typedef Ipp64u cpHash[IPP_SHA512_DIGEST_BITSIZE/BITSIZE(Ipp64u)]; /* hash value */

/* hash update function */
typedef void (*cpHashProc)(void* pHash, const Ipp8u* pMsg, int msgLen, const void* pParam);

/* generalized hash context */
struct _cpHashCtx {
   IppCtxId    idCtx;                     /* hash identifier   */
   IppHashAlgId   algID;                  /* hash algorithm ID */
   Ipp64u      msgLenLo;                  /* processed message:*/
   Ipp64u      msgLenHi;                  /*           length  */
   cpHashProc  hashProc;                  /* hash update func  */
   const void* pParam;                    /* hashProc's params */
   cpHash      msgHash;                   /* intermadiate hash */
   int         msgBuffIdx;                /* buffer entry      */
   Ipp8u       msgBuffer[MBS_HASH_MAX];   /* buffer            */
};

/* accessors */
#define HASH_CTX_ID(stt)   ((stt)->idCtx)
#define HASH_ALG_ID(stt)   ((stt)->algID)
#define HASH_LENLO(stt)    ((stt)->msgLenLo)
#define HASH_LENHI(stt)    ((stt)->msgLenHi)
#define HASH_FUNC(stt)     ((stt)->hashProc)
#define HASH_FUNC_PAR(stt) ((stt)->pParam)
#define HASH_VALUE(stt)    ((stt)->msgHash)
#define HAHS_BUFFIDX(stt)  ((stt)->msgBuffIdx)
#define HASH_BUFF(stt)     ((stt)->msgBuffer)
#define HASH_VALID_ID(pCtx)   (HASH_CTX_ID((pCtx))==idCtxHash)


/* initial hash values */
extern const Ipp32u SHA1_IV[];
extern const Ipp32u SHA256_IV[];
extern const Ipp32u SHA224_IV[];
extern const Ipp64u SHA512_IV[];
extern const Ipp64u SHA384_IV[];
extern const Ipp32u MD5_IV[];
extern const Ipp32u SM3_IV[];
extern const Ipp64u SHA512_224_IV[];
extern const Ipp64u SHA512_256_IV[];

/* hash alg additive constants */
extern __ALIGN16 const Ipp32u SHA1_cnt[];
extern __ALIGN16 const Ipp32u SHA256_cnt[];
extern __ALIGN16 const Ipp64u SHA512_cnt[];
extern __ALIGN16 const Ipp32u MD5_cnt[];
extern __ALIGN16 const Ipp32u SM3_cnt[];


/*  hash alg opt argument */
extern const void* cpHashProcFuncOpt[];

/* enabled hash alg */
extern const IppHashAlgId cpEnabledHashAlgID[];

/* hash alg IV (init value) */
extern const Ipp8u* cpHashIV[];

/* hash alg attribute DB */
extern const cpHashAttr cpHashAlgAttr[];

/* IV size helper */
__INLINE int cpHashIvSize(IppHashAlgId algID)
{ return cpHashAlgAttr[algID].ivSize; }

/* hash size helper */
__INLINE int cpHashSize(IppHashAlgId algID)
{ return cpHashAlgAttr[algID].hashSize; }

/* message block size helper */
__INLINE int cpHashMBS(IppHashAlgId algID)
{ return cpHashAlgAttr[algID].msgBlkSize; }

/* maps algID into enabled IppHashAlgId value */
__INLINE IppHashAlgId cpValidHashAlg(IppHashAlgId algID)
{
   /* maps algID into the valid range */
   algID = (((int)ippHashAlg_Unknown < (int)algID) && ((int)algID < (int)ippHashAlg_MaxNo))? algID : ippHashAlg_Unknown;
   return cpEnabledHashAlgID[algID];
}


/* processing functions */
#define UpdateSHA1 OWNAPI(UpdateSHA1)
void UpdateSHA1  (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
#define UpdateSHA256 OWNAPI(UpdateSHA256)
void UpdateSHA256(void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
#define UpdateSHA512 OWNAPI(UpdateSHA512)
void UpdateSHA512(void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
#define UpdateMD5 OWNAPI(UpdateMD5)
void UpdateMD5   (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
#define UpdateSM3 OWNAPI(UpdateSM3)
void UpdateSM3   (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);

#if (_SHA_NI_ENABLING_ == _FEATURE_TICKTOCK_) || (_SHA_NI_ENABLING_ == _FEATURE_ON_)
#define UpdateSHA1ni OWNAPI(UpdateSHA1ni)
void UpdateSHA1ni  (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
#define UpdateSHA256ni OWNAPI(UpdateSHA256ni)
void UpdateSHA256ni(void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
#endif

/* general methods */
#define cpReInitHash OWNAPI(cpReInitHash)
int cpReInitHash(IppsHashState* pCtx, IppHashAlgId algID);

#endif /* _PCP_HASH_H */