Home | History | Annotate | Download | only in ippcp
      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 //     PRNG Functions
     46 //
     47 //  Contents:
     48 //     HW random generator
     49 //
     50 //
     51 */
     52 
     53 #include "owndefs.h"
     54 
     55 #include "owncp.h"
     56 #include "pcpbn.h"
     57 #include "pcptool.h"
     58 
     59 #if !defined (_PCP_PRN_GEN_HW_H)
     60 #define _PCP_PRN_GEN_HW_H
     61 
     62 #if ((_IPP>=_IPP_G9) || (_IPP32E>=_IPP32E_E9))
     63 __INLINE int cpRand_hw_sample(BNU_CHUNK_T* pSample)
     64 {
     65 #define LOCAL_COUNTER (8)
     66    int n;
     67    int success = 0;
     68    for(n=0; n<LOCAL_COUNTER && !success; n++)
     69       #if (_IPP_ARCH == _IPP_ARCH_IA32)
     70       success = _rdrand32_step(pSample);
     71       #elif (_IPP_ARCH == _IPP_ARCH_EM64T)
     72       success = _rdrand64_step(pSample);
     73       #else
     74       #error Unknown CPU arch
     75       #endif
     76    return success;
     77 #undef LOCAL_COUNTER
     78 }
     79 
     80 #if (_IPP32E>=_IPP32E_E9)
     81 __INLINE int cpRand_hw_sample32(Ipp32u* pSample)
     82 {
     83 #define LOCAL_COUNTER (8)
     84    int n;
     85    int success = 0;
     86    for(n=0; n<LOCAL_COUNTER && !success; n++)
     87       success = _rdrand32_step(pSample);
     88    return success;
     89 #undef LOCAL_COUNTER
     90 }
     91 #endif
     92 
     93 /*F*
     94 // Name: cpRandHW_buffer
     95 //
     96 // Purpose: Generates a pseudorandom bit sequence
     97 //          based on RDRAND instruction of the specified nBits length.
     98 //
     99 // Returns:                   Reason:
    100 //    1                        no errors
    101 //    0                        random bit sequence can't be generated
    102 //
    103 // Parameters:
    104 //    pBuffer  pointer to the buffer
    105 //    bufLen    buffer length
    106 *F*/
    107 
    108 __INLINE int cpRandHW_buffer(Ipp32u* pBuffer, int bufLen)
    109 {
    110    int nSamples = bufLen/(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u));
    111 
    112    int n;
    113    /* collect nSamples randoms */
    114    for(n=0; n<nSamples; n++, pBuffer+=(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u))) {
    115       if( !cpRand_hw_sample((BNU_CHUNK_T*)pBuffer))
    116          return 0;
    117    }
    118 
    119    #if (_IPP32E>=_IPP32E_E9)
    120    if( bufLen%(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u)) ) {
    121       if( !cpRand_hw_sample32(pBuffer)) {
    122          return 0;
    123       }
    124    }
    125    #endif
    126    return 1;
    127 }
    128 #endif
    129 
    130 #endif /* #if !defined (_PCP_PRN_GEN_HW_H) */
    131