blob: ec71833d752e57ba014edb4122b017e8b10ee6f2 [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
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010011#include "webrtc/pc/externalhmac.h"
henrike@webrtc.org2d213e42014-03-06 18:51:21 +000012
13#include <stdlib.h> // For malloc/free.
14
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000015#include "webrtc/base/logging.h"
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000016
mattdr0d8ade52016-10-25 09:47:26 -070017#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
mattdr8cab52d2016-10-10 15:33:37 -070021
mattdr51f29192016-09-28 14:08:46 -070022#if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)
23
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000024// Begin test case 0 */
henrike@webrtc.org8b610112014-03-18 21:39:10 +000025static const uint8_t kExternalHmacTestCase0Key[20] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000026 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.org8b610112014-03-18 21:39:10 +000031static const uint8_t kExternalHmacTestCase0Data[8] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000032 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
33};
34
henrike@webrtc.org8b610112014-03-18 21:39:10 +000035static const uint8_t kExternalHmacFakeTag[10] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000036 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
37};
38
mattdr8cab52d2016-10-10 15:33:37 -070039static const srtp_auth_test_case_t kExternalHmacTestCase0 = {
henrike@webrtc.org8b610112014-03-18 21:39:10 +000040 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.orga7b98182014-02-21 15:51:43 +000048};
49
henrike@webrtc.org8b610112014-03-18 21:39:10 +000050static const char kExternalHmacDescription[] =
51 "external hmac sha-1 authentication";
52
mattdr8cab52d2016-10-10 15:33:37 -070053// srtp_auth_type_t external_hmac is the hmac metaobject
henrike@webrtc.org8b610112014-03-18 21:39:10 +000054
mattdr8cab52d2016-10-10 15:33:37 -070055static 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.org8b610112014-03-18 21:39:10 +000066
mattdr8cab52d2016-10-10 15:33:37 -070067srtp_err_status_t external_hmac_alloc(srtp_auth_t** a,
68 int key_len,
69 int out_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000070 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)
mattdr8cab52d2016-10-10 15:33:37 -070075 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000076
77 // Check output length - should be less than 20 bytes/
78 if (out_len > 20)
mattdr8cab52d2016-10-10 15:33:37 -070079 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000080
81 // Allocate memory for auth and hmac_ctx_t structures.
mattdr8cab52d2016-10-10 15:33:37 -070082 pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))];
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000083 if (pointer == NULL)
mattdr8cab52d2016-10-10 15:33:37 -070084 return srtp_err_status_alloc_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000085
86 // Set pointers
mattdr8cab52d2016-10-10 15:33:37 -070087 *a = (srtp_auth_t *)pointer;
henrike@webrtc.org8b610112014-03-18 21:39:10 +000088 // |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.
mattdr8cab52d2016-10-10 15:33:37 -070091 (*a)->type = const_cast<srtp_auth_type_t*>(&external_hmac);
92 (*a)->state = pointer + sizeof(srtp_auth_t);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000093 (*a)->out_len = out_len;
94 (*a)->key_len = key_len;
95 (*a)->prefix_len = 0;
96
mattdr8cab52d2016-10-10 15:33:37 -070097 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000098}
99
mattdr8cab52d2016-10-10 15:33:37 -0700100srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000101 // Zeroize entire state
mattdr8cab52d2016-10-10 15:33:37 -0700102 memset((uint8_t *)a, 0, sizeof(ExternalHmacContext) + sizeof(srtp_auth_t));
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000103
104 // Free memory
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000105 delete[] a;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000106
mattdr8cab52d2016-10-10 15:33:37 -0700107 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000108}
109
mattdr8cab52d2016-10-10 15:33:37 -0700110srtp_err_status_t external_hmac_init(ExternalHmacContext* state,
111 const uint8_t* key,
112 int key_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000113 if (key_len > HMAC_KEY_LENGTH)
mattdr8cab52d2016-10-10 15:33:37 -0700114 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000115
116 memset(state->key, 0, key_len);
117 memcpy(state->key, key, key_len);
118 state->key_length = key_len;
mattdr8cab52d2016-10-10 15:33:37 -0700119 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_start(ExternalHmacContext* state) {
123 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000124}
125
mattdr8cab52d2016-10-10 15:33:37 -0700126srtp_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.orga7b98182014-02-21 15:51:43 +0000130}
131
mattdr8cab52d2016-10-10 15:33:37 -0700132srtp_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.org8b610112014-03-18 21:39:10 +0000137 memcpy(result, kExternalHmacFakeTag, tag_len);
mattdr8cab52d2016-10-10 15:33:37 -0700138 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000139}
140
mattdr8cab52d2016-10-10 15:33:37 -0700141srtp_err_status_t external_crypto_init() {
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000142 // |external_hmac| is const. const_cast is required as libsrtp expects
143 // non-const.
mattdr8cab52d2016-10-10 15:33:37 -0700144 srtp_err_status_t status = srtp_replace_auth_type(
145 const_cast<srtp_auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000146 if (status) {
147 LOG(LS_ERROR) << "Error in replacing default auth module, error: "
148 << status;
mattdr8cab52d2016-10-10 15:33:37 -0700149 return srtp_err_status_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000150 }
mattdr8cab52d2016-10-10 15:33:37 -0700151 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000152}
153
154#endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)