|
| 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 */ |
0 commit comments