1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/srtp/src/crypto/cipher/null_cipher.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +/* 1.5 + * null_cipher.c 1.6 + * 1.7 + * A null cipher implementation. This cipher leaves the plaintext 1.8 + * unchanged. 1.9 + * 1.10 + * David A. McGrew 1.11 + * Cisco Systems, Inc. 1.12 + */ 1.13 + 1.14 +/* 1.15 + * 1.16 + * Copyright (c) 2001-2006, Cisco Systems, Inc. 1.17 + * All rights reserved. 1.18 + * 1.19 + * Redistribution and use in source and binary forms, with or without 1.20 + * modification, are permitted provided that the following conditions 1.21 + * are met: 1.22 + * 1.23 + * Redistributions of source code must retain the above copyright 1.24 + * notice, this list of conditions and the following disclaimer. 1.25 + * 1.26 + * Redistributions in binary form must reproduce the above 1.27 + * copyright notice, this list of conditions and the following 1.28 + * disclaimer in the documentation and/or other materials provided 1.29 + * with the distribution. 1.30 + * 1.31 + * Neither the name of the Cisco Systems, Inc. nor the names of its 1.32 + * contributors may be used to endorse or promote products derived 1.33 + * from this software without specific prior written permission. 1.34 + * 1.35 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.36 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.37 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 1.38 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 1.39 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 1.40 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.41 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 1.42 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.43 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1.44 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.45 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1.46 + * OF THE POSSIBILITY OF SUCH DAMAGE. 1.47 + * 1.48 + */ 1.49 + 1.50 +#include "datatypes.h" 1.51 +#include "null_cipher.h" 1.52 +#include "alloc.h" 1.53 + 1.54 +/* the null_cipher uses the cipher debug module */ 1.55 + 1.56 +extern debug_module_t mod_cipher; 1.57 + 1.58 +err_status_t 1.59 +null_cipher_alloc(cipher_t **c, int key_len) { 1.60 + extern cipher_type_t null_cipher; 1.61 + uint8_t *pointer; 1.62 + 1.63 + debug_print(mod_cipher, 1.64 + "allocating cipher with key length %d", key_len); 1.65 + 1.66 + /* allocate memory a cipher of type null_cipher */ 1.67 + pointer = (uint8_t*)crypto_alloc(sizeof(null_cipher_ctx_t) + sizeof(cipher_t)); 1.68 + if (pointer == NULL) 1.69 + return err_status_alloc_fail; 1.70 + 1.71 + /* set pointers */ 1.72 + *c = (cipher_t *)pointer; 1.73 + (*c)->type = &null_cipher; 1.74 + (*c)->state = pointer + sizeof(cipher_t); 1.75 + 1.76 + /* set key size */ 1.77 + (*c)->key_len = key_len; 1.78 + 1.79 + /* increment ref_count */ 1.80 + null_cipher.ref_count++; 1.81 + 1.82 + return err_status_ok; 1.83 + 1.84 +} 1.85 + 1.86 +err_status_t 1.87 +null_cipher_dealloc(cipher_t *c) { 1.88 + extern cipher_type_t null_cipher; 1.89 + 1.90 + /* zeroize entire state*/ 1.91 + octet_string_set_to_zero((uint8_t *)c, 1.92 + sizeof(null_cipher_ctx_t) + sizeof(cipher_t)); 1.93 + 1.94 + /* free memory of type null_cipher */ 1.95 + crypto_free(c); 1.96 + 1.97 + /* decrement reference count */ 1.98 + null_cipher.ref_count--; 1.99 + 1.100 + return err_status_ok; 1.101 + 1.102 +} 1.103 + 1.104 +err_status_t 1.105 +null_cipher_init(null_cipher_ctx_t *ctx, const uint8_t *key, int key_len) { 1.106 + 1.107 + debug_print(mod_cipher, "initializing null cipher", NULL); 1.108 + 1.109 + return err_status_ok; 1.110 +} 1.111 + 1.112 +err_status_t 1.113 +null_cipher_set_iv(null_cipher_ctx_t *c, void *iv) { 1.114 + return err_status_ok; 1.115 +} 1.116 + 1.117 +err_status_t 1.118 +null_cipher_encrypt(null_cipher_ctx_t *c, 1.119 + unsigned char *buf, unsigned int *bytes_to_encr) { 1.120 + return err_status_ok; 1.121 +} 1.122 + 1.123 +char 1.124 +null_cipher_description[] = "null cipher"; 1.125 + 1.126 +cipher_test_case_t 1.127 +null_cipher_test_0 = { 1.128 + 0, /* octets in key */ 1.129 + NULL, /* key */ 1.130 + 0, /* packet index */ 1.131 + 0, /* octets in plaintext */ 1.132 + NULL, /* plaintext */ 1.133 + 0, /* octets in plaintext */ 1.134 + NULL, /* ciphertext */ 1.135 + NULL /* pointer to next testcase */ 1.136 +}; 1.137 + 1.138 + 1.139 +/* 1.140 + * note: the decrypt function is idential to the encrypt function 1.141 + */ 1.142 + 1.143 +cipher_type_t null_cipher = { 1.144 + (cipher_alloc_func_t) null_cipher_alloc, 1.145 + (cipher_dealloc_func_t) null_cipher_dealloc, 1.146 + (cipher_init_func_t) null_cipher_init, 1.147 + (cipher_encrypt_func_t) null_cipher_encrypt, 1.148 + (cipher_decrypt_func_t) null_cipher_encrypt, 1.149 + (cipher_set_iv_func_t) null_cipher_set_iv, 1.150 + (char *) null_cipher_description, 1.151 + (int) 0, 1.152 + (cipher_test_case_t *) &null_cipher_test_0, 1.153 + (debug_module_t *) NULL, 1.154 + (cipher_type_id_t) NULL_CIPHER 1.155 +}; 1.156 +