blob: 8e730e9f4cb0fb7a24bb789720a6946e281557a9 [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"
Joachim Bauch5b32f232018-03-07 20:02:26 +010016#include "rtc_base/zero_memory.h"
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000017
mattdr0d8ade52016-10-25 09:47:26 -070018#include "third_party/libsrtp/crypto/include/crypto_kernel.h"
19#include "third_party/libsrtp/include/srtp.h"
mattdr51f29192016-09-28 14:08:46 -070020
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000021// Begin test case 0 */
henrike@webrtc.org8b610112014-03-18 21:39:10 +000022static const uint8_t kExternalHmacTestCase0Key[20] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000023 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
24 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
25 0x0b, 0x0b, 0x0b, 0x0b
26};
27
henrike@webrtc.org8b610112014-03-18 21:39:10 +000028static const uint8_t kExternalHmacTestCase0Data[8] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000029 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
30};
31
henrike@webrtc.org8b610112014-03-18 21:39:10 +000032static const uint8_t kExternalHmacFakeTag[10] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000033 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
34};
35
mattdr8cab52d2016-10-10 15:33:37 -070036static const srtp_auth_test_case_t kExternalHmacTestCase0 = {
henrike@webrtc.org8b610112014-03-18 21:39:10 +000037 20, // Octets in key
38 const_cast<uint8_t*>(kExternalHmacTestCase0Key), // Key
39 8, // Octets in data
40 const_cast<uint8_t*>(kExternalHmacTestCase0Data), // Data
41 10, // Octets in tag
42 const_cast<uint8_t*>(kExternalHmacFakeTag), // Tag
43 NULL // Pointer to next
44 // testcase
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000045};
46
henrike@webrtc.org8b610112014-03-18 21:39:10 +000047static const char kExternalHmacDescription[] =
48 "external hmac sha-1 authentication";
49
mattdr8cab52d2016-10-10 15:33:37 -070050// srtp_auth_type_t external_hmac is the hmac metaobject
henrike@webrtc.org8b610112014-03-18 21:39:10 +000051
mattdr8cab52d2016-10-10 15:33:37 -070052static const srtp_auth_type_t external_hmac = {
53 external_hmac_alloc,
54 external_hmac_dealloc,
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -080055 external_hmac_init,
56 external_hmac_compute,
57 external_hmac_update,
58 external_hmac_start,
mattdr8cab52d2016-10-10 15:33:37 -070059 const_cast<char*>(kExternalHmacDescription),
60 const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0),
61 EXTERNAL_HMAC_SHA1
62};
henrike@webrtc.org8b610112014-03-18 21:39:10 +000063
mattdr8cab52d2016-10-10 15:33:37 -070064srtp_err_status_t external_hmac_alloc(srtp_auth_t** a,
65 int key_len,
66 int out_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000067 uint8_t* pointer;
68
69 // Check key length - note that we don't support keys larger
70 // than 20 bytes yet
71 if (key_len > 20)
mattdr8cab52d2016-10-10 15:33:37 -070072 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000073
74 // Check output length - should be less than 20 bytes/
75 if (out_len > 20)
mattdr8cab52d2016-10-10 15:33:37 -070076 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000077
78 // Allocate memory for auth and hmac_ctx_t structures.
mattdr8cab52d2016-10-10 15:33:37 -070079 pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))];
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000080 if (pointer == NULL)
mattdr8cab52d2016-10-10 15:33:37 -070081 return srtp_err_status_alloc_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000082
83 // Set pointers
Steve Anton36b29d12017-10-30 09:57:42 -070084 *a = reinterpret_cast<srtp_auth_t*>(pointer);
henrike@webrtc.org8b610112014-03-18 21:39:10 +000085 // |external_hmac| is const and libsrtp expects |type| to be non-const.
86 // const conversion is required. |external_hmac| is constant because we don't
87 // want to increase global count in Chrome.
mattdr8cab52d2016-10-10 15:33:37 -070088 (*a)->type = const_cast<srtp_auth_type_t*>(&external_hmac);
89 (*a)->state = pointer + sizeof(srtp_auth_t);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000090 (*a)->out_len = out_len;
91 (*a)->key_len = key_len;
92 (*a)->prefix_len = 0;
93
mattdr8cab52d2016-10-10 15:33:37 -070094 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000095}
96
mattdr8cab52d2016-10-10 15:33:37 -070097srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) {
Joachim Bauch5b32f232018-03-07 20:02:26 +010098 rtc::ExplicitZeroMemory(a, sizeof(ExternalHmacContext) + sizeof(srtp_auth_t));
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000099
100 // Free memory
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000101 delete[] a;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000102
mattdr8cab52d2016-10-10 15:33:37 -0700103 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000104}
105
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800106srtp_err_status_t external_hmac_init(void* state,
mattdr8cab52d2016-10-10 15:33:37 -0700107 const uint8_t* key,
108 int key_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000109 if (key_len > HMAC_KEY_LENGTH)
mattdr8cab52d2016-10-10 15:33:37 -0700110 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000111
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800112 ExternalHmacContext* context = static_cast<ExternalHmacContext*>(state);
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800113 memcpy(context->key, key, key_len);
114 context->key_length = key_len;
mattdr8cab52d2016-10-10 15:33:37 -0700115 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000116}
117
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800118srtp_err_status_t external_hmac_start(void* /*state*/) {
mattdr8cab52d2016-10-10 15:33:37 -0700119 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000120}
121
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800122srtp_err_status_t external_hmac_update(void* /*state*/,
123 const uint8_t* /*message*/,
124 int /*msg_octets*/) {
mattdr8cab52d2016-10-10 15:33:37 -0700125 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000126}
127
Vlad Tsyrkleviche8e8ad82017-12-06 18:38:22 -0800128srtp_err_status_t external_hmac_compute(void* /*state*/,
129 const uint8_t* /*message*/,
130 int /*msg_octets*/,
131 int tag_len,
132 uint8_t* result) {
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000133 memcpy(result, kExternalHmacFakeTag, tag_len);
mattdr8cab52d2016-10-10 15:33:37 -0700134 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000135}
136
mattdr8cab52d2016-10-10 15:33:37 -0700137srtp_err_status_t external_crypto_init() {
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000138 // |external_hmac| is const. const_cast is required as libsrtp expects
139 // non-const.
mattdr8cab52d2016-10-10 15:33:37 -0700140 srtp_err_status_t status = srtp_replace_auth_type(
141 const_cast<srtp_auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000142 if (status) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100143 RTC_LOG(LS_ERROR) << "Error in replacing default auth module, error: "
144 << status;
mattdr8cab52d2016-10-10 15:33:37 -0700145 return srtp_err_status_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000146 }
mattdr8cab52d2016-10-10 15:33:37 -0700147 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000148}