Imported Upstream version 0.1.17
[platform/upstream/libnice.git] / stun / tests / test-hmac.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2007 Nokia Corporation. All rights reserved.
5  *  Contact: Rémi Denis-Courmont
6  *
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/
11  *
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.
16  *
17  * The Original Code is the Nice GLib ICE library.
18  *
19  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20  * Corporation. All Rights Reserved.
21  *
22  * Contributors:
23  *   Rémi Denis-Courmont, Nokia
24  *
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.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 # include <config.h>
38 #endif
39
40
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <assert.h>
46
47 #include <stun/stunhmac.h>
48
49 static void print_bytes (const uint8_t *bytes, int len)
50 {
51   int i;
52
53   printf ("0x");
54   for (i = 0; i < len; i++)
55     printf ("%02x", bytes[i]);
56   printf ("\n");
57 }
58
59 static void test_hmac (const uint8_t *key, const uint8_t *str,
60     const uint8_t *expected) {
61   uint8_t hmac[20];
62
63   /* Arbitrary. */
64   size_t msg_len = 300;
65
66   stun_sha1 (str, strlen ((const char *) str), msg_len, hmac,
67              key, strlen ((const char *) key), TRUE  /* padding */);
68
69   printf ("HMAC of '%s' with key '%s' is : ", str, key);
70   print_bytes (hmac, sizeof (hmac));
71   printf ("Expected : ");
72   print_bytes (expected, sizeof (hmac));
73
74   if (memcmp (hmac, expected, sizeof (hmac)))
75     exit (1);
76 }
77
78 int main (void)
79 {
80   const uint8_t hmac1[] = { 0x83, 0x5a, 0x9b, 0x05, 0xea,
81                             0xd7, 0x68, 0x45, 0x48, 0x74,
82                             0x6b, 0xa3, 0x37, 0xe0, 0xa9,
83                             0x3f, 0x4d, 0xb3, 0x9c, 0xa1 };
84
85   test_hmac ((const uint8_t *) "key",
86              (const uint8_t *) "some complicated input string which is over 44 bytes long",
87              hmac1);
88
89   return 0;
90 }