xref: /aosp_15_r20/external/libsrtp2/crypto/include/cipher.h (revision 90e502c7aef8d77d0622bb67d75435c6190cfc1a)
1*90e502c7SAndroid Build Coastguard Worker /*
2*90e502c7SAndroid Build Coastguard Worker  * cipher.h
3*90e502c7SAndroid Build Coastguard Worker  *
4*90e502c7SAndroid Build Coastguard Worker  * common interface to ciphers
5*90e502c7SAndroid Build Coastguard Worker  *
6*90e502c7SAndroid Build Coastguard Worker  * David A. McGrew
7*90e502c7SAndroid Build Coastguard Worker  * Cisco Systems, Inc.
8*90e502c7SAndroid Build Coastguard Worker  */
9*90e502c7SAndroid Build Coastguard Worker /*
10*90e502c7SAndroid Build Coastguard Worker  *
11*90e502c7SAndroid Build Coastguard Worker  * Copyright (c) 2001-2017 Cisco Systems, Inc.
12*90e502c7SAndroid Build Coastguard Worker  * All rights reserved.
13*90e502c7SAndroid Build Coastguard Worker  *
14*90e502c7SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
15*90e502c7SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
16*90e502c7SAndroid Build Coastguard Worker  * are met:
17*90e502c7SAndroid Build Coastguard Worker  *
18*90e502c7SAndroid Build Coastguard Worker  *   Redistributions of source code must retain the above copyright
19*90e502c7SAndroid Build Coastguard Worker  *   notice, this list of conditions and the following disclaimer.
20*90e502c7SAndroid Build Coastguard Worker  *
21*90e502c7SAndroid Build Coastguard Worker  *   Redistributions in binary form must reproduce the above
22*90e502c7SAndroid Build Coastguard Worker  *   copyright notice, this list of conditions and the following
23*90e502c7SAndroid Build Coastguard Worker  *   disclaimer in the documentation and/or other materials provided
24*90e502c7SAndroid Build Coastguard Worker  *   with the distribution.
25*90e502c7SAndroid Build Coastguard Worker  *
26*90e502c7SAndroid Build Coastguard Worker  *   Neither the name of the Cisco Systems, Inc. nor the names of its
27*90e502c7SAndroid Build Coastguard Worker  *   contributors may be used to endorse or promote products derived
28*90e502c7SAndroid Build Coastguard Worker  *   from this software without specific prior written permission.
29*90e502c7SAndroid Build Coastguard Worker  *
30*90e502c7SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31*90e502c7SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32*90e502c7SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33*90e502c7SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34*90e502c7SAndroid Build Coastguard Worker  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35*90e502c7SAndroid Build Coastguard Worker  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36*90e502c7SAndroid Build Coastguard Worker  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37*90e502c7SAndroid Build Coastguard Worker  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*90e502c7SAndroid Build Coastguard Worker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*90e502c7SAndroid Build Coastguard Worker  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40*90e502c7SAndroid Build Coastguard Worker  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41*90e502c7SAndroid Build Coastguard Worker  * OF THE POSSIBILITY OF SUCH DAMAGE.
42*90e502c7SAndroid Build Coastguard Worker  *
43*90e502c7SAndroid Build Coastguard Worker  */
44*90e502c7SAndroid Build Coastguard Worker 
45*90e502c7SAndroid Build Coastguard Worker #ifndef SRTP_CIPHER_H
46*90e502c7SAndroid Build Coastguard Worker #define SRTP_CIPHER_H
47*90e502c7SAndroid Build Coastguard Worker 
48*90e502c7SAndroid Build Coastguard Worker #include "srtp.h"
49*90e502c7SAndroid Build Coastguard Worker #include "crypto_types.h" /* for values of cipher_type_id_t */
50*90e502c7SAndroid Build Coastguard Worker 
51*90e502c7SAndroid Build Coastguard Worker #ifdef __cplusplus
52*90e502c7SAndroid Build Coastguard Worker extern "C" {
53*90e502c7SAndroid Build Coastguard Worker #endif
54*90e502c7SAndroid Build Coastguard Worker 
55*90e502c7SAndroid Build Coastguard Worker /*
56*90e502c7SAndroid Build Coastguard Worker  * srtp_cipher_direction_t defines a particular cipher operation.
57*90e502c7SAndroid Build Coastguard Worker  *
58*90e502c7SAndroid Build Coastguard Worker  * A srtp_cipher_direction_t is an enum that describes a particular cipher
59*90e502c7SAndroid Build Coastguard Worker  * operation, i.e. encryption or decryption.  For some ciphers, this
60*90e502c7SAndroid Build Coastguard Worker  * distinction does not matter, but for others, it is essential.
61*90e502c7SAndroid Build Coastguard Worker  */
62*90e502c7SAndroid Build Coastguard Worker typedef enum {
63*90e502c7SAndroid Build Coastguard Worker     srtp_direction_encrypt, /**< encryption (convert plaintext to ciphertext) */
64*90e502c7SAndroid Build Coastguard Worker     srtp_direction_decrypt, /**< decryption (convert ciphertext to plaintext) */
65*90e502c7SAndroid Build Coastguard Worker     srtp_direction_any      /**< encryption or decryption                     */
66*90e502c7SAndroid Build Coastguard Worker } srtp_cipher_direction_t;
67*90e502c7SAndroid Build Coastguard Worker 
68*90e502c7SAndroid Build Coastguard Worker /*
69*90e502c7SAndroid Build Coastguard Worker  * the srtp_cipher_pointer_t definition is needed
70*90e502c7SAndroid Build Coastguard Worker  * as srtp_cipher_t is not yet defined
71*90e502c7SAndroid Build Coastguard Worker  */
72*90e502c7SAndroid Build Coastguard Worker typedef struct srtp_cipher_t *srtp_cipher_pointer_t;
73*90e502c7SAndroid Build Coastguard Worker 
74*90e502c7SAndroid Build Coastguard Worker /*
75*90e502c7SAndroid Build Coastguard Worker  *  a srtp_cipher_alloc_func_t allocates (but does not initialize) a
76*90e502c7SAndroid Build Coastguard Worker  * srtp_cipher_t
77*90e502c7SAndroid Build Coastguard Worker  */
78*90e502c7SAndroid Build Coastguard Worker typedef srtp_err_status_t (*srtp_cipher_alloc_func_t)(srtp_cipher_pointer_t *cp,
79*90e502c7SAndroid Build Coastguard Worker                                                       int key_len,
80*90e502c7SAndroid Build Coastguard Worker                                                       int tag_len);
81*90e502c7SAndroid Build Coastguard Worker 
82*90e502c7SAndroid Build Coastguard Worker /*
83*90e502c7SAndroid Build Coastguard Worker  * a srtp_cipher_init_func_t [re-]initializes a cipher_t with a given key
84*90e502c7SAndroid Build Coastguard Worker  */
85*90e502c7SAndroid Build Coastguard Worker typedef srtp_err_status_t (*srtp_cipher_init_func_t)(void *state,
86*90e502c7SAndroid Build Coastguard Worker                                                      const uint8_t *key);
87*90e502c7SAndroid Build Coastguard Worker 
88*90e502c7SAndroid Build Coastguard Worker /* a srtp_cipher_dealloc_func_t de-allocates a cipher_t */
89*90e502c7SAndroid Build Coastguard Worker typedef srtp_err_status_t (*srtp_cipher_dealloc_func_t)(
90*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_pointer_t cp);
91*90e502c7SAndroid Build Coastguard Worker 
92*90e502c7SAndroid Build Coastguard Worker /*
93*90e502c7SAndroid Build Coastguard Worker  * a srtp_cipher_set_aad_func_t processes the AAD data for AEAD ciphers
94*90e502c7SAndroid Build Coastguard Worker  */
95*90e502c7SAndroid Build Coastguard Worker typedef srtp_err_status_t (*srtp_cipher_set_aad_func_t)(void *state,
96*90e502c7SAndroid Build Coastguard Worker                                                         const uint8_t *aad,
97*90e502c7SAndroid Build Coastguard Worker                                                         uint32_t aad_len);
98*90e502c7SAndroid Build Coastguard Worker 
99*90e502c7SAndroid Build Coastguard Worker /* a srtp_cipher_encrypt_func_t encrypts data in-place */
100*90e502c7SAndroid Build Coastguard Worker typedef srtp_err_status_t (*srtp_cipher_encrypt_func_t)(
101*90e502c7SAndroid Build Coastguard Worker     void *state,
102*90e502c7SAndroid Build Coastguard Worker     uint8_t *buffer,
103*90e502c7SAndroid Build Coastguard Worker     unsigned int *octets_to_encrypt);
104*90e502c7SAndroid Build Coastguard Worker 
105*90e502c7SAndroid Build Coastguard Worker /* a srtp_cipher_decrypt_func_t decrypts data in-place */
106*90e502c7SAndroid Build Coastguard Worker typedef srtp_err_status_t (*srtp_cipher_decrypt_func_t)(
107*90e502c7SAndroid Build Coastguard Worker     void *state,
108*90e502c7SAndroid Build Coastguard Worker     uint8_t *buffer,
109*90e502c7SAndroid Build Coastguard Worker     unsigned int *octets_to_decrypt);
110*90e502c7SAndroid Build Coastguard Worker 
111*90e502c7SAndroid Build Coastguard Worker /*
112*90e502c7SAndroid Build Coastguard Worker  * a srtp_cipher_set_iv_func_t function sets the current initialization vector
113*90e502c7SAndroid Build Coastguard Worker  */
114*90e502c7SAndroid Build Coastguard Worker typedef srtp_err_status_t (*srtp_cipher_set_iv_func_t)(
115*90e502c7SAndroid Build Coastguard Worker     void *state,
116*90e502c7SAndroid Build Coastguard Worker     uint8_t *iv,
117*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_direction_t direction);
118*90e502c7SAndroid Build Coastguard Worker 
119*90e502c7SAndroid Build Coastguard Worker /*
120*90e502c7SAndroid Build Coastguard Worker  * a cipher_get_tag_func_t function is used to get the authentication
121*90e502c7SAndroid Build Coastguard Worker  * tag that was calculated by an AEAD cipher.
122*90e502c7SAndroid Build Coastguard Worker  */
123*90e502c7SAndroid Build Coastguard Worker typedef srtp_err_status_t (*srtp_cipher_get_tag_func_t)(void *state,
124*90e502c7SAndroid Build Coastguard Worker                                                         uint8_t *tag,
125*90e502c7SAndroid Build Coastguard Worker                                                         uint32_t *len);
126*90e502c7SAndroid Build Coastguard Worker 
127*90e502c7SAndroid Build Coastguard Worker /*
128*90e502c7SAndroid Build Coastguard Worker  * srtp_cipher_test_case_t is a (list of) key, salt, plaintext, ciphertext,
129*90e502c7SAndroid Build Coastguard Worker  * and aad values that are known to be correct for a
130*90e502c7SAndroid Build Coastguard Worker  * particular cipher.  this data can be used to test an implementation
131*90e502c7SAndroid Build Coastguard Worker  * in an on-the-fly self test of the correctness of the implementation.
132*90e502c7SAndroid Build Coastguard Worker  * (see the srtp_cipher_type_self_test() function below)
133*90e502c7SAndroid Build Coastguard Worker  */
134*90e502c7SAndroid Build Coastguard Worker typedef struct srtp_cipher_test_case_t {
135*90e502c7SAndroid Build Coastguard Worker     int key_length_octets;                 /* octets in key            */
136*90e502c7SAndroid Build Coastguard Worker     const uint8_t *key;                    /* key                      */
137*90e502c7SAndroid Build Coastguard Worker     uint8_t *idx;                          /* packet index             */
138*90e502c7SAndroid Build Coastguard Worker     unsigned int plaintext_length_octets;  /* octets in plaintext      */
139*90e502c7SAndroid Build Coastguard Worker     const uint8_t *plaintext;              /* plaintext                */
140*90e502c7SAndroid Build Coastguard Worker     unsigned int ciphertext_length_octets; /* octets in plaintext      */
141*90e502c7SAndroid Build Coastguard Worker     const uint8_t *ciphertext;             /* ciphertext               */
142*90e502c7SAndroid Build Coastguard Worker     int aad_length_octets;                 /* octets in AAD            */
143*90e502c7SAndroid Build Coastguard Worker     const uint8_t *aad;                    /* AAD                      */
144*90e502c7SAndroid Build Coastguard Worker     int tag_length_octets;                 /* Length of AEAD tag       */
145*90e502c7SAndroid Build Coastguard Worker     const struct srtp_cipher_test_case_t
146*90e502c7SAndroid Build Coastguard Worker         *next_test_case; /* pointer to next testcase */
147*90e502c7SAndroid Build Coastguard Worker } srtp_cipher_test_case_t;
148*90e502c7SAndroid Build Coastguard Worker 
149*90e502c7SAndroid Build Coastguard Worker /* srtp_cipher_type_t defines the 'metadata' for a particular cipher type */
150*90e502c7SAndroid Build Coastguard Worker typedef struct srtp_cipher_type_t {
151*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_alloc_func_t alloc;
152*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_dealloc_func_t dealloc;
153*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_init_func_t init;
154*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_set_aad_func_t set_aad;
155*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_encrypt_func_t encrypt;
156*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_encrypt_func_t decrypt;
157*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_set_iv_func_t set_iv;
158*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_get_tag_func_t get_tag;
159*90e502c7SAndroid Build Coastguard Worker     const char *description;
160*90e502c7SAndroid Build Coastguard Worker     const srtp_cipher_test_case_t *test_data;
161*90e502c7SAndroid Build Coastguard Worker     srtp_cipher_type_id_t id;
162*90e502c7SAndroid Build Coastguard Worker } srtp_cipher_type_t;
163*90e502c7SAndroid Build Coastguard Worker 
164*90e502c7SAndroid Build Coastguard Worker /*
165*90e502c7SAndroid Build Coastguard Worker  * srtp_cipher_t defines an instantiation of a particular cipher, with fixed
166*90e502c7SAndroid Build Coastguard Worker  * key length, key and salt values
167*90e502c7SAndroid Build Coastguard Worker  */
168*90e502c7SAndroid Build Coastguard Worker typedef struct srtp_cipher_t {
169*90e502c7SAndroid Build Coastguard Worker     const srtp_cipher_type_t *type;
170*90e502c7SAndroid Build Coastguard Worker     void *state;
171*90e502c7SAndroid Build Coastguard Worker     int key_len;
172*90e502c7SAndroid Build Coastguard Worker     int algorithm;
173*90e502c7SAndroid Build Coastguard Worker } srtp_cipher_t;
174*90e502c7SAndroid Build Coastguard Worker 
175*90e502c7SAndroid Build Coastguard Worker /* some bookkeeping functions */
176*90e502c7SAndroid Build Coastguard Worker int srtp_cipher_get_key_length(const srtp_cipher_t *c);
177*90e502c7SAndroid Build Coastguard Worker 
178*90e502c7SAndroid Build Coastguard Worker /*
179*90e502c7SAndroid Build Coastguard Worker  * srtp_cipher_type_self_test() tests a cipher against test cases provided in
180*90e502c7SAndroid Build Coastguard Worker  * an array of values of key/srtp_xtd_seq_num_t/plaintext/ciphertext
181*90e502c7SAndroid Build Coastguard Worker  * that is known to be good
182*90e502c7SAndroid Build Coastguard Worker  */
183*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_type_self_test(const srtp_cipher_type_t *ct);
184*90e502c7SAndroid Build Coastguard Worker 
185*90e502c7SAndroid Build Coastguard Worker /*
186*90e502c7SAndroid Build Coastguard Worker  * srtp_cipher_type_test() tests a cipher against external test cases provided
187*90e502c7SAndroid Build Coastguard Worker  * in
188*90e502c7SAndroid Build Coastguard Worker  * an array of values of key/srtp_xtd_seq_num_t/plaintext/ciphertext
189*90e502c7SAndroid Build Coastguard Worker  * that is known to be good
190*90e502c7SAndroid Build Coastguard Worker  */
191*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_type_test(
192*90e502c7SAndroid Build Coastguard Worker     const srtp_cipher_type_t *ct,
193*90e502c7SAndroid Build Coastguard Worker     const srtp_cipher_test_case_t *test_data);
194*90e502c7SAndroid Build Coastguard Worker 
195*90e502c7SAndroid Build Coastguard Worker /*
196*90e502c7SAndroid Build Coastguard Worker  * srtp_cipher_bits_per_second(c, l, t) computes (an estimate of) the
197*90e502c7SAndroid Build Coastguard Worker  * number of bits that a cipher implementation can encrypt in a second
198*90e502c7SAndroid Build Coastguard Worker  *
199*90e502c7SAndroid Build Coastguard Worker  * c is a cipher (which MUST be allocated and initialized already), l
200*90e502c7SAndroid Build Coastguard Worker  * is the length in octets of the test data to be encrypted, and t is
201*90e502c7SAndroid Build Coastguard Worker  * the number of trials
202*90e502c7SAndroid Build Coastguard Worker  *
203*90e502c7SAndroid Build Coastguard Worker  * if an error is encountered, then the value 0 is returned
204*90e502c7SAndroid Build Coastguard Worker  */
205*90e502c7SAndroid Build Coastguard Worker uint64_t srtp_cipher_bits_per_second(srtp_cipher_t *c,
206*90e502c7SAndroid Build Coastguard Worker                                      int octets_in_buffer,
207*90e502c7SAndroid Build Coastguard Worker                                      int num_trials);
208*90e502c7SAndroid Build Coastguard Worker 
209*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_type_alloc(const srtp_cipher_type_t *ct,
210*90e502c7SAndroid Build Coastguard Worker                                          srtp_cipher_t **c,
211*90e502c7SAndroid Build Coastguard Worker                                          int key_len,
212*90e502c7SAndroid Build Coastguard Worker                                          int tlen);
213*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_dealloc(srtp_cipher_t *c);
214*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_init(srtp_cipher_t *c, const uint8_t *key);
215*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_set_iv(srtp_cipher_t *c,
216*90e502c7SAndroid Build Coastguard Worker                                      uint8_t *iv,
217*90e502c7SAndroid Build Coastguard Worker                                      int direction);
218*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_output(srtp_cipher_t *c,
219*90e502c7SAndroid Build Coastguard Worker                                      uint8_t *buffer,
220*90e502c7SAndroid Build Coastguard Worker                                      uint32_t *num_octets_to_output);
221*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_encrypt(srtp_cipher_t *c,
222*90e502c7SAndroid Build Coastguard Worker                                       uint8_t *buffer,
223*90e502c7SAndroid Build Coastguard Worker                                       uint32_t *num_octets_to_output);
224*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_decrypt(srtp_cipher_t *c,
225*90e502c7SAndroid Build Coastguard Worker                                       uint8_t *buffer,
226*90e502c7SAndroid Build Coastguard Worker                                       uint32_t *num_octets_to_output);
227*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_get_tag(srtp_cipher_t *c,
228*90e502c7SAndroid Build Coastguard Worker                                       uint8_t *buffer,
229*90e502c7SAndroid Build Coastguard Worker                                       uint32_t *tag_len);
230*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_cipher_set_aad(srtp_cipher_t *c,
231*90e502c7SAndroid Build Coastguard Worker                                       const uint8_t *aad,
232*90e502c7SAndroid Build Coastguard Worker                                       uint32_t aad_len);
233*90e502c7SAndroid Build Coastguard Worker 
234*90e502c7SAndroid Build Coastguard Worker /*
235*90e502c7SAndroid Build Coastguard Worker  * srtp_replace_cipher_type(ct, id)
236*90e502c7SAndroid Build Coastguard Worker  *
237*90e502c7SAndroid Build Coastguard Worker  * replaces srtp's existing cipher implementation for the cipher_type id
238*90e502c7SAndroid Build Coastguard Worker  * with a new one passed in externally.  The new cipher must pass all the
239*90e502c7SAndroid Build Coastguard Worker  * existing cipher_type's self tests as well as its own.
240*90e502c7SAndroid Build Coastguard Worker  */
241*90e502c7SAndroid Build Coastguard Worker srtp_err_status_t srtp_replace_cipher_type(const srtp_cipher_type_t *ct,
242*90e502c7SAndroid Build Coastguard Worker                                            srtp_cipher_type_id_t id);
243*90e502c7SAndroid Build Coastguard Worker 
244*90e502c7SAndroid Build Coastguard Worker #ifdef __cplusplus
245*90e502c7SAndroid Build Coastguard Worker }
246*90e502c7SAndroid Build Coastguard Worker #endif
247*90e502c7SAndroid Build Coastguard Worker 
248*90e502c7SAndroid Build Coastguard Worker #endif /* SRTP_CIPHER_H */
249