Some fix to compile run test check.
[bertos.git] / bertos / sec / kdf / pbkdf2_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief PBKDF2 testsuite
34  * \author Giovanni Bajo <rasky@develer.com>
35  *
36  */
37
38
39 #include "pbkdf2.h"
40
41 #include <cfg/test.h>
42 #include <cfg/debug.h>
43
44 #include <sec/mac/hmac.h>
45 #include <sec/hash/sha1.h>
46
47 #include <cpu/detect.h>
48
49 #include <string.h>
50
51
52 int PBKDF2_testSetup(void)
53 {
54         kdbg_init();
55         return 0;
56 }
57
58 int PBKDF2_testTearDown(void)
59 {
60         return 0;
61 }
62
63 int PBKDF2_testRun(void)
64 {
65         Kdf *kdf = PBKDF2_stackinit(hmac_stackinit(SHA1_stackinit()));
66
67         uint8_t res[32];
68
69         PBKDF2_set_iterations(kdf, 1);
70         kdf_begin(kdf, "password", 8, (const uint8_t*)"salt", 4);
71         kdf_read(kdf, res, 20);
72         ASSERT(memcmp(res, "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6", 20) == 0);
73
74         PBKDF2_set_iterations(kdf, 2);
75         kdf_begin(kdf, "password", 8, (const uint8_t*)"salt", 4);
76         kdf_read(kdf, res, 20);
77         ASSERT(memcmp(res, "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57", 20) == 0);
78
79         PBKDF2_set_iterations(kdf, 4096);
80         kdf_begin(kdf, "password", 8, (const uint8_t*)"salt", 4);
81         kdf_read(kdf, res, 20);
82         ASSERT(memcmp(res, "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1", 20) == 0);
83
84 #if CPU_X86
85         // Too slow for an embedded system...
86         PBKDF2_set_iterations(kdf, 16777216);
87         kdf_begin(kdf, "password", 8, (const uint8_t*)"salt", 4);
88         kdf_read(kdf, res, 20);
89         ASSERT(memcmp(res, "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84", 20) == 0);
90 #endif
91
92         PBKDF2_set_iterations(kdf, 4096);
93         kdf_begin(kdf, "pass\0word", 9, (const uint8_t*)"sa\0lt", 5);
94         kdf_read(kdf, res, 16);
95         ASSERT(memcmp(res, "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37\xd7\xf0\x34\x25\xe0\xc3", 16) == 0);
96
97         PBKDF2_set_iterations(kdf, 4096);
98         kdf_begin(kdf, "passwordPASSWORDpassword", 24, (const uint8_t*)"saltSALTsaltSALTsaltSALTsaltSALTsalt", 36);
99         kdf_read(kdf, res, 25);
100         ASSERT(memcmp(res, "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96\x4c\xf2\xf0\x70\x38", 25) == 0);
101
102         return 0;
103 }
104
105 TEST_MAIN(PBKDF2);