Author: Not specified Language: c
Description: Not specified Timestamp: 2018-07-03 00:36:56 +0000
View raw paste Reply
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/socket.h>
  5. #include <linux/if_alg.h>
  6. #include <linux/socket.h>
  7. #include <sys/mman.h>
  8. #include <time.h>
  9.  
  10. #define SHA384_DIGEST_SZ 48
  11.  
  12. #define DATA_SZ 16777216 // ~55500 us
  13. //#define DATA_SZ 268435456
  14.  
  15. int main(void) {
  16.         struct sockaddr_alg sa = { .salg_family = AF_ALG, .salg_type = "hash",
  17.                         .salg_name = "xilinx-keccak-384" };
  18.         unsigned char digest[SHA384_DIGEST_SZ];
  19.         int i, sockfd, fd;
  20.  
  21.         FILE* in = fopen("data.bin", "rb");
  22.         char *input = (char*) mmap(0, DATA_SZ * sizeof(char),
  23.                         PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fileno(in), 0);
  24.         fclose(in);
  25.  
  26.         sockfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
  27.         bind(sockfd, (struct sockaddr *) &sa, sizeof(sa));
  28.         fd = accept(sockfd, NULL, 0);
  29.         clock_t begin = clock();
  30.         write(fd, input, DATA_SZ);
  31.         read(fd, digest, SHA384_DIGEST_SZ);
  32.         clock_t end = clock();
  33.         close(fd);
  34.         close(sockfd);
  35.  
  36.         munmap(input, DATA_SZ * sizeof(char));
  37.  
  38.         printf("Latency: %lf\n", (double) (end - begin));
  39.         for (i = 0; i < SHA384_DIGEST_SZ; i++)
  40.                 printf("%02x", digest[i]);
  41.         printf("\n");
  42.         return 0;
  43. }
  44.  
View raw paste Reply