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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/logging.h" |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 16 | |
mattdr | 0d8ade5 | 2016-10-25 09:47:26 -0700 | [diff] [blame] | 17 | #include "third_party/libsrtp/crypto/include/crypto_kernel.h" |
| 18 | #include "third_party/libsrtp/include/srtp.h" |
mattdr | 51f2919 | 2016-09-28 14:08:46 -0700 | [diff] [blame] | 19 | |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 20 | // Begin test case 0 */ |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 21 | static const uint8_t kExternalHmacTestCase0Key[20] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 22 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 23 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 24 | 0x0b, 0x0b, 0x0b, 0x0b |
| 25 | }; |
| 26 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 27 | static const uint8_t kExternalHmacTestCase0Data[8] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 28 | 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There" |
| 29 | }; |
| 30 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 31 | static const uint8_t kExternalHmacFakeTag[10] = { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 32 | 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd |
| 33 | }; |
| 34 | |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 35 | static const srtp_auth_test_case_t kExternalHmacTestCase0 = { |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 36 | 20, // Octets in key |
| 37 | const_cast<uint8_t*>(kExternalHmacTestCase0Key), // Key |
| 38 | 8, // Octets in data |
| 39 | const_cast<uint8_t*>(kExternalHmacTestCase0Data), // Data |
| 40 | 10, // Octets in tag |
| 41 | const_cast<uint8_t*>(kExternalHmacFakeTag), // Tag |
| 42 | NULL // Pointer to next |
| 43 | // testcase |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 46 | static const char kExternalHmacDescription[] = |
| 47 | "external hmac sha-1 authentication"; |
| 48 | |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 49 | // srtp_auth_type_t external_hmac is the hmac metaobject |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 50 | |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 51 | static const srtp_auth_type_t external_hmac = { |
| 52 | external_hmac_alloc, |
| 53 | external_hmac_dealloc, |
Vlad Tsyrklevich | e8e8ad8 | 2017-12-06 18:38:22 -0800 | [diff] [blame] | 54 | external_hmac_init, |
| 55 | external_hmac_compute, |
| 56 | external_hmac_update, |
| 57 | external_hmac_start, |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 58 | const_cast<char*>(kExternalHmacDescription), |
| 59 | const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0), |
| 60 | EXTERNAL_HMAC_SHA1 |
| 61 | }; |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 62 | |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 63 | srtp_err_status_t external_hmac_alloc(srtp_auth_t** a, |
| 64 | int key_len, |
| 65 | int out_len) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 66 | uint8_t* pointer; |
| 67 | |
| 68 | // Check key length - note that we don't support keys larger |
| 69 | // than 20 bytes yet |
| 70 | if (key_len > 20) |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 71 | return srtp_err_status_bad_param; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 72 | |
| 73 | // Check output length - should be less than 20 bytes/ |
| 74 | if (out_len > 20) |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 75 | return srtp_err_status_bad_param; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 76 | |
| 77 | // Allocate memory for auth and hmac_ctx_t structures. |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 78 | pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))]; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 79 | if (pointer == NULL) |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 80 | return srtp_err_status_alloc_fail; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 81 | |
| 82 | // Set pointers |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 83 | *a = reinterpret_cast<srtp_auth_t*>(pointer); |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 84 | // |external_hmac| is const and libsrtp expects |type| to be non-const. |
| 85 | // const conversion is required. |external_hmac| is constant because we don't |
| 86 | // want to increase global count in Chrome. |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 87 | (*a)->type = const_cast<srtp_auth_type_t*>(&external_hmac); |
| 88 | (*a)->state = pointer + sizeof(srtp_auth_t); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 89 | (*a)->out_len = out_len; |
| 90 | (*a)->key_len = key_len; |
| 91 | (*a)->prefix_len = 0; |
| 92 | |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 93 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 96 | srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 97 | // Zeroize entire state |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 98 | memset(reinterpret_cast<uint8_t*>(a), 0, |
| 99 | sizeof(ExternalHmacContext) + sizeof(srtp_auth_t)); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 100 | |
| 101 | // Free memory |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 102 | delete[] a; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 103 | |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 104 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Vlad Tsyrklevich | e8e8ad8 | 2017-12-06 18:38:22 -0800 | [diff] [blame] | 107 | srtp_err_status_t external_hmac_init(void* state, |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 108 | const uint8_t* key, |
| 109 | int key_len) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 110 | if (key_len > HMAC_KEY_LENGTH) |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 111 | return srtp_err_status_bad_param; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 112 | |
Vlad Tsyrklevich | e8e8ad8 | 2017-12-06 18:38:22 -0800 | [diff] [blame] | 113 | ExternalHmacContext* context = static_cast<ExternalHmacContext*>(state); |
Vlad Tsyrklevich | e8e8ad8 | 2017-12-06 18:38:22 -0800 | [diff] [blame] | 114 | memcpy(context->key, key, key_len); |
| 115 | context->key_length = key_len; |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 116 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Vlad Tsyrklevich | e8e8ad8 | 2017-12-06 18:38:22 -0800 | [diff] [blame] | 119 | srtp_err_status_t external_hmac_start(void* /*state*/) { |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 120 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Vlad Tsyrklevich | e8e8ad8 | 2017-12-06 18:38:22 -0800 | [diff] [blame] | 123 | srtp_err_status_t external_hmac_update(void* /*state*/, |
| 124 | const uint8_t* /*message*/, |
| 125 | int /*msg_octets*/) { |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 126 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Vlad Tsyrklevich | e8e8ad8 | 2017-12-06 18:38:22 -0800 | [diff] [blame] | 129 | srtp_err_status_t external_hmac_compute(void* /*state*/, |
| 130 | const uint8_t* /*message*/, |
| 131 | int /*msg_octets*/, |
| 132 | int tag_len, |
| 133 | uint8_t* result) { |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 134 | memcpy(result, kExternalHmacFakeTag, tag_len); |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 135 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 136 | } |
| 137 | |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 138 | srtp_err_status_t external_crypto_init() { |
henrike@webrtc.org | 8b61011 | 2014-03-18 21:39:10 +0000 | [diff] [blame] | 139 | // |external_hmac| is const. const_cast is required as libsrtp expects |
| 140 | // non-const. |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 141 | srtp_err_status_t status = srtp_replace_auth_type( |
| 142 | const_cast<srtp_auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 143 | if (status) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 144 | RTC_LOG(LS_ERROR) << "Error in replacing default auth module, error: " |
| 145 | << status; |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 146 | return srtp_err_status_fail; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 147 | } |
mattdr | 8cab52d | 2016-10-10 15:33:37 -0700 | [diff] [blame] | 148 | return srtp_err_status_ok; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 149 | } |