1 /*******************************************************************************
2 * Copyright 2015-2018 Intel Corporation
3 * All Rights Reserved.
4 *
5 * If this software was obtained under the Intel Simplified Software License,
6 * the following terms apply:
7 *
8 * The source code, information and material ("Material") contained herein is
9 * owned by Intel Corporation or its suppliers or licensors, and title to such
10 * Material remains with Intel Corporation or its suppliers or licensors. The
11 * Material contains proprietary information of Intel or its suppliers and
12 * licensors. The Material is protected by worldwide copyright laws and treaty
13 * provisions. No part of the Material may be used, copied, reproduced,
14 * modified, published, uploaded, posted, transmitted, distributed or disclosed
15 * in any way without Intel's prior express written permission. No license under
16 * any patent, copyright or other intellectual property rights in the Material
17 * is granted to or conferred upon you, either expressly, by implication,
18 * inducement, estoppel or otherwise. Any license under such intellectual
19 * property rights must be express and approved by Intel in writing.
20 *
21 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
22 * notice or any other notice embedded in Materials by Intel or Intel's
23 * suppliers or licensors in any way.
24 *
25 *
26 * If this software was obtained under the Apache License, Version 2.0 (the
27 * "License"), the following terms apply:
28 *
29 * You may not use this file except in compliance with the License. You may
30 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
31 *
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 *
37 * See the License for the specific language governing permissions and
38 * limitations under the License.
39 *******************************************************************************/
40
41 /*
42 //
43 // Purpose:
44 // Cryptography Primitive.
45 // TRNG Functions
46 //
47 // Contents:
48 // ippsTRNGenHW()
49 // ippsTRNGenHW_BN()
50 //
51 //
52 */
53
54 #include "owndefs.h"
55
56 #include "owncp.h"
57 #include "pcpbn.h"
58 #include "pcptool.h"
59
60 #if ((_IPP>=_IPP_G9) || (_IPP32E>=_IPP32E_E9))
cpSeed_hw_sample(BNU_CHUNK_T * pSample)61 static int cpSeed_hw_sample(BNU_CHUNK_T* pSample)
62 {
63 #define LOCAL_COUNTER (32) /* this constant has been tuned manually stated from 1024, looks that 16 is enouhg, but increased twice */
64 int n;
65 int success = 0;
66 for(n=0; n<LOCAL_COUNTER && !success; n++)
67 #if (_IPP_ARCH == _IPP_ARCH_IA32)
68 success = _rdseed32_step(pSample);
69 #elif (_IPP_ARCH == _IPP_ARCH_EM64T)
70 #pragma warning(push) // temporary, compiler bug workaround
71 #pragma warning(disable : 167)
72 success = _rdseed64_step(pSample);
73 #pragma warning(pop)
74 #else
75 #error Unknown CPU arch
76 #endif
77 return success;
78 #undef LOCAL_COUNTER
79 }
80
81 #if (_IPP32E>=_IPP32E_E9)
cpSeed_hw_sample32(Ipp32u * pSample)82 static int cpSeed_hw_sample32(Ipp32u* pSample)
83 {
84 #define LOCAL_COUNTER (32) /* this constant has been tuned manually stated from 1024, looks that 16 is enouhg, but increased twice */
85 int n;
86 int success = 0;
87 for(n=0; n<LOCAL_COUNTER && !success; n++)
88 success = _rdseed32_step(pSample);
89 return success;
90 #undef LOCAL_COUNTER
91 }
92 #endif
93
cpSeedHW_buffer(Ipp32u * pRand,int bufLen)94 static int cpSeedHW_buffer(Ipp32u* pRand, int bufLen)
95 {
96 int nSamples = bufLen/(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u));
97
98 int n;
99 /* collect nSamples randoms */
100 for(n=0; n<nSamples; n++, pRand+=(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u))) {
101 if( !cpSeed_hw_sample((BNU_CHUNK_T*)pRand))
102 return 0;
103 }
104
105 #if (_IPP32E>=_IPP32E_E9)
106 if( bufLen%(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u)) ) {
107 if( !cpSeed_hw_sample32(pRand)) {
108 return 0;
109 }
110 }
111 #endif
112 return 1;
113 }
114 #endif
115
116 /*F*
117 // Name: ippsTRNGenRDSEED
118 //
119 // Purpose: Generates a true random bit sequence
120 // based on Intel(R) instruction RDSEED of the specified nBits length.
121 //
122 // Returns: Reason:
123 // ippStsNullPtrErr NULL == pRand
124 //
125 // ippStsLengthErr 1 > nBits
126 //
127 // ippStsNotSupportedModeErr unsupported rdrand instruction
128 //
129 // ippStsErr random bit sequence can't be generated
130 //
131 // ippStsNoErr no error
132 //
133 // Parameters:
134 // pRand pointer to the buffer
135 // nBits number of bits be requested
136 // pCtx pointer to the context
137 *F*/
138 IPPFUN(IppStatus, ippsTRNGenRDSEED,(Ipp32u* pRand, int nBits, void* pCtx))
139 {
140 /* test PRNG buffer */
141 IPP_BAD_PTR1_RET(pRand);
142
143 /* test sizes */
144 IPP_BADARG_RET(nBits< 1, ippStsLengthErr);
145
146 UNREFERENCED_PARAMETER(pCtx);
147
148 #if ((_IPP>=_IPP_G9) || (_IPP32E>=_IPP32E_E9))
149 if( IsFeatureEnabled(ippCPUID_RDSEED) ) {
150 cpSize rndSize = BITS2WORD32_SIZE(nBits);
151 Ipp32u rndMask = MAKEMASK32(nBits);
152
153 if(cpSeedHW_buffer(pRand, rndSize)) {
154 pRand[rndSize-1] &= rndMask;
155 return ippStsNoErr;
156 }
157 else
158 return ippStsErr;
159 }
160 /* unsupported rdrand instruction */
161 else
162 #endif
163 IPP_ERROR_RET(ippStsNotSupportedModeErr);
164 }
165
166
167 /*F*
168 // Name: ippsTRNGenRDSEED_BN
169 //
170 // Purpose: Generates a true random big number
171 // based on Intel(R) instruction RDSEED of the specified nBits length.
172 //
173 // Returns: Reason:
174 // ippStsNullPtrErr NULL == pRand
175 //
176 // ippStsLengthErr 1 > nBits
177 // nBits > BN_ROOM(pRand)
178 //
179 // ippStsNotSupportedModeErr unsupported rdrand instruction
180 //
181 // ippStsErr random big number can't be generated
182 //
183 // ippStsNoErr no error
184 //
185 // Parameters:
186 // pRand pointer to the big number
187 // nBits number of bits be requested
188 // pCtx pointer to the context
189 *F*/
190 IPPFUN(IppStatus, ippsTRNGenRDSEED_BN,(IppsBigNumState* pRand, int nBits, void* pCtx))
191 {
192 /* test random BN */
193 IPP_BAD_PTR1_RET(pRand);
194 pRand = (IppsBigNumState*)( IPP_ALIGNED_PTR(pRand, BN_ALIGNMENT) );
195 IPP_BADARG_RET(!BN_VALID_ID(pRand), ippStsContextMatchErr);
196
197 /* test sizes */
198 IPP_BADARG_RET(nBits< 1, ippStsLengthErr);
199 IPP_BADARG_RET(nBits> BN_ROOM(pRand)*BNU_CHUNK_BITS, ippStsLengthErr);
200
201 UNREFERENCED_PARAMETER(pCtx);
202
203 #if ((_IPP>=_IPP_G9) || (_IPP32E>=_IPP32E_E9))
204 if( IsFeatureEnabled(ippCPUID_RDSEED) ) {
205 BNU_CHUNK_T* pRandBN = BN_NUMBER(pRand);
206 cpSize rndSize = BITS_BNU_CHUNK(nBits);
207 BNU_CHUNK_T rndMask = MASK_BNU_CHUNK(nBits);
208
209 if(cpSeedHW_buffer((Ipp32u*)pRandBN, rndSize*sizeof(BNU_CHUNK_T)/sizeof(Ipp32u))) {
210 pRandBN[rndSize-1] &= rndMask;
211
212 FIX_BNU(pRandBN, rndSize);
213 BN_SIZE(pRand) = rndSize;
214 BN_SIGN(pRand) = ippBigNumPOS;
215
216 return ippStsNoErr;
217 }
218 else
219 return ippStsErr;
220 }
221
222 /* unsupported rdrand instruction */
223 else
224 #endif
225 IPP_ERROR_RET(ippStsNotSupportedModeErr);
226 }
227