Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * crypto_kernel.h |
michael@0 | 3 | * |
michael@0 | 4 | * header for the cryptographic kernel |
michael@0 | 5 | * |
michael@0 | 6 | * David A. McGrew |
michael@0 | 7 | * Cisco Systems, Inc. |
michael@0 | 8 | */ |
michael@0 | 9 | /* |
michael@0 | 10 | * |
michael@0 | 11 | * Copyright(c) 2001-2006 Cisco Systems, Inc. |
michael@0 | 12 | * All rights reserved. |
michael@0 | 13 | * |
michael@0 | 14 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 15 | * modification, are permitted provided that the following conditions |
michael@0 | 16 | * are met: |
michael@0 | 17 | * |
michael@0 | 18 | * Redistributions of source code must retain the above copyright |
michael@0 | 19 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 20 | * |
michael@0 | 21 | * Redistributions in binary form must reproduce the above |
michael@0 | 22 | * copyright notice, this list of conditions and the following |
michael@0 | 23 | * disclaimer in the documentation and/or other materials provided |
michael@0 | 24 | * with the distribution. |
michael@0 | 25 | * |
michael@0 | 26 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
michael@0 | 27 | * contributors may be used to endorse or promote products derived |
michael@0 | 28 | * from this software without specific prior written permission. |
michael@0 | 29 | * |
michael@0 | 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 31 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 32 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
michael@0 | 33 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
michael@0 | 34 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
michael@0 | 35 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 36 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
michael@0 | 37 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
michael@0 | 39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 40 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
michael@0 | 41 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 42 | * |
michael@0 | 43 | */ |
michael@0 | 44 | |
michael@0 | 45 | |
michael@0 | 46 | #ifndef CRYPTO_KERNEL |
michael@0 | 47 | #define CRYPTO_KERNEL |
michael@0 | 48 | |
michael@0 | 49 | #include "rand_source.h" |
michael@0 | 50 | #include "prng.h" |
michael@0 | 51 | #include "cipher.h" |
michael@0 | 52 | #include "auth.h" |
michael@0 | 53 | #include "cryptoalg.h" |
michael@0 | 54 | #include "stat.h" |
michael@0 | 55 | #include "err.h" |
michael@0 | 56 | #include "crypto_types.h" |
michael@0 | 57 | #include "key.h" |
michael@0 | 58 | #include "crypto.h" |
michael@0 | 59 | |
michael@0 | 60 | /* |
michael@0 | 61 | * crypto_kernel_state_t defines the possible states: |
michael@0 | 62 | * |
michael@0 | 63 | * insecure - not yet initialized |
michael@0 | 64 | * secure - initialized and passed self-tests |
michael@0 | 65 | */ |
michael@0 | 66 | |
michael@0 | 67 | typedef enum { |
michael@0 | 68 | crypto_kernel_state_insecure, |
michael@0 | 69 | crypto_kernel_state_secure |
michael@0 | 70 | } crypto_kernel_state_t; |
michael@0 | 71 | |
michael@0 | 72 | /* |
michael@0 | 73 | * linked list of cipher types |
michael@0 | 74 | */ |
michael@0 | 75 | |
michael@0 | 76 | typedef struct kernel_cipher_type { |
michael@0 | 77 | cipher_type_id_t id; |
michael@0 | 78 | cipher_type_t *cipher_type; |
michael@0 | 79 | struct kernel_cipher_type *next; |
michael@0 | 80 | } kernel_cipher_type_t; |
michael@0 | 81 | |
michael@0 | 82 | /* |
michael@0 | 83 | * linked list of auth types |
michael@0 | 84 | */ |
michael@0 | 85 | |
michael@0 | 86 | typedef struct kernel_auth_type { |
michael@0 | 87 | auth_type_id_t id; |
michael@0 | 88 | auth_type_t *auth_type; |
michael@0 | 89 | struct kernel_auth_type *next; |
michael@0 | 90 | } kernel_auth_type_t; |
michael@0 | 91 | |
michael@0 | 92 | /* |
michael@0 | 93 | * linked list of debug modules |
michael@0 | 94 | */ |
michael@0 | 95 | |
michael@0 | 96 | typedef struct kernel_debug_module { |
michael@0 | 97 | debug_module_t *mod; |
michael@0 | 98 | struct kernel_debug_module *next; |
michael@0 | 99 | } kernel_debug_module_t; |
michael@0 | 100 | |
michael@0 | 101 | |
michael@0 | 102 | /* |
michael@0 | 103 | * crypto_kernel_t is the data structure for the crypto kernel |
michael@0 | 104 | * |
michael@0 | 105 | * note that there is *exactly one* instance of this data type, |
michael@0 | 106 | * a global variable defined in crypto_kernel.c |
michael@0 | 107 | */ |
michael@0 | 108 | |
michael@0 | 109 | typedef struct { |
michael@0 | 110 | crypto_kernel_state_t state; /* current state of kernel */ |
michael@0 | 111 | kernel_cipher_type_t *cipher_type_list; /* list of all cipher types */ |
michael@0 | 112 | kernel_auth_type_t *auth_type_list; /* list of all auth func types */ |
michael@0 | 113 | kernel_debug_module_t *debug_module_list; /* list of all debug modules */ |
michael@0 | 114 | } crypto_kernel_t; |
michael@0 | 115 | |
michael@0 | 116 | |
michael@0 | 117 | /* |
michael@0 | 118 | * crypto_kernel_t external api |
michael@0 | 119 | */ |
michael@0 | 120 | |
michael@0 | 121 | |
michael@0 | 122 | /* |
michael@0 | 123 | * The function crypto_kernel_init() initialized the crypto kernel and |
michael@0 | 124 | * runs the self-test operations on the random number generators and |
michael@0 | 125 | * crypto algorithms. Possible return values are: |
michael@0 | 126 | * |
michael@0 | 127 | * err_status_ok initialization successful |
michael@0 | 128 | * <other> init failure |
michael@0 | 129 | * |
michael@0 | 130 | * If any value other than err_status_ok is returned, the |
michael@0 | 131 | * crypto_kernel MUST NOT be used. |
michael@0 | 132 | */ |
michael@0 | 133 | |
michael@0 | 134 | err_status_t |
michael@0 | 135 | crypto_kernel_init(void); |
michael@0 | 136 | |
michael@0 | 137 | |
michael@0 | 138 | /* |
michael@0 | 139 | * The function crypto_kernel_shutdown() de-initializes the |
michael@0 | 140 | * crypto_kernel, zeroizes keys and other cryptographic material, and |
michael@0 | 141 | * deallocates any dynamically allocated memory. Possible return |
michael@0 | 142 | * values are: |
michael@0 | 143 | * |
michael@0 | 144 | * err_status_ok shutdown successful |
michael@0 | 145 | * <other> shutdown failure |
michael@0 | 146 | * |
michael@0 | 147 | */ |
michael@0 | 148 | |
michael@0 | 149 | err_status_t |
michael@0 | 150 | crypto_kernel_shutdown(void); |
michael@0 | 151 | |
michael@0 | 152 | /* |
michael@0 | 153 | * The function crypto_kernel_stats() checks the the crypto_kernel, |
michael@0 | 154 | * running tests on the ciphers, auth funcs, and rng, and prints out a |
michael@0 | 155 | * status report. Possible return values are: |
michael@0 | 156 | * |
michael@0 | 157 | * err_status_ok all tests were passed |
michael@0 | 158 | * <other> a test failed |
michael@0 | 159 | * |
michael@0 | 160 | */ |
michael@0 | 161 | |
michael@0 | 162 | err_status_t |
michael@0 | 163 | crypto_kernel_status(void); |
michael@0 | 164 | |
michael@0 | 165 | |
michael@0 | 166 | /* |
michael@0 | 167 | * crypto_kernel_list_debug_modules() outputs a list of debugging modules |
michael@0 | 168 | * |
michael@0 | 169 | */ |
michael@0 | 170 | |
michael@0 | 171 | err_status_t |
michael@0 | 172 | crypto_kernel_list_debug_modules(void); |
michael@0 | 173 | |
michael@0 | 174 | /* |
michael@0 | 175 | * crypto_kernel_load_cipher_type() |
michael@0 | 176 | * |
michael@0 | 177 | */ |
michael@0 | 178 | |
michael@0 | 179 | err_status_t |
michael@0 | 180 | crypto_kernel_load_cipher_type(cipher_type_t *ct, cipher_type_id_t id); |
michael@0 | 181 | |
michael@0 | 182 | err_status_t |
michael@0 | 183 | crypto_kernel_load_auth_type(auth_type_t *ct, auth_type_id_t id); |
michael@0 | 184 | |
michael@0 | 185 | /* |
michael@0 | 186 | * crypto_kernel_replace_cipher_type(ct, id) |
michael@0 | 187 | * |
michael@0 | 188 | * replaces the crypto kernel's existing cipher for the cipher_type id |
michael@0 | 189 | * with a new one passed in externally. The new cipher must pass all the |
michael@0 | 190 | * existing cipher_type's self tests as well as its own. |
michael@0 | 191 | */ |
michael@0 | 192 | err_status_t |
michael@0 | 193 | crypto_kernel_replace_cipher_type(cipher_type_t *ct, cipher_type_id_t id); |
michael@0 | 194 | |
michael@0 | 195 | |
michael@0 | 196 | /* |
michael@0 | 197 | * crypto_kernel_replace_auth_type(ct, id) |
michael@0 | 198 | * |
michael@0 | 199 | * replaces the crypto kernel's existing cipher for the auth_type id |
michael@0 | 200 | * with a new one passed in externally. The new auth type must pass all the |
michael@0 | 201 | * existing auth_type's self tests as well as its own. |
michael@0 | 202 | */ |
michael@0 | 203 | err_status_t |
michael@0 | 204 | crypto_kernel_replace_auth_type(auth_type_t *ct, auth_type_id_t id); |
michael@0 | 205 | |
michael@0 | 206 | |
michael@0 | 207 | err_status_t |
michael@0 | 208 | crypto_kernel_load_debug_module(debug_module_t *new_dm); |
michael@0 | 209 | |
michael@0 | 210 | /* |
michael@0 | 211 | * crypto_kernel_alloc_cipher(id, cp, key_len); |
michael@0 | 212 | * |
michael@0 | 213 | * allocates a cipher of type id at location *cp, with key length |
michael@0 | 214 | * key_len octets. Return values are: |
michael@0 | 215 | * |
michael@0 | 216 | * err_status_ok no problems |
michael@0 | 217 | * err_status_alloc_fail an allocation failure occured |
michael@0 | 218 | * err_status_fail couldn't find cipher with identifier 'id' |
michael@0 | 219 | */ |
michael@0 | 220 | |
michael@0 | 221 | err_status_t |
michael@0 | 222 | crypto_kernel_alloc_cipher(cipher_type_id_t id, |
michael@0 | 223 | cipher_pointer_t *cp, |
michael@0 | 224 | int key_len); |
michael@0 | 225 | |
michael@0 | 226 | /* |
michael@0 | 227 | * crypto_kernel_alloc_auth(id, ap, key_len, tag_len); |
michael@0 | 228 | * |
michael@0 | 229 | * allocates an auth function of type id at location *ap, with key |
michael@0 | 230 | * length key_len octets and output tag length of tag_len. Return |
michael@0 | 231 | * values are: |
michael@0 | 232 | * |
michael@0 | 233 | * err_status_ok no problems |
michael@0 | 234 | * err_status_alloc_fail an allocation failure occured |
michael@0 | 235 | * err_status_fail couldn't find auth with identifier 'id' |
michael@0 | 236 | */ |
michael@0 | 237 | |
michael@0 | 238 | err_status_t |
michael@0 | 239 | crypto_kernel_alloc_auth(auth_type_id_t id, |
michael@0 | 240 | auth_pointer_t *ap, |
michael@0 | 241 | int key_len, |
michael@0 | 242 | int tag_len); |
michael@0 | 243 | |
michael@0 | 244 | |
michael@0 | 245 | /* |
michael@0 | 246 | * crypto_kernel_set_debug_module(mod_name, v) |
michael@0 | 247 | * |
michael@0 | 248 | * sets dynamic debugging to the value v (0 for off, 1 for on) for the |
michael@0 | 249 | * debug module with the name mod_name |
michael@0 | 250 | * |
michael@0 | 251 | * returns err_status_ok on success, err_status_fail otherwise |
michael@0 | 252 | */ |
michael@0 | 253 | |
michael@0 | 254 | err_status_t |
michael@0 | 255 | crypto_kernel_set_debug_module(char *mod_name, int v); |
michael@0 | 256 | |
michael@0 | 257 | /** |
michael@0 | 258 | * @brief writes a random octet string. |
michael@0 | 259 | * |
michael@0 | 260 | * The function call crypto_get_random(dest, len) writes len octets of |
michael@0 | 261 | * random data to the location to which dest points, and returns an |
michael@0 | 262 | * error code. This error code @b must be checked, and if a failure is |
michael@0 | 263 | * reported, the data in the buffer @b must @b not be used. |
michael@0 | 264 | * |
michael@0 | 265 | * @warning If the return code is not checked, then non-random |
michael@0 | 266 | * data may be in the buffer. This function will fail |
michael@0 | 267 | * unless it is called after crypto_kernel_init(). |
michael@0 | 268 | * |
michael@0 | 269 | * @return |
michael@0 | 270 | * - err_status_ok if no problems occured. |
michael@0 | 271 | * - [other] a problem occured, and no assumptions should |
michael@0 | 272 | * be made about the contents of the destination |
michael@0 | 273 | * buffer. |
michael@0 | 274 | * |
michael@0 | 275 | * @ingroup SRTP |
michael@0 | 276 | */ |
michael@0 | 277 | err_status_t |
michael@0 | 278 | crypto_get_random(unsigned char *buffer, unsigned int length); |
michael@0 | 279 | |
michael@0 | 280 | #endif /* CRYPTO_KERNEL */ |