tizen 2.3.1 release
[framework/connectivity/bluez.git] / unit / test-crypto.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2011  Intel Corporation
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "src/shared/crypto.h"
29
30 #include <string.h>
31 #include <glib.h>
32
33 static struct bt_crypto *crypto;
34
35 struct test_data {
36         const uint8_t *msg;
37         uint16_t msg_len;
38         const uint8_t *t;
39 };
40
41 static const uint8_t key[] = {
42         0x3c, 0x4f, 0xcf, 0x09, 0x88, 0x15, 0xf7, 0xab, 0xa6, 0xd2, 0xae, 0x28,
43         0x16, 0x15, 0x7e, 0x2b
44 };
45
46 static const uint8_t msg_1[] = { 0x00 };
47
48 static const uint8_t t_msg_1[] = {
49         0x00, 0x00, 0x00, 0x00, 0xb3, 0xa8, 0x59, 0x41, 0x27, 0xeb, 0xc2, 0xc0
50 };
51
52 static const struct test_data test_data_1 = {
53         .msg = msg_1,
54         .msg_len = 0,
55         .t = t_msg_1
56 };
57
58 static const uint8_t msg_2[] = {
59         0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11,
60         0x73, 0x93, 0x17, 0x2a
61
62 };
63
64 static const uint8_t t_msg_2[] = {
65         0x00, 0x00, 0x00, 0x00, 0x27, 0x39, 0x74, 0xf4, 0x39, 0x2a, 0x23, 0x2a
66 };
67
68 static const struct test_data test_data_2 = {
69         .msg = msg_2,
70         .msg_len = 16,
71         .t = t_msg_2
72 };
73
74 static const uint8_t msg_3[] = {
75         0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11,
76         0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
77         0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46,
78         0xa3, 0x5c, 0xe4, 0x11
79 };
80
81 static const uint8_t t_msg_3[12] = {
82         0x00, 0x00, 0x00, 0x00, 0xb7, 0xca, 0x94, 0xab, 0x87, 0xc7, 0x82, 0x18
83 };
84
85 static const struct test_data test_data_3 = {
86         .msg = msg_3,
87         .msg_len = 40,
88         .t = t_msg_3
89 };
90
91 static const uint8_t msg_4[] = {
92         0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11,
93         0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
94         0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46,
95         0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
96         0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b,
97         0xe6, 0x6c, 0x37, 0x10
98 };
99
100 static const uint8_t t_msg_4[12] = {
101         0x00, 0x00, 0x00, 0x00, 0x44, 0xe1, 0xe6, 0xce, 0x1d, 0xf5, 0x13, 0x68
102 };
103
104 static const struct test_data test_data_4 = {
105         .msg = msg_4,
106         .msg_len = 64,
107         .t = t_msg_4
108 };
109
110 static void print_buf(const uint8_t *t, uint8_t len)
111 {
112         int i;
113
114         for (i = 0; i < len; i++)
115                 g_print("0x%02x, ", t[i]);
116
117         g_print("\n");
118 }
119
120 static bool result_compare(const uint8_t exp[12], uint8_t res[12])
121 {
122         int i;
123         for (i = 0; i < 12; i++)
124                 if (exp[i] != res[i])
125                         return false;
126
127         return true;
128 }
129
130 static void test_sign(gconstpointer data)
131 {
132         uint8_t t[12];
133         const struct test_data *d = data;
134
135         memset(t, 0, 12);
136         if (!bt_crypto_sign_att(crypto, key, d->msg, d->msg_len, 0, t))
137                 g_assert(true);
138
139         if (g_test_verbose()) {
140                 g_print("Result T: ");
141                 print_buf(t, 12);
142                 g_print("Expected T:");
143                 print_buf(d->t, 12);
144         }
145
146         g_assert(result_compare(d->t, t));
147 }
148
149 int main(int argc, char *argv[])
150 {
151         int exit_status;
152
153         crypto = bt_crypto_new();
154         if (!crypto)
155                 return 0;
156
157         g_test_init(&argc, &argv, NULL);
158
159         g_test_add_data_func("/crypto/sign_att_1", &test_data_1, test_sign);
160         g_test_add_data_func("/crypto/sign_att_2", &test_data_2, test_sign);
161         g_test_add_data_func("/crypto/sign_att_3", &test_data_3, test_sign);
162         g_test_add_data_func("/crypto/sign_att_4", &test_data_4, test_sign);
163
164         exit_status = g_test_run();
165
166         bt_crypto_unref(crypto);
167
168         return exit_status;
169 }