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