📄 cipher.c
字号:
/* * cipher.c * * cipher meta-functions * * David A. McGrew * Cisco Systems, Inc. * *//* * * Copyright (c) 2001, 2002, 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 "cipher.h"#define PRINT_DEBUG 0 /* set to 1 to print debugging data */#include <time.h> /* for clock() */#if PRINT_DEBUG#include <stdio.h>#endif/* some bookkeeping functions */intcipher_get_key_length(const cipher_t *c) { return c->key_len;}intcipher_get_salt_length(const cipher_t *c) { return c->key_len;}/* * cipher_type_self_test(ct) tests a cipher of type ct against test cases * provided in an array of values of key, salt, xtd_seq_num_t, * plaintext, and ciphertext that is known to be good */#define SELF_TEST_BUF_OCTETS 64err_status_tcipher_type_self_test(const cipher_type_t *ct) { const cipher_test_case_t *test_case = ct->test_data; cipher_t *c; err_status_t status; octet_t buffer[SELF_TEST_BUF_OCTETS]; int i, case_num = 0;#if PRINT_DEBUG printf("running self-test for cipher %s\n", ct->description);#endif /* loop over all test cases */ while (test_case != NULL) { /* allocate cipher */ status = cipher_type_alloc(ct, &c, test_case->key_length_octets); if (status) return status; /* initialize cipher */ status = cipher_init(c, test_case->key, test_case->salt); if (status) { cipher_dealloc(c); return status; } /* copy plaintext into test buffer */ if (test_case->plaintext_length_octets > SELF_TEST_BUF_OCTETS) { cipher_dealloc(c); return err_status_bad_param; } for (i=0; i < test_case->plaintext_length_octets; i++) buffer[i] = test_case->plaintext[i];#if PRINT_DEBUG printf("plaintext: %s\n", octet_string_hex_string(buffer, test_case->plaintext_length_octets));#endif /* set the segment index */ status = cipher_set_segment(c, test_case->idx); if (status) { cipher_dealloc(c); return status; } /* encrypt */ status = cipher_encrypt(c, buffer, test_case->plaintext_length_octets); if (status) { cipher_dealloc(c); return status; } /* compare the resulting ciphertext with that in the test case */ status = err_status_ok; for (i=0; i < test_case->plaintext_length_octets; i++) if (buffer[i] != test_case->ciphertext[i]) { status = err_status_algo_fail;#if PRINT_DEBUG printf("test case %d failed at byte %d\n", case_num, i); #endif } if (status) {#if PRINT_DEBUG printf("c computed: %s\n", octet_string_hex_string(buffer, 2*test_case->plaintext_length_octets)); printf("c expected: %s\n", octet_string_hex_string(test_case->ciphertext, 2*test_case->plaintext_length_octets));#endif cipher_dealloc(c); return err_status_algo_fail; } /* deallocate the cipher */ status = cipher_dealloc(c); if (status) return status; /* * the cipher passed the test case, so move on to the next test * case in the list; if NULL, we'll quit and return an OK */ test_case = test_case->next_test_case; ++case_num; } return err_status_ok;}/* * cipher_bits_per_second(c, l, t) computes (and estimate of) the * number of bits that a cipher implementation can encrypt in a second * * c is a cipher (which MUST be allocated an initialized already), l * is the length in octets of the test data to be encrypted, and t is * the number of trials * * if an error is encountered, the value 0.0 is returned */doublecipher_bits_per_second(cipher_t *c, int octets_in_buffer, int num_trials) { int i; xtd_seq_num_t idx; clock_t timer; unsigned char *enc_buf; enc_buf = malloc(octets_in_buffer); if (enc_buf == NULL) return 0.0; /* indicate bad parameters by returning null */ /* time repeated trials */ index_init(&idx); timer = clock(); for(i=0; i < num_trials; i++, index_advance(&idx, 1)) { cipher_set_segment(c, idx); cipher_encrypt(c, enc_buf, octets_in_buffer); } timer = clock() - timer; free(enc_buf); return (double) CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -