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