xref: /netbsd-src/crypto/external/bsd/libsaslc/dist/test/t_crypto.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /* $Id: t_crypto.c,v 1.1.1.1 2010/11/27 21:23:59 agc Exp $ */
2 
3 /* Copyright (c) 2010 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Mateusz Kocielski.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *  	  This product includes software developed by the NetBSD
20  *  	  Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <atf-c.h>
39 #include <stdio.h>
40 #include <saslc.h>
41 #include <string.h>
42 #include "crypto.h"
43 
44 typedef struct {
45 		char *in;
46 		char *key;
47 		char *out;
48 } hmac_md5_test_case_t;
49 
50 #define HMAC_MD5_TEST_CASES 5
51 hmac_md5_test_case_t hmac_md5_test_cases[HMAC_MD5_TEST_CASES] = {
52 		{
53 				/* taken from the RFC2195 */
54 				"<1896.697170952@postoffice.reston.mci.net>" /* in */,
55 				"tanstaaftanstaaf" /* key */,
56 				"b913a602c7eda7a495b4e6e7334d3890" /* out */
57 		},
58 				/* taken from the draft-ietf-sasl-crammd5 */
59 		{
60 				"<1896.697170952@postoffice.example.net>" /* in */,
61 				"tanstaaftanstaaf" /* key */,
62 				"3dbc88f0624776a737b39093f6eb6427" /* out */
63 		},
64 		{
65 				"<68451038525716401353.0@localhost>" /* in */,
66 				"Open, Sesame" /* key */,
67 				   "6fa32b6e768f073132588e3418e00f71" /* out */
68 		},
69 		{
70 				/* taken from RFC2104 */
71 				"what do ya want for nothing?" /* in */,
72 				"Jefe" /* key */,
73 				"750c783e6ab0b503eaa86e310a5db738" /* out */
74 		},
75 				/* taken from RFC2202 */
76 		{
77 				"Test Using Larger Than Block-Size Key - Hash Key First" /* in */,
78 				"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" /* key */,
79 				"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd" /* out */
80 		}
81 };
82 
83 typedef struct {
84 		char *in;
85 		char *out;
86 } md5_test_case_t;
87 
88 #define MD5_TEST_CASES 4
89 md5_test_case_t md5_test_cases[HMAC_MD5_TEST_CASES] = {
90 		{
91 				"this is very hard test" /* in */,
92 				"c9145ff35600132e4c9b821e19c27783" /* out */
93 		},
94 		{
95 				"shm" /* in */,
96 				"99ebb038380e15bc896c3a17733ab484" /* out */
97 		},
98 		{
99 				"this is a bit longer test, isn't it?" /* in */,
100 				"b69f7a5e9c1f701ed90033b87ccca94c" /* out */
101 		},
102 		{
103 				"OK, enough",
104 				"fecb46e815d0ba4b7c89d050e30124ea" /* out */
105 		}
106 };
107 
108 typedef struct {
109 		char *in;
110 		char *out;
111 		size_t len;
112 } base64_test_case_t;
113 
114 #define BASE64_TEST_CASES 4
115 base64_test_case_t base64_test_cases[BASE64_TEST_CASES] = {
116 		{
117 				"this is very hard test" /* in */,
118 				"dGhpcyBpcyB2ZXJ5IGhhcmQgdGVzdA==" /* out */,
119 				22 /* len */
120 		},
121 		{
122 				"shm" /* in */,
123 				"c2ht" /* out */,
124 				3 /* len */
125 		},
126 		{
127 				"this is a bit longer test, isn't it?" /* in */,
128 				"dGhpcyBpcyBhIGJpdCBsb25nZXIgdGVzdCwgaXNuJ3QgaXQ/" /* out */,
129 				36 /* len */
130 		},
131 		{
132 				"OK, enough",
133 				"T0ssIGVub3VnaA==" /* out */,
134 				10 /* len */
135 		}
136 };
137 
138 
139 
140 ATF_TC(t_crypto_hmac_md5);
141 ATF_TC_HEAD(t_crypto_hmac_md5, tc)
142 {
143 		atf_tc_set_md_var(tc, "descr", "saslc__crypto_hmac_md5() tests");
144 }
145 ATF_TC_BODY(t_crypto_hmac_md5, tc)
146 {
147 		const char *digest;
148 		int i;
149 		for (i = 0; i < HMAC_MD5_TEST_CASES; i++) {
150 				digest = saslc__crypto_hmac_md5(hmac_md5_test_cases[i].key,
151 					strlen(hmac_md5_test_cases[i].key), hmac_md5_test_cases[i].in,
152 					strlen(hmac_md5_test_cases[i].in));
153 				ATF_CHECK_STREQ_MSG(digest, hmac_md5_test_cases[i].out,
154 					"saslc__crypto_hmac_md5() failed on %s %s got %s should be: %s",
155 					hmac_md5_test_cases[i].in, hmac_md5_test_cases[i].key, digest,
156 					hmac_md5_test_cases[i].out);
157 				free((void *)digest);
158 		}
159 }
160 
161 ATF_TC(t_crypto_md5);
162 ATF_TC_HEAD(t_crypto_md5, tc)
163 {
164 		atf_tc_set_md_var(tc, "descr", "saslc__hmac_md5() tests");
165 }
166 ATF_TC_BODY(t_crypto_md5, tc)
167 {
168 		const char *digest;
169 		int i;
170 		for (i = 0; i < MD5_TEST_CASES; i++) {
171 				digest = saslc__crypto_md5(md5_test_cases[i].in);
172 				ATF_CHECK_STREQ_MSG(digest, md5_test_cases[i].out,
173 					"saslc__crypto_md5() failed on %s got %s should be: %s",
174 					md5_test_cases[i].in, digest, md5_test_cases[i].out);
175 				free((void *)digest);
176 		}
177 }
178 
179 ATF_TC(t_crypto_base64);
180 ATF_TC_HEAD(t_crypto_base64, tc)
181 {
182 		atf_tc_set_md_var(tc, "descr", "saslc__crypto_nonce() tests");
183 }
184 ATF_TC_BODY(t_crypto_base64, tc)
185 {
186 		const char *enc;
187 		int i;
188 		for (i = 0; i < BASE64_TEST_CASES; i++) {
189 				enc = saslc__crypto_base64(base64_test_cases[i].in,
190 					base64_test_cases[i].len);
191 				ATF_CHECK_STREQ_MSG(enc, base64_test_cases[i].out,
192 					"saslc__crypto_base64() failed on %s got %s should be: %s",
193 					base64_test_cases[i].in, enc, base64_test_cases[i].out);
194 				free((void *)enc);
195 		}
196 }
197 
198 ATF_TC(t_crypto_nonce);
199 ATF_TC_HEAD(t_crypto_nonce, tc)
200 {
201 		atf_tc_set_md_var(tc, "descr", "saslc__crypto_nonce() tests");
202 }
203 ATF_TC_BODY(t_crypto_nonce, tc)
204 {
205 		/* Any better ideas how to test that? ... */
206 		unsigned char *x, *y;
207 
208 		x = saslc__crypto_nonce(1024);
209 		y = saslc__crypto_nonce(1024);
210 
211 		ATF_CHECK_EQ(((strncmp(x, y, 1024)==0)?1:0), 0);
212 
213 		free(x);
214 		free(y);
215 }
216 
217 
218 
219 ATF_TP_ADD_TCS(tp)
220 {
221 		ATF_TP_ADD_TC(tp, t_crypto_hmac_md5);
222 		ATF_TP_ADD_TC(tp, t_crypto_md5);
223 		ATF_TP_ADD_TC(tp, t_crypto_base64);
224 		ATF_TP_ADD_TC(tp, t_crypto_nonce);
225 		return atf_no_error();
226 }
227