1 /******************************************************************************* 2 * Copyright 2014-2018 Intel Corporation 3 * All Rights Reserved. 4 * 5 * If this software was obtained under the Intel Simplified Software License, 6 * the following terms apply: 7 * 8 * The source code, information and material ("Material") contained herein is 9 * owned by Intel Corporation or its suppliers or licensors, and title to such 10 * Material remains with Intel Corporation or its suppliers or licensors. The 11 * Material contains proprietary information of Intel or its suppliers and 12 * licensors. The Material is protected by worldwide copyright laws and treaty 13 * provisions. No part of the Material may be used, copied, reproduced, 14 * modified, published, uploaded, posted, transmitted, distributed or disclosed 15 * in any way without Intel's prior express written permission. No license under 16 * any patent, copyright or other intellectual property rights in the Material 17 * is granted to or conferred upon you, either expressly, by implication, 18 * inducement, estoppel or otherwise. Any license under such intellectual 19 * property rights must be express and approved by Intel in writing. 20 * 21 * Unless otherwise agreed by Intel in writing, you may not remove or alter this 22 * notice or any other notice embedded in Materials by Intel or Intel's 23 * suppliers or licensors in any way. 24 * 25 * 26 * If this software was obtained under the Apache License, Version 2.0 (the 27 * "License"), the following terms apply: 28 * 29 * You may not use this file except in compliance with the License. You may 30 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 31 * 32 * 33 * Unless required by applicable law or agreed to in writing, software 34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 36 * 37 * See the License for the specific language governing permissions and 38 * limitations under the License. 39 *******************************************************************************/ 40 41 /* 42 // 43 // Purpose: 44 // Cryptography Primitive. 45 // Security Hash Standard 46 // Internal Definitions and Internal Functions Prototypes 47 // 48 // 49 */ 50 51 #if !defined(_PCP_HASH_H) 52 #define _PCP_HASH_H 53 54 55 /* messge block size */ 56 #define MBS_SHA1 (64) /* SHA1 message block size (bytes) */ 57 #define MBS_SHA256 (64) /* SHA256 and SHA224 */ 58 #define MBS_SHA224 (64) /* SHA224 */ 59 #define MBS_SHA512 (128) /* SHA512 and SHA384 */ 60 #define MBS_SHA384 (128) /* SHA384 */ 61 #define MBS_MD5 (64) /* MD5 */ 62 #define MBS_SM3 (64) /* SM3 */ 63 #define MBS_HASH_MAX (MBS_SHA512) /* max message block size (bytes) */ 64 65 #define MAX_HASH_SIZE (IPP_SHA512_DIGEST_BITSIZE/8) /* hash of the max len (bytes) */ 66 67 /* size of processed message length representation (bytes) */ 68 #define MLR_SHA1 (sizeof(Ipp64u)) 69 #define MLR_SHA256 (sizeof(Ipp64u)) 70 #define MLR_SHA224 (sizeof(Ipp64u)) 71 #define MLR_SHA512 (sizeof(Ipp64u)*2) 72 #define MLR_SHA384 (sizeof(Ipp64u)*2) 73 #define MLR_MD5 (sizeof(Ipp64u)) 74 #define MLR_SM3 (sizeof(Ipp64u)) 75 76 /* hold some old definition for a purpose */ 77 typedef Ipp32u DigestSHA1[5]; /* SHA1 digest */ 78 typedef Ipp32u DigestSHA224[7]; /* SHA224 digest */ 79 typedef Ipp32u DigestSHA256[8]; /* SHA256 digest */ 80 typedef Ipp64u DigestSHA384[6]; /* SHA384 digest */ 81 typedef Ipp64u DigestSHA512[8]; /* SHA512 digest */ 82 typedef Ipp32u DigestMD5[4]; /* MD5 digest */ 83 typedef Ipp32u DigestSM3[8]; /* SM3 digest */ 84 85 #define HASH_ALIGNMENT ((int)(sizeof(void*))) 86 #define SHA1_ALIGNMENT HASH_ALIGNMENT 87 #define SHA224_ALIGNMENT HASH_ALIGNMENT 88 #define SHA256_ALIGNMENT HASH_ALIGNMENT 89 #define SHA384_ALIGNMENT HASH_ALIGNMENT 90 #define SHA512_ALIGNMENT HASH_ALIGNMENT 91 #define MD5_ALIGNMENT HASH_ALIGNMENT 92 #define SM3_ALIGNMENT HASH_ALIGNMENT 93 94 95 struct _cpSHA1 { 96 IppCtxId idCtx; /* SHA1 identifier */ 97 int msgBuffIdx; /* buffer entry */ 98 Ipp64u msgLenLo; /* message length (bytes) */ 99 Ipp8u msgBuffer[MBS_SHA1]; /* buffer */ 100 DigestSHA1 msgHash; /* intermediate hash */ 101 }; 102 103 struct _cpSHA256 { 104 IppCtxId idCtx; /* SHA224 identifier */ 105 int msgBuffIdx; /* buffer entry */ 106 Ipp64u msgLenLo; /* message length */ 107 Ipp8u msgBuffer[MBS_SHA256]; /* buffer */ 108 DigestSHA256 msgHash; /* intermediate hash */ 109 }; 110 111 struct _cpSHA512 { 112 IppCtxId idCtx; /* SHA384 identifier */ 113 int msgBuffIdx; /* buffer entry */ 114 Ipp64u msgLenLo; /* message length */ 115 Ipp64u msgLenHi; /* message length */ 116 Ipp8u msgBuffer[MBS_SHA512]; /* buffer */ 117 DigestSHA512 msgHash; /* intermediate hash */ 118 }; 119 120 struct _cpMD5 { 121 IppCtxId idCtx; /* MD5 identifier */ 122 int msgBuffIdx; /* buffer entry */ 123 Ipp64u msgLenLo; /* message length */ 124 Ipp8u msgBuffer[MBS_MD5]; /* buffer */ 125 DigestMD5 msgHash; /* intermediate hash */ 126 }; 127 128 struct _cpSM3 { 129 IppCtxId idCtx; /* SM3 identifier */ 130 int msgBuffIdx; /* buffer entry */ 131 Ipp64u msgLenLo; /* message length */ 132 Ipp8u msgBuffer[MBS_SM3]; /* buffer */ 133 DigestSM3 msgHash; /* intermediate hash */ 134 }; 135 136 137 /* hash alg attributes */ 138 typedef struct _cpHashAttr { 139 int ivSize; /* attr: length (bytes) of initial value cpHashIV */ 140 int hashSize; /* attr: length (bytes) of hash */ 141 int msgBlkSize; /* attr: length (bytes) of message block */ 142 int msgLenRepSize; /* attr: length (bytes) in representation of processed message length */ 143 Ipp64u msgLenMax[2]; /* attr: max message length (bytes) (low high) */ 144 } cpHashAttr; 145 146 /* hash value */ 147 typedef Ipp64u cpHash[IPP_SHA512_DIGEST_BITSIZE/BITSIZE(Ipp64u)]; /* hash value */ 148 149 /* hash update function */ 150 typedef void (*cpHashProc)(void* pHash, const Ipp8u* pMsg, int msgLen, const void* pParam); 151 152 /* generalized hash context */ 153 struct _cpHashCtx { 154 IppCtxId idCtx; /* hash identifier */ 155 IppHashAlgId algID; /* hash algorithm ID */ 156 Ipp64u msgLenLo; /* processed message:*/ 157 Ipp64u msgLenHi; /* length */ 158 cpHashProc hashProc; /* hash update func */ 159 const void* pParam; /* hashProc's params */ 160 cpHash msgHash; /* intermadiate hash */ 161 int msgBuffIdx; /* buffer entry */ 162 Ipp8u msgBuffer[MBS_HASH_MAX]; /* buffer */ 163 }; 164 165 /* accessors */ 166 #define HASH_CTX_ID(stt) ((stt)->idCtx) 167 #define HASH_ALG_ID(stt) ((stt)->algID) 168 #define HASH_LENLO(stt) ((stt)->msgLenLo) 169 #define HASH_LENHI(stt) ((stt)->msgLenHi) 170 #define HASH_FUNC(stt) ((stt)->hashProc) 171 #define HASH_FUNC_PAR(stt) ((stt)->pParam) 172 #define HASH_VALUE(stt) ((stt)->msgHash) 173 #define HAHS_BUFFIDX(stt) ((stt)->msgBuffIdx) 174 #define HASH_BUFF(stt) ((stt)->msgBuffer) 175 #define HASH_VALID_ID(pCtx) (HASH_CTX_ID((pCtx))==idCtxHash) 176 177 178 /* initial hash values */ 179 extern const Ipp32u SHA1_IV[]; 180 extern const Ipp32u SHA256_IV[]; 181 extern const Ipp32u SHA224_IV[]; 182 extern const Ipp64u SHA512_IV[]; 183 extern const Ipp64u SHA384_IV[]; 184 extern const Ipp32u MD5_IV[]; 185 extern const Ipp32u SM3_IV[]; 186 extern const Ipp64u SHA512_224_IV[]; 187 extern const Ipp64u SHA512_256_IV[]; 188 189 /* hash alg additive constants */ 190 extern __ALIGN16 const Ipp32u SHA1_cnt[]; 191 extern __ALIGN16 const Ipp32u SHA256_cnt[]; 192 extern __ALIGN16 const Ipp64u SHA512_cnt[]; 193 extern __ALIGN16 const Ipp32u MD5_cnt[]; 194 extern __ALIGN16 const Ipp32u SM3_cnt[]; 195 196 /* hash alg opt argument */ 197 extern const void* cpHashProcFuncOpt[]; 198 199 /* enabled hash alg */ 200 extern const IppHashAlgId cpEnabledHashAlgID[]; 201 202 /* hash alg IV (init value) */ 203 extern const Ipp8u* cpHashIV[]; 204 205 /* hash alg attribute DB */ 206 extern const cpHashAttr cpHashAlgAttr[]; 207 208 /* IV size helper */ 209 __INLINE int cpHashIvSize(IppHashAlgId algID) 210 { return cpHashAlgAttr[algID].ivSize; } 211 212 /* hash size helper */ 213 __INLINE int cpHashSize(IppHashAlgId algID) 214 { return cpHashAlgAttr[algID].hashSize; } 215 216 /* message block size helper */ 217 __INLINE int cpHashMBS(IppHashAlgId algID) 218 { return cpHashAlgAttr[algID].msgBlkSize; } 219 220 /* maps algID into enabled IppHashAlgId value */ 221 __INLINE IppHashAlgId cpValidHashAlg(IppHashAlgId algID) 222 { 223 /* maps algID into the valid range */ 224 algID = (((int)ippHashAlg_Unknown < (int)algID) && ((int)algID < (int)ippHashAlg_MaxNo))? algID : ippHashAlg_Unknown; 225 return cpEnabledHashAlgID[algID]; 226 } 227 228 /* common functions */ 229 #define cpComputeDigest OWNAPI(cpComputeDigest) 230 void cpComputeDigest(Ipp8u* pHashTag, int hashTagLen, const IppsHashState* pCtx); 231 232 /* processing functions */ 233 #define UpdateSHA1 OWNAPI(UpdateSHA1) 234 void UpdateSHA1 (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam); 235 #define UpdateSHA256 OWNAPI(UpdateSHA256) 236 void UpdateSHA256(void* pHash, const Ipp8u* mblk, int mlen, const void* pParam); 237 #define UpdateSHA512 OWNAPI(UpdateSHA512) 238 void UpdateSHA512(void* pHash, const Ipp8u* mblk, int mlen, const void* pParam); 239 #define UpdateMD5 OWNAPI(UpdateMD5) 240 void UpdateMD5 (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam); 241 #define UpdateSM3 OWNAPI(UpdateSM3) 242 void UpdateSM3 (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam); 243 244 #if (_SHA_NI_ENABLING_ == _FEATURE_TICKTOCK_) || (_SHA_NI_ENABLING_ == _FEATURE_ON_) 245 #define UpdateSHA1ni OWNAPI(UpdateSHA1ni) 246 void UpdateSHA1ni (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam); 247 #define UpdateSHA256ni OWNAPI(UpdateSHA256ni) 248 void UpdateSHA256ni(void* pHash, const Ipp8u* mblk, int mlen, const void* pParam); 249 #endif 250 251 /* general methods */ 252 #define cpInitHash OWNAPI(cpInitHash) 253 int cpInitHash(IppsHashState* pCtx, IppHashAlgId algID); 254 #define cpReInitHash OWNAPI(cpReInitHash) 255 int cpReInitHash(IppsHashState* pCtx, IppHashAlgId algID); 256 257 #endif /* _PCP_HASH_H */ 258