blob: d5cfc951b5258fea5364e9d29d0e42e632256940 [file] [log] [blame]
henrike@webrtc.orga7b98182014-02-21 15:51:431/*
2 * libjingle
3 * Copyright 2014 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#if defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)
29
30#ifdef SRTP_RELATIVE_PATH
31#include "srtp.h" // NOLINT
32#else
33#include "third_party/libsrtp/include/srtp.h"
34#endif // SRTP_RELATIVE_PATH
35
36#include "talk/session/media/external_hmac.h"
37
38#include "talk/base/logging.h"
39
40// The debug module for authentiation
41debug_module_t mod_external_hmac = {
42 0, // Debugging is off by default
43 (char*)"external-hmac-sha-1" // Printable name for module
44};
45
46extern auth_type_t external_hmac;
47
48// Begin test case 0 */
49uint8_t
50external_hmac_test_case_0_key[20] = {
51 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
52 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
53 0x0b, 0x0b, 0x0b, 0x0b
54};
55
56uint8_t
57external_hmac_test_case_0_data[8] = {
58 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
59};
60
61uint8_t
62external_hmac_fake_tag[10] = {
63 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
64};
65
66auth_test_case_t
67external_hmac_test_case_0 = {
68 20, // Octets in key
69 external_hmac_test_case_0_key, // Key
70 8, // Octets in data
71 external_hmac_test_case_0_data, // Data
72 10, // Octets in tag
73 external_hmac_fake_tag, // Tag
74 NULL // Pointer to next testcase
75};
76
77err_status_t
78external_hmac_alloc(auth_t** a, int key_len, int out_len) {
79 uint8_t* pointer;
80
81 // Check key length - note that we don't support keys larger
82 // than 20 bytes yet
83 if (key_len > 20)
84 return err_status_bad_param;
85
86 // Check output length - should be less than 20 bytes/
87 if (out_len > 20)
88 return err_status_bad_param;
89
90 // Allocate memory for auth and hmac_ctx_t structures.
91 pointer = reinterpret_cast<uint8_t*>(
92 crypto_alloc(sizeof(external_hmac_ctx_t) + sizeof(auth_t)));
93 if (pointer == NULL)
94 return err_status_alloc_fail;
95
96 // Set pointers
97 *a = (auth_t *)pointer;
98 (*a)->type = &external_hmac;
99 (*a)->state = pointer + sizeof(auth_t);
100 (*a)->out_len = out_len;
101 (*a)->key_len = key_len;
102 (*a)->prefix_len = 0;
103
104 // Increment global count of all hmac uses.
105 external_hmac.ref_count++;
106
107 return err_status_ok;
108}
109
110err_status_t
111external_hmac_dealloc(auth_t* a) {
112 // Zeroize entire state
113 octet_string_set_to_zero((uint8_t *)a,
114 sizeof(external_hmac_ctx_t) + sizeof(auth_t));
115
116 // Free memory
117 crypto_free(a);
118
119 // Decrement global count of all hmac uses.
120 external_hmac.ref_count--;
121
122 return err_status_ok;
123}
124
125err_status_t
126external_hmac_init(external_hmac_ctx_t* state,
127 const uint8_t* key, int key_len) {
128 if (key_len > HMAC_KEY_LENGTH)
129 return err_status_bad_param;
130
131 memset(state->key, 0, key_len);
132 memcpy(state->key, key, key_len);
133 state->key_length = key_len;
134 return err_status_ok;
135}
136
137err_status_t
138external_hmac_start(external_hmac_ctx_t* state) {
139 return err_status_ok;
140}
141
142err_status_t
143external_hmac_update(external_hmac_ctx_t* state, const uint8_t* message,
144 int msg_octets) {
145 return err_status_ok;
146}
147
148err_status_t
149external_hmac_compute(external_hmac_ctx_t* state, const void* message,
150 int msg_octets, int tag_len, uint8_t* result) {
151 memcpy(result, external_hmac_fake_tag, tag_len);
152 return err_status_ok;
153}
154
155char external_hmac_description[] = "external hmac sha-1 authentication";
156
157 // auth_type_t external_hmac is the hmac metaobject
158
159auth_type_t
160external_hmac = {
161 (auth_alloc_func) external_hmac_alloc,
162 (auth_dealloc_func) external_hmac_dealloc,
163 (auth_init_func) external_hmac_init,
164 (auth_compute_func) external_hmac_compute,
165 (auth_update_func) external_hmac_update,
166 (auth_start_func) external_hmac_start,
167 (char *) external_hmac_description,
168 (int) 0, /* instance count */
169 (auth_test_case_t *) &external_hmac_test_case_0,
170 (debug_module_t *) &mod_external_hmac,
171 (auth_type_id_t) EXTERNAL_HMAC_SHA1
172};
173
174err_status_t
175external_crypto_init() {
176 err_status_t status = crypto_kernel_replace_auth_type(
177 &external_hmac, EXTERNAL_HMAC_SHA1);
178 if (status) {
179 LOG(LS_ERROR) << "Error in replacing default auth module, error: "
180 << status;
181 return err_status_fail;
182 }
183 return err_status_ok;
184}
185
186#endif // defined(HAVE_SRTP) && defined(ENABLE_EXTERNAL_AUTH)