Debian Codegraph @

sources/ippcp/drbg/internal/pcphashdrbg_df.c

raw
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 * Hashes input strings and returns the requested number of bits
19 *
20 * Input parameters:
21 * Pointers to the strings that are hashed to get seed material:
22 * inputParam1 input string 1 (a non NULL value)
23 * inputParam2 optional input string (can be NULL)
24 * inputParam3 optional input string (can be NULL)
25 * inputParam1Len inputParam1 length in bits
26 * inputParam2Len inputParam2 length in bits (can be zero)
27 * inputParam3Len inputParam3 length in bits (can be zero)
28 * nBitsToReturn length of requestedBits string
29 * pDrbgCtx pointer to the DRBG state
30 *
31 * Output parameters:
32 * requestedBits resulted string (V or C) that contains secret values
33 *
34 * NIST.SP.800-90Ar1 Section 10.3.1 Derivation Function Using a Hash Function (Hash_df)
35 *
36 * Hash_df Process:
37 * temp = NULL
38 * len = [no_of_bits_to_return / outlen]
39 * counter = 0x01
40 * For (i = 1 to len) {
41 * temp = temp || Hash (counter || no_of_bits_to_return || input_string)
42 * counter = counter + 1
43 * requested_bits = leftmost (temp, no_of_bits_to_return)
44 * Return (SUCCESS, requested_bits)
45 */
46
47 #include "owndefs.h"
48 #include "owncp.h"
49 #include "pcpbn.h"
50 #include "pcptool.h"
51 #include "drbg/pcphashdrbg.h"
52 #include "hash/pcphash_rmf.h"
53
54 IPP_OWN_DEFN(IppStatus,
55 cpHashDRBG_df,
56 (const Ipp8u* inputParam1,
57 const int inputParam1Len,
58 const Ipp8u* inputParam2,
59 const int inputParam2Len,
60 const Ipp8u* inputParam3,
61 const int inputParam3Len,
62 Ipp8u* requestedBits,
63 const int nBitsToReturn,
64 IppsHashDRBGState* pDrbgCtx))
65 {
66 /* The maximum length (max_number_of_bits) is implementation dependent,
67 but *shall* be less than or equal to (255 × outlen)
68 (outlen is Output Block Length or digest bit size) */
69 if (nBitsToReturn > 255 * pDrbgCtx->pHashMethod->hashLen * BYTESIZE) {
70 return ippStsOutOfRangeErr;
71 }
72
73 IppStatus sts = ippStsNoErr;
74
75 Ipp8u* pOutput = requestedBits;
76
77 Ipp8u counter = 1; /* An 8-bit binary value representing the integer "1" */
78 Ipp8u bitsToReturn[4];
79
80 /* present an 32-bit unsigned int in BE format */
81 bitsToReturn[0] = (Ipp8u)((nBitsToReturn >> 24) & 0xFF);
82 bitsToReturn[1] = (Ipp8u)((nBitsToReturn >> 16) & 0xFF);
83 bitsToReturn[2] = (Ipp8u)((nBitsToReturn >> 8) & 0xFF);
84 bitsToReturn[3] = (Ipp8u)(nBitsToReturn & 0xFF);
85
86 /*
87 For i = 1 to len do
88 // no_of_bits_to_return is used as a 32-bit string
89 temp = temp || Hash (counter || no_of_bits_to_return || input_msg)
90 counter = counter + 1
91 */
92
93 int outputHashBufLen = pDrbgCtx->pHashMethod->hashLen;
94
95 int residual = BITS2WORD8_SIZE(nBitsToReturn);
96 while (residual > 0) {
97 /* counter || no_of_bits_to_return || input_msg */
98 sts = ippsHashInit_rmf(pDrbgCtx->hashState, pDrbgCtx->pHashMethod);
99 sts |= ippsHashUpdate_rmf(&counter, sizeof(counter), pDrbgCtx->hashState);
100 sts |= ippsHashUpdate_rmf(bitsToReturn, sizeof(bitsToReturn), pDrbgCtx->hashState);
101 sts |=
102 ippsHashUpdate_rmf(inputParam1, BITS2WORD8_SIZE(inputParam1Len), pDrbgCtx->hashState);
103 if (inputParam2Len) {
104 sts |= ippsHashUpdate_rmf(inputParam2,
105 BITS2WORD8_SIZE(inputParam2Len),
106 pDrbgCtx->hashState);
107 }
108 if (inputParam3Len) {
109 sts |= ippsHashUpdate_rmf(inputParam3,
110 BITS2WORD8_SIZE(inputParam3Len),
111 pDrbgCtx->hashState);
112 }
113 sts |= ippsHashFinal_rmf(pDrbgCtx->hashOutputBuf, pDrbgCtx->hashState);
114 if (ippStsNoErr != sts) {
115 /* zeroize requestedBits */
116 PurgeBlock((void*)pOutput, BITS2WORD8_SIZE(nBitsToReturn));
117 return ippStsHashOperationErr;
118 }
119
120 COPY_BNU(pOutput, pDrbgCtx->hashOutputBuf, IPP_MIN(residual, outputHashBufLen));
121
122 counter++;
123 pOutput += IPP_MIN(residual, outputHashBufLen);
124 residual -= outputHashBufLen;
125 }
126
127 /* zeroize Hash DRBG hashState */
128 PurgeBlock((void*)pDrbgCtx->hashState, HASH_DRBG_HASH_STATE_SIZE(pDrbgCtx));
129
130 return ippStsNoErr;
131 }