test_dec.c revision a74702c6
1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */ 250aadb33SDmitri Tikhonov/* 350aadb33SDmitri Tikhonov * test_dec.c -- Benchmark decryption using aligned and non-aligned buffers. 450aadb33SDmitri Tikhonov */ 550aadb33SDmitri Tikhonov 650aadb33SDmitri Tikhonov#include <assert.h> 750aadb33SDmitri Tikhonov#include <stdint.h> 850aadb33SDmitri Tikhonov#include <stdio.h> 950aadb33SDmitri Tikhonov#include <stdlib.h> 1050aadb33SDmitri Tikhonov#include <string.h> 11461e84d8SAmol Deshpande#ifndef WIN32 1250aadb33SDmitri Tikhonov#include <unistd.h> 13461e84d8SAmol Deshpande#else 14461e84d8SAmol Deshpande#include <getopt.h> 15461e84d8SAmol Deshpande#endif 1650aadb33SDmitri Tikhonov 1750aadb33SDmitri Tikhonov#include <openssl/aead.h> 1850aadb33SDmitri Tikhonov#include <openssl/rand.h> 1950aadb33SDmitri Tikhonov 2050aadb33SDmitri Tikhonov#define MAX_SIZE 1400 2150aadb33SDmitri Tikhonov 2250aadb33SDmitri Tikhonovint 2350aadb33SDmitri Tikhonovmain (int argc, char **argv) 2450aadb33SDmitri Tikhonov{ 2550aadb33SDmitri Tikhonov EVP_AEAD_CTX aead_ctx; 2650aadb33SDmitri Tikhonov int opt, n = 1, r; 2750aadb33SDmitri Tikhonov size_t sealed_len, opened_len; 2850aadb33SDmitri Tikhonov unsigned char key[16]; 2950aadb33SDmitri Tikhonov unsigned char data[1328]; 3050aadb33SDmitri Tikhonov unsigned char sealed_buf[63 + MAX_SIZE], *sealed = sealed_buf; 3150aadb33SDmitri Tikhonov unsigned char opened_buf[63 + MAX_SIZE], *opened = opened_buf; 3250aadb33SDmitri Tikhonov 3350aadb33SDmitri Tikhonov while (-1 != (opt = getopt(argc, argv, "an:"))) 3450aadb33SDmitri Tikhonov { 3550aadb33SDmitri Tikhonov switch (opt) 3650aadb33SDmitri Tikhonov { 3750aadb33SDmitri Tikhonov case 'a': 3850aadb33SDmitri Tikhonov if ((uintptr_t) sealed & (64 - 1)) 3950aadb33SDmitri Tikhonov sealed += 64 - ((uintptr_t) sealed & (64 - 1)); 4050aadb33SDmitri Tikhonov if ((uintptr_t) opened & (64 - 1)) 4150aadb33SDmitri Tikhonov opened += 64 - ((uintptr_t) opened & (64 - 1)); 4250aadb33SDmitri Tikhonov break; 4350aadb33SDmitri Tikhonov case 'n': /* Number of decrypt iterations */ 4450aadb33SDmitri Tikhonov n = atoi(optarg); 4550aadb33SDmitri Tikhonov break; 4650aadb33SDmitri Tikhonov default: 4750aadb33SDmitri Tikhonov exit(EXIT_FAILURE); 4850aadb33SDmitri Tikhonov } 4950aadb33SDmitri Tikhonov } 5050aadb33SDmitri Tikhonov 5150aadb33SDmitri Tikhonov RAND_bytes(key, sizeof(key)); 5250aadb33SDmitri Tikhonov RAND_bytes(data, sizeof(data)); 5350aadb33SDmitri Tikhonov 5450aadb33SDmitri Tikhonov EVP_AEAD_CTX_init(&aead_ctx, EVP_aead_aes_128_gcm(), key, sizeof(key), 5550aadb33SDmitri Tikhonov 12, NULL); 5650aadb33SDmitri Tikhonov r = EVP_AEAD_CTX_seal(&aead_ctx, sealed, &sealed_len, MAX_SIZE, 5750aadb33SDmitri Tikhonov key, sizeof(key), data, sizeof(data), NULL, 0); 5850aadb33SDmitri Tikhonov if (!r) 5950aadb33SDmitri Tikhonov { 6050aadb33SDmitri Tikhonov fprintf(stderr, "cannot seal\n"); 6150aadb33SDmitri Tikhonov exit(EXIT_FAILURE); 6250aadb33SDmitri Tikhonov } 6350aadb33SDmitri Tikhonov 6450aadb33SDmitri Tikhonov printf("buffers are %saligned\n", ((uintptr_t) opened & (64 - 1)) || 6550aadb33SDmitri Tikhonov ((uintptr_t) sealed & (64 - 1)) ? "not " : ""); 6650aadb33SDmitri Tikhonov 6750aadb33SDmitri Tikhonov /* Check that decryption works first time around */ 6850aadb33SDmitri Tikhonov r = EVP_AEAD_CTX_open(&aead_ctx, opened, &opened_len, MAX_SIZE, 6950aadb33SDmitri Tikhonov key, sizeof(key), sealed, sealed_len, NULL, 0); 7050aadb33SDmitri Tikhonov assert(r && opened_len == sizeof(data) && 7150aadb33SDmitri Tikhonov 0 == memcmp(data, opened, sizeof(data))); 7250aadb33SDmitri Tikhonov --n; 7350aadb33SDmitri Tikhonov 7450aadb33SDmitri Tikhonov /* Do no bother checking return value in the loop */ 7550aadb33SDmitri Tikhonov while (n-- > 0) 7650aadb33SDmitri Tikhonov { 7750aadb33SDmitri Tikhonov EVP_AEAD_CTX_open(&aead_ctx, opened, &opened_len, MAX_SIZE, 7850aadb33SDmitri Tikhonov key, sizeof(key), sealed, sealed_len, NULL, 0); 7950aadb33SDmitri Tikhonov } 8050aadb33SDmitri Tikhonov 8150aadb33SDmitri Tikhonov exit(EXIT_SUCCESS); 8250aadb33SDmitri Tikhonov} 83