henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH) |
| 29 | |
henrike@webrtc.org | 2d213e4 | 2014-03-06 18:51:21 +0000 | [diff] [blame^] | 30 | #include "talk/session/media/externalhmac.h" |
| 31 | |
| 32 | #include <stdlib.h> // For malloc/free. |
| 33 | |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 34 | #ifdef SRTP_RELATIVE_PATH |
| 35 | #include "srtp.h" // NOLINT |
| 36 | #else |
| 37 | #include "third_party/libsrtp/include/srtp.h" |
| 38 | #endif // SRTP_RELATIVE_PATH |
| 39 | |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 40 | #include "talk/base/logging.h" |
| 41 | |
| 42 | // The debug module for authentiation |
| 43 | debug_module_t mod_external_hmac = { |
| 44 | 0, // Debugging is off by default |
| 45 | (char*)"external-hmac-sha-1" // Printable name for module |
| 46 | }; |
| 47 | |
| 48 | extern auth_type_t external_hmac; |
| 49 | |
| 50 | // Begin test case 0 */ |
| 51 | uint8_t |
| 52 | external_hmac_test_case_0_key[20] = { |
| 53 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 54 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 55 | 0x0b, 0x0b, 0x0b, 0x0b |
| 56 | }; |
| 57 | |
| 58 | uint8_t |
| 59 | external_hmac_test_case_0_data[8] = { |
| 60 | 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There" |
| 61 | }; |
| 62 | |
| 63 | uint8_t |
| 64 | external_hmac_fake_tag[10] = { |
| 65 | 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd |
| 66 | }; |
| 67 | |
| 68 | auth_test_case_t |
| 69 | external_hmac_test_case_0 = { |
| 70 | 20, // Octets in key |
| 71 | external_hmac_test_case_0_key, // Key |
| 72 | 8, // Octets in data |
| 73 | external_hmac_test_case_0_data, // Data |
| 74 | 10, // Octets in tag |
| 75 | external_hmac_fake_tag, // Tag |
| 76 | NULL // Pointer to next testcase |
| 77 | }; |
| 78 | |
| 79 | err_status_t |
| 80 | external_hmac_alloc(auth_t** a, int key_len, int out_len) { |
| 81 | uint8_t* pointer; |
| 82 | |
| 83 | // Check key length - note that we don't support keys larger |
| 84 | // than 20 bytes yet |
| 85 | if (key_len > 20) |
| 86 | return err_status_bad_param; |
| 87 | |
| 88 | // Check output length - should be less than 20 bytes/ |
| 89 | if (out_len > 20) |
| 90 | return err_status_bad_param; |
| 91 | |
| 92 | // Allocate memory for auth and hmac_ctx_t structures. |
| 93 | pointer = reinterpret_cast<uint8_t*>( |
henrike@webrtc.org | 2d213e4 | 2014-03-06 18:51:21 +0000 | [diff] [blame^] | 94 | malloc(sizeof(external_hmac_ctx_t) + sizeof(auth_t))); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 95 | if (pointer == NULL) |
| 96 | return err_status_alloc_fail; |
| 97 | |
| 98 | // Set pointers |
| 99 | *a = (auth_t *)pointer; |
| 100 | (*a)->type = &external_hmac; |
| 101 | (*a)->state = pointer + sizeof(auth_t); |
| 102 | (*a)->out_len = out_len; |
| 103 | (*a)->key_len = key_len; |
| 104 | (*a)->prefix_len = 0; |
| 105 | |
| 106 | // Increment global count of all hmac uses. |
| 107 | external_hmac.ref_count++; |
| 108 | |
| 109 | return err_status_ok; |
| 110 | } |
| 111 | |
| 112 | err_status_t |
| 113 | external_hmac_dealloc(auth_t* a) { |
| 114 | // Zeroize entire state |
henrike@webrtc.org | 2d213e4 | 2014-03-06 18:51:21 +0000 | [diff] [blame^] | 115 | memset((uint8_t *)a, sizeof(external_hmac_ctx_t) + sizeof(auth_t)); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 116 | |
| 117 | // Free memory |
henrike@webrtc.org | 2d213e4 | 2014-03-06 18:51:21 +0000 | [diff] [blame^] | 118 | free(a); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 119 | |
| 120 | // Decrement global count of all hmac uses. |
| 121 | external_hmac.ref_count--; |
| 122 | |
| 123 | return err_status_ok; |
| 124 | } |
| 125 | |
| 126 | err_status_t |
| 127 | external_hmac_init(external_hmac_ctx_t* state, |
| 128 | const uint8_t* key, int key_len) { |
| 129 | if (key_len > HMAC_KEY_LENGTH) |
| 130 | return err_status_bad_param; |
| 131 | |
| 132 | memset(state->key, 0, key_len); |
| 133 | memcpy(state->key, key, key_len); |
| 134 | state->key_length = key_len; |
| 135 | return err_status_ok; |
| 136 | } |
| 137 | |
| 138 | err_status_t |
| 139 | external_hmac_start(external_hmac_ctx_t* state) { |
| 140 | return err_status_ok; |
| 141 | } |
| 142 | |
| 143 | err_status_t |
| 144 | external_hmac_update(external_hmac_ctx_t* state, const uint8_t* message, |
| 145 | int msg_octets) { |
| 146 | return err_status_ok; |
| 147 | } |
| 148 | |
| 149 | err_status_t |
| 150 | external_hmac_compute(external_hmac_ctx_t* state, const void* message, |
| 151 | int msg_octets, int tag_len, uint8_t* result) { |
| 152 | memcpy(result, external_hmac_fake_tag, tag_len); |
| 153 | return err_status_ok; |
| 154 | } |
| 155 | |
| 156 | char external_hmac_description[] = "external hmac sha-1 authentication"; |
| 157 | |
| 158 | // auth_type_t external_hmac is the hmac metaobject |
| 159 | |
| 160 | auth_type_t |
| 161 | external_hmac = { |
| 162 | (auth_alloc_func) external_hmac_alloc, |
| 163 | (auth_dealloc_func) external_hmac_dealloc, |
| 164 | (auth_init_func) external_hmac_init, |
| 165 | (auth_compute_func) external_hmac_compute, |
| 166 | (auth_update_func) external_hmac_update, |
| 167 | (auth_start_func) external_hmac_start, |
| 168 | (char *) external_hmac_description, |
| 169 | (int) 0, /* instance count */ |
| 170 | (auth_test_case_t *) &external_hmac_test_case_0, |
| 171 | (debug_module_t *) &mod_external_hmac, |
| 172 | (auth_type_id_t) EXTERNAL_HMAC_SHA1 |
| 173 | }; |
| 174 | |
| 175 | err_status_t |
| 176 | external_crypto_init() { |
| 177 | err_status_t status = crypto_kernel_replace_auth_type( |
| 178 | &external_hmac, EXTERNAL_HMAC_SHA1); |
| 179 | if (status) { |
| 180 | LOG(LS_ERROR) << "Error in replacing default auth module, error: " |
| 181 | << status; |
| 182 | return err_status_fail; |
| 183 | } |
| 184 | return err_status_ok; |
| 185 | } |
| 186 | |
| 187 | #endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH) |