Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * crypto_kernel.c |
| 3 | * |
| 4 | * header for the cryptographic kernel |
| 5 | * |
| 6 | * David A. McGrew |
| 7 | * Cisco Systems, Inc. |
| 8 | */ |
| 9 | /* |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 10 | * |
jfigus | 7882dd9 | 2013-08-02 16:08:23 -0400 | [diff] [blame] | 11 | * Copyright(c) 2001-2006,2013 Cisco Systems, Inc. |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 12 | * All rights reserved. |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 13 | * |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 17 | * |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 18 | * Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 20 | * |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 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. |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 25 | * |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 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. |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 29 | * |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 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 | |
Teerapap Changwichukarn | 6cffe24 | 2014-09-24 11:24:07 +0800 | [diff] [blame] | 46 | #ifdef HAVE_CONFIG_H |
| 47 | #include <config.h> |
| 48 | #endif |
| 49 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 50 | #include "alloc.h" |
| 51 | |
| 52 | #include "crypto_kernel.h" |
| 53 | |
| 54 | /* the debug module for the crypto_kernel */ |
| 55 | |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 56 | srtp_debug_module_t srtp_mod_crypto_kernel = { |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 57 | 0, /* debugging is off by default */ |
| 58 | "crypto kernel" /* printable name for module */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * other debug modules that can be included in the kernel |
| 63 | */ |
| 64 | |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 65 | extern srtp_debug_module_t srtp_mod_auth; |
| 66 | extern srtp_debug_module_t srtp_mod_cipher; |
| 67 | extern srtp_debug_module_t mod_stat; |
| 68 | extern srtp_debug_module_t mod_alloc; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 69 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 70 | /* |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 71 | * cipher types that can be included in the kernel |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 72 | */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 73 | |
jfigus | 9de994f | 2014-11-20 09:17:19 -0500 | [diff] [blame] | 74 | extern srtp_cipher_type_t srtp_null_cipher; |
jfigus | 9a84043 | 2014-11-19 15:48:21 -0500 | [diff] [blame] | 75 | extern srtp_cipher_type_t srtp_aes_icm; |
jfigus | a3127b8 | 2014-11-19 14:46:52 -0500 | [diff] [blame] | 76 | #ifdef OPENSSL |
jfigus | 9a84043 | 2014-11-19 15:48:21 -0500 | [diff] [blame] | 77 | extern srtp_cipher_type_t srtp_aes_gcm_128_openssl; |
| 78 | extern srtp_cipher_type_t srtp_aes_gcm_256_openssl; |
jfigus | 0d3a268 | 2013-04-02 15:42:37 -0400 | [diff] [blame] | 79 | #endif |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | /* |
| 83 | * auth func types that can be included in the kernel |
| 84 | */ |
| 85 | |
jfigus | 9de994f | 2014-11-20 09:17:19 -0500 | [diff] [blame] | 86 | extern srtp_auth_type_t srtp_null_auth; |
jfigus | ae4f3b1 | 2014-11-20 09:34:08 -0500 | [diff] [blame] | 87 | extern srtp_auth_type_t srtp_hmac; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 88 | |
| 89 | /* crypto_kernel is a global variable, the only one of its datatype */ |
| 90 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 91 | srtp_crypto_kernel_t crypto_kernel = { |
| 92 | srtp_crypto_kernel_state_insecure, /* start off in insecure state */ |
| 93 | NULL, /* no cipher types yet */ |
| 94 | NULL, /* no auth types yet */ |
| 95 | NULL /* no debug modules yet */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
David McGrew | b0ad070 | 2006-03-17 20:51:24 +0000 | [diff] [blame] | 98 | #define MAX_RNG_TRIALS 25 |
David McGrew | 026b47c | 2006-03-17 18:22:41 +0000 | [diff] [blame] | 99 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 100 | srtp_err_status_t srtp_crypto_kernel_init () |
| 101 | { |
| 102 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 103 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 104 | /* check the security state */ |
| 105 | if (crypto_kernel.state == srtp_crypto_kernel_state_secure) { |
David McGrew | 98beafa | 2005-10-07 12:59:57 +0000 | [diff] [blame] | 106 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 107 | /* |
| 108 | * we're already in the secure state, but we've been asked to |
| 109 | * re-initialize, so we just re-run the self-tests and then return |
| 110 | */ |
| 111 | return srtp_crypto_kernel_status(); |
| 112 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 113 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 114 | /* initialize error reporting system */ |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 115 | status = srtp_err_reporting_init("crypto"); |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 116 | if (status) { |
| 117 | return status; |
| 118 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 119 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 120 | /* load debug modules */ |
| 121 | status = srtp_crypto_kernel_load_debug_module(&srtp_mod_crypto_kernel); |
| 122 | if (status) { |
| 123 | return status; |
| 124 | } |
| 125 | status = srtp_crypto_kernel_load_debug_module(&srtp_mod_auth); |
| 126 | if (status) { |
| 127 | return status; |
| 128 | } |
| 129 | status = srtp_crypto_kernel_load_debug_module(&srtp_mod_cipher); |
| 130 | if (status) { |
| 131 | return status; |
| 132 | } |
| 133 | status = srtp_crypto_kernel_load_debug_module(&mod_stat); |
| 134 | if (status) { |
| 135 | return status; |
| 136 | } |
| 137 | status = srtp_crypto_kernel_load_debug_module(&mod_alloc); |
| 138 | if (status) { |
| 139 | return status; |
| 140 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 141 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 142 | /* initialize random number generator */ |
| 143 | status = rand_source_init(); |
| 144 | if (status) { |
| 145 | return status; |
| 146 | } |
| 147 | |
| 148 | /* run FIPS-140 statistical tests on rand_source */ |
| 149 | status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, MAX_RNG_TRIALS); |
| 150 | if (status) { |
| 151 | return status; |
| 152 | } |
| 153 | |
| 154 | /* load cipher types */ |
| 155 | status = srtp_crypto_kernel_load_cipher_type(&srtp_null_cipher, SRTP_NULL_CIPHER); |
| 156 | if (status) { |
| 157 | return status; |
| 158 | } |
| 159 | status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_icm, SRTP_AES_ICM); |
| 160 | if (status) { |
| 161 | return status; |
| 162 | } |
jfigus | a3127b8 | 2014-11-19 14:46:52 -0500 | [diff] [blame] | 163 | #ifdef OPENSSL |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 164 | status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_gcm_128_openssl, SRTP_AES_128_GCM); |
| 165 | if (status) { |
| 166 | return status; |
| 167 | } |
| 168 | status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_gcm_256_openssl, SRTP_AES_256_GCM); |
| 169 | if (status) { |
| 170 | return status; |
| 171 | } |
jfigus | 0d3a268 | 2013-04-02 15:42:37 -0400 | [diff] [blame] | 172 | #endif |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 173 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 174 | /* load auth func types */ |
| 175 | status = srtp_crypto_kernel_load_auth_type(&srtp_null_auth, SRTP_NULL_AUTH); |
| 176 | if (status) { |
| 177 | return status; |
| 178 | } |
| 179 | status = srtp_crypto_kernel_load_auth_type(&srtp_hmac, SRTP_HMAC_SHA1); |
| 180 | if (status) { |
| 181 | return status; |
| 182 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 183 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 184 | /* change state to secure */ |
| 185 | crypto_kernel.state = srtp_crypto_kernel_state_secure; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 186 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 187 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 188 | } |
| 189 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 190 | srtp_err_status_t srtp_crypto_kernel_status () |
| 191 | { |
| 192 | srtp_err_status_t status; |
| 193 | srtp_kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list; |
| 194 | srtp_kernel_auth_type_t *atype = crypto_kernel.auth_type_list; |
| 195 | srtp_kernel_debug_module_t *dm = crypto_kernel.debug_module_list; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 196 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 197 | /* run FIPS-140 statistical tests on rand_source */ |
| 198 | printf("testing rand_source..."); |
| 199 | status = stat_test_rand_source_with_repetition(rand_source_get_octet_string, MAX_RNG_TRIALS); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 200 | if (status) { |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 201 | printf("failed\n"); |
| 202 | crypto_kernel.state = srtp_crypto_kernel_state_insecure; |
| 203 | return status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 204 | } |
| 205 | printf("passed\n"); |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 206 | |
| 207 | /* for each cipher type, describe and test */ |
| 208 | while (ctype != NULL) { |
| 209 | printf("cipher: %s\n", ctype->cipher_type->description); |
| 210 | printf(" self-test: "); |
| 211 | status = srtp_cipher_type_self_test(ctype->cipher_type); |
| 212 | if (status) { |
| 213 | printf("failed with error code %d\n", status); |
| 214 | exit(status); |
| 215 | } |
| 216 | printf("passed\n"); |
| 217 | ctype = ctype->next; |
| 218 | } |
| 219 | |
| 220 | /* for each auth type, describe and test */ |
| 221 | while (atype != NULL) { |
| 222 | printf("auth func: %s\n", atype->auth_type->description); |
| 223 | printf(" self-test: "); |
| 224 | status = srtp_auth_type_self_test(atype->auth_type); |
| 225 | if (status) { |
| 226 | printf("failed with error code %d\n", status); |
| 227 | exit(status); |
| 228 | } |
| 229 | printf("passed\n"); |
| 230 | atype = atype->next; |
| 231 | } |
| 232 | |
| 233 | /* describe each debug module */ |
| 234 | printf("debug modules loaded:\n"); |
| 235 | while (dm != NULL) { |
| 236 | printf(" %s ", dm->mod->name); |
| 237 | if (dm->mod->on) { |
| 238 | printf("(on)\n"); |
| 239 | } else{ |
| 240 | printf("(off)\n"); |
| 241 | } |
| 242 | dm = dm->next; |
| 243 | } |
| 244 | |
| 245 | return srtp_err_status_ok; |
| 246 | } |
| 247 | |
| 248 | srtp_err_status_t srtp_crypto_kernel_list_debug_modules () |
| 249 | { |
| 250 | srtp_kernel_debug_module_t *dm = crypto_kernel.debug_module_list; |
| 251 | |
| 252 | /* describe each debug module */ |
| 253 | printf("debug modules loaded:\n"); |
| 254 | while (dm != NULL) { |
| 255 | printf(" %s ", dm->mod->name); |
| 256 | if (dm->mod->on) { |
| 257 | printf("(on)\n"); |
| 258 | } else{ |
| 259 | printf("(off)\n"); |
| 260 | } |
| 261 | dm = dm->next; |
| 262 | } |
| 263 | |
| 264 | return srtp_err_status_ok; |
| 265 | } |
| 266 | |
| 267 | srtp_err_status_t srtp_crypto_kernel_shutdown () |
| 268 | { |
| 269 | srtp_err_status_t status; |
| 270 | |
| 271 | /* |
| 272 | * free dynamic memory used in crypto_kernel at present |
| 273 | */ |
| 274 | |
| 275 | /* walk down cipher type list, freeing memory */ |
| 276 | while (crypto_kernel.cipher_type_list != NULL) { |
| 277 | srtp_kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list; |
| 278 | crypto_kernel.cipher_type_list = ctype->next; |
| 279 | debug_print(srtp_mod_crypto_kernel, |
| 280 | "freeing memory for cipher %s", |
| 281 | ctype->cipher_type->description); |
| 282 | srtp_crypto_free(ctype); |
| 283 | } |
| 284 | |
| 285 | /* walk down authetication module list, freeing memory */ |
| 286 | while (crypto_kernel.auth_type_list != NULL) { |
| 287 | srtp_kernel_auth_type_t *atype = crypto_kernel.auth_type_list; |
| 288 | crypto_kernel.auth_type_list = atype->next; |
| 289 | debug_print(srtp_mod_crypto_kernel, |
| 290 | "freeing memory for authentication %s", |
| 291 | atype->auth_type->description); |
| 292 | srtp_crypto_free(atype); |
| 293 | } |
| 294 | |
| 295 | /* walk down debug module list, freeing memory */ |
| 296 | while (crypto_kernel.debug_module_list != NULL) { |
| 297 | srtp_kernel_debug_module_t *kdm = crypto_kernel.debug_module_list; |
| 298 | crypto_kernel.debug_module_list = kdm->next; |
| 299 | debug_print(srtp_mod_crypto_kernel, |
| 300 | "freeing memory for debug module %s", |
| 301 | kdm->mod->name); |
| 302 | srtp_crypto_free(kdm); |
| 303 | } |
| 304 | |
| 305 | /* de-initialize random number generator */ status = rand_source_deinit(); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 306 | if (status) { |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 307 | return status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 308 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 309 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 310 | /* return to insecure state */ |
| 311 | crypto_kernel.state = srtp_crypto_kernel_state_insecure; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 312 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 313 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 314 | } |
| 315 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 316 | static inline srtp_err_status_t srtp_crypto_kernel_do_load_cipher_type (srtp_cipher_type_t *new_ct, srtp_cipher_type_id_t id, int replace) |
| 317 | { |
| 318 | srtp_kernel_cipher_type_t *ctype, *new_ctype; |
| 319 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 320 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 321 | /* defensive coding */ |
| 322 | if (new_ct == NULL) { |
| 323 | return srtp_err_status_bad_param; |
Jonathan Lennox | dc4d884 | 2010-05-20 23:53:15 +0000 | [diff] [blame] | 324 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 325 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 326 | if (new_ct->id != id) { |
| 327 | return srtp_err_status_bad_param; |
| 328 | } |
| 329 | |
| 330 | /* check cipher type by running self-test */ |
| 331 | status = srtp_cipher_type_self_test(new_ct); |
| 332 | if (status) { |
| 333 | return status; |
| 334 | } |
| 335 | |
| 336 | /* walk down list, checking if this type is in the list already */ |
| 337 | ctype = crypto_kernel.cipher_type_list; |
| 338 | while (ctype != NULL) { |
| 339 | if (id == ctype->id) { |
| 340 | if (!replace) { |
| 341 | return srtp_err_status_bad_param; |
| 342 | } |
| 343 | status = srtp_cipher_type_test(new_ct, ctype->cipher_type->test_data); |
| 344 | if (status) { |
| 345 | return status; |
| 346 | } |
| 347 | new_ctype = ctype; |
| 348 | break; |
| 349 | }else if (new_ct == ctype->cipher_type) { |
| 350 | return srtp_err_status_bad_param; |
| 351 | } |
| 352 | ctype = ctype->next; |
| 353 | } |
| 354 | |
| 355 | /* if not found, put new_ct at the head of the list */ |
| 356 | if (ctype == NULL) { |
| 357 | /* allocate memory */ |
| 358 | new_ctype = (srtp_kernel_cipher_type_t*)srtp_crypto_alloc(sizeof(srtp_kernel_cipher_type_t)); |
| 359 | if (new_ctype == NULL) { |
| 360 | return srtp_err_status_alloc_fail; |
| 361 | } |
| 362 | new_ctype->next = crypto_kernel.cipher_type_list; |
| 363 | |
| 364 | /* set head of list to new cipher type */ |
| 365 | crypto_kernel.cipher_type_list = new_ctype; |
| 366 | } |
| 367 | |
| 368 | /* set fields */ |
| 369 | new_ctype->cipher_type = new_ct; |
| 370 | new_ctype->id = id; |
| 371 | |
| 372 | /* load debug module, if there is one present */ |
| 373 | if (new_ct->debug != NULL) { |
| 374 | srtp_crypto_kernel_load_debug_module(new_ct->debug); |
| 375 | } |
| 376 | /* we could check for errors here */ |
| 377 | |
| 378 | return srtp_err_status_ok; |
| 379 | } |
| 380 | |
| 381 | srtp_err_status_t srtp_crypto_kernel_load_cipher_type (srtp_cipher_type_t *new_ct, srtp_cipher_type_id_t id) |
| 382 | { |
| 383 | return srtp_crypto_kernel_do_load_cipher_type(new_ct, id, 0); |
| 384 | } |
| 385 | |
| 386 | srtp_err_status_t srtp_crypto_kernel_replace_cipher_type (srtp_cipher_type_t *new_ct, srtp_cipher_type_id_t id) |
| 387 | { |
| 388 | return srtp_crypto_kernel_do_load_cipher_type(new_ct, id, 1); |
| 389 | } |
| 390 | |
| 391 | srtp_err_status_t srtp_crypto_kernel_do_load_auth_type (srtp_auth_type_t *new_at, srtp_auth_type_id_t id, int replace) |
| 392 | { |
| 393 | srtp_kernel_auth_type_t *atype, *new_atype; |
| 394 | srtp_err_status_t status; |
| 395 | |
| 396 | /* defensive coding */ |
| 397 | if (new_at == NULL) { |
| 398 | return srtp_err_status_bad_param; |
| 399 | } |
| 400 | |
| 401 | if (new_at->id != id) { |
| 402 | return srtp_err_status_bad_param; |
| 403 | } |
| 404 | |
| 405 | /* check auth type by running self-test */ |
| 406 | status = srtp_auth_type_self_test(new_at); |
| 407 | if (status) { |
| 408 | return status; |
| 409 | } |
| 410 | |
| 411 | /* walk down list, checking if this type is in the list already */ |
| 412 | atype = crypto_kernel.auth_type_list; |
| 413 | while (atype != NULL) { |
| 414 | if (id == atype->id) { |
| 415 | if (!replace) { |
| 416 | return srtp_err_status_bad_param; |
| 417 | } |
| 418 | status = srtp_auth_type_test(new_at, atype->auth_type->test_data); |
| 419 | if (status) { |
| 420 | return status; |
| 421 | } |
| 422 | new_atype = atype; |
| 423 | break; |
| 424 | }else if (new_at == atype->auth_type) { |
| 425 | return srtp_err_status_bad_param; |
| 426 | } |
| 427 | atype = atype->next; |
| 428 | } |
| 429 | |
| 430 | /* if not found, put new_at at the head of the list */ |
| 431 | if (atype == NULL) { |
| 432 | /* allocate memory */ |
| 433 | new_atype = (srtp_kernel_auth_type_t*)srtp_crypto_alloc(sizeof(srtp_kernel_auth_type_t)); |
| 434 | if (new_atype == NULL) { |
| 435 | return srtp_err_status_alloc_fail; |
| 436 | } |
| 437 | |
| 438 | new_atype->next = crypto_kernel.auth_type_list; |
| 439 | /* set head of list to new auth type */ |
| 440 | crypto_kernel.auth_type_list = new_atype; |
| 441 | } |
| 442 | |
| 443 | /* set fields */ |
| 444 | new_atype->auth_type = new_at; |
| 445 | new_atype->id = id; |
| 446 | |
| 447 | /* load debug module, if there is one present */ |
| 448 | if (new_at->debug != NULL) { |
| 449 | srtp_crypto_kernel_load_debug_module(new_at->debug); |
| 450 | } |
| 451 | /* we could check for errors here */ |
| 452 | |
| 453 | return srtp_err_status_ok; |
| 454 | |
| 455 | } |
| 456 | |
| 457 | srtp_err_status_t srtp_crypto_kernel_load_auth_type (srtp_auth_type_t *new_at, srtp_auth_type_id_t id) |
| 458 | { |
| 459 | return srtp_crypto_kernel_do_load_auth_type(new_at, id, 0); |
| 460 | } |
| 461 | |
| 462 | srtp_err_status_t srtp_crypto_kernel_replace_auth_type (srtp_auth_type_t *new_at, srtp_auth_type_id_t id) |
| 463 | { |
| 464 | return srtp_crypto_kernel_do_load_auth_type(new_at, id, 1); |
| 465 | } |
| 466 | |
| 467 | |
| 468 | srtp_cipher_type_t * srtp_crypto_kernel_get_cipher_type (srtp_cipher_type_id_t id) |
| 469 | { |
| 470 | srtp_kernel_cipher_type_t *ctype; |
| 471 | |
| 472 | /* walk down list, looking for id */ |
| 473 | ctype = crypto_kernel.cipher_type_list; |
| 474 | while (ctype != NULL) { |
| 475 | if (id == ctype->id) { |
| 476 | return ctype->cipher_type; |
| 477 | } |
| 478 | ctype = ctype->next; |
| 479 | } |
| 480 | |
| 481 | /* haven't found the right one, indicate failure by returning NULL */ |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | srtp_err_status_t srtp_crypto_kernel_alloc_cipher (srtp_cipher_type_id_t id, srtp_cipher_pointer_t *cp, int key_len, int tag_len) |
| 487 | { |
| 488 | srtp_cipher_type_t *ct; |
| 489 | |
| 490 | /* |
| 491 | * if the crypto_kernel is not yet initialized, we refuse to allocate |
| 492 | * any ciphers - this is a bit extra-paranoid |
| 493 | */ |
| 494 | if (crypto_kernel.state != srtp_crypto_kernel_state_secure) { |
| 495 | return srtp_err_status_init_fail; |
| 496 | } |
| 497 | |
| 498 | ct = srtp_crypto_kernel_get_cipher_type(id); |
| 499 | if (!ct) { |
| 500 | return srtp_err_status_fail; |
| 501 | } |
| 502 | |
| 503 | return ((ct)->alloc(cp, key_len, tag_len)); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | |
| 508 | srtp_auth_type_t * srtp_crypto_kernel_get_auth_type (srtp_auth_type_id_t id) |
| 509 | { |
| 510 | srtp_kernel_auth_type_t *atype; |
| 511 | |
| 512 | /* walk down list, looking for id */ |
| 513 | atype = crypto_kernel.auth_type_list; |
| 514 | while (atype != NULL) { |
| 515 | if (id == atype->id) { |
| 516 | return atype->auth_type; |
| 517 | } |
| 518 | atype = atype->next; |
| 519 | } |
| 520 | |
| 521 | /* haven't found the right one, indicate failure by returning NULL */ |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | srtp_err_status_t srtp_crypto_kernel_alloc_auth (srtp_auth_type_id_t id, auth_pointer_t *ap, int key_len, int tag_len) |
| 526 | { |
| 527 | srtp_auth_type_t *at; |
| 528 | |
| 529 | /* |
| 530 | * if the crypto_kernel is not yet initialized, we refuse to allocate |
| 531 | * any auth functions - this is a bit extra-paranoid |
| 532 | */ |
| 533 | if (crypto_kernel.state != srtp_crypto_kernel_state_secure) { |
| 534 | return srtp_err_status_init_fail; |
| 535 | } |
| 536 | |
| 537 | at = srtp_crypto_kernel_get_auth_type(id); |
| 538 | if (!at) { |
| 539 | return srtp_err_status_fail; |
| 540 | } |
| 541 | |
| 542 | return ((at)->alloc(ap, key_len, tag_len)); |
| 543 | } |
| 544 | |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 545 | srtp_err_status_t srtp_crypto_kernel_load_debug_module (srtp_debug_module_t *new_dm) |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 546 | { |
| 547 | srtp_kernel_debug_module_t *kdm, *new; |
| 548 | |
| 549 | /* defensive coding */ |
| 550 | if (new_dm == NULL) { |
| 551 | return srtp_err_status_bad_param; |
| 552 | } |
| 553 | |
| 554 | /* walk down list, checking if this type is in the list already */ |
| 555 | kdm = crypto_kernel.debug_module_list; |
| 556 | while (kdm != NULL) { |
| 557 | if (strncmp(new_dm->name, kdm->mod->name, 64) == 0) { |
| 558 | return srtp_err_status_bad_param; |
| 559 | } |
| 560 | kdm = kdm->next; |
| 561 | } |
| 562 | |
| 563 | /* put new_dm at the head of the list */ |
| 564 | /* allocate memory */ |
| 565 | new = (srtp_kernel_debug_module_t*)srtp_crypto_alloc(sizeof(srtp_kernel_debug_module_t)); |
| 566 | if (new == NULL) { |
| 567 | return srtp_err_status_alloc_fail; |
| 568 | } |
| 569 | |
| 570 | /* set fields */ |
| 571 | new->mod = new_dm; |
| 572 | new->next = crypto_kernel.debug_module_list; |
Jonathan Lennox | dc4d884 | 2010-05-20 23:53:15 +0000 | [diff] [blame] | 573 | |
| 574 | /* set head of list to new cipher type */ |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 575 | crypto_kernel.debug_module_list = new; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 576 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 577 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 578 | } |
| 579 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 580 | srtp_err_status_t srtp_crypto_kernel_set_debug_module (char *name, int on) |
| 581 | { |
| 582 | srtp_kernel_debug_module_t *kdm; |
Jonathan Lennox | dc4d884 | 2010-05-20 23:53:15 +0000 | [diff] [blame] | 583 | |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 584 | /* walk down list, checking if this type is in the list already */ |
| 585 | kdm = crypto_kernel.debug_module_list; |
| 586 | while (kdm != NULL) { |
| 587 | if (strncmp(name, kdm->mod->name, 64) == 0) { |
| 588 | kdm->mod->on = on; |
| 589 | return srtp_err_status_ok; |
| 590 | } |
| 591 | kdm = kdm->next; |
Jonathan Lennox | dc4d884 | 2010-05-20 23:53:15 +0000 | [diff] [blame] | 592 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 593 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 594 | return srtp_err_status_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 595 | } |