Tagging module util-vserver - util-vserver-0.30.215-6
[util-vserver.git] / lib_internal / testsuite / crypto.c
1 /*      --*- c -*--
2  * Copyright (C) 2008 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 and/or 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #define ENSC_TESTSUITE
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <lib_internal/crypto-wrapper.h>
24 #include <lib_internal/coreassert.h>
25
26 static unsigned int
27 hex2digit(char c)
28 {
29         if (c>='0' && c<='9')
30                 return c-'0';
31         c &= ~0x20;
32         if (c>='A' && c<='F')
33                 return c-'A' + 10;
34
35         assert(0);
36         return 0;
37 }
38
39 static void
40 convert_digest_ascii2bin(void *dst_v, char const *digest, size_t d_len)
41 {
42         unsigned char   *dst = dst_v;
43         
44         while (d_len>0) {
45                 *dst  = hex2digit(*digest++)<<4;
46                 *dst |= hex2digit(*digest++);
47
48                 ++dst;
49                 --d_len;
50         }
51 }
52
53 static void
54 test_digest(char const *name, size_t d_len,
55             void const *buf,  size_t buf_len,
56             char const *digest)
57 {
58         ensc_hash_method const  *m = ensc_crypto_hash_find(name);
59         ensc_hash_context       ctx;
60         unsigned char           *exp_digest[d_len/8];
61         unsigned char           *bin_digest[d_len/8];
62         size_t                  bin_digest_len;
63         size_t                  i;
64
65         d_len /= 8;
66         convert_digest_ascii2bin(exp_digest, digest, d_len);
67
68         assert(m);
69         assert(ensc_crypto_hash_get_digestsize(m)==d_len);
70
71         {
72                 char const              *tmp_name = ensc_crypto_hash_get_name(m);
73                 ensc_hash_method const  *tmp_meth = tmp_name ? ensc_crypto_hash_find(tmp_name) : NULL;
74
75                 assert(tmp_name!=NULL);
76                 assert(tmp_meth!=NULL);
77                 assert(ensc_crypto_hash_get_digestsize(tmp_meth)==d_len);
78         }
79
80         ensc_crypto_hashctx_init(&ctx, m);
81         assert(ensc_crypto_hashctx_get_digestsize(&ctx)==d_len);
82
83         /* run it multiple times to test for correct reset/init behavior */
84         for (i=0; i<3; ++i) {
85                 assert(ensc_crypto_hashctx_reset(&ctx)==0);
86                 assert(ensc_crypto_hashctx_update(&ctx, buf, buf_len)==0);
87
88                 switch (i) {
89                 case 0:
90                 case 2:
91                         break;
92
93                 case 1:
94                         assert(ensc_crypto_hashctx_update(&ctx, "gremlin", 7)==0);
95                         break;
96                 }
97
98                 assert(ensc_crypto_hashctx_get_digest(&ctx, bin_digest, &bin_digest_len, d_len)==0);
99                 assert(bin_digest_len==d_len);
100
101                 
102                 switch (i) {
103                 case 0:
104                 case 2:
105                         assert(memcmp(exp_digest, bin_digest, d_len)==0);
106                         break;
107
108                 case 1:
109                         assert(memcmp(exp_digest, bin_digest, d_len)!=0);
110                         break;
111                 }
112         }
113
114         ensc_crypto_hashctx_free(&ctx);
115 }
116
117 int main()
118 {
119         ensc_crypto_init();
120         assert(ensc_crypto_hash_get_default()!=NULL);
121
122         /* MD-5 */
123
124         test_digest("md5",  128, "",    0, "d41d8cd98f00b204e9800998ecf8427e");
125         test_digest("md-5", 128, "",    0, "d41d8cd98f00b204e9800998ecf8427e");
126         test_digest("MD5",  128, "",    0, "d41d8cd98f00b204e9800998ecf8427e");
127         test_digest("MD-5", 128, "",    0, "d41d8cd98f00b204e9800998ecf8427e");
128
129         test_digest("md5",  128, "foo", 3, "acbd18db4cc2f85cedef654fccc4a4d8");
130         
131         /* SHA-1 */
132         test_digest("sha1",  160, "",    0, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
133         test_digest("sha-1", 160, "",    0, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
134         test_digest("SHA1",  160, "",    0, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
135         test_digest("SHA-1", 160, "",    0, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
136         
137         test_digest("sha1",  160, "foo", 3, "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33");
138
139         /* SHA-256 */
140         test_digest("sha256",  256, "",    0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
141         test_digest("sha-256", 256, "",    0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
142         test_digest("SHA256",  256, "",    0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
143         test_digest("SHA-256", 256, "",    0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
144
145         test_digest("sha256",  256, "foo", 3, "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae");
146
147 #if ENSC_CRYPTO_API != ENSC_CRYPTO_API_BEECRYPT  /* see comments in crypto-wrapper-beecrypt.h */
148         /* SHA-384 */
149         test_digest("sha384",  384, "",    0, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b");
150         test_digest("sha-384", 384, "",    0, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b");
151         test_digest("SHA384",  384, "",    0, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b");
152         test_digest("SHA-384", 384, "",    0, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b");
153
154         test_digest("sha384",  384, "foo", 3, "98c11ffdfdd540676b1a137cb1a22b2a70350c9a44171d6b1180c6be5cbb2ee3f79d532c8a1dd9ef2e8e08e752a3babb");
155 #endif
156
157         /* SHA-512 */
158         test_digest("sha512",  512, "",    0, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
159         test_digest("sha-512", 512, "",    0, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
160         test_digest("SHA512",  512, "",    0, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
161         test_digest("SHA-512", 512, "",    0, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
162
163         test_digest("sha512",  512, "foo", 3, "f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7");
164 }