blob: a094ff84327eb40824afcc327d916e028a6bccd3 [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
mattdr51f29192016-09-28 14:08:46 -070015#ifdef HAVE_SRTP
jiayl@webrtc.orga197a5e2015-03-23 22:11:49 +000016extern "C" {
kjellander7bc7c062016-04-22 04:57:49 -070017#ifdef SRTP_RELATIVE_PATH
18#include "crypto_kernel.h" // NOLINT
19#include "srtp.h" // NOLINT
20#else
mattdr51f29192016-09-28 14:08:46 -070021#include "third_party/libsrtp/crypto/include/crypto_kernel.h"
22#include "third_party/libsrtp/include/srtp.h"
kjellander7bc7c062016-04-22 04:57:49 -070023#endif // SRTP_RELATIVE_PATH
jiayl@webrtc.orga197a5e2015-03-23 22:11:49 +000024}
mattdr51f29192016-09-28 14:08:46 -070025#endif // HAVE_SRTP
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000026
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000027#include "webrtc/base/logging.h"
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000028
mattdr8cab52d2016-10-10 15:33:37 -070029#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 14:08:46 -070041#if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)
42
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000043// Begin test case 0 */
henrike@webrtc.org8b610112014-03-18 21:39:10 +000044static const uint8_t kExternalHmacTestCase0Key[20] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000045 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:10 +000050static const uint8_t kExternalHmacTestCase0Data[8] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000051 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
52};
53
henrike@webrtc.org8b610112014-03-18 21:39:10 +000054static const uint8_t kExternalHmacFakeTag[10] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000055 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
56};
57
mattdr8cab52d2016-10-10 15:33:37 -070058static const srtp_auth_test_case_t kExternalHmacTestCase0 = {
henrike@webrtc.org8b610112014-03-18 21:39:10 +000059 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:43 +000067};
68
henrike@webrtc.org8b610112014-03-18 21:39:10 +000069static const char kExternalHmacDescription[] =
70 "external hmac sha-1 authentication";
71
mattdr8cab52d2016-10-10 15:33:37 -070072// srtp_auth_type_t external_hmac is the hmac metaobject
henrike@webrtc.org8b610112014-03-18 21:39:10 +000073
mattdr8cab52d2016-10-10 15:33:37 -070074#ifdef COMPILING_AGAINST_LIBSRTP1
75static const srtp_auth_type_t external_hmac = {
henrike@webrtc.org8b610112014-03-18 21:39:10 +000076 external_hmac_alloc,
77 external_hmac_dealloc,
mattdr8cab52d2016-10-10 15:33:37 -070078 (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:10 +000082 const_cast<char*>(kExternalHmacDescription),
83 0, // Instance count.
mattdr8cab52d2016-10-10 15:33:37 -070084 const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0),
henrike@webrtc.org8b610112014-03-18 21:39:10 +000085 NULL, // No debugging module.
86 EXTERNAL_HMAC_SHA1
87};
mattdr8cab52d2016-10-10 15:33:37 -070088#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:10 +0000101
mattdr8cab52d2016-10-10 15:33:37 -0700102srtp_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:43 +0000105 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 15:33:37 -0700110 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000111
112 // Check output length - should be less than 20 bytes/
113 if (out_len > 20)
mattdr8cab52d2016-10-10 15:33:37 -0700114 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000115
116 // Allocate memory for auth and hmac_ctx_t structures.
mattdr8cab52d2016-10-10 15:33:37 -0700117 pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))];
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000118 if (pointer == NULL)
mattdr8cab52d2016-10-10 15:33:37 -0700119 return srtp_err_status_alloc_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000120
121 // Set pointers
mattdr8cab52d2016-10-10 15:33:37 -0700122 *a = (srtp_auth_t *)pointer;
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000123 // |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 15:33:37 -0700126 (*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:43 +0000128 (*a)->out_len = out_len;
129 (*a)->key_len = key_len;
130 (*a)->prefix_len = 0;
131
mattdr8cab52d2016-10-10 15:33:37 -0700132 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000133}
134
mattdr8cab52d2016-10-10 15:33:37 -0700135srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000136 // Zeroize entire state
mattdr8cab52d2016-10-10 15:33:37 -0700137 memset((uint8_t *)a, 0, sizeof(ExternalHmacContext) + sizeof(srtp_auth_t));
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000138
139 // Free memory
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000140 delete[] a;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000141
mattdr8cab52d2016-10-10 15:33:37 -0700142 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000143}
144
mattdr8cab52d2016-10-10 15:33:37 -0700145srtp_err_status_t external_hmac_init(ExternalHmacContext* state,
146 const uint8_t* key,
147 int key_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000148 if (key_len > HMAC_KEY_LENGTH)
mattdr8cab52d2016-10-10 15:33:37 -0700149 return srtp_err_status_bad_param;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000150
151 memset(state->key, 0, key_len);
152 memcpy(state->key, key, key_len);
153 state->key_length = key_len;
mattdr8cab52d2016-10-10 15:33:37 -0700154 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000155}
156
mattdr8cab52d2016-10-10 15:33:37 -0700157srtp_err_status_t external_hmac_start(ExternalHmacContext* state) {
158 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000159}
160
mattdr8cab52d2016-10-10 15:33:37 -0700161srtp_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:43 +0000165}
166
mattdr8cab52d2016-10-10 15:33:37 -0700167srtp_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:10 +0000172 memcpy(result, kExternalHmacFakeTag, tag_len);
mattdr8cab52d2016-10-10 15:33:37 -0700173 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000174}
175
mattdr8cab52d2016-10-10 15:33:37 -0700176srtp_err_status_t external_crypto_init() {
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000177 // |external_hmac| is const. const_cast is required as libsrtp expects
178 // non-const.
mattdr8cab52d2016-10-10 15:33:37 -0700179 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:43 +0000181 if (status) {
182 LOG(LS_ERROR) << "Error in replacing default auth module, error: "
183 << status;
mattdr8cab52d2016-10-10 15:33:37 -0700184 return srtp_err_status_fail;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000185 }
mattdr8cab52d2016-10-10 15:33:37 -0700186 return srtp_err_status_ok;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000187}
188
189#endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)