/*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/* Internal tests for the modes module */
#include <stdio.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/modes.h>
#include "../crypto/modes/modes_lcl.h"
#include "testutil.h"
#include "e_os.h"
typedef struct {
size_t size;
const unsigned char *data;
} SIZED_DATA;
/**********************************************************************
*
* Test of cts128
*
***/
typedef struct {
const char *case_name;
int num;
const AES_KEY *encrypt_key_schedule;
const AES_KEY *decrypt_key_schedule;
const unsigned char *input;
SIZED_DATA iv;
const SIZED_DATA *vector;
} CTS128_FIXTURE;
static CTS128_FIXTURE setup_cts128(const char *const test_case_name)
{
CTS128_FIXTURE fixture;
fixture.case_name = test_case_name;
return fixture;
}
static int execute_cts128(CTS128_FIXTURE fixture)
{
const unsigned char *test_iv = fixture.iv.data;
size_t test_iv_len = fixture.iv.size;
const unsigned char *vector = fixture.vector->data;
size_t len = fixture.vector->size;
const unsigned char *test_input = fixture.input;
const AES_KEY *encrypt_key_schedule = fixture.encrypt_key_schedule;
const AES_KEY *decrypt_key_schedule = fixture.decrypt_key_schedule;
unsigned char iv[16];
unsigned char cleartext[64], ciphertext[64];
size_t tail;
fprintf(stderr, "vector_%" OSSLzu "\n", len);
fflush(stdout);
if ((tail = len % 16) == 0)
tail = 16;
tail += 16;
/* test block-based encryption */
memcpy(iv, test_iv, test_iv_len);
CRYPTO_cts128_encrypt_block(test_input, ciphertext, len,
encrypt_key_schedule, iv,
(block128_f)AES_encrypt);
if (memcmp(ciphertext, vector, len)) {
fprintf(stderr, "block encrypt: output_%" OSSLzu " mismatch\n", len);
return 0;
}
if (memcmp(iv, vector + len - tail, sizeof(iv))) {
fprintf(stderr, "block encrypt: iv_%" OSSLzu " mismatch\n", len);
return 0;
}
/* test block-based decryption */
memcpy(iv, test_iv, test_iv_len);
CRYPTO_cts128_decrypt_block(ciphertext, cleartext, len,
decrypt_key_schedule, iv,
(block128_f)AES_decrypt);
if (memcmp(cleartext, test_input, len)) {
fprintf(stderr, "block decrypt: input_%" OSSLzu " mismatch\n", len);
return 0;
}
if (memcmp(iv, vector + len - tail, sizeof(iv))) {
fprintf(stderr, "block decrypt: iv_%" OSSLzu " mismatch\n", len);
return 0;
}
/* test streamed encryption */
memcpy(iv, test_iv, test_iv_len);
CRYPTO_cts128_encrypt(test_input, ciphertext, len, encrypt_key_schedule,
iv,