Adding new stun API stun_agent_forget_transaction to forget a saved transaction id...
[sipe-libnice.git] / stun / tests / test-hmac.c
blob9e8caae6bc864494dab13aa8548331448d939cfb
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
41 #include "stun/sha1.h"
42 #include "stun/md5.h"
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <assert.h>
48 void print_bytes (uint8_t *bytes, int len)
50 int i;
52 printf ("0x");
53 for (i = 0; i < len; i++)
54 printf ("%02x", bytes[i]);
55 printf ("\n");
58 void test_sha1 (uint8_t *str, uint8_t *expected) {
59 SHA1_CTX ctx;
60 uint8_t sha1[20];
62 SHA1Init(&ctx);
63 SHA1Update(&ctx, str, strlen (str));
64 SHA1Final(sha1, &ctx);
66 printf ("SHA1 of '%s' : ", str);
67 print_bytes (sha1, SHA1_MAC_LEN);
68 printf ("Expected : ");
69 print_bytes (expected, SHA1_MAC_LEN);
71 if (memcmp (sha1, expected, SHA1_MAC_LEN))
72 exit (1);
76 void test_hmac (uint8_t *key, uint8_t *str, uint8_t *expected) {
77 uint8_t hmac[20];
79 hmac_sha1(key, strlen (key), str, strlen (str), hmac);
80 printf ("HMAC of '%s' with key '%s' is : ", str, key);
81 print_bytes (hmac, SHA1_MAC_LEN);
82 printf ("Expected : ");
83 print_bytes (expected, SHA1_MAC_LEN);
85 if (memcmp (hmac, expected, SHA1_MAC_LEN))
86 exit (1);
89 void test_md5 (uint8_t *str, uint8_t *expected) {
90 MD5_CTX ctx;
91 uint8_t md5[20];
92 int i;
94 MD5Init(&ctx);
95 MD5Update(&ctx, str, strlen (str));
96 MD5Final(md5, &ctx);
98 printf ("MD5 of '%s' : 0x", str);
99 print_bytes (md5, MD5_MAC_LEN);
100 printf ("Expected : ");
101 print_bytes (expected, MD5_MAC_LEN);
103 if (memcmp (md5, expected, MD5_MAC_LEN))
104 exit (1);
107 int main (void)
110 uint8_t hello_world_hmac[] = {0x8a, 0x3a, 0x84, 0xbc, 0xd0,
111 0xd0, 0x06, 0x5e, 0x97, 0xf1,
112 0x75, 0xd3, 0x70, 0x44, 0x7c,
113 0x7d, 0x02, 0xe0, 0x09, 0x73};
114 uint8_t abc_sha1[] = {0xa9, 0x99, 0x3e, 0x36, 0x47,
115 0x06, 0x81, 0x6a, 0xba, 0x3e,
116 0x25, 0x71, 0x78, 0x50, 0xc2,
117 0x6c, 0x9c, 0xd0, 0xd8, 0x9d};
118 uint8_t abcd_etc_sha1[] = {0x84, 0x98, 0x3e, 0x44, 0x1c,
119 0x3b, 0xd2, 0x6e, 0xba, 0xae,
120 0x4a, 0xa1, 0xf9, 0x51, 0x29,
121 0xe5, 0xe5, 0x46, 0x70, 0xf1};
122 uint8_t abc_md5[] = {0x90, 0x01, 0x50, 0x98,
123 0x3c, 0xd2, 0x4f, 0xb0,
124 0xd6, 0x96, 0x3f, 0x7d,
125 0x28, 0xe1, 0x7f, 0x72};
126 uint8_t abcd_etc_md5[] = {0x82, 0x15, 0xef, 0x07,
127 0x96, 0xa2, 0x0b, 0xca,
128 0xaa, 0xe1, 0x16, 0xd3,
129 0x87, 0x6c, 0x66, 0x4a};
131 test_hmac ("hello", "world", hello_world_hmac);
133 test_sha1 ("abc", abc_sha1);
134 test_md5 ("abc", abc_md5);
136 test_sha1 ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
137 abcd_etc_sha1);
138 test_md5 ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
139 abcd_etc_md5);
141 return 0;