blob: a094ff84327eb40824afcc327d916e028a6bccd3 [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
kjellander@webrtc.org9b8df252016-02-12 05:47:5911#include "webrtc/pc/externalhmac.h"
henrike@webrtc.org2d213e42014-03-06 18:51:2112
13#include <stdlib.h> // For malloc/free.
14
mattdr51f29192016-09-28 21:08:4615#ifdef HAVE_SRTP
jiayl@webrtc.orga197a5e2015-03-23 22:11:4916extern "C" {
kjellander7bc7c062016-04-22 11:57:4917#ifdef SRTP_RELATIVE_PATH
18#include "crypto_kernel.h" // NOLINT
19#include "srtp.h" // NOLINT
20#else
mattdr51f29192016-09-28 21:08:4621#include "third_party/libsrtp/crypto/include/crypto_kernel.h"
22#include "third_party/libsrtp/include/srtp.h"
kjellander7bc7c062016-04-22 11:57:4923#endif // SRTP_RELATIVE_PATH
jiayl@webrtc.orga197a5e2015-03-23 22:11:4924}
mattdr51f29192016-09-28 21:08:4625#endif // HAVE_SRTP
henrike@webrtc.orga7b98182014-02-21 15:51:4326
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5227#include "webrtc/base/logging.h"
henrike@webrtc.orga7b98182014-02-21 15:51:4328
mattdr8cab52d2016-10-10 22:33:3729#ifdef COMPILING_AGAINST_LIBSRTP1
30#define srtp_auth_type_t auth_type_t
31
32#define srtp_auth_init_func auth_init_func
33#define srtp_auth_compute_func auth_compute_func
34#define srtp_auth_update_func auth_update_func
35#define srtp_auth_start_func auth_start_func
36#define srtp_auth_test_case_t auth_test_case_t
37
38#define srtp_replace_auth_type crypto_kernel_replace_auth_type
39#endif // COMPILING_AGAINST_LIBSRTP1
40
mattdr51f29192016-09-28 21:08:4641#if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)
42
henrike@webrtc.orga7b98182014-02-21 15:51:4343// Begin test case 0 */
henrike@webrtc.org8b610112014-03-18 21:39:1044static const uint8_t kExternalHmacTestCase0Key[20] = {
henrike@webrtc.orga7b98182014-02-21 15:51:4345 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
46 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
47 0x0b, 0x0b, 0x0b, 0x0b
48};
49
henrike@webrtc.org8b610112014-03-18 21:39:1050static const uint8_t kExternalHmacTestCase0Data[8] = {
henrike@webrtc.orga7b98182014-02-21 15:51:4351 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
52};
53
henrike@webrtc.org8b610112014-03-18 21:39:1054static const uint8_t kExternalHmacFakeTag[10] = {
henrike@webrtc.orga7b98182014-02-21 15:51:4355 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
56};
57
mattdr8cab52d2016-10-10 22:33:3758static const srtp_auth_test_case_t kExternalHmacTestCase0 = {
henrike@webrtc.org8b610112014-03-18 21:39:1059 20, // Octets in key
60 const_cast<uint8_t*>(kExternalHmacTestCase0Key), // Key
61 8, // Octets in data
62 const_cast<uint8_t*>(kExternalHmacTestCase0Data), // Data
63 10, // Octets in tag
64 const_cast<uint8_t*>(kExternalHmacFakeTag), // Tag
65 NULL // Pointer to next
66 // testcase
henrike@webrtc.orga7b98182014-02-21 15:51:4367};
68
henrike@webrtc.org8b610112014-03-18 21:39:1069static const char kExternalHmacDescription[] =
70 "external hmac sha-1 authentication";
71
mattdr8cab52d2016-10-10 22:33:3772// srtp_auth_type_t external_hmac is the hmac metaobject
henrike@webrtc.org8b610112014-03-18 21:39:1073
mattdr8cab52d2016-10-10 22:33:3774#ifdef COMPILING_AGAINST_LIBSRTP1
75static const srtp_auth_type_t external_hmac = {
henrike@webrtc.org8b610112014-03-18 21:39:1076 external_hmac_alloc,
77 external_hmac_dealloc,
mattdr8cab52d2016-10-10 22:33:3778 (srtp_auth_init_func) external_hmac_init,
79 (srtp_auth_compute_func) external_hmac_compute,
80 (srtp_auth_update_func) external_hmac_update,
81 (srtp_auth_start_func) external_hmac_start,
henrike@webrtc.org8b610112014-03-18 21:39:1082 const_cast<char*>(kExternalHmacDescription),
83 0, // Instance count.
mattdr8cab52d2016-10-10 22:33:3784 const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0),
henrike@webrtc.org8b610112014-03-18 21:39:1085 NULL, // No debugging module.
86 EXTERNAL_HMAC_SHA1
87};
mattdr8cab52d2016-10-10 22:33:3788#else
89static const srtp_auth_type_t external_hmac = {
90 external_hmac_alloc,
91 external_hmac_dealloc,
92 (srtp_auth_init_func) external_hmac_init,
93 (srtp_auth_compute_func) external_hmac_compute,
94 (srtp_auth_update_func) external_hmac_update,
95 (srtp_auth_start_func) external_hmac_start,
96 const_cast<char*>(kExternalHmacDescription),
97 const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0),
98 EXTERNAL_HMAC_SHA1
99};
100#endif // COMPILING_AGAINST_LIBSRTP1
henrike@webrtc.org8b610112014-03-18 21:39:10101
mattdr8cab52d2016-10-10 22:33:37102srtp_err_status_t external_hmac_alloc(srtp_auth_t** a,
103 int key_len,
104 int out_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43105 uint8_t* pointer;
106
107 // Check key length - note that we don't support keys larger
108 // than 20 bytes yet
109 if (key_len > 20)
mattdr8cab52d2016-10-10 22:33:37110 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43111
112 // Check output length - should be less than 20 bytes/
113 if (out_len > 20)
mattdr8cab52d2016-10-10 22:33:37114 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43115
116 // Allocate memory for auth and hmac_ctx_t structures.
mattdr8cab52d2016-10-10 22:33:37117 pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))];
henrike@webrtc.orga7b98182014-02-21 15:51:43118 if (pointer == NULL)
mattdr8cab52d2016-10-10 22:33:37119 return srtp_err_status_alloc_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43120
121 // Set pointers
mattdr8cab52d2016-10-10 22:33:37122 *a = (srtp_auth_t *)pointer;
henrike@webrtc.org8b610112014-03-18 21:39:10123 // |external_hmac| is const and libsrtp expects |type| to be non-const.
124 // const conversion is required. |external_hmac| is constant because we don't
125 // want to increase global count in Chrome.
mattdr8cab52d2016-10-10 22:33:37126 (*a)->type = const_cast<srtp_auth_type_t*>(&external_hmac);
127 (*a)->state = pointer + sizeof(srtp_auth_t);
henrike@webrtc.orga7b98182014-02-21 15:51:43128 (*a)->out_len = out_len;
129 (*a)->key_len = key_len;
130 (*a)->prefix_len = 0;
131
mattdr8cab52d2016-10-10 22:33:37132 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43133}
134
mattdr8cab52d2016-10-10 22:33:37135srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) {
henrike@webrtc.orga7b98182014-02-21 15:51:43136 // Zeroize entire state
mattdr8cab52d2016-10-10 22:33:37137 memset((uint8_t *)a, 0, sizeof(ExternalHmacContext) + sizeof(srtp_auth_t));
henrike@webrtc.orga7b98182014-02-21 15:51:43138
139 // Free memory
henrike@webrtc.org8b610112014-03-18 21:39:10140 delete[] a;
henrike@webrtc.orga7b98182014-02-21 15:51:43141
mattdr8cab52d2016-10-10 22:33:37142 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43143}
144
mattdr8cab52d2016-10-10 22:33:37145srtp_err_status_t external_hmac_init(ExternalHmacContext* state,
146 const uint8_t* key,
147 int key_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43148 if (key_len > HMAC_KEY_LENGTH)
mattdr8cab52d2016-10-10 22:33:37149 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43150
151 memset(state->key, 0, key_len);
152 memcpy(state->key, key, key_len);
153 state->key_length = key_len;
mattdr8cab52d2016-10-10 22:33:37154 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43155}
156
mattdr8cab52d2016-10-10 22:33:37157srtp_err_status_t external_hmac_start(ExternalHmacContext* state) {
158 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43159}
160
mattdr8cab52d2016-10-10 22:33:37161srtp_err_status_t external_hmac_update(ExternalHmacContext* state,
162 const uint8_t* message,
163 int msg_octets) {
164 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43165}
166
mattdr8cab52d2016-10-10 22:33:37167srtp_err_status_t external_hmac_compute(ExternalHmacContext* state,
168 const void* message,
169 int msg_octets,
170 int tag_len,
171 uint8_t* result) {
henrike@webrtc.org8b610112014-03-18 21:39:10172 memcpy(result, kExternalHmacFakeTag, tag_len);
mattdr8cab52d2016-10-10 22:33:37173 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43174}
175
mattdr8cab52d2016-10-10 22:33:37176srtp_err_status_t external_crypto_init() {
henrike@webrtc.org8b610112014-03-18 21:39:10177 // |external_hmac| is const. const_cast is required as libsrtp expects
178 // non-const.
mattdr8cab52d2016-10-10 22:33:37179 srtp_err_status_t status = srtp_replace_auth_type(
180 const_cast<srtp_auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1);
henrike@webrtc.orga7b98182014-02-21 15:51:43181 if (status) {
182 LOG(LS_ERROR) << "Error in replacing default auth module, error: "
183 << status;
mattdr8cab52d2016-10-10 22:33:37184 return srtp_err_status_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43185 }
mattdr8cab52d2016-10-10 22:33:37186 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43187}
188
189#endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)