blob: d88cfdc7ec3a3e134102b5f274d04709cec6e3a9 [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
11#if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)
12
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010013#include "webrtc/pc/externalhmac.h"
henrike@webrtc.org2d213e42014-03-06 18:51:21 +000014
15#include <stdlib.h> // For malloc/free.
16
jiayl@webrtc.orga197a5e2015-03-23 22:11:49 +000017extern "C" {
kjellander7bc7c062016-04-22 04:57:49 -070018#ifdef SRTP_RELATIVE_PATH
19#include "crypto_kernel.h" // NOLINT
20#include "srtp.h" // NOLINT
21#else
jiayl@webrtc.orga197a5e2015-03-23 22:11:49 +000022#include "third_party/libsrtp/srtp/crypto/include/crypto_kernel.h"
kjellandera96e2d72016-02-04 23:52:28 -080023#include "third_party/libsrtp/srtp/include/srtp.h"
kjellander7bc7c062016-04-22 04:57:49 -070024#endif // SRTP_RELATIVE_PATH
jiayl@webrtc.orga197a5e2015-03-23 22:11:49 +000025}
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
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000029// Begin test case 0 */
henrike@webrtc.org8b610112014-03-18 21:39:10 +000030static const uint8_t kExternalHmacTestCase0Key[20] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000031 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
32 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
33 0x0b, 0x0b, 0x0b, 0x0b
34};
35
henrike@webrtc.org8b610112014-03-18 21:39:10 +000036static const uint8_t kExternalHmacTestCase0Data[8] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000037 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
38};
39
henrike@webrtc.org8b610112014-03-18 21:39:10 +000040static const uint8_t kExternalHmacFakeTag[10] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000041 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
42};
43
henrike@webrtc.org8b610112014-03-18 21:39:10 +000044static const auth_test_case_t kExternalHmacTestCase0 = {
45 20, // Octets in key
46 const_cast<uint8_t*>(kExternalHmacTestCase0Key), // Key
47 8, // Octets in data
48 const_cast<uint8_t*>(kExternalHmacTestCase0Data), // Data
49 10, // Octets in tag
50 const_cast<uint8_t*>(kExternalHmacFakeTag), // Tag
51 NULL // Pointer to next
52 // testcase
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000053};
54
henrike@webrtc.org8b610112014-03-18 21:39:10 +000055static const char kExternalHmacDescription[] =
56 "external hmac sha-1 authentication";
57
58// auth_type_t external_hmac is the hmac metaobject
59
60static const auth_type_t external_hmac = {
61 external_hmac_alloc,
62 external_hmac_dealloc,
63 (auth_init_func) external_hmac_init,
64 (auth_compute_func) external_hmac_compute,
65 (auth_update_func) external_hmac_update,
66 (auth_start_func) external_hmac_start,
67 const_cast<char*>(kExternalHmacDescription),
68 0, // Instance count.
69 const_cast<auth_test_case_t*>(&kExternalHmacTestCase0),
70 NULL, // No debugging module.
71 EXTERNAL_HMAC_SHA1
72};
73
74
75err_status_t external_hmac_alloc(auth_t** a, int key_len, int out_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000076 uint8_t* pointer;
77
78 // Check key length - note that we don't support keys larger
79 // than 20 bytes yet
80 if (key_len > 20)
81 return err_status_bad_param;
82
83 // Check output length - should be less than 20 bytes/
84 if (out_len > 20)
85 return err_status_bad_param;
86
87 // Allocate memory for auth and hmac_ctx_t structures.
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +000088 pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(auth_t))];
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000089 if (pointer == NULL)
90 return err_status_alloc_fail;
91
92 // Set pointers
93 *a = (auth_t *)pointer;
henrike@webrtc.org8b610112014-03-18 21:39:10 +000094 // |external_hmac| is const and libsrtp expects |type| to be non-const.
95 // const conversion is required. |external_hmac| is constant because we don't
96 // want to increase global count in Chrome.
97 (*a)->type = const_cast<auth_type_t*>(&external_hmac);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000098 (*a)->state = pointer + sizeof(auth_t);
99 (*a)->out_len = out_len;
100 (*a)->key_len = key_len;
101 (*a)->prefix_len = 0;
102
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000103 return err_status_ok;
104}
105
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000106err_status_t external_hmac_dealloc(auth_t* a) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000107 // Zeroize entire state
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000108 memset((uint8_t *)a, 0, sizeof(ExternalHmacContext) + sizeof(auth_t));
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000109
110 // Free memory
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000111 delete[] a;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000112
113 return err_status_ok;
114}
115
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000116err_status_t external_hmac_init(ExternalHmacContext* state,
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000117 const uint8_t* key,
118 int key_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000119 if (key_len > HMAC_KEY_LENGTH)
120 return err_status_bad_param;
121
122 memset(state->key, 0, key_len);
123 memcpy(state->key, key, key_len);
124 state->key_length = key_len;
125 return err_status_ok;
126}
127
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000128err_status_t external_hmac_start(ExternalHmacContext* state) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000129 return err_status_ok;
130}
131
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000132err_status_t external_hmac_update(ExternalHmacContext* state,
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000133 const uint8_t* message,
134 int msg_octets) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000135 return err_status_ok;
136}
137
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000138err_status_t external_hmac_compute(ExternalHmacContext* state,
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000139 const void* message,
140 int msg_octets,
141 int tag_len,
142 uint8_t* result) {
143 memcpy(result, kExternalHmacFakeTag, tag_len);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000144 return err_status_ok;
145}
146
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000147err_status_t external_crypto_init() {
148 // |external_hmac| is const. const_cast is required as libsrtp expects
149 // non-const.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000150 err_status_t status = crypto_kernel_replace_auth_type(
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000151 const_cast<auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000152 if (status) {
153 LOG(LS_ERROR) << "Error in replacing default auth module, error: "
154 << status;
155 return err_status_fail;
156 }
157 return err_status_ok;
158}
159
160#endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)