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