blob: f85e753e7191661dfd3ee734a45fc8cadedcb38b [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
mattdr51f29192016-09-28 14:08:46 -070029#if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)
30
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000031// Begin test case 0 */
henrike@webrtc.org8b610112014-03-18 21:39:10 +000032static const uint8_t kExternalHmacTestCase0Key[20] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000033 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
34 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
35 0x0b, 0x0b, 0x0b, 0x0b
36};
37
henrike@webrtc.org8b610112014-03-18 21:39:10 +000038static const uint8_t kExternalHmacTestCase0Data[8] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000039 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
40};
41
henrike@webrtc.org8b610112014-03-18 21:39:10 +000042static const uint8_t kExternalHmacFakeTag[10] = {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000043 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
44};
45
henrike@webrtc.org8b610112014-03-18 21:39:10 +000046static const auth_test_case_t kExternalHmacTestCase0 = {
47 20, // Octets in key
48 const_cast<uint8_t*>(kExternalHmacTestCase0Key), // Key
49 8, // Octets in data
50 const_cast<uint8_t*>(kExternalHmacTestCase0Data), // Data
51 10, // Octets in tag
52 const_cast<uint8_t*>(kExternalHmacFakeTag), // Tag
53 NULL // Pointer to next
54 // testcase
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000055};
56
henrike@webrtc.org8b610112014-03-18 21:39:10 +000057static const char kExternalHmacDescription[] =
58 "external hmac sha-1 authentication";
59
60// auth_type_t external_hmac is the hmac metaobject
61
62static const auth_type_t external_hmac = {
63 external_hmac_alloc,
64 external_hmac_dealloc,
65 (auth_init_func) external_hmac_init,
66 (auth_compute_func) external_hmac_compute,
67 (auth_update_func) external_hmac_update,
68 (auth_start_func) external_hmac_start,
69 const_cast<char*>(kExternalHmacDescription),
70 0, // Instance count.
71 const_cast<auth_test_case_t*>(&kExternalHmacTestCase0),
72 NULL, // No debugging module.
73 EXTERNAL_HMAC_SHA1
74};
75
76
77err_status_t external_hmac_alloc(auth_t** a, int key_len, int out_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000078 uint8_t* pointer;
79
80 // Check key length - note that we don't support keys larger
81 // than 20 bytes yet
82 if (key_len > 20)
83 return err_status_bad_param;
84
85 // Check output length - should be less than 20 bytes/
86 if (out_len > 20)
87 return err_status_bad_param;
88
89 // Allocate memory for auth and hmac_ctx_t structures.
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +000090 pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(auth_t))];
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000091 if (pointer == NULL)
92 return err_status_alloc_fail;
93
94 // Set pointers
95 *a = (auth_t *)pointer;
henrike@webrtc.org8b610112014-03-18 21:39:10 +000096 // |external_hmac| is const and libsrtp expects |type| to be non-const.
97 // const conversion is required. |external_hmac| is constant because we don't
98 // want to increase global count in Chrome.
99 (*a)->type = const_cast<auth_type_t*>(&external_hmac);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000100 (*a)->state = pointer + sizeof(auth_t);
101 (*a)->out_len = out_len;
102 (*a)->key_len = key_len;
103 (*a)->prefix_len = 0;
104
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000105 return err_status_ok;
106}
107
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000108err_status_t external_hmac_dealloc(auth_t* a) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000109 // Zeroize entire state
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000110 memset((uint8_t *)a, 0, sizeof(ExternalHmacContext) + sizeof(auth_t));
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000111
112 // Free memory
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000113 delete[] a;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000114
115 return err_status_ok;
116}
117
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000118err_status_t external_hmac_init(ExternalHmacContext* state,
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000119 const uint8_t* key,
120 int key_len) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000121 if (key_len > HMAC_KEY_LENGTH)
122 return err_status_bad_param;
123
124 memset(state->key, 0, key_len);
125 memcpy(state->key, key, key_len);
126 state->key_length = key_len;
127 return err_status_ok;
128}
129
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000130err_status_t external_hmac_start(ExternalHmacContext* state) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000131 return err_status_ok;
132}
133
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000134err_status_t external_hmac_update(ExternalHmacContext* state,
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000135 const uint8_t* message,
136 int msg_octets) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000137 return err_status_ok;
138}
139
pbos@webrtc.orga9cf0792014-12-18 09:12:21 +0000140err_status_t external_hmac_compute(ExternalHmacContext* state,
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000141 const void* message,
142 int msg_octets,
143 int tag_len,
144 uint8_t* result) {
145 memcpy(result, kExternalHmacFakeTag, tag_len);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000146 return err_status_ok;
147}
148
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000149err_status_t external_crypto_init() {
150 // |external_hmac| is const. const_cast is required as libsrtp expects
151 // non-const.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000152 err_status_t status = crypto_kernel_replace_auth_type(
henrike@webrtc.org8b610112014-03-18 21:39:10 +0000153 const_cast<auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000154 if (status) {
155 LOG(LS_ERROR) << "Error in replacing default auth module, error: "
156 << status;
157 return err_status_fail;
158 }
159 return err_status_ok;
160}
161
162#endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)