blob: 899c33822caaacad9e589771e91ed0b6796bc3e0 [file] [log] [blame]
henrike@webrtc.orga7b98182014-02-21 15:51:431/*
kjellander65c7f672016-02-12 08:05:012 * Copyright 2014 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.orga7b98182014-02-21 15:51:433 *
kjellander65c7f672016-02-12 08:05:014 * 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:439 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "pc/externalhmac.h"
henrike@webrtc.org2d213e42014-03-06 18:51:2112
13#include <stdlib.h> // For malloc/free.
14
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "rtc_base/logging.h"
henrike@webrtc.orga7b98182014-02-21 15:51:4316
mattdr0d8ade52016-10-25 16:47:2617#include "third_party/libsrtp/crypto/include/crypto_kernel.h"
18#include "third_party/libsrtp/include/srtp.h"
mattdr51f29192016-09-28 21:08:4619
henrike@webrtc.orga7b98182014-02-21 15:51:4320// Begin test case 0 */
henrike@webrtc.org8b610112014-03-18 21:39:1021static const uint8_t kExternalHmacTestCase0Key[20] = {
henrike@webrtc.orga7b98182014-02-21 15:51:4322 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:1027static const uint8_t kExternalHmacTestCase0Data[8] = {
henrike@webrtc.orga7b98182014-02-21 15:51:4328 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
29};
30
henrike@webrtc.org8b610112014-03-18 21:39:1031static const uint8_t kExternalHmacFakeTag[10] = {
henrike@webrtc.orga7b98182014-02-21 15:51:4332 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
33};
34
mattdr8cab52d2016-10-10 22:33:3735static const srtp_auth_test_case_t kExternalHmacTestCase0 = {
henrike@webrtc.org8b610112014-03-18 21:39:1036 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:4344};
45
henrike@webrtc.org8b610112014-03-18 21:39:1046static const char kExternalHmacDescription[] =
47 "external hmac sha-1 authentication";
48
mattdr8cab52d2016-10-10 22:33:3749// srtp_auth_type_t external_hmac is the hmac metaobject
henrike@webrtc.org8b610112014-03-18 21:39:1050
mattdr8cab52d2016-10-10 22:33:3751static const srtp_auth_type_t external_hmac = {
52 external_hmac_alloc,
53 external_hmac_dealloc,
Vlad Tsyrkleviche8e8ad82017-12-07 02:38:2254 external_hmac_init,
55 external_hmac_compute,
56 external_hmac_update,
57 external_hmac_start,
mattdr8cab52d2016-10-10 22:33:3758 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:1062
mattdr8cab52d2016-10-10 22:33:3763srtp_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:4366 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 22:33:3771 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:4372
73 // Check output length - should be less than 20 bytes/
74 if (out_len > 20)
mattdr8cab52d2016-10-10 22:33:3775 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:4376
77 // Allocate memory for auth and hmac_ctx_t structures.
mattdr8cab52d2016-10-10 22:33:3778 pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))];
henrike@webrtc.orga7b98182014-02-21 15:51:4379 if (pointer == NULL)
mattdr8cab52d2016-10-10 22:33:3780 return srtp_err_status_alloc_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:4381
82 // Set pointers
Steve Anton36b29d12017-10-30 16:57:4283 *a = reinterpret_cast<srtp_auth_t*>(pointer);
henrike@webrtc.org8b610112014-03-18 21:39:1084 // |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 22:33:3787 (*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:4389 (*a)->out_len = out_len;
90 (*a)->key_len = key_len;
91 (*a)->prefix_len = 0;
92
mattdr8cab52d2016-10-10 22:33:3793 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:4394}
95
mattdr8cab52d2016-10-10 22:33:3796srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) {
henrike@webrtc.orga7b98182014-02-21 15:51:4397 // Zeroize entire state
Steve Anton36b29d12017-10-30 16:57:4298 memset(reinterpret_cast<uint8_t*>(a), 0,
99 sizeof(ExternalHmacContext) + sizeof(srtp_auth_t));
henrike@webrtc.orga7b98182014-02-21 15:51:43100
101 // Free memory
henrike@webrtc.org8b610112014-03-18 21:39:10102 delete[] a;
henrike@webrtc.orga7b98182014-02-21 15:51:43103
mattdr8cab52d2016-10-10 22:33:37104 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43105}
106
Vlad Tsyrkleviche8e8ad82017-12-07 02:38:22107srtp_err_status_t external_hmac_init(void* state,
mattdr8cab52d2016-10-10 22:33:37108 const uint8_t* key,
109 int key_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43110 if (key_len > HMAC_KEY_LENGTH)
mattdr8cab52d2016-10-10 22:33:37111 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43112
Vlad Tsyrkleviche8e8ad82017-12-07 02:38:22113 ExternalHmacContext* context = static_cast<ExternalHmacContext*>(state);
Vlad Tsyrkleviche8e8ad82017-12-07 02:38:22114 memcpy(context->key, key, key_len);
115 context->key_length = key_len;
mattdr8cab52d2016-10-10 22:33:37116 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43117}
118
Vlad Tsyrkleviche8e8ad82017-12-07 02:38:22119srtp_err_status_t external_hmac_start(void* /*state*/) {
mattdr8cab52d2016-10-10 22:33:37120 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43121}
122
Vlad Tsyrkleviche8e8ad82017-12-07 02:38:22123srtp_err_status_t external_hmac_update(void* /*state*/,
124 const uint8_t* /*message*/,
125 int /*msg_octets*/) {
mattdr8cab52d2016-10-10 22:33:37126 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43127}
128
Vlad Tsyrkleviche8e8ad82017-12-07 02:38:22129srtp_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.org8b610112014-03-18 21:39:10134 memcpy(result, kExternalHmacFakeTag, tag_len);
mattdr8cab52d2016-10-10 22:33:37135 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43136}
137
mattdr8cab52d2016-10-10 22:33:37138srtp_err_status_t external_crypto_init() {
henrike@webrtc.org8b610112014-03-18 21:39:10139 // |external_hmac| is const. const_cast is required as libsrtp expects
140 // non-const.
mattdr8cab52d2016-10-10 22:33:37141 srtp_err_status_t status = srtp_replace_auth_type(
142 const_cast<srtp_auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1);
henrike@webrtc.orga7b98182014-02-21 15:51:43143 if (status) {
Mirko Bonadei675513b2017-11-09 10:09:25144 RTC_LOG(LS_ERROR) << "Error in replacing default auth module, error: "
145 << status;
mattdr8cab52d2016-10-10 22:33:37146 return srtp_err_status_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43147 }
mattdr8cab52d2016-10-10 22:33:37148 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43149}