| 1 |
/************************************************************************* |
| 2 |
* Copyright (C) 2026 Intel Corporation |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*************************************************************************/ |
| 16 |
|
| 17 |
#include "owndefs.h" |
| 18 |
#include "owncp.h" |
| 19 |
#include "pcpbn.h" |
| 20 |
#include "hash/pcphash.h" |
| 21 |
#include "hash/pcphash_rmf.h" |
| 22 |
#include "drbg/pcphashdrbg.h" |
| 23 |
#include "pcptool.h" |
| 24 |
|
| 25 |
/* Returns a mask of BNU_CHUNK_T type based on the condition (nsA < nsB): |
| 26 |
* - 0xFFFFFFFF, if the condition is true, |
| 27 |
* - 0x00000000 otherwise. |
| 28 |
* The masks are used to avoid branches when one operand is shorter than the other. */ |
| 29 |
IPPCP_INLINE BNU_CHUNK_T maskFromCond(cpSize nsA, cpSize nsB) |
| 30 |
{ |
| 31 |
return (BNU_CHUNK_T)(-(BNS_CHUNK_T)(nsA < nsB)); |
| 32 |
} |
| 33 |
|
| 34 |
/* to not lose carry use this function if nsA > nsB */ |
| 35 |
IPPCP_INLINE void cpAddWithCarry_BNU(BNU_CHUNK_T* pR, |
| 36 |
const BNU_CHUNK_T* pA, |
| 37 |
cpSize nsA, |
| 38 |
const BNU_CHUNK_T* pB, |
| 39 |
cpSize nsB) |
| 40 |
{ |
| 41 |
cpSize maxLen = nsA; |
| 42 |
BNU_CHUNK_T carry = 0; |
| 43 |
|
| 44 |
for (int i = 0; i < maxLen; i++) { |
| 45 |
BNU_CHUNK_T m1 = maskFromCond(i, nsA); |
| 46 |
BNU_CHUNK_T m2 = maskFromCond(i, nsB); |
| 47 |
|
| 48 |
BNU_CHUNK_T a = pA[i] & m1; |
| 49 |
BNU_CHUNK_T b = pB[i] & m2; |
| 50 |
|
| 51 |
ADD_ABC(carry, pR[i], a, b, carry); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static void cpEndiannessReverseInplace(Ipp8u* arr, const int len) |
| 56 |
{ |
| 57 |
#define SWAPXOR(x, y) \ |
| 58 |
(x) ^= (y); \ |
| 59 |
(y) ^= (x); \ |
| 60 |
(x) ^= (y); |
| 61 |
|
| 62 |
for (int i = 0; i < len / 2; ++i) { |
| 63 |
SWAPXOR(arr[i], arr[len - 1 - i]) |
| 64 |
} |
| 65 |
return; |
| 66 |
#undef SWAPXOR |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
* The Hashgen Process as listed in SP800-90Ar1 10.1.1.4. |
| 71 |
* Generates pseudorandom numbers and put them into resulted array |
| 72 |
* |
| 73 |
* Input parameters: |
| 74 |
* outputLen32 the number of 32-bit dwords to be returned |
| 75 |
* pDrbg pointer to the DRBG state |
| 76 |
* |
| 77 |
* Output parameters: |
| 78 |
* output generated 32-bit numbers to be returned to the cpHashDRBG_Gen function |
| 79 |
* |
| 80 |
* NIST.SP.800-90Ar1. Section 10.1.1.4 Generating Pseudorandom Bits Using Hash_DRBG |
| 81 |
* |
| 82 |
* Hashgen Process: |
| 83 |
* data = V |
| 84 |
* w = NULL |
| 85 |
* for (i = 1 to m) { |
| 86 |
* W = W || Hash(data) |
| 87 |
* data = (data + 1) mod (2^seedlen) |
| 88 |
* } |
| 89 |
* out = leftmost(W, requested_no_of_bits) |
| 90 |
* Return (returned_bits) |
| 91 |
*/ |
| 92 |
|
| 93 |
static IppStatus cpHashGen(Ipp32u* output, int outputLen32, IppsHashDRBGState* pDrbg) |
| 94 |
{ |
| 95 |
IppStatus sts = ippStsNoErr; |
| 96 |
|
| 97 |
int seedBytesExtLen = BITS2WORD8_SIZE(HASH_DRBG_SEEDBITS_LEN_EXT(pDrbg)); |
| 98 |
|
| 99 |
Ipp32u* pW = output; |
| 100 |
Ipp8u* pTempBuf = pDrbg->tempBuf; |
| 101 |
BNU_CHUNK_T* pBN_data = (BNU_CHUNK_T*)pDrbg->tempBuf; |
| 102 |
|
| 103 |
COPY_BNU(pDrbg->tempBuf, pDrbg->V, seedBytesExtLen); |
| 104 |
|
| 105 |
int outputHash_wLen = pDrbg->pHashMethod->hashLen / 4; // WORD8 -> WORD32 |
| 106 |
|
| 107 |
int residual = outputLen32; |
| 108 |
while (residual > 0) { |
| 109 |
/* w = Hash (data) */ |
| 110 |
sts = ippsHashInit_rmf(pDrbg->hashState, pDrbg->pHashMethod); |
| 111 |
sts |= ippsHashUpdate_rmf(1 + pTempBuf, |
| 112 |
BITS2WORD8_SIZE(HASH_DRBG_SEEDBITS_LEN(pDrbg)), |
| 113 |
pDrbg->hashState); |
| 114 |
sts |= ippsHashFinal_rmf(pDrbg->hashOutputBuf, pDrbg->hashState); |
| 115 |
if (sts != ippStsNoErr) { |
| 116 |
/* zeroize tempBuf */ |
| 117 |
PurgeBlock((void*)pDrbg->tempBuf, seedBytesExtLen); |
| 118 |
return ippStsHashOperationErr; |
| 119 |
} |
| 120 |
|
| 121 |
/* W = W || w */ |
| 122 |
COPY_BNU(pW, (Ipp32u*)pDrbg->hashOutputBuf, IPP_MIN(residual, outputHash_wLen)); |
| 123 |
|
| 124 |
pW += outputHash_wLen; |
| 125 |
residual -= outputHash_wLen; |
| 126 |
|
| 127 |
if (residual > 0) { |
| 128 |
/* data = (data + 1) mod 2^seedlen */ |
| 129 |
/* Little Endian to Big Endian */ |
| 130 |
cpEndiannessReverseInplace(pTempBuf, seedBytesExtLen); |
| 131 |
cpInc_BNU(pBN_data, pBN_data, BITS_BNU_CHUNK(HASH_DRBG_SEEDBITS_LEN(pDrbg)), 1); |
| 132 |
/* Big Endian to Little Endian */ |
| 133 |
cpEndiannessReverseInplace(pTempBuf, seedBytesExtLen); |
| 134 |
/* applying (data mod 2^seed_bits_len) == pBN_data[seed_bytes_len-1] &= 0xffff */ |
| 135 |
// zero out the zeroth element if single bites are added there due to overflow |
| 136 |
pTempBuf[0] = 0; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/* zeroize tempBuf since it's no longer needed */ |
| 141 |
PurgeBlock((void*)pDrbg->tempBuf, seedBytesExtLen); |
| 142 |
|
| 143 |
return sts; |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* The Hash_DRBG_Generate Process as listed in SP800-90Ar1 10.1.1.4. |
| 148 |
* Hashes input data, updates V and returns the requested number of bytes |
| 149 |
* |
| 150 |
* Input parameters: |
| 151 |
* randLen32 the number of 32-bit dwords to be returned |
| 152 |
* addlInput pointer to the array containing additional input (optional) |
| 153 |
* addlInputBitsLen length of the addlInput in bits (may be zero) |
| 154 |
* pDrbg pointer to the DRBG state |
| 155 |
* |
| 156 |
* Output parameters: |
| 157 |
* pRand resulted array for 32-bit pseudorandom numbers |
| 158 |
* |
| 159 |
* NIST.SP.800-90Ar1. Section 10.1.1.4 Generating Pseudorandom Bits Using Hash_DRBG |
| 160 |
* |
| 161 |
* Hash_DRBG_Generate Process: |
| 162 |
* if (reseed_counter > reseed_interval) |
| 163 |
* return 1; // reseed required |
| 164 |
* if (additional_input != NULL) { |
| 165 |
* w = Hash (0x02 || V || additional_input) |
| 166 |
* V = (V + w) mod 2^seedlen |
| 167 |
* } |
| 168 |
* (returned_bits) = Hashgen (requested_number_of_bits, V) |
| 169 |
* H = Hash (0x03 || V) |
| 170 |
* V = (V + H + C + reseed_counter) mod 2^seedlen |
| 171 |
* reseed_counter = reseed_counter + 1 |
| 172 |
* Return (SUCCESS, returned_bits, V, C, reseed_counter) |
| 173 |
*/ |
| 174 |
|
| 175 |
IPP_OWN_DEFN(IppStatus, |
| 176 |
cpHashDRBG_Gen, |
| 177 |
(Ipp32u * pRand, |
| 178 |
cpSize randLen32, |
| 179 |
const int predictionResistanceRequest, |
| 180 |
const Ipp8u* addlInput, |
| 181 |
const int addlInputBitsLen, |
| 182 |
IppsHashDRBG_EntropyInputCtx* pEntrInputCtx, |
| 183 |
IppsHashDRBGState* pDrbg)) |
| 184 |
{ |
| 185 |
IppStatus sts = ippStsNoErr; |
| 186 |
|
| 187 |
int addlInputBitsLenTmp = addlInputBitsLen; |
| 188 |
|
| 189 |
/* A reseed is required if |
| 190 |
1) reseed_counter > MAX_RESEED_INTERVAL |
| 191 |
(NIST.SP.800-90Ar1 Section 9.3.2 "Reseeding at the End of the Seedlife"); |
| 192 |
2) prediction resistance is requested. */ |
| 193 |
if ((HASH_DRBG_RESEED_COUNTER(pDrbg) > MAX_RESEED_INTERVAL) || predictionResistanceRequest) { |
| 194 |
sts = ippsHashDRBG_Reseed(predictionResistanceRequest, |
| 195 |
addlInput, |
| 196 |
addlInputBitsLenTmp, |
| 197 |
pEntrInputCtx, |
| 198 |
pDrbg); |
| 199 |
if (ippStsNoErr != sts) { |
| 200 |
return sts; |
| 201 |
} |
| 202 |
addlInput = NULL; |
| 203 |
addlInputBitsLenTmp = 0; |
| 204 |
} |
| 205 |
|
| 206 |
int seedBytesLen = BITS2WORD8_SIZE(HASH_DRBG_SEEDBITS_LEN(pDrbg)); |
| 207 |
int seedBytesExtLen = BITS2WORD8_SIZE(HASH_DRBG_SEEDBITS_LEN_EXT(pDrbg)); |
| 208 |
|
| 209 |
Ipp8u* pV = pDrbg->V; |
| 210 |
|
| 211 |
__ALIGN64 Ipp8u reseedCnt[8]; |
| 212 |
|
| 213 |
/* present unsigned long long in big-endian format */ |
| 214 |
reseedCnt[0] = (Ipp8u)((HASH_DRBG_RESEED_COUNTER(pDrbg) >> 56) & 0xFF); |
| 215 |
reseedCnt[1] = (Ipp8u)((HASH_DRBG_RESEED_COUNTER(pDrbg) >> 48) & 0xFF); |
| 216 |
reseedCnt[2] = (Ipp8u)((HASH_DRBG_RESEED_COUNTER(pDrbg) >> 40) & 0xFF); |
| 217 |
reseedCnt[3] = (Ipp8u)((HASH_DRBG_RESEED_COUNTER(pDrbg) >> 32) & 0xFF); |
| 218 |
reseedCnt[4] = (Ipp8u)((HASH_DRBG_RESEED_COUNTER(pDrbg) >> 24) & 0xFF); |
| 219 |
reseedCnt[5] = (Ipp8u)((HASH_DRBG_RESEED_COUNTER(pDrbg) >> 16) & 0xFF); |
| 220 |
reseedCnt[6] = (Ipp8u)((HASH_DRBG_RESEED_COUNTER(pDrbg) >> 8) & 0xFF); |
| 221 |
reseedCnt[7] = (Ipp8u)(HASH_DRBG_RESEED_COUNTER(pDrbg) & 0xFF); |
| 222 |
|
| 223 |
int outputHashBufLen = pDrbg->pHashMethod->hashLen; |
| 224 |
|
| 225 |
Ipp8u prefix; |
| 226 |
|
| 227 |
if (addlInputBitsLenTmp) { |
| 228 |
/* |
| 229 |
w = Hash (0x02 || V || addlInput) |
| 230 |
V = (V + w) mod 2^seedlen |
| 231 |
*/ |
| 232 |
prefix = 2; |
| 233 |
sts = ippsHashInit_rmf(pDrbg->hashState, pDrbg->pHashMethod); |
| 234 |
sts |= ippsHashUpdate_rmf(&prefix, sizeof(prefix), pDrbg->hashState); |
| 235 |
sts |= ippsHashUpdate_rmf(pV + 1, seedBytesLen, pDrbg->hashState); |
| 236 |
sts |= |
| 237 |
ippsHashUpdate_rmf(addlInput, BITS2WORD8_SIZE(addlInputBitsLenTmp), pDrbg->hashState); |
| 238 |
sts |= ippsHashFinal_rmf(pDrbg->hashOutputBuf, pDrbg->hashState) != ippStsNoErr; |
| 239 |
if (sts != ippStsNoErr) { |
| 240 |
return ippStsHashOperationErr; |
| 241 |
} |
| 242 |
|
| 243 |
BNU_CHUNK_T* pBN_V = (BNU_CHUNK_T*)pDrbg->V; |
| 244 |
|
| 245 |
/* V = V + w */ |
| 246 |
/* Little Endian to Big Endian */ |
| 247 |
cpEndiannessReverseInplace(pV, seedBytesExtLen); |
| 248 |
/* Little Endian to Big Endian */ |
| 249 |
cpEndiannessReverseInplace(pDrbg->hashOutputBuf, outputHashBufLen); |
| 250 |
cpAddWithCarry_BNU(pBN_V, |
| 251 |
pBN_V, |
| 252 |
BITS_BNU_CHUNK(HASH_DRBG_SEEDBITS_LEN(pDrbg)), |
| 253 |
(BNU_CHUNK_T*)pDrbg->hashOutputBuf, |
| 254 |
BITS_BNU_CHUNK(pDrbg->pHashMethod->hashLen * BYTESIZE)); |
| 255 |
/* Big Endian to Little Endian */ |
| 256 |
cpEndiannessReverseInplace(pV, seedBytesExtLen); |
| 257 |
|
| 258 |
/* V mod 2^seedlen */ |
| 259 |
pV[0] = 0; |
| 260 |
} |
| 261 |
/* Hashgen (requested_number_of_bits, V) */ |
| 262 |
sts = cpHashGen(pRand, randLen32, pDrbg); |
| 263 |
if (ippStsNoErr != sts) { |
| 264 |
return sts; |
| 265 |
} |
| 266 |
|
| 267 |
/* H = Hash (0x03 || V) */ |
| 268 |
prefix = 3; |
| 269 |
|
| 270 |
sts = ippsHashInit_rmf(pDrbg->hashState, pDrbg->pHashMethod); |
| 271 |
sts |= ippsHashUpdate_rmf(&prefix, sizeof(prefix), pDrbg->hashState); |
| 272 |
sts |= ippsHashUpdate_rmf(pV + 1, seedBytesLen, pDrbg->hashState); |
| 273 |
sts |= ippsHashFinal_rmf(pDrbg->hashOutputBuf, pDrbg->hashState); |
| 274 |
if (sts != ippStsNoErr) { |
| 275 |
return ippStsHashOperationErr; |
| 276 |
} |
| 277 |
|
| 278 |
/* V = (V + H + C + reseedCounter) mod 2^seedlen */ |
| 279 |
BNU_CHUNK_T* pBN_V = (BNU_CHUNK_T*)pDrbg->V; |
| 280 |
|
| 281 |
/* Little Endian to Big Endian */ |
| 282 |
cpEndiannessReverseInplace(pV, seedBytesExtLen); |
| 283 |
/* Little Endian to Big Endian */ |
| 284 |
cpEndiannessReverseInplace(pDrbg->hashOutputBuf, outputHashBufLen); |
| 285 |
/* V + H */ |
| 286 |
cpAddWithCarry_BNU(pBN_V, |
| 287 |
pBN_V, |
| 288 |
BITS_BNU_CHUNK(HASH_DRBG_SEEDBITS_LEN(pDrbg)), |
| 289 |
(BNU_CHUNK_T*)pDrbg->hashOutputBuf, |
| 290 |
BITS_BNU_CHUNK(pDrbg->pHashMethod->hashLen * BYTESIZE)); |
| 291 |
|
| 292 |
/* Little Endian to Big Endian */ |
| 293 |
cpEndiannessReverseInplace(pDrbg->C, seedBytesLen); |
| 294 |
/* V + C */ |
| 295 |
cpAdd_BNU(pBN_V, pBN_V, (BNU_CHUNK_T*)pDrbg->C, BITS_BNU_CHUNK(HASH_DRBG_SEEDBITS_LEN(pDrbg))); |
| 296 |
/* Big Endian to Little Endian */ |
| 297 |
cpEndiannessReverseInplace(pDrbg->C, seedBytesLen); |
| 298 |
|
| 299 |
/* Little Endian to Big Endian */ |
| 300 |
cpEndiannessReverseInplace(reseedCnt, sizeof(reseedCnt)); |
| 301 |
/* V + reseedCnt */ |
| 302 |
cpAddWithCarry_BNU(pBN_V, |
| 303 |
pBN_V, |
| 304 |
BITS_BNU_CHUNK(HASH_DRBG_SEEDBITS_LEN(pDrbg)), |
| 305 |
(BNU_CHUNK_T*)reseedCnt, |
| 306 |
BITS_BNU_CHUNK(BITSIZE(reseedCnt))); |
| 307 |
|
| 308 |
/* Big Endian to Little Endian */ |
| 309 |
cpEndiannessReverseInplace(pV, seedBytesExtLen); |
| 310 |
|
| 311 |
/* V mod 2^seedlen */ |
| 312 |
pV[0] = 0; |
| 313 |
|
| 314 |
/* reseed_counter = reseed_counter + 1 */ |
| 315 |
HASH_DRBG_RESEED_COUNTER(pDrbg)++; |
| 316 |
|
| 317 |
return sts; |
| 318 |
} |