Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
Created January 25, 2023 15:09
Show Gist options
  • Save stevedoyle/c3e311f1fb6d259832a1606e53bd4924 to your computer and use it in GitHub Desktop.
Save stevedoyle/c3e311f1fb6d259832a1606e53bd4924 to your computer and use it in GitHub Desktop.
Example of using IPP Crypto Primitives for SHA hashing
#include <iostream>
#include <stdint.h>
#include <x86intrin.h>
#include "ippcp.h"
#include "examples_common.h"
#define DATA_SIZE 1024*1024
#define DIGEST_SIZE 32
#define ITERATIONS 10000
static inline uint32_t rdtsc(void)
{
uint32_t a = 0;
asm volatile("rdtsc":"=a"(a)::"edx");
return a;
}
int main()
{
IppStatus status = ippStsNoErr;
Ipp8u pDigest[DIGEST_SIZE] = {};
Ipp8u* data = new Ipp8u[DATA_SIZE];
int ctxSize = 0;
ippsHashGetSize(&ctxSize);
// Allocate memory for the hash context structure
IppsHashState* pCtx = (IppsHashState*)(new Ipp8u[ctxSize]);
const uint64_t start = __rdtsc();
for(uint32_t i=0; i<ITERATIONS; ++i) {
// Initialize Hash context
status = ippsHashInit(pCtx, ippHashAlg_SHA256);
if(status != ippStsNoErr)
std::cout << "Error with ippsHashInit()" << std::endl;
// Call HashUpdate to digest the data until completion.
status = ippsHashUpdate(data, DATA_SIZE, pCtx);
if(status != ippStsNoErr)
std::cout << "Error with ippsHashUpdate()" << std::endl;
// Call HashFinal to compute the digest value
status = ippsHashFinal(pDigest, pCtx);
if(status != ippStsNoErr)
std::cout << "Error with ippsHashFinal()" << std::endl;
}
const uint64_t end = __rdtsc();
std::cout << "Elapsed cycles: " << (end - start)/ITERATIONS << std::endl;
delete [] (Ipp8u*)pCtx;
pCtx = 0;
delete [] data;
data = 0;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment