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