Add win32 directory to EXTRA_DIST
[sipe-libnice.git] / stun / stunhmac.c
blobdb6fb1692f8230719d142b5ce02bae55bb43be7a
1 /*
2 * This file is part of the Nice GLib ICE library.
4 * (C) 2008-2009 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2007-2009 Nokia Corporation. All rights reserved.
7 * Contact: Rémi Denis-Courmont
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
17 * License.
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
24 * Contributors:
25 * Youness Alaoui, Collabora Ltd.
26 * Rémi Denis-Courmont, Nokia
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
39 #ifdef HAVE_CONFIG_H
40 # include <config.h>
41 #endif
43 #include "sha1.h"
44 #include "md5.h"
45 #include "rand.h"
47 #include "stunmessage.h"
48 #include "stunhmac.h"
50 #include <string.h>
51 #include <assert.h>
53 void stun_sha1 (const uint8_t *msg, size_t len, size_t msg_len, uint8_t *sha,
54 const void *key, size_t keylen, int padding)
56 uint16_t fakelen = htons (msg_len);
57 const uint8_t *vector[4];
58 size_t lengths[4];
59 uint8_t pad_char[64] = {0};
60 size_t num_elements;
62 assert (len >= 44u);
64 vector[0] = msg;
65 lengths[0] = 2;
66 vector[1] = (const uint8_t *)&fakelen;
67 lengths[1] = 2;
68 vector[2] = msg + 4;
69 lengths[2] = len - 28;
70 num_elements = 3;
72 /* RFC 3489 specifies that the message's size should be 64 bytes,
73 and \x00 padding should be done */
74 if (padding && ((len - 24) % 64) > 0) {
75 uint16_t pad_size = 64 - ((len - 24) % 64);
77 vector[3] = pad_char;
78 lengths[3] = pad_size;
79 num_elements++;
82 hmac_sha1_vector(key, keylen, num_elements, vector, lengths, sha);
85 static const uint8_t *priv_trim_var (const uint8_t *var, size_t *var_len)
87 const uint8_t *ptr = var;
89 while (*ptr == '"') {
90 ptr++;
91 (*var_len)--;
93 while(ptr[*var_len-1] == '"' ||
94 ptr[*var_len-1] == 0) {
95 (*var_len)--;
98 return ptr;
102 void stun_hash_creds (const uint8_t *realm, size_t realm_len,
103 const uint8_t *username, size_t username_len,
104 const uint8_t *password, size_t password_len,
105 unsigned char md5[16])
107 MD5_CTX ctx;
108 const uint8_t *username_trimmed = priv_trim_var (username, &username_len);
109 const uint8_t *password_trimmed = priv_trim_var (password, &password_len);
110 const uint8_t *realm_trimmed = priv_trim_var (realm, &realm_len);
111 const uint8_t *colon = (uint8_t *)":";
113 MD5Init (&ctx);
114 MD5Update (&ctx, username_trimmed, username_len);
115 MD5Update (&ctx, colon, 1);
116 MD5Update (&ctx, realm_trimmed, realm_len);
117 MD5Update (&ctx, colon, 1);
118 MD5Update (&ctx, password_trimmed, password_len);
119 MD5Final (md5, &ctx);
123 void stun_make_transid (StunTransactionId id)
125 RAND_bytes (id, 16);