michael@0: /* michael@0: * cipher.c michael@0: * michael@0: * cipher meta-functions michael@0: * michael@0: * David A. McGrew michael@0: * Cisco Systems, Inc. michael@0: * michael@0: */ michael@0: michael@0: /* michael@0: * michael@0: * Copyright (c) 2001-2006, Cisco Systems, Inc. michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * Redistributions in binary form must reproduce the above michael@0: * copyright notice, this list of conditions and the following michael@0: * disclaimer in the documentation and/or other materials provided michael@0: * with the distribution. michael@0: * michael@0: * Neither the name of the Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS michael@0: * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE michael@0: * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, michael@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR michael@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, michael@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED michael@0: * OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: #include "cipher.h" michael@0: #include "rand_source.h" /* used in invertibiltiy tests */ michael@0: #include "alloc.h" /* for crypto_alloc(), crypto_free() */ michael@0: michael@0: debug_module_t mod_cipher = { michael@0: 0, /* debugging is off by default */ michael@0: "cipher" /* printable module name */ michael@0: }; michael@0: michael@0: err_status_t michael@0: cipher_output(cipher_t *c, uint8_t *buffer, int num_octets_to_output) { michael@0: michael@0: /* zeroize the buffer */ michael@0: octet_string_set_to_zero(buffer, num_octets_to_output); michael@0: michael@0: /* exor keystream into buffer */ michael@0: return cipher_encrypt(c, buffer, (unsigned int *) &num_octets_to_output); michael@0: } michael@0: michael@0: /* some bookkeeping functions */ michael@0: michael@0: int michael@0: cipher_get_key_length(const cipher_t *c) { michael@0: return c->key_len; michael@0: } michael@0: michael@0: /* michael@0: * cipher_type_test(ct, test_data) tests a cipher of type ct against michael@0: * test cases provided in a list test_data of values of key, salt, iv, michael@0: * plaintext, and ciphertext that is known to be good michael@0: */ michael@0: michael@0: #define SELF_TEST_BUF_OCTETS 128 michael@0: #define NUM_RAND_TESTS 128 michael@0: #define MAX_KEY_LEN 64 michael@0: michael@0: err_status_t michael@0: cipher_type_test(const cipher_type_t *ct, const cipher_test_case_t *test_data) { michael@0: const cipher_test_case_t *test_case = test_data; michael@0: cipher_t *c; michael@0: err_status_t status; michael@0: uint8_t buffer[SELF_TEST_BUF_OCTETS]; michael@0: uint8_t buffer2[SELF_TEST_BUF_OCTETS]; michael@0: unsigned int len; michael@0: int i, j, case_num = 0; michael@0: michael@0: debug_print(mod_cipher, "running self-test for cipher %s", michael@0: ct->description); michael@0: michael@0: /* michael@0: * check to make sure that we have at least one test case, and michael@0: * return an error if we don't - we need to be paranoid here michael@0: */ michael@0: if (test_case == NULL) michael@0: return err_status_cant_check; michael@0: michael@0: /* michael@0: * loop over all test cases, perform known-answer tests of both the michael@0: * encryption and decryption functions michael@0: */ michael@0: while (test_case != NULL) { michael@0: michael@0: /* allocate cipher */ michael@0: status = cipher_type_alloc(ct, &c, test_case->key_length_octets); michael@0: if (status) michael@0: return status; michael@0: michael@0: /* michael@0: * test the encrypt function michael@0: */ michael@0: debug_print(mod_cipher, "testing encryption", NULL); michael@0: michael@0: /* initialize cipher */ michael@0: status = cipher_init(c, test_case->key, direction_encrypt); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: /* copy plaintext into test buffer */ michael@0: if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) { michael@0: cipher_dealloc(c); michael@0: return err_status_bad_param; michael@0: } michael@0: for (i=0; i < test_case->plaintext_length_octets; i++) michael@0: buffer[i] = test_case->plaintext[i]; michael@0: michael@0: debug_print(mod_cipher, "plaintext: %s", michael@0: octet_string_hex_string(buffer, michael@0: test_case->plaintext_length_octets)); michael@0: michael@0: /* set the initialization vector */ michael@0: status = cipher_set_iv(c, test_case->idx); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: /* encrypt */ michael@0: len = test_case->plaintext_length_octets; michael@0: status = cipher_encrypt(c, buffer, &len); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: debug_print(mod_cipher, "ciphertext: %s", michael@0: octet_string_hex_string(buffer, michael@0: test_case->ciphertext_length_octets)); michael@0: michael@0: /* compare the resulting ciphertext with that in the test case */ michael@0: if (len != (unsigned int)test_case->ciphertext_length_octets) michael@0: return err_status_algo_fail; michael@0: status = err_status_ok; michael@0: for (i=0; i < test_case->ciphertext_length_octets; i++) michael@0: if (buffer[i] != test_case->ciphertext[i]) { michael@0: status = err_status_algo_fail; michael@0: debug_print(mod_cipher, "test case %d failed", case_num); michael@0: debug_print(mod_cipher, "(failure at byte %d)", i); michael@0: break; michael@0: } michael@0: if (status) { michael@0: michael@0: debug_print(mod_cipher, "c computed: %s", michael@0: octet_string_hex_string(buffer, michael@0: 2*test_case->plaintext_length_octets)); michael@0: debug_print(mod_cipher, "c expected: %s", michael@0: octet_string_hex_string(test_case->ciphertext, michael@0: 2*test_case->plaintext_length_octets)); michael@0: michael@0: cipher_dealloc(c); michael@0: return err_status_algo_fail; michael@0: } michael@0: michael@0: /* michael@0: * test the decrypt function michael@0: */ michael@0: debug_print(mod_cipher, "testing decryption", NULL); michael@0: michael@0: /* re-initialize cipher for decryption */ michael@0: status = cipher_init(c, test_case->key, direction_decrypt); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: /* copy ciphertext into test buffer */ michael@0: if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) { michael@0: cipher_dealloc(c); michael@0: return err_status_bad_param; michael@0: } michael@0: for (i=0; i < test_case->ciphertext_length_octets; i++) michael@0: buffer[i] = test_case->ciphertext[i]; michael@0: michael@0: debug_print(mod_cipher, "ciphertext: %s", michael@0: octet_string_hex_string(buffer, michael@0: test_case->plaintext_length_octets)); michael@0: michael@0: /* set the initialization vector */ michael@0: status = cipher_set_iv(c, test_case->idx); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: /* decrypt */ michael@0: len = test_case->ciphertext_length_octets; michael@0: status = cipher_decrypt(c, buffer, &len); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: debug_print(mod_cipher, "plaintext: %s", michael@0: octet_string_hex_string(buffer, michael@0: test_case->plaintext_length_octets)); michael@0: michael@0: /* compare the resulting plaintext with that in the test case */ michael@0: if (len != (unsigned int)test_case->plaintext_length_octets) michael@0: return err_status_algo_fail; michael@0: status = err_status_ok; michael@0: for (i=0; i < test_case->plaintext_length_octets; i++) michael@0: if (buffer[i] != test_case->plaintext[i]) { michael@0: status = err_status_algo_fail; michael@0: debug_print(mod_cipher, "test case %d failed", case_num); michael@0: debug_print(mod_cipher, "(failure at byte %d)", i); michael@0: } michael@0: if (status) { michael@0: michael@0: debug_print(mod_cipher, "p computed: %s", michael@0: octet_string_hex_string(buffer, michael@0: 2*test_case->plaintext_length_octets)); michael@0: debug_print(mod_cipher, "p expected: %s", michael@0: octet_string_hex_string(test_case->plaintext, michael@0: 2*test_case->plaintext_length_octets)); michael@0: michael@0: cipher_dealloc(c); michael@0: return err_status_algo_fail; michael@0: } michael@0: michael@0: /* deallocate the cipher */ michael@0: status = cipher_dealloc(c); michael@0: if (status) michael@0: return status; michael@0: michael@0: /* michael@0: * the cipher passed the test case, so move on to the next test michael@0: * case in the list; if NULL, we'l proceed to the next test michael@0: */ michael@0: test_case = test_case->next_test_case; michael@0: ++case_num; michael@0: } michael@0: michael@0: /* now run some random invertibility tests */ michael@0: michael@0: /* allocate cipher, using paramaters from the first test case */ michael@0: test_case = test_data; michael@0: status = cipher_type_alloc(ct, &c, test_case->key_length_octets); michael@0: if (status) michael@0: return status; michael@0: michael@0: rand_source_init(); michael@0: michael@0: for (j=0; j < NUM_RAND_TESTS; j++) { michael@0: unsigned length; michael@0: int plaintext_len; michael@0: uint8_t key[MAX_KEY_LEN]; michael@0: uint8_t iv[MAX_KEY_LEN]; michael@0: michael@0: /* choose a length at random (leaving room for IV and padding) */ michael@0: length = rand() % (SELF_TEST_BUF_OCTETS - 64); michael@0: debug_print(mod_cipher, "random plaintext length %d\n", length); michael@0: status = rand_source_get_octet_string(buffer, length); michael@0: if (status) return status; michael@0: michael@0: debug_print(mod_cipher, "plaintext: %s", michael@0: octet_string_hex_string(buffer, length)); michael@0: michael@0: /* copy plaintext into second buffer */ michael@0: for (i=0; (unsigned int)i < length; i++) michael@0: buffer2[i] = buffer[i]; michael@0: michael@0: /* choose a key at random */ michael@0: if (test_case->key_length_octets > MAX_KEY_LEN) michael@0: return err_status_cant_check; michael@0: status = rand_source_get_octet_string(key, test_case->key_length_octets); michael@0: if (status) return status; michael@0: michael@0: /* chose a random initialization vector */ michael@0: status = rand_source_get_octet_string(iv, MAX_KEY_LEN); michael@0: if (status) return status; michael@0: michael@0: /* initialize cipher */ michael@0: status = cipher_init(c, key, direction_encrypt); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: /* set initialization vector */ michael@0: status = cipher_set_iv(c, test_case->idx); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: /* encrypt buffer with cipher */ michael@0: plaintext_len = length; michael@0: status = cipher_encrypt(c, buffer, &length); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: debug_print(mod_cipher, "ciphertext: %s", michael@0: octet_string_hex_string(buffer, length)); michael@0: michael@0: /* michael@0: * re-initialize cipher for decryption, re-set the iv, then michael@0: * decrypt the ciphertext michael@0: */ michael@0: status = cipher_init(c, key, direction_decrypt); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: status = cipher_set_iv(c, test_case->idx); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: status = cipher_decrypt(c, buffer, &length); michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return status; michael@0: } michael@0: michael@0: debug_print(mod_cipher, "plaintext[2]: %s", michael@0: octet_string_hex_string(buffer, length)); michael@0: michael@0: /* compare the resulting plaintext with the original one */ michael@0: if (length != (unsigned)plaintext_len) michael@0: return err_status_algo_fail; michael@0: status = err_status_ok; michael@0: for (i=0; i < plaintext_len; i++) michael@0: if (buffer[i] != buffer2[i]) { michael@0: status = err_status_algo_fail; michael@0: debug_print(mod_cipher, "random test case %d failed", case_num); michael@0: debug_print(mod_cipher, "(failure at byte %d)", i); michael@0: } michael@0: if (status) { michael@0: cipher_dealloc(c); michael@0: return err_status_algo_fail; michael@0: } michael@0: michael@0: } michael@0: michael@0: status = cipher_dealloc(c); michael@0: if (status) michael@0: return status; michael@0: michael@0: return err_status_ok; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * cipher_type_self_test(ct) performs cipher_type_test on ct's internal michael@0: * list of test data. michael@0: */ michael@0: michael@0: err_status_t michael@0: cipher_type_self_test(const cipher_type_t *ct) { michael@0: return cipher_type_test(ct, ct->test_data); michael@0: } michael@0: michael@0: /* michael@0: * cipher_bits_per_second(c, l, t) computes (an estimate of) the michael@0: * number of bits that a cipher implementation can encrypt in a second michael@0: * michael@0: * c is a cipher (which MUST be allocated and initialized already), l michael@0: * is the length in octets of the test data to be encrypted, and t is michael@0: * the number of trials michael@0: * michael@0: * if an error is encountered, the value 0 is returned michael@0: */ michael@0: michael@0: uint64_t michael@0: cipher_bits_per_second(cipher_t *c, int octets_in_buffer, int num_trials) { michael@0: int i; michael@0: v128_t nonce; michael@0: clock_t timer; michael@0: unsigned char *enc_buf; michael@0: unsigned int len = octets_in_buffer; michael@0: michael@0: enc_buf = (unsigned char*) crypto_alloc(octets_in_buffer); michael@0: if (enc_buf == NULL) michael@0: return 0; /* indicate bad parameters by returning null */ michael@0: michael@0: /* time repeated trials */ michael@0: v128_set_to_zero(&nonce); michael@0: timer = clock(); michael@0: for(i=0; i < num_trials; i++, nonce.v32[3] = i) { michael@0: cipher_set_iv(c, &nonce); michael@0: cipher_encrypt(c, enc_buf, &len); michael@0: } michael@0: timer = clock() - timer; michael@0: michael@0: crypto_free(enc_buf); michael@0: michael@0: if (timer == 0) { michael@0: /* Too fast! */ michael@0: return 0; michael@0: } michael@0: michael@0: return (uint64_t)CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer; michael@0: }