Coverage Report

Created: 2025-07-18 06:15

/src/libsrtp/crypto/cipher/null_cipher.c
Line
Count
Source (jump to first uncovered line)
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
                                                size_t key_len,
59
                                                size_t tlen)
60
43.0k
{
61
43.0k
    extern const srtp_cipher_type_t srtp_null_cipher;
62
43.0k
    (void)tlen;
63
64
43.0k
    debug_print(srtp_mod_cipher, "allocating cipher with key length %zu",
65
43.0k
                key_len);
66
67
    /* allocate memory a cipher of type null_cipher */
68
43.0k
    *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
69
43.0k
    if (*c == NULL) {
70
0
        return srtp_err_status_alloc_fail;
71
0
    }
72
73
    /* set pointers */
74
43.0k
    (*c)->algorithm = SRTP_NULL_CIPHER;
75
43.0k
    (*c)->type = &srtp_null_cipher;
76
43.0k
    (*c)->state = (void *)0x1; /* The null cipher does not maintain state */
77
78
    /* set key size */
79
43.0k
    (*c)->key_len = key_len;
80
81
43.0k
    return srtp_err_status_ok;
82
43.0k
}
83
84
static srtp_err_status_t srtp_null_cipher_dealloc(srtp_cipher_t *c)
85
43.0k
{
86
43.0k
    extern const srtp_cipher_type_t srtp_null_cipher;
87
88
    /* zeroize entire state*/
89
43.0k
    octet_string_set_to_zero(c, sizeof(srtp_cipher_t));
90
91
    /* free memory of type null_cipher */
92
43.0k
    srtp_crypto_free(c);
93
94
43.0k
    return srtp_err_status_ok;
95
43.0k
}
96
97
static srtp_err_status_t srtp_null_cipher_init(void *cv, const uint8_t *key)
98
42.9k
{
99
    /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
100
42.9k
    (void)cv;
101
42.9k
    (void)key;
102
42.9k
    debug_print0(srtp_mod_cipher, "initializing null cipher");
103
104
42.9k
    return srtp_err_status_ok;
105
42.9k
}
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
136k
{
111
    /* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
112
136k
    (void)cv;
113
136k
    (void)iv;
114
136k
    (void)dir;
115
136k
    return srtp_err_status_ok;
116
136k
}
117
118
static srtp_err_status_t srtp_null_cipher_encrypt(void *cv,
119
                                                  const uint8_t *src,
120
                                                  size_t src_len,
121
                                                  uint8_t *dst,
122
                                                  size_t *dst_len)
123
64.4k
{
124
64.4k
    (void)cv;
125
64.4k
    if (src != dst) {
126
0
        if (*dst_len < src_len) {
127
0
            return srtp_err_status_buffer_small;
128
0
        }
129
0
        memcpy(dst, src, src_len);
130
0
    }
131
64.4k
    *dst_len = src_len;
132
64.4k
    return srtp_err_status_ok;
133
64.4k
}
134
135
static const char srtp_null_cipher_description[] = "null cipher";
136
137
static const srtp_cipher_test_case_t srtp_null_cipher_test_0 = {
138
    0,    /* octets in key            */
139
    NULL, /* key                      */
140
    0,    /* packet index             */
141
    0,    /* octets in plaintext      */
142
    NULL, /* plaintext                */
143
    0,    /* octets in plaintext      */
144
    NULL, /* ciphertext               */
145
    0,    /* */
146
    NULL, /* */
147
    0,    /* */
148
    NULL  /* pointer to next testcase */
149
};
150
151
/*
152
 * note: the decrypt function is identical to the encrypt function
153
 */
154
155
const srtp_cipher_type_t srtp_null_cipher = {
156
    srtp_null_cipher_alloc,       /* */
157
    srtp_null_cipher_dealloc,     /* */
158
    srtp_null_cipher_init,        /* */
159
    0,                            /* set_aad */
160
    srtp_null_cipher_encrypt,     /* */
161
    srtp_null_cipher_encrypt,     /* */
162
    srtp_null_cipher_set_iv,      /* */
163
    srtp_null_cipher_description, /* */
164
    &srtp_null_cipher_test_0,     /* */
165
    SRTP_NULL_CIPHER              /* */
166
};