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 
srtp_null_cipher_alloc(srtp_cipher_t ** c,int key_len,int tlen)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 
63     debug_print(srtp_mod_cipher, "allocating cipher with key length %d",
64                 key_len);
65 
66     /* allocate memory a cipher of type null_cipher */
67     *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
68     if (*c == NULL) {
69         return srtp_err_status_alloc_fail;
70     }
71 
72     /* set pointers */
73     (*c)->algorithm = SRTP_NULL_CIPHER;
74     (*c)->type = &srtp_null_cipher;
75     (*c)->state = (void *)0x1; /* The null cipher does not maintain state */
76 
77     /* set key size */
78     (*c)->key_len = key_len;
79 
80     return srtp_err_status_ok;
81 }
82 
srtp_null_cipher_dealloc(srtp_cipher_t * c)83 static srtp_err_status_t srtp_null_cipher_dealloc(srtp_cipher_t *c)
84 {
85     extern const srtp_cipher_type_t srtp_null_cipher;
86 
87     /* zeroize entire state*/
88     octet_string_set_to_zero(c, sizeof(srtp_cipher_t));
89 
90     /* free memory of type null_cipher */
91     srtp_crypto_free(c);
92 
93     return srtp_err_status_ok;
94 }
95 
srtp_null_cipher_init(void * cv,const uint8_t * key)96 static srtp_err_status_t srtp_null_cipher_init(void *cv, const uint8_t *key)
97 {
98     /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
99 
100     debug_print(srtp_mod_cipher, "initializing null cipher", NULL);
101 
102     return srtp_err_status_ok;
103 }
104 
srtp_null_cipher_set_iv(void * cv,uint8_t * iv,srtp_cipher_direction_t dir)105 static srtp_err_status_t srtp_null_cipher_set_iv(void *cv,
106                                                  uint8_t *iv,
107                                                  srtp_cipher_direction_t dir)
108 {
109     /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
110     return srtp_err_status_ok;
111 }
112 
srtp_null_cipher_encrypt(void * cv,unsigned char * buf,unsigned int * bytes_to_encr)113 static srtp_err_status_t srtp_null_cipher_encrypt(void *cv,
114                                                   unsigned char *buf,
115                                                   unsigned int *bytes_to_encr)
116 {
117     /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
118     return srtp_err_status_ok;
119 }
120 
121 static const char srtp_null_cipher_description[] = "null cipher";
122 
123 static const srtp_cipher_test_case_t srtp_null_cipher_test_0 = {
124     0,    /* octets in key            */
125     NULL, /* key                      */
126     0,    /* packet index             */
127     0,    /* octets in plaintext      */
128     NULL, /* plaintext                */
129     0,    /* octets in plaintext      */
130     NULL, /* ciphertext               */
131     0,    /* */
132     NULL, /* */
133     0,    /* */
134     NULL  /* pointer to next testcase */
135 };
136 
137 /*
138  * note: the decrypt function is idential to the encrypt function
139  */
140 
141 const srtp_cipher_type_t srtp_null_cipher = {
142     srtp_null_cipher_alloc,       /* */
143     srtp_null_cipher_dealloc,     /* */
144     srtp_null_cipher_init,        /* */
145     0,                            /* set_aad */
146     srtp_null_cipher_encrypt,     /* */
147     srtp_null_cipher_encrypt,     /* */
148     srtp_null_cipher_set_iv,      /* */
149     0,                            /* get_tag */
150     srtp_null_cipher_description, /* */
151     &srtp_null_cipher_test_0,     /* */
152     SRTP_NULL_CIPHER              /* */
153 };
154