1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/*************************************************************************
* Copyright (C) 2019 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*************************************************************************/
/* IntelĀ® Cryptography Primitives Library */
/*!
*
* \file
* \brief Common header for Intel Cryptography Primitives Library examples
*
*/
#ifndef EXAMPLES_COMMON_H_
#define EXAMPLES_COMMON_H_
#include <stdio.h>
/*! Macro that prints status message depending on condition */
#define PRINT_EXAMPLE_STATUS(function_name, description, success_condition) \
printf("+--------------------------------------------------------------|\n"); \
printf(" Function: %s\n", function_name); \
printf(" Description: %s\n", description); \
if (success_condition) { \
printf(" Status: PASSED!\n"); \
} else { \
printf(" Status: FAILED!\n"); \
} \
printf("+--------------------------------------------------------------|\n");
/*!
* Helper function to print status message for skipped tests.
*
* \param[in] Function name to display
* \param[in] Example's description
* \param[in] The reason for the skip
*/
inline void printSkippedExampleDetails(const char* function_name,
const char* description,
const char* skip_reason)
{
printf("+--------------------------------------------------------------|\n");
printf(" Function: %s\n", function_name);
printf(" Description: %s\n", description);
printf(" Status: SKIPPED!\n");
printf(" %s\n", skip_reason);
printf("+--------------------------------------------------------------|\n");
}
/*!
* Helper function to compare expected and actual function return statuses and display
* an error message if those are different.
*
* \param[in] Function name to display
* \param[in] Expected status
* \param[in] Actual status
*
* \return zero if statuses are not equal, otherwise - non-zero value
*/
inline int checkStatus(const char* funcName, IppStatus expectedStatus, IppStatus status)
{
if (expectedStatus != status) {
printf("%s: unexpected return status\n", funcName);
printf("Expected: %s\n", ippcpGetStatusString(expectedStatus));
printf("Received: %s\n", ippcpGetStatusString(status));
return 0;
}
return 1;
}
/*!
* Helper function to convert bit size into byte size.
*
* \param[in] Size in bits
*
* \return size in bytes
*/
inline int bitSizeInBytes(int nBits) { return (nBits + 7) >> 3; }
/*!
* Helper function to convert bit size into word size.
*
* \param[in] Size in bits
*
* \return size in words
*/
inline int bitSizeInWords(int nBits) { return (nBits + 31) >> 5; }
/*!
* Helper function to check if the current CPU supports hardware
* RNG (RDRAND instruction).
*
* \return zero if hardware RNG is not supported, otherwise - non-zero value
*/
inline int isAvailablePRNG_HW(void)
{
ippcpInit();
if (ippCPUID_RDRAND == (ippCPUID_RDRAND & ippcpGetEnabledCpuFeatures())) {
Ipp32u outBuffer[1];
return (ippStsNotSupportedModeErr ==
ippsPRNGenRDRAND(outBuffer, sizeof(outBuffer) * 8, NULL))
? 0
: 1;
}
return 0;
}
#endif /* #ifndef EXAMPLES_COMMON_H_ */
|