| 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 "drbg/pcphashdrbg.h" |
| 20 |
#include "drbg/pcphashdrbg_entropy_input.h" |
| 21 |
#include "pcpbnumisc.h" |
| 22 |
#include "pcpprng.h" |
| 23 |
|
| 24 |
/* |
| 25 |
* Obtains entropyInput either by calling the callback function (if it isn't NULL) or by RDSEED |
| 26 |
* |
| 27 |
* Input parameters: |
| 28 |
* minEntropyInBits Minimum number bits of entropy |
| 29 |
* predictionResistanceRequest |
| 30 |
* pEntrInputCtx Pointer to the Entropy input context |
| 31 |
* |
| 32 |
* Output parameters: |
| 33 |
* pEntrInputBitsLen Length of the buffer containing the entropy input |
| 34 |
* (*shall* be equal to or greater than min_length bits, and less |
| 35 |
* than or equal to max_length bits) |
| 36 |
* |
| 37 |
* NIST.SP.800-90C Section 8 "DRBG Construction" |
| 38 |
*/ |
| 39 |
|
| 40 |
/* The function call is: |
| 41 |
(status, entropy_input) = Get_entropy_input(min_entropy, min_length, |
| 42 |
max_length, prediction_resistance_request) |
| 43 |
Since min_length = min_entropy, the second parameter for callback function is omitted |
| 44 |
*/ |
| 45 |
|
| 46 |
// Minimum length of the entropyInput is equal to: |
| 47 |
// - sec.strength + ½ sec.strength, if the function is invoked during Instantiating |
| 48 |
// - sec.strength, if the function is invoked during Reseeding |
| 49 |
|
| 50 |
/* clang-format off */ |
| 51 |
IPP_OWN_DEFN(IppStatus, |
| 52 |
cpHashDRBG_GetEntropyInput, |
| 53 |
(const int minEntropyInBits, |
| 54 |
const int predictionResistanceRequest, |
| 55 |
int* pEntrInputBitsLen, |
| 56 |
IppsHashDRBG_EntropyInputCtx* pEntrInputCtx)) |
| 57 |
/* clang-format on */ |
| 58 |
{ |
| 59 |
IppStatus sts = ippStsNoErr; |
| 60 |
if (NULL == pEntrInputCtx->getEntropyInput) { |
| 61 |
/* use RDSEED as the primary source of random numbers */ |
| 62 |
sts = ippsTRNGenRDSEED((Ipp32u*)pEntrInputCtx->entropyInput, |
| 63 |
pEntrInputCtx->entrInputBufBitsLen, |
| 64 |
NULL); |
| 65 |
/* use RDRAND if RDSEED instruction isn't support or random bit sequence can't be generated */ |
| 66 |
if (ippStsNotSupportedModeErr == sts || ippStsErr == sts) { |
| 67 |
sts = ippsPRNGenRDRAND((Ipp32u*)pEntrInputCtx->entropyInput, |
| 68 |
pEntrInputCtx->entrInputBufBitsLen, |
| 69 |
NULL); |
| 70 |
} |
| 71 |
if (ippStsNoErr != sts) { |
| 72 |
return sts; |
| 73 |
} |
| 74 |
|
| 75 |
*pEntrInputBitsLen = pEntrInputCtx->entrInputBufBitsLen; |
| 76 |
} else { |
| 77 |
sts = pEntrInputCtx->getEntropyInput(pEntrInputCtx->entropyInput, |
| 78 |
pEntrInputBitsLen, |
| 79 |
minEntropyInBits, |
| 80 |
pEntrInputCtx->entrInputBufBitsLen, |
| 81 |
predictionResistanceRequest); |
| 82 |
if (ippStsNoErr != sts) { |
| 83 |
return sts; |
| 84 |
} |
| 85 |
|
| 86 |
/* entrInputBitsLen < (sec.strength + sec.strength/2) |
| 87 |
1) According to the NIST.SP.800-90Ar1 Table 2: "Definitions for Hash-Based DRBG Mechanisms" |
| 88 |
required minimum entropy for instantiate equals to securityStrength. |
| 89 |
2) According to the NIST.SP.800-90Ar1 Section 8.6.7 "Nonce", the nonce shall be either: |
| 90 |
a. A value with at least (security_strength/2) bits of entropy, or |
| 91 |
b. A value that is expected to repeat no more often than a (security_strength/2)-bit |
| 92 |
random string would be expected to repeat */ |
| 93 |
if ((*pEntrInputBitsLen < minEntropyInBits) || |
| 94 |
(*pEntrInputBitsLen > pEntrInputCtx->entrInputBufBitsLen)) { |
| 95 |
return ippStsOutOfRangeErr; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return sts; |
| 100 |
} |