henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2014 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 11 | #include "webrtc/pc/externalhmac.h" |
henrike@webrtc.org | 2d213e4 | 2014-03-06 18:51:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <stdlib.h> // For malloc/free. |
| 14 | |
mattdr | 51f2919 | 2016-09-28 14:08:46 -0700 | [diff] [blame^] | 15 | #ifdef HAVE_SRTP |
jiayl@webrtc.org | a197a5e | 2015-03-23 22:11:49 +0000 | [diff] [blame] | 16 | extern "C" { |
kjellander | 7bc7c06 | 2016-04-22 04:57:49 -0700 | [diff] [blame] | 17 | #ifdef SRTP_RELATIVE_PATH |
| 18 | #include "crypto_kernel.h" // NOLINT |
| 19 | #include "srtp.h" // NOLINT |
| 20 | #else |
mattdr | 51f2919 | 2016-09-28 14:08:46 -0700 | [diff] [blame^] | 21 | #include "third_party/libsrtp/crypto/include/crypto_kernel.h" |
| 22 | #include "third_party/libsrtp/include/srtp.h" |
kjellander | 7bc7c06 | 2016-04-22 04:57:49 -0700 | [diff] [blame] | 23 | #endif // SRTP_RELATIVE_PATH |
jiayl@webrtc.org | a197a5e | 2015-03-23 22:11:49 +0000 | [diff] [blame] | 24 | } |
mattdr | 51f2919 | 2016-09-28 14:08:46 -0700 | [diff] [blame^] | 25 | #endif // HAVE_SRTP |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 26 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 27 | #include "webrtc/base/logging.h" |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 28 | |
mattdr | 51f2919 | 2016-09-28 14:08:46 -0700 | [diff] [blame^] | 29 | #if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH) |
| 30 | |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 31 | // Begin test case 0 */ |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 32 | static const uint8_t kExternalHmacTestCase0Key[20] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 33 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 34 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 35 | 0x0b, 0x0b, 0x0b, 0x0b |
| 36 | }; |
| 37 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 38 | static const uint8_t kExternalHmacTestCase0Data[8] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 39 | 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There" |
| 40 | }; |
| 41 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 42 | static const uint8_t kExternalHmacFakeTag[10] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 43 | 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd |
| 44 | }; |
| 45 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 46 | static const auth_test_case_t kExternalHmacTestCase0 = { |
| 47 | 20, // Octets in key |
| 48 | const_cast<uint8_t*>(kExternalHmacTestCase0Key), // Key |
| 49 | 8, // Octets in data |
| 50 | const_cast<uint8_t*>(kExternalHmacTestCase0Data), // Data |
| 51 | 10, // Octets in tag |
| 52 | const_cast<uint8_t*>(kExternalHmacFakeTag), // Tag |
| 53 | NULL // Pointer to next |
| 54 | // testcase |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 57 | static const char kExternalHmacDescription[] = |
| 58 | "external hmac sha-1 authentication"; |
| 59 | |
| 60 | // auth_type_t external_hmac is the hmac metaobject |
| 61 | |
| 62 | static const auth_type_t external_hmac = { |
| 63 | external_hmac_alloc, |
| 64 | external_hmac_dealloc, |
| 65 | (auth_init_func) external_hmac_init, |
| 66 | (auth_compute_func) external_hmac_compute, |
| 67 | (auth_update_func) external_hmac_update, |
| 68 | (auth_start_func) external_hmac_start, |
| 69 | const_cast<char*>(kExternalHmacDescription), |
| 70 | 0, // Instance count. |
| 71 | const_cast<auth_test_case_t*>(&kExternalHmacTestCase0), |
| 72 | NULL, // No debugging module. |
| 73 | EXTERNAL_HMAC_SHA1 |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | err_status_t external_hmac_alloc(auth_t** a, int key_len, int out_len) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 78 | uint8_t* pointer; |
| 79 | |
| 80 | // Check key length - note that we don't support keys larger |
| 81 | // than 20 bytes yet |
| 82 | if (key_len > 20) |
| 83 | return err_status_bad_param; |
| 84 | |
| 85 | // Check output length - should be less than 20 bytes/ |
| 86 | if (out_len > 20) |
| 87 | return err_status_bad_param; |
| 88 | |
| 89 | // Allocate memory for auth and hmac_ctx_t structures. |
pbos@webrtc.org | a9cf079 | 2014-12-18 09:12:21 +0000 | [diff] [blame] | 90 | pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(auth_t))]; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 91 | if (pointer == NULL) |
| 92 | return err_status_alloc_fail; |
| 93 | |
| 94 | // Set pointers |
| 95 | *a = (auth_t *)pointer; |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 96 | // |external_hmac| is const and libsrtp expects |type| to be non-const. |
| 97 | // const conversion is required. |external_hmac| is constant because we don't |
| 98 | // want to increase global count in Chrome. |
| 99 | (*a)->type = const_cast<auth_type_t*>(&external_hmac); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 100 | (*a)->state = pointer + sizeof(auth_t); |
| 101 | (*a)->out_len = out_len; |
| 102 | (*a)->key_len = key_len; |
| 103 | (*a)->prefix_len = 0; |
| 104 | |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 105 | return err_status_ok; |
| 106 | } |
| 107 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 108 | err_status_t external_hmac_dealloc(auth_t* a) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 109 | // Zeroize entire state |
pbos@webrtc.org | a9cf079 | 2014-12-18 09:12:21 +0000 | [diff] [blame] | 110 | memset((uint8_t *)a, 0, sizeof(ExternalHmacContext) + sizeof(auth_t)); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 111 | |
| 112 | // Free memory |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 113 | delete[] a; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 114 | |
| 115 | return err_status_ok; |
| 116 | } |
| 117 | |
pbos@webrtc.org | a9cf079 | 2014-12-18 09:12:21 +0000 | [diff] [blame] | 118 | err_status_t external_hmac_init(ExternalHmacContext* state, |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 119 | const uint8_t* key, |
| 120 | int key_len) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 121 | if (key_len > HMAC_KEY_LENGTH) |
| 122 | return err_status_bad_param; |
| 123 | |
| 124 | memset(state->key, 0, key_len); |
| 125 | memcpy(state->key, key, key_len); |
| 126 | state->key_length = key_len; |
| 127 | return err_status_ok; |
| 128 | } |
| 129 | |
pbos@webrtc.org | a9cf079 | 2014-12-18 09:12:21 +0000 | [diff] [blame] | 130 | err_status_t external_hmac_start(ExternalHmacContext* state) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 131 | return err_status_ok; |
| 132 | } |
| 133 | |
pbos@webrtc.org | a9cf079 | 2014-12-18 09:12:21 +0000 | [diff] [blame] | 134 | err_status_t external_hmac_update(ExternalHmacContext* state, |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 135 | const uint8_t* message, |
| 136 | int msg_octets) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 137 | return err_status_ok; |
| 138 | } |
| 139 | |
pbos@webrtc.org | a9cf079 | 2014-12-18 09:12:21 +0000 | [diff] [blame] | 140 | err_status_t external_hmac_compute(ExternalHmacContext* state, |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 141 | const void* message, |
| 142 | int msg_octets, |
| 143 | int tag_len, |
| 144 | uint8_t* result) { |
| 145 | memcpy(result, kExternalHmacFakeTag, tag_len); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 146 | return err_status_ok; |
| 147 | } |
| 148 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 149 | err_status_t external_crypto_init() { |
| 150 | // |external_hmac| is const. const_cast is required as libsrtp expects |
| 151 | // non-const. |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 152 | err_status_t status = crypto_kernel_replace_auth_type( |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 153 | const_cast<auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 154 | if (status) { |
| 155 | LOG(LS_ERROR) << "Error in replacing default auth module, error: " |
| 156 | << status; |
| 157 | return err_status_fail; |
| 158 | } |
| 159 | return err_status_ok; |
| 160 | } |
| 161 | |
| 162 | #endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH) |