/*
* cipher_driver.c
*
* A driver for the generic cipher type
*
* David A. McGrew
* Cisco Systems, Inc.
*/
/*
*
* Copyright (c) 2001-2006, Cisco Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* Neither the name of the Cisco Systems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h> /* for printf() */
#include <stdlib.h> /* for rand() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for getopt() */
#include "cipher.h"
#include "aes_icm.h"
#include "null_cipher.h"
#define PRINT_DEBUG 0
void
cipher_driver_test_throughput(cipher_t *c);
err_status_t
cipher_driver_self_test(cipher_type_t *ct);
/*
* cipher_driver_test_buffering(ct) tests the cipher's output
* buffering for correctness by checking the consistency of succesive
* calls
*/
err_status_t
cipher_driver_test_buffering(cipher_t *c);
/*
* functions for testing cipher cache thrash
*/
err_status_t
cipher_driver_test_array_throughput(cipher_type_t *ct,
int klen, int num_cipher);
void
cipher_array_test_throughput(cipher_t *ca[], int num_cipher);
uint64_t
cipher_array_bits_per_second(cipher_t *cipher_array[], int num_cipher,
unsigned octets_in_buffer, int num_trials);
err_status_t
cipher_array_delete(cipher_t *cipher_array[], int num_cipher);
err_status_t
cipher_array_alloc_init(cipher_t ***cipher_array, int num_ciphers,
cipher_type_t *ctype, int klen);
void
usage(char *prog_name) {
printf("usage: %s [ -t | -v | -a ]\n", prog_name);
exit(255);
}
void
check_status(err_status_t s) {
if (s) {
printf("error (code %d)\n", s);
exit(s);
}
return;
}
/*
* null_cipher, aes_icm, and aes_cbc are the cipher meta-objects
* defined in the files in crypto/cipher subdirectory. these are
* declared external so that we can use these cipher types here
*/
extern cipher_type_t null_cipher;
extern cipher_type_t aes_icm;
extern cipher_type_t aes_cbc;
int
main(int argc, char *argv[]) {
cipher_t *c = NULL;
err_status_t status;
unsigned char test_key[20] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13
};
int q;
unsigned do_timing_test = 0;
unsigned do_validation = 0;
unsigned do_array_timing_test = 0;
/* process input arguments */
while (1) {
q = getopt(argc, argv, "tva");
if (q == -1)
break;
switch (q) {
case 't':
do_timing_test = 1;
break;
case 'v':
do_validation = 1;
break;
case 'a':
do_array_timing_test = 1;
break;
default:
usage(argv[0]);
}
}
printf("cipher test driver\n"
"David A. McGrew\n"
"Cisco Systems, Inc.\n");
if (!do_validation && !do_timing_test && !do_array_timing_test)
usage(argv[0]);
/* arry timing (cache thrash) test */
if (do_array_timing_test) {
int max_num_cipher = 1 << 16; /* number of ciphers in cipher_array */
int num_cipher;
for (num_cipher=1; num_cipher < max_num_cipher; num_cipher *=8)
cipher_driver_test_array_throughput(&null_cipher, 0, num_cipher);
for (num_cipher=1; num_cipher < max_num_cipher; num_cipher *=8)
cipher_driver_test_array_throughput(&aes_icm, 30, num_cipher);
for (num_cipher=1; num_cipher < max_num_cipher; num_cipher *=8)
cipher_driver_test_array_throughput(&aes_cbc, 16, num_cipher);
}
if (do_validation) {
cipher_driver_self_test(&null_cipher);
cipher_driver_self_test(&aes_icm);
cipher_driver_self_test(&aes_cbc);
}
/* do timing and/or buffer_test on null_cipher */
status = cipher_type_alloc(&null_cipher, &c, 0);
check_status(status);
status = cipher_init(c, NULL, direction_encrypt);
check_status(status);
if (do_timing_test)
cipher_driver_test_throughput(c);
if (do_validation) {
status = cipher_driver_test_buffering(c);
check_status(status);
}
status = cipher_dealloc(c);
check_status(status);
/* run the throughput test on the aes_icm cipher */
status = cipher_type_alloc(&aes_icm, &c, 30);
if (status) {
fprintf(stderr, "error: can't allocate cipher\n");
exit(status);
}
status = cipher_init(c, test_key, direction_encrypt);
check_status(status);
if (do_timing_test)
cipher_driver_test_throughput(c);
if (do_validation) {
status = cipher_driver_test_buffering(c);
check_status(status);
}
status = cipher_dealloc(c);
check_status(status);
return 0;
}
void
cipher_driver_test_throughput(cipher_t *c) {
int i;
int min_enc_len = 32;
int max_enc_len = 2048; /* should be a power of two */
int num_trials = 100000;
printf("timing %s throughput:\n", c->type->description);
fflush(stdout);
for (i=min_enc_len; i <= max_enc_len; i = i * 2)
printf("msg len: %d\tgigabits per second: %f\n",
i, cipher_bits_per_second(c, i, num_trials) / 1e9);
}
err_status_t
cipher_driver_self_test(cipher_type_t *ct) {
err_status_t status;
printf("running cipher self-test for %s...", ct->description);
status = cipher_type_self_test(ct);
if (status) {
printf("failed with error code %d\n", status);
exit(status);
}
printf("passed\n");
return err_status_ok;
}
/*
* cipher_driver_test_buffering(ct) tests the cipher's output
* buffering for correctness by checking the consistency of succesive
* calls
*/
err_status_t
cipher_driver_test_buffering(cipher_t *c) {
int i, j, num_trials = 1000;
unsigned len, buflen = 1024;
uint8_t buffer0[buflen], buffer1[buflen], *current, *end;
uint8_t idx[16] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34
};
err_status_t status;
printf("testing output buffering for cipher %s...",
c->type->description);
for (i=0; i < num_trials; i++) {
/* set buffers to zero */
for (j=0; j < buflen; j++)
buffer0[j] = buffer1[j] = 0;
/* initialize cipher */
status = cipher_set_iv(c, idx);
if (status)
return status;
/* generate 'reference' value by encrypting all at once */
status = cipher_encrypt(c, buffer0, &buflen);
if (status)
return status;
/* re-initialize cipher */
status = cipher_set_iv(c, idx);
if (status)
return status;
/* now loop over short lengths until buffer1 is encrypted */
current = buffer1;
end = buffer1 + buflen;
while (current < end) {
/* choose a short length */
len = rand() & 0x0
cipher_driver.rar_The Test
版权申诉
61 浏览量
2022-09-24
07:08:08
上传
评论
收藏 4KB RAR 举报
JonSco
- 粉丝: 72
- 资源: 1万+
最新资源
- threadmanager.cpp
- 腾讯云小程序 - 一站式开发与部署平台
- 基于JSP+Java+Servlet采用MVC模式开发的购物网站+源码(毕业设计&课程设计&项目开发)
- 基于组态王的升降式横移立体车库控制系统+源码(毕业设计&课程设计&项目开发)
- 基于python+Django和协同过滤算法的电影推荐系统+源码(毕业设计&课程设计&项目开发)
- 环境配置 vscode+jupyter
- 项目全部代码,还包含使用到的图片
- 项目全部代码,还包含使用到的图片
- 基于java+MapReduce实现基于物品协同过滤算法,即电影推荐系统+源码+开发文档+算法解析(毕业设计&课程设计&项目开发
- 节点编程(整合所有节点编程代码).7z
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈