blob: 6c6d000a0583b92428ad8acafb1548805c6cf400 [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,
54 (srtp_auth_init_func) external_hmac_init,
55 (srtp_auth_compute_func) external_hmac_compute,
56 (srtp_auth_update_func) external_hmac_update,
57 (srtp_auth_start_func) external_hmac_start,
58 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
mattdr8cab52d2016-10-10 15:33:37 -070083 *a = (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
mattdr8cab52d2016-10-10 15:33:37 -070098 memset((uint8_t *)a, 0, 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
mattdr8cab52d2016-10-10 15:33:37 -0700106srtp_err_status_t external_hmac_init(ExternalHmacContext* state,
107 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
112 memset(state->key, 0, key_len);
113 memcpy(state->key, key, key_len);
114 state->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
mattdr8cab52d2016-10-10 15:33:37 -0700118srtp_err_status_t external_hmac_start(ExternalHmacContext* state) {
119 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000120}
121
mattdr8cab52d2016-10-10 15:33:37 -0700122srtp_err_status_t external_hmac_update(ExternalHmacContext* state,
123 const uint8_t* message,
124 int msg_octets) {
125 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000126}
127
mattdr8cab52d2016-10-10 15:33:37 -0700128srtp_err_status_t external_hmac_compute(ExternalHmacContext* state,
129 const void* 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) {
143 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}