blob: 0e82fffc2863abfd27cbbf8f863e5e7aecd59471 [file] [log] [blame]
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2014 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.orga7b98182014-02-21 15:51:43 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/externalhmac.h"
henrike@webrtc.org2d213e42014-03-06 18:51:21 +000012
13#include <stdlib.h> // For malloc/free.
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/logging.h"
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000016
mattdr0d8ade52016-10-25 09:47:26 -070017#include "third_party/libsrtp/crypto/include/crypto_kernel.h"
18#include "third_party/libsrtp/include/srtp.h"
mattdr51f29192016-09-28 14:08:46 -070019
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000020// Begin test case 0 */
henrike@webrtc.org8b610112014-03-18 21:39:10 +000021static const uint8_t kExternalHmacTestCase0Key[20] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000022 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.org8b610112014-03-18 21:39:10 +000027static const uint8_t kExternalHmacTestCase0Data[8] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000028 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
29};
30
henrike@webrtc.org8b610112014-03-18 21:39:10 +000031static const uint8_t kExternalHmacFakeTag[10] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000032 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
33};
34
mattdr8cab52d2016-10-10 15:33:37 -070035static const srtp_auth_test_case_t kExternalHmacTestCase0 = {
henrike@webrtc.org8b610112014-03-18 21:39:10 +000036 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.orga7b98182014-02-21 15:51:43 +000044};
45
henrike@webrtc.org8b610112014-03-18 21:39:10 +000046static const char kExternalHmacDescription[] =
47 "external hmac sha-1 authentication";
48
mattdr8cab52d2016-10-10 15:33:37 -070049// srtp_auth_type_t external_hmac is the hmac metaobject
henrike@webrtc.org8b610112014-03-18 21:39:10 +000050
mattdr8cab52d2016-10-10 15:33:37 -070051static const srtp_auth_type_t external_hmac = {
52 external_hmac_alloc,
53 external_hmac_dealloc,
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -080054 external_hmac_init,
55 external_hmac_compute,
56 external_hmac_update,
57 external_hmac_start,
mattdr8cab52d2016-10-10 15:33:37 -070058 const_cast<char*>(kExternalHmacDescription),
59 const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0),
60 EXTERNAL_HMAC_SHA1
61};
henrike@webrtc.org8b610112014-03-18 21:39:10 +000062
mattdr8cab52d2016-10-10 15:33:37 -070063srtp_err_status_t external_hmac_alloc(srtp_auth_t** a,
64 int key_len,
65 int out_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000066 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)
mattdr8cab52d2016-10-10 15:33:37 -070071 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000072
73 // Check output length - should be less than 20 bytes/
74 if (out_len > 20)
mattdr8cab52d2016-10-10 15:33:37 -070075 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000076
77 // Allocate memory for auth and hmac_ctx_t structures.
mattdr8cab52d2016-10-10 15:33:37 -070078 pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))];
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000079 if (pointer == NULL)
mattdr8cab52d2016-10-10 15:33:37 -070080 return srtp_err_status_alloc_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000081
82 // Set pointers
Steve Anton36b29d12017-10-30 09:57:42 -070083 *a = reinterpret_cast<srtp_auth_t*>(pointer);
henrike@webrtc.org8b610112014-03-18 21:39:10 +000084 // |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.
mattdr8cab52d2016-10-10 15:33:37 -070087 (*a)->type = const_cast<srtp_auth_type_t*>(&external_hmac);
88 (*a)->state = pointer + sizeof(srtp_auth_t);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000089 (*a)->out_len = out_len;
90 (*a)->key_len = key_len;
91 (*a)->prefix_len = 0;
92
mattdr8cab52d2016-10-10 15:33:37 -070093 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000094}
95
mattdr8cab52d2016-10-10 15:33:37 -070096srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000097 // Zeroize entire state
Steve Anton36b29d12017-10-30 09:57:42 -070098 memset(reinterpret_cast<uint8_t*>(a), 0,
99 sizeof(ExternalHmacContext) + sizeof(srtp_auth_t));
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000100
101 // Free memory
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000102 delete[] a;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000103
mattdr8cab52d2016-10-10 15:33:37 -0700104 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000105}
106
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800107srtp_err_status_t external_hmac_init(void* state,
mattdr8cab52d2016-10-10 15:33:37 -0700108 const uint8_t* key,
109 int key_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000110 if (key_len > HMAC_KEY_LENGTH)
mattdr8cab52d2016-10-10 15:33:37 -0700111 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000112
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800113 ExternalHmacContext* context = static_cast<ExternalHmacContext*>(state);
114 memset(context->key, 0, key_len);
115 memcpy(context->key, key, key_len);
116 context->key_length = key_len;
mattdr8cab52d2016-10-10 15:33:37 -0700117 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000118}
119
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800120srtp_err_status_t external_hmac_start(void* /*state*/) {
mattdr8cab52d2016-10-10 15:33:37 -0700121 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000122}
123
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800124srtp_err_status_t external_hmac_update(void* /*state*/,
125 const uint8_t* /*message*/,
126 int /*msg_octets*/) {
mattdr8cab52d2016-10-10 15:33:37 -0700127 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000128}
129
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800130srtp_err_status_t external_hmac_compute(void* /*state*/,
131 const uint8_t* /*message*/,
132 int /*msg_octets*/,
133 int tag_len,
134 uint8_t* result) {
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000135 memcpy(result, kExternalHmacFakeTag, tag_len);
mattdr8cab52d2016-10-10 15:33:37 -0700136 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000137}
138
mattdr8cab52d2016-10-10 15:33:37 -0700139srtp_err_status_t external_crypto_init() {
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000140 // |external_hmac| is const. const_cast is required as libsrtp expects
141 // non-const.
mattdr8cab52d2016-10-10 15:33:37 -0700142 srtp_err_status_t status = srtp_replace_auth_type(
143 const_cast<srtp_auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000144 if (status) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100145 RTC_LOG(LS_ERROR) << "Error in replacing default auth module, error: "
146 << status;
mattdr8cab52d2016-10-10 15:33:37 -0700147 return srtp_err_status_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000148 }
mattdr8cab52d2016-10-10 15:33:37 -0700149 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000150}