Skip to content

Commit 1880dc0

Browse files
egrabovskayaipl_ci
authored andcommitted
[Library][Feature] Hash DRBG implementation (#719)
1 parent fce7116 commit 1880dc0

28 files changed

Lines changed: 2270 additions & 17 deletions

include/ippcp.h

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,6 @@ A more secure alternative is available: ippsPRNGenRDRAND and ippsTRNGenRDSEED."
714714

715715
IPPAPI(IppStatus, ippsPRNGGetSize,(int* pSize))
716716
IPPAPI(IppStatus, ippsPRNGInit, (int seedBits, IppsPRNGState* pCtx))
717-
718717
IPPAPI(IppStatus, ippsPRNGSetModulus,(const IppsBigNumState* pMod, IppsPRNGState* pCtx))
719718
IPPAPI(IppStatus, ippsPRNGSetH0, (const IppsBigNumState* pH0, IppsPRNGState* pCtx))
720719
IPPAPI(IppStatus, ippsPRNGSetAugment,(const IppsBigNumState* pAug, IppsPRNGState* pCtx))
@@ -730,6 +729,52 @@ IPPAPI(IppStatus, ippsPRNGenRDRAND_BN,(IppsBigNumState* pRand, int nBits, void*
730729
IPPAPI(IppStatus, ippsTRNGenRDSEED, (Ipp32u* pRand, int nBits, void* pCtx))
731730
IPPAPI(IppStatus, ippsTRNGenRDSEED_BN,(IppsBigNumState* pRand, int nBits, void* pCtx))
732731

732+
IPPAPI(IppStatus, ippsHashDRBG_EntropyInputCtxGetSize, (int* pEntrInputSize,
733+
const int entrInputBufBitsLen,
734+
const IppsHashMethod* pHashMethod))
735+
IPPAPI(IppStatus, ippsHashDRBG_EntropyInputCtxInit, (IppsHashDRBG_EntropyInputCtx* pEntrInputCtx,
736+
const int entrInputBitsLen,
737+
IppEntropyInputSupplier getEntropyInput))
738+
IPPAPI(IppStatus, ippsHashDRBG_GetSize, (int* pSize, const IppsHashMethod* pHashMethod))
739+
IPPAPI(IppStatus, ippsHashDRBG_Init, (const IppsHashMethod* pHashMethod,
740+
IppsHashDRBGState* pDrbgCtx))
741+
IPPAPI(IppStatus, ippsHashDRBG_Instantiate, (const int requestedInstSecurityStrength,
742+
const int predictionResistanceFlag,
743+
const Ipp8u* persStr,
744+
const int persStrBitsLen,
745+
IppsHashDRBG_EntropyInputCtx* pEntrInputCtx,
746+
IppsHashDRBGState* pDrbgCtx))
747+
IPPAPI(IppStatus, ippsHashDRBG_Uninstantiate, (IppsHashDRBGState* pDrbgCtx))
748+
IPPAPI(IppStatus, ippsHashDRBG_Reseed, (const int predictionResistanceRequest,
749+
const Ipp8u* addlInput,
750+
const int addlInputBitsLen,
751+
IppsHashDRBG_EntropyInputCtx* pEntrInputCtx,
752+
IppsHashDRBGState* pDrbgCtx))
753+
IPPAPI(IppStatus, ippsHashDRBG_Gen, (Ipp32u* pRand,
754+
int nBits,
755+
const int requestedSecurityStrength,
756+
const int predictionResistanceRequest,
757+
const Ipp8u* addlInput,
758+
const int addlInputBitsLen,
759+
IppsHashDRBG_EntropyInputCtx* pEntrInputCtx,
760+
IppsHashDRBGState* pDrbgCtx))
761+
IPPAPI(IppStatus, ippsHashDRBG_GenBN, (IppsBigNumState* pRand,
762+
int nBits,
763+
const int requestedSecurityStrength,
764+
const int predictionResistanceRequest,
765+
const Ipp8u* addlInput,
766+
const int addlInputBitsLen,
767+
IppsHashDRBG_EntropyInputCtx* pEntrInputCtx,
768+
IppsHashDRBGState* pDrbgCtx))
769+
IPPAPI(IppStatus, ippsHashDRBG_InstantiateTest, (IppsHashDRBG_EntropyInputCtx* pEntrInputCtxTempBuf,
770+
IppsHashDRBGState* pDrbgCtxTempBuf))
771+
IPPAPI(IppStatus, ippsHashDRBG_ReseedTest, (IppsHashDRBG_EntropyInputCtx* pEntrInputCtxTempBuf,
772+
IppsHashDRBGState* pDrbgCtxTempBuf))
773+
IPPAPI(IppStatus, ippsHashDRBG_GenTest, (Ipp32u* pRand,
774+
int nBits,
775+
IppsHashDRBG_EntropyInputCtx* pEntrInputCtxTempBuf,
776+
IppsHashDRBGState* pDrbgCtxTempBuf))
777+
733778
/* Probable Prime Number Generation */
734779
IPPAPI(IppStatus, ippsPrimeGetSize,(int nMaxBits, int* pSize))
735780
IPPAPI(IppStatus, ippsPrimeInit, (int nMaxBits, IppsPrimeState* pCtx))

include/ippcp/fips_cert.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,12 @@ enum FIPS_IPPCP_FUNC {
254254
PBKDF2_PKCS5v2,
255255
MLDSA_KeyGen,
256256
MLDSA_Sign,
257-
MLDSA_Verify
257+
MLDSA_Verify,
258+
HashDRBG_Instantiate,
259+
HashDRBG_Uninstantiate,
260+
HashDRBG_Reseed,
261+
HashDRBG_Gen,
262+
HashDRBG_GenBN
258263
};
259264

260265
/**

include/ippcpdefs.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ typedef signed int IppStatus;
438438

439439
#endif /* IPPSTATUS_H__ */
440440

441+
#define ippStsHashOperationErr -1019 /* Hash DRBG: an internal error that can occur when hashing */
441442
#define ippStsMLDSAMaxIterations -1018 /* The ML-DSA iterations reach maximum. Data weren't generated successfully. */
442443
#define ippStsInvalidPoint -1017 /* ippStsInvalidPoint ECC: Invalid point (out of EC).*/
443444
#define ippStsQuadraticNonResidueErr -1016 /* SQRT operation on quadratic non-residue value. */
@@ -634,13 +635,17 @@ typedef enum {
634635
ippSlidingWindows = 1, IppsSlidingWindows = 1
635636
} IppsExpMethod;
636637

637-
typedef struct _cpBigNum IppsBigNumState;
638-
typedef struct _cpMontgomery IppsMontState;
639-
typedef struct _cpPRNG IppsPRNGState;
640-
typedef struct _cpPrime IppsPrimeState;
638+
typedef struct _cpBigNum IppsBigNumState;
639+
typedef struct _cpMontgomery IppsMontState;
640+
typedef struct _cpPRNG IppsPRNGState;
641+
typedef struct _cpPrime IppsPrimeState;
642+
typedef struct _cpHashDRBG IppsHashDRBGState;
643+
typedef struct _cpEntropyInputCtx IppsHashDRBG_EntropyInputCtx;
641644

642645
/* External Bit Supplier */
643646
typedef IppStatus (IPP_CALL *IppBitSupplier)(Ipp32u* pRand, int nBits, void* pEbsParams);
647+
/* External entropyInput Supplier */
648+
typedef IppStatus (IPP_CALL *IppEntropyInputSupplier)(Ipp8u* entropyInput, int* entropyInputBitsLen, const int minEntropyInBits, const int maxBitsLen, const int predictionResistanceRequest);
644649

645650
#define IPP_IS_EQ (0)
646651
#define IPP_IS_GT (1)

sources/include/drbg/pcphashdrbg.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
/*
18+
// Purpose:
19+
// Hash DRBG state, internal definitions and function declarations
20+
*/
21+
22+
#if !defined(_CP_HASHDRBG_H)
23+
#define _CP_HASHDRBG_H
24+
25+
#define HASH_DRBG_MIN_SEED_BITS_LEN 440
26+
#define HASH_DRBG_MAX_SEED_BITS_LEN 888
27+
28+
#define HASH_DRBG_MIN_SEED_BYTES_LEN (HASH_DRBG_MIN_SEED_BITS_LEN / 8)
29+
#define HASH_DRBG_MAX_SEED_BYTES_LEN (HASH_DRBG_MAX_SEED_BITS_LEN / 8)
30+
31+
#define HASH_DRBG_MIN_SEC_STRENGTH 128
32+
#define HASH_DRBG_SEC_STRENGTH_192 192
33+
#define HASH_DRBG_MAX_BITS_SEC_STRENGTH 256
34+
35+
/* Constants of maximum values according to the NIST.SP.800-90Ar1
36+
Table 2: "Definitions for Hash-Based DRBG Mechanisms". */
37+
/* MAX_INPUT_LEN for personalization_string, additional_input and
38+
entropy_input equals to 2^35 bits. To avoid overflowing, use maximum integer
39+
value (2^31 - 1) since the lengths of the input arrays are passed as int */
40+
#define MAX_INPUT_LEN (~(1 << 31)) // (2^31 - 1) bits
41+
/* MAX_RESEED_INTERVAL equals to 2^48,
42+
MAX_BITS_NUMBER_PER_REQUEST equals to 2^19 bits.
43+
Lower these two values to the minimum allowed values since
44+
the limits set in 90A are unreasonably big */
45+
#define MAX_RESEED_INTERVAL (Ipp64u)(1ul << 24) // 2^24
46+
#define MAX_BITS_NUMBER_PER_REQUEST (1 << 16) // 2^16 bits
47+
48+
struct _cpHashDRBG {
49+
Ipp32u idCtx; /* DRBG identifier */
50+
int seedBitsLen; /* Secret values length */
51+
Ipp32u securityStrength; /* Security strength of the DRBG instantiation */
52+
int predictionResistanceFlag; /* Indicates whether or not prediction resistance may be required by
53+
the consuming application during requests for pseudorandom bits */
54+
int hashStateSize_rmf; /* The size of hashState */
55+
Ipp64u reseedCounter; /* Indicates the number of requests for pseudorandom bits
56+
since new entropy_input was obtained during
57+
instantiation or reseeding */
58+
IppsHashMethod* pHashMethod; /* Hash method used by the DRBG mechanism; ippsHashMethod_SHA256()
59+
is set by default if no hash method was passed */
60+
Ipp8u* V; /* Secret values (stores one extra byte at the very beginning) */
61+
Ipp8u* C; /* Secret values */
62+
Ipp8u* tempBuf; /* Temporary buffer to store the values of V
63+
(also like V, stores one extra byte) */
64+
IppsHashState_rmf* hashState; /* Pointer to IppsHashState_rmf context */
65+
Ipp8u* hashOutputBuf; /* Buffer to store hash output digest */
66+
};
67+
68+
#define HASH_DRBG_SET_ID(ctx) ((ctx)->idCtx = (Ipp32u)idCtxHashDRBG ^ (Ipp32u)IPP_UINT_PTR(ctx))
69+
#define HASH_DRBG_SEEDBITS_LEN(ctx) ((ctx)->seedBitsLen)
70+
/* Extended size for V and tempBuf */
71+
#define HASH_DRBG_SEEDBITS_LEN_EXT(ctx) ((ctx)->seedBitsLen + 8)
72+
#define HASH_DRBG_RESEED_COUNTER(ctx) ((ctx)->reseedCounter)
73+
#define HASH_DRBG_SECURITY_STRENGTH(ctx) ((ctx)->securityStrength)
74+
#define HASH_DRBG_PRED_RESISTANCE_FLAG(ctx) ((ctx)->predictionResistanceFlag)
75+
#define HASH_DRBG_HASH_STATE_SIZE(ctx) ((ctx)->hashStateSize_rmf)
76+
77+
#define HASH_DRBG_VALID_ID(ctx) \
78+
((((ctx)->idCtx) ^ (Ipp32u)IPP_UINT_PTR((ctx))) == (Ipp32u)idCtxHashDRBG)
79+
80+
#define cpHashDRBG_GetEntropyInput OWNAPI(cpHashDRBG_GetEntropyInput)
81+
IPP_OWN_DECL(IppStatus,
82+
cpHashDRBG_GetEntropyInput,
83+
(const int minEntropy,
84+
const int predictionResistanceRequest,
85+
int* pEntrInputBitsLen,
86+
IppsHashDRBG_EntropyInputCtx* pEntrInputCtx))
87+
#define cpHashDRBG_df OWNAPI(cpHashDRBG_df)
88+
IPP_OWN_DECL(IppStatus,
89+
cpHashDRBG_df,
90+
(const Ipp8u* inputParam1,
91+
const cpSize inputParam1Len,
92+
const Ipp8u* inputParam2,
93+
const cpSize inputParam2Len,
94+
const Ipp8u* inputParam3,
95+
const cpSize inputParam3Len,
96+
Ipp8u* requestedBits,
97+
const cpSize nBitsToReturn,
98+
IppsHashDRBGState* drbgCtx))
99+
#define cpHashDRBG_Gen OWNAPI(cpHashDRBG_Gen)
100+
IPP_OWN_DECL(IppStatus,
101+
cpHashDRBG_Gen,
102+
(Ipp32u * pRand,
103+
int randBytesLen,
104+
const int predictionResistanceRequest,
105+
const Ipp8u* additionalInput,
106+
const int additionalInputLen,
107+
IppsHashDRBG_EntropyInputCtx* pEntrInputCtx,
108+
IppsHashDRBGState* pDrbg))
109+
110+
#endif /* _CP_HASHDRBG_H */
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
/*
18+
// Purpose:
19+
// Entropy input context
20+
*/
21+
22+
#if !defined(_CP_HASHDRBG_ENTROPYINPUT_H)
23+
#define _CP_HASHDRBG_ENTROPYINPUT_H
24+
25+
struct _cpEntropyInputCtx {
26+
Ipp32u idCtx; /* Entropy input context identifier */
27+
Ipp8u* entropyInput; /* Buffer to store both entropyInput and nonce for Instantiation
28+
and entropyInput for Reseeding */
29+
int entrInputBufBitsLen; /* The length of the Entropy input buffer set by a user */
30+
IppEntropyInputSupplier getEntropyInput; /* Callback function */
31+
};
32+
33+
#define HASH_DRBG_ENTR_INPUT_SET_ID(ctx) \
34+
((ctx)->idCtx = (Ipp32u)idCtxHashDRBGEntrInput ^ (Ipp32u)IPP_UINT_PTR(ctx))
35+
#define HASH_DRBG_ENTR_INPUT_VALID_ID(ctx) \
36+
((((ctx)->idCtx) ^ (Ipp32u)IPP_UINT_PTR((ctx))) == (Ipp32u)idCtxHashDRBGEntrInput)
37+
38+
#endif /* _CP_HASHDRBG_ENTROPYINPUT_H */

sources/include/owndefs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ typedef enum {
393393
idCtxPubKeyLMS,
394394
idCtxSignLMS,
395395
idCtxMLKEM,
396-
idCtxMLDSA
396+
idCtxMLDSA,
397+
idCtxHashDRBGEntrInput,
398+
idCtxHashDRBG
397399
} IppCtxId;
398400

399401

sources/ippcp/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ file(GLOB LIBRARY_C_SOURCES_ORIGINAL
229229
${IPP_CRYPTO_SOURCES_DIR}/stateful_sig/*.c
230230
${IPP_CRYPTO_SOURCES_DIR}/ml_dsa/*.c
231231
${IPP_CRYPTO_SOURCES_DIR}/ml_dsa/*/*.c
232+
${IPP_CRYPTO_SOURCES_DIR}/drbg/*.c
233+
${IPP_CRYPTO_SOURCES_DIR}/drbg/*/*.c
232234
)
233235

234236
file(GLOB LIBRARY_ASM_SOURCES_ORIGINAL
@@ -398,7 +400,7 @@ foreach(opt ${PLATFORM_LIST})
398400
${LIBRARY_C_SOURCES_${opt}}
399401
${IPPCP_FIPS_CERT_LIBRARY_C_SOURCES}
400402
$<TARGET_OBJECTS:${IPPCP_DYN_ITER_ASMOBJS}>)
401-
403+
402404
## gres: copy LINK_FLAG_DYNAMIC_LINUX
403405
set(LINK_FLAGS ${LINK_FLAG_DYNAMIC_LINUX})
404406
## add export file

sources/ippcp/cpinit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ static struct {
478478
/* -9702 */ {MSG_NO_SHARED, "No shared libraries were found in the Waterfall procedure"},
479479
/* -9701 */ {MSG_NO_DLL, "No DLLs were found in the Waterfall procedure"},
480480
/* -9700 */ {MSG_LOAD_DLL_ERR, "Error at loading of %s library"},
481+
/* -1019 */ {ippStsHashOperationErr, "Hash DRBG: an internal error occurred when hashing"},
481482
/* -1018 */ {ippStsMLDSAMaxIterations, "ippStsMLDSAMaxIterations: The ML-DSA iterations reach maximum. Data weren't generated successfully"},
482483
/* -1017 */ {ippStsInvalidPoint, "ippStsInvalidPoint ECC: Invalid point (out of EC)"},
483484
/* -1016 */ {ippStsQuadraticNonResidueErr, "ippStsQuadraticNonResidueErr: SQRT operation on quadratic non-residue value"},

0 commit comments

Comments
 (0)