source: rtems/testsuites/libtests/md501/init.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include "tmacros.h"
20
21#include <md5.h>
22
23const char rtems_test_name[] = "MD5 1";
24
25#ifndef MD5_BLOCK_SIZE
26  #define MD5_BLOCK_SIZE 64
27#endif
28
29#ifndef MD5_DIGEST_LENGTH
30  #define MD5_DIGEST_LENGTH 16
31#endif
32
33/* Test cases according to RFC 2202 */
34
35static const uint8_t key_1[] = {
36  0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
37  0x0b, 0x0b, 0x0b
38};
39
40static const uint8_t data_1[] = {
41  'H', 'i', ' ', 'T', 'h', 'e', 'r', 'e'
42};
43
44static const uint8_t digest_1[MD5_DIGEST_LENGTH] = {
45  0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15,
46  0x8b, 0xfc, 0x9d
47};
48
49static const uint8_t key_2[] = {
50  'J', 'e', 'f', 'e'
51};
52
53static const uint8_t data_2[] = {
54  'w', 'h', 'a', 't', ' ', 'd', 'o', ' ', 'y', 'a', ' ', 'w', 'a', 'n', 't',
55  ' ', 'f', 'o', 'r', ' ', 'n', 'o', 't', 'h', 'i', 'n', 'g', '?'
56};
57
58static const uint8_t digest_2[MD5_DIGEST_LENGTH] = {
59  0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 0xea, 0xa8, 0x6e, 0x31, 0x0a,
60  0x5d, 0xb7, 0x38
61};
62
63static const uint8_t key_3[] = {
64  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
65  0xaa, 0xaa, 0xaa
66};
67
68static const uint8_t data_3[] = {
69  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
70  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
71  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
72  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
73};
74
75static const uint8_t digest_3[MD5_DIGEST_LENGTH] = {
76  0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, 0xdb, 0xb8, 0xc7, 0x33, 0xf0,
77  0xe8, 0xb3, 0xf6
78};
79
80static const uint8_t key_4[] = {
81  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
82  0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19
83};
84
85static const uint8_t data_4[] = {
86  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
87  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
88  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
89  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd
90};
91
92static const uint8_t digest_4[MD5_DIGEST_LENGTH] = {
93  0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a, 0x3a, 0xea, 0x3a, 0x75, 0x16, 0x47, 0x46,
94  0xff, 0xaa, 0x79
95};
96
97static const uint8_t key_5[] = {
98  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
99  0x0c, 0x0c, 0x0c
100};
101
102static const uint8_t data_5[] = {
103  'T', 'e', 's', 't', ' ', 'W', 'i', 't', 'h', ' ', 'T', 'r', 'u', 'n', 'c',
104  'a', 't', 'i', 'o', 'n'
105};
106
107static const uint8_t digest_5[MD5_DIGEST_LENGTH] = {
108  0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00, 0xf9, 0xba, 0xb9, 0x95, 0x69,
109  0x0e, 0xfd, 0x4c
110};
111
112static const uint8_t key_6[] = {
113  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
114  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
115  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
116  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
117  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
118  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
119  0xaa, 0xaa
120};
121
122static const uint8_t data_6[] = {
123  'T', 'e', 's', 't', ' ', 'U', 's', 'i', 'n', 'g', ' ', 'L', 'a', 'r', 'g',
124  'e', 'r', ' ', 'T', 'h', 'a', 'n', ' ', 'B', 'l', 'o', 'c', 'k', '-', 'S',
125  'i', 'z', 'e', ' ', 'K', 'e', 'y', ' ', '-', ' ', 'H', 'a', 's', 'h', ' ',
126  'K', 'e', 'y', ' ', 'F', 'i', 'r', 's', 't'
127};
128
129static const uint8_t digest_6[MD5_DIGEST_LENGTH] = {
130  0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f, 0x0b, 0x62, 0xe6, 0xce, 0x61,
131  0xb9, 0xd0, 0xcd
132};
133
134static const uint8_t key_7[] = {
135  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
136  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
137  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
138  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
139  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
140  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
141  0xaa, 0xaa
142};
143
144static const uint8_t data_7[] = {
145  'T', 'e', 's', 't', ' ', 'U', 's', 'i', 'n', 'g', ' ', 'L', 'a', 'r', 'g',
146  'e', 'r', ' ', 'T', 'h', 'a', 'n', ' ', 'B', 'l', 'o', 'c', 'k', '-', 'S',
147  'i', 'z', 'e', ' ', 'K', 'e', 'y', ' ', 'a', 'n', 'd', ' ', 'L', 'a', 'r',
148  'g', 'e', 'r', ' ', 'T', 'h', 'a', 'n', ' ', 'O', 'n', 'e', ' ', 'B', 'l',
149  'o', 'c', 'k', '-', 'S', 'i', 'z', 'e', ' ', 'D', 'a', 't', 'a'
150};
151
152static const uint8_t digest_7[MD5_DIGEST_LENGTH] = {
153  0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, 0x1f, 0xb1, 0xf5, 0x62, 0xdb,
154  0x3a, 0xa5, 0x3e
155};
156
157static void test_md5(
158 const uint8_t *key,
159 size_t key_size,
160 const uint8_t *data,
161 size_t data_size,
162 const uint8_t expected_digest[MD5_DIGEST_LENGTH]
163)
164{
165  MD5_CTX inner_ctx;
166  MD5_CTX outer_ctx;
167  uint8_t inner_digest[MD5_DIGEST_LENGTH];
168  uint8_t outer_digest[MD5_DIGEST_LENGTH];
169  uint8_t key_buf[MD5_DIGEST_LENGTH];
170  uint8_t buf[MD5_BLOCK_SIZE];
171  size_t i;
172
173  if (key_size > MD5_BLOCK_SIZE) {
174    MD5_CTX tmp_ctx;
175
176    MD5Init(&tmp_ctx);
177    MD5Update(&tmp_ctx, key, key_size);
178    MD5Final(&key_buf[0], &tmp_ctx);
179
180    key = &key_buf[0];
181    key_size = MD5_DIGEST_LENGTH;
182  }
183
184  /* Inner digest */
185
186  MD5Init(&inner_ctx);
187
188  /* Pad the key for the inner digest */
189  for (i = 0; i < key_size; ++i) {
190    buf[i] = key[i] ^ 0x36;
191  }
192  for (i = key_size; i < MD5_BLOCK_SIZE; ++i) {
193    buf[i] = 0x36;
194  }
195
196  MD5Update(&inner_ctx, &buf[0], MD5_BLOCK_SIZE);
197  MD5Update(&inner_ctx, data, data_size);
198
199  MD5Final(&inner_digest[0], &inner_ctx);
200
201  /* Outer digest */
202
203  MD5Init(&outer_ctx);
204
205  /* Pad the key for outer digest */
206  for (i = 0; i < key_size; ++i) {
207    buf[i] = key[i] ^ 0x5c;
208  }
209  for (i = key_size; i < MD5_BLOCK_SIZE; ++i) {
210    buf[i] = 0x5c;
211  }
212
213  MD5Update(&outer_ctx, &buf[0], MD5_BLOCK_SIZE);
214  MD5Update(&outer_ctx, &inner_digest[0], MD5_DIGEST_LENGTH);
215
216  MD5Final(&outer_digest[0], &outer_ctx);
217
218  rtems_test_assert(
219    memcmp(&outer_digest[0], expected_digest, sizeof(outer_digest)) == 0
220  );
221}
222
223static void test(void)
224{
225  test_md5(&key_1[0], sizeof(key_1), &data_1[0], sizeof(data_1), &digest_1[0]);
226  test_md5(&key_2[0], sizeof(key_2), &data_2[0], sizeof(data_2), &digest_2[0]);
227  test_md5(&key_3[0], sizeof(key_3), &data_3[0], sizeof(data_3), &digest_3[0]);
228  test_md5(&key_4[0], sizeof(key_4), &data_4[0], sizeof(data_4), &digest_4[0]);
229  test_md5(&key_5[0], sizeof(key_5), &data_5[0], sizeof(data_5), &digest_5[0]);
230  test_md5(&key_6[0], sizeof(key_6), &data_6[0], sizeof(data_6), &digest_6[0]);
231  test_md5(&key_7[0], sizeof(key_7), &data_7[0], sizeof(data_7), &digest_7[0]);
232}
233
234static void Init(rtems_task_argument arg)
235{
236  TEST_BEGIN();
237
238  test();
239
240  TEST_END();
241
242  rtems_test_exit(0);
243}
244
245#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
246#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
247
248#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
249
250#define CONFIGURE_MAXIMUM_TASKS 1
251
252#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
253
254#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
255
256#define CONFIGURE_INIT
257
258#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.