null_cipher.c (5154B)
1 /* 2 * null_cipher.c 3 * 4 * A null cipher implementation. This cipher leaves the plaintext 5 * unchanged. 6 * 7 * David A. McGrew 8 * Cisco Systems, Inc. 9 */ 10 11 /* 12 * 13 * Copyright (c) 2001-2017 Cisco Systems, Inc. 14 * All rights reserved. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 20 * Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 23 * Redistributions in binary form must reproduce the above 24 * copyright notice, this list of conditions and the following 25 * disclaimer in the documentation and/or other materials provided 26 * with the distribution. 27 * 28 * Neither the name of the Cisco Systems, Inc. nor the names of its 29 * contributors may be used to endorse or promote products derived 30 * from this software without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 36 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 43 * OF THE POSSIBILITY OF SUCH DAMAGE. 44 * 45 */ 46 47 #ifdef HAVE_CONFIG_H 48 #include <config.h> 49 #endif 50 51 #include "datatypes.h" 52 #include "null_cipher.h" 53 #include "err.h" /* for srtp_debug */ 54 #include "alloc.h" 55 #include "cipher_types.h" 56 57 static srtp_err_status_t srtp_null_cipher_alloc(srtp_cipher_t **c, 58 int key_len, 59 int tlen) 60 { 61 extern const srtp_cipher_type_t srtp_null_cipher; 62 (void)tlen; 63 64 debug_print(srtp_mod_cipher, "allocating cipher with key length %d", 65 key_len); 66 67 /* allocate memory a cipher of type null_cipher */ 68 *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t)); 69 if (*c == NULL) { 70 return srtp_err_status_alloc_fail; 71 } 72 73 /* set pointers */ 74 (*c)->algorithm = SRTP_NULL_CIPHER; 75 (*c)->type = &srtp_null_cipher; 76 (*c)->state = (void *)0x1; /* The null cipher does not maintain state */ 77 78 /* set key size */ 79 (*c)->key_len = key_len; 80 81 return srtp_err_status_ok; 82 } 83 84 static srtp_err_status_t srtp_null_cipher_dealloc(srtp_cipher_t *c) 85 { 86 extern const srtp_cipher_type_t srtp_null_cipher; 87 88 /* zeroize entire state*/ 89 octet_string_set_to_zero(c, sizeof(srtp_cipher_t)); 90 91 /* free memory of type null_cipher */ 92 srtp_crypto_free(c); 93 94 return srtp_err_status_ok; 95 } 96 97 static srtp_err_status_t srtp_null_cipher_init(void *cv, const uint8_t *key) 98 { 99 /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */ 100 (void)cv; 101 (void)key; 102 debug_print0(srtp_mod_cipher, "initializing null cipher"); 103 104 return srtp_err_status_ok; 105 } 106 107 static srtp_err_status_t srtp_null_cipher_set_iv(void *cv, 108 uint8_t *iv, 109 srtp_cipher_direction_t dir) 110 { 111 /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */ 112 (void)cv; 113 (void)iv; 114 (void)dir; 115 return srtp_err_status_ok; 116 } 117 118 static srtp_err_status_t srtp_null_cipher_encrypt(void *cv, 119 unsigned char *buf, 120 unsigned int *bytes_to_encr) 121 { 122 /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */ 123 (void)cv; 124 (void)buf; 125 (void)bytes_to_encr; 126 return srtp_err_status_ok; 127 } 128 129 static const char srtp_null_cipher_description[] = "null cipher"; 130 131 static const srtp_cipher_test_case_t srtp_null_cipher_test_0 = { 132 0, /* octets in key */ 133 NULL, /* key */ 134 0, /* packet index */ 135 0, /* octets in plaintext */ 136 NULL, /* plaintext */ 137 0, /* octets in plaintext */ 138 NULL, /* ciphertext */ 139 0, /* */ 140 NULL, /* */ 141 0, /* */ 142 NULL /* pointer to next testcase */ 143 }; 144 145 /* 146 * note: the decrypt function is idential to the encrypt function 147 */ 148 149 const srtp_cipher_type_t srtp_null_cipher = { 150 srtp_null_cipher_alloc, /* */ 151 srtp_null_cipher_dealloc, /* */ 152 srtp_null_cipher_init, /* */ 153 0, /* set_aad */ 154 srtp_null_cipher_encrypt, /* */ 155 srtp_null_cipher_encrypt, /* */ 156 srtp_null_cipher_set_iv, /* */ 157 0, /* get_tag */ 158 srtp_null_cipher_description, /* */ 159 &srtp_null_cipher_test_0, /* */ 160 SRTP_NULL_CIPHER /* */ 161 };