1 | /* |
---|
2 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
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 <sha256.h> |
---|
20 | #include <sha512.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <string.h> |
---|
23 | |
---|
24 | #include "tmacros.h" |
---|
25 | |
---|
26 | const char rtems_test_name[] = "SHA"; |
---|
27 | |
---|
28 | static const char *const test_vectors[] = { |
---|
29 | "abc", |
---|
30 | "", |
---|
31 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
---|
32 | "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" |
---|
33 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" |
---|
34 | }; |
---|
35 | |
---|
36 | static const unsigned char |
---|
37 | test_sha256_results[RTEMS_ARRAY_SIZE(test_vectors)][32] = { |
---|
38 | { |
---|
39 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, |
---|
40 | 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, |
---|
41 | 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
---|
42 | 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad |
---|
43 | }, { |
---|
44 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, |
---|
45 | 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, |
---|
46 | 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
---|
47 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
---|
48 | }, { |
---|
49 | 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, |
---|
50 | 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, |
---|
51 | 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, |
---|
52 | 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 |
---|
53 | }, { |
---|
54 | 0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, |
---|
55 | 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37, |
---|
56 | 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, |
---|
57 | 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1 |
---|
58 | } |
---|
59 | }; |
---|
60 | |
---|
61 | static const unsigned char |
---|
62 | test_sha512_results[RTEMS_ARRAY_SIZE(test_vectors)][64] = { |
---|
63 | { |
---|
64 | 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, |
---|
65 | 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, |
---|
66 | 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, |
---|
67 | 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, |
---|
68 | 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, |
---|
69 | 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, |
---|
70 | 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, |
---|
71 | 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f |
---|
72 | }, { |
---|
73 | 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, |
---|
74 | 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, |
---|
75 | 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, |
---|
76 | 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, |
---|
77 | 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, |
---|
78 | 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, |
---|
79 | 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, |
---|
80 | 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e |
---|
81 | }, { |
---|
82 | 0x20, 0x4a, 0x8f, 0xc6, 0xdd, 0xa8, 0x2f, 0x0a, |
---|
83 | 0x0c, 0xed, 0x7b, 0xeb, 0x8e, 0x08, 0xa4, 0x16, |
---|
84 | 0x57, 0xc1, 0x6e, 0xf4, 0x68, 0xb2, 0x28, 0xa8, |
---|
85 | 0x27, 0x9b, 0xe3, 0x31, 0xa7, 0x03, 0xc3, 0x35, |
---|
86 | 0x96, 0xfd, 0x15, 0xc1, 0x3b, 0x1b, 0x07, 0xf9, |
---|
87 | 0xaa, 0x1d, 0x3b, 0xea, 0x57, 0x78, 0x9c, 0xa0, |
---|
88 | 0x31, 0xad, 0x85, 0xc7, 0xa7, 0x1d, 0xd7, 0x03, |
---|
89 | 0x54, 0xec, 0x63, 0x12, 0x38, 0xca, 0x34, 0x45 |
---|
90 | }, { |
---|
91 | 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, |
---|
92 | 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f, |
---|
93 | 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, |
---|
94 | 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, |
---|
95 | 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, |
---|
96 | 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, |
---|
97 | 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, |
---|
98 | 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 |
---|
99 | } |
---|
100 | }; |
---|
101 | |
---|
102 | static void print_result(const unsigned char *r, size_t n) |
---|
103 | { |
---|
104 | size_t i; |
---|
105 | |
---|
106 | for (i = 0; i < n; ++i) { |
---|
107 | if (i == 0) { |
---|
108 | /* Do nothing */ |
---|
109 | } else if (i % 32 == 0) { |
---|
110 | printf("\n"); |
---|
111 | } else if (i % 8 == 0) { |
---|
112 | printf(" "); |
---|
113 | } |
---|
114 | |
---|
115 | printf("%02x", r[i]); |
---|
116 | } |
---|
117 | |
---|
118 | printf("\n"); |
---|
119 | } |
---|
120 | |
---|
121 | static void test_sha256(void) |
---|
122 | { |
---|
123 | size_t i; |
---|
124 | |
---|
125 | printf("test SHA256\n"); |
---|
126 | |
---|
127 | for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) { |
---|
128 | SHA256_CTX ctx; |
---|
129 | unsigned char r[32]; |
---|
130 | const char *s = test_vectors[i]; |
---|
131 | |
---|
132 | SHA256_Init(&ctx); |
---|
133 | SHA256_Update(&ctx, s, strlen(s)); |
---|
134 | SHA256_Final(r, &ctx); |
---|
135 | |
---|
136 | print_result(&r[0], sizeof(r)); |
---|
137 | |
---|
138 | rtems_test_assert( |
---|
139 | memcmp(&r[0], &test_sha256_results[i][0], sizeof(r)) == 0 |
---|
140 | ); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | static void test_sha512(void) |
---|
145 | { |
---|
146 | size_t i; |
---|
147 | |
---|
148 | printf("test SHA512\n"); |
---|
149 | |
---|
150 | for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) { |
---|
151 | SHA512_CTX ctx; |
---|
152 | unsigned char r[64]; |
---|
153 | const char *s = test_vectors[i]; |
---|
154 | |
---|
155 | SHA512_Init(&ctx); |
---|
156 | SHA512_Update(&ctx, s, strlen(s)); |
---|
157 | SHA512_Final(r, &ctx); |
---|
158 | |
---|
159 | print_result(&r[0], sizeof(r)); |
---|
160 | |
---|
161 | rtems_test_assert( |
---|
162 | memcmp(&r[0], &test_sha512_results[i][0], sizeof(r)) == 0 |
---|
163 | ); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | static void Init(rtems_task_argument arg) |
---|
168 | { |
---|
169 | TEST_BEGIN(); |
---|
170 | |
---|
171 | test_sha256(); |
---|
172 | test_sha512(); |
---|
173 | |
---|
174 | TEST_END(); |
---|
175 | rtems_test_exit(0); |
---|
176 | } |
---|
177 | |
---|
178 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
179 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
180 | |
---|
181 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
182 | |
---|
183 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
184 | |
---|
185 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
186 | |
---|
187 | #define CONFIGURE_INIT |
---|
188 | |
---|
189 | #include <rtems/confdefs.h> |
---|