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