1 /* $NetBSD: t_crypto.c,v 1.4 2011/02/12 23:21:33 christos 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 #include <sys/cdefs.h> 38 __RCSID("$NetBSD: t_crypto.c,v 1.4 2011/02/12 23:21:33 christos Exp $"); 39 40 #include <atf-c.h> 41 #include <saslc.h> 42 #include <stdio.h> 43 #include <string.h> 44 45 #include "crypto.h" 46 47 48 typedef struct { 49 char *in; 50 char *key; 51 char *out; 52 } hmac_md5_test_case_t; 53 54 #define HMAC_MD5_TEST_CASES 5 55 hmac_md5_test_case_t hmac_md5_test_cases[HMAC_MD5_TEST_CASES] = { 56 { /* taken from the RFC2195 */ 57 "<1896.697170952@postoffice.reston.mci.net>" /* in */, 58 "tanstaaftanstaaf" /* key */, 59 "b913a602c7eda7a495b4e6e7334d3890" /* out */ 60 }, 61 { /* taken from the draft-ietf-sasl-crammd5 */ 62 "<1896.697170952@postoffice.example.net>" /* in */, 63 "tanstaaftanstaaf" /* key */, 64 "3dbc88f0624776a737b39093f6eb6427" /* out */ 65 }, 66 { 67 "<68451038525716401353.0@localhost>" /* in */, 68 "Open, Sesame" /* key */, 69 "6fa32b6e768f073132588e3418e00f71" /* out */ 70 }, 71 { /* taken from RFC2104 */ 72 "what do ya want for nothing?" /* in */, 73 "Jefe" /* key */, 74 "750c783e6ab0b503eaa86e310a5db738" /* out */ 75 }, 76 { /* taken from RFC2202 */ 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" 79 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" 80 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" 81 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" 82 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" 83 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" 84 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" /* key */, 85 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd" /* out */ 86 } 87 }; 88 89 typedef struct { 90 char *in; 91 char *out; 92 } md5_test_case_t; 93 94 #define MD5_TEST_CASES 4 95 md5_test_case_t md5_test_cases[HMAC_MD5_TEST_CASES] = { 96 { 97 "this is very hard test" /* in */, 98 "c9145ff35600132e4c9b821e19c27783" /* out */ 99 }, 100 { 101 "shm" /* in */, 102 "99ebb038380e15bc896c3a17733ab484" /* out */ 103 }, 104 { 105 "this is a bit longer test, isn't it?" /* in */, 106 "b69f7a5e9c1f701ed90033b87ccca94c" /* out */ 107 }, 108 { 109 "OK, enough", 110 "fecb46e815d0ba4b7c89d050e30124ea" /* out */ 111 } 112 }; 113 114 typedef struct { 115 char *in; 116 char *out; 117 size_t len; 118 } base64_test_case_t; 119 120 #define BASE64_TEST_CASES 4 121 base64_test_case_t base64_test_cases[BASE64_TEST_CASES] = { 122 { 123 "this is very hard test" /* in */, 124 "dGhpcyBpcyB2ZXJ5IGhhcmQgdGVzdA==" /* out */, 125 22 /* len */ 126 }, 127 { 128 "shm" /* in */, 129 "c2ht" /* out */, 130 3 /* len */ 131 }, 132 { 133 "this is a bit longer test, isn't it?" /* in */, 134 "dGhpcyBpcyBhIGJpdCBsb25nZXIgdGVzdCwgaXNuJ3QgaXQ/" /* out */, 135 36 /* len */ 136 }, 137 { 138 "OK, enough", 139 "T0ssIGVub3VnaA==" /* out */, 140 10 /* len */ 141 } 142 }; 143 144 ATF_TC(t_crypto_hmac_md5); 145 ATF_TC_HEAD(t_crypto_hmac_md5, tc) 146 { 147 atf_tc_set_md_var(tc, "descr", "saslc__crypto_hmac_md5() tests"); 148 } 149 ATF_TC_BODY(t_crypto_hmac_md5, tc) 150 { 151 const char *digest; 152 int i; 153 154 for (i = 0; i < HMAC_MD5_TEST_CASES; i++) { 155 digest = saslc__crypto_hmac_md5_hex(hmac_md5_test_cases[i].key, 156 strlen(hmac_md5_test_cases[i].key), hmac_md5_test_cases[i].in, 157 strlen(hmac_md5_test_cases[i].in)); 158 ATF_CHECK_STREQ_MSG(digest, hmac_md5_test_cases[i].out, 159 "saslc__crypto_hmac_md5_hex() failed on %s %s got %s should be: %s", 160 hmac_md5_test_cases[i].in, hmac_md5_test_cases[i].key, digest, 161 hmac_md5_test_cases[i].out); 162 free((void *)digest); 163 } 164 } 165 166 ATF_TC(t_crypto_md5); 167 ATF_TC_HEAD(t_crypto_md5, tc) 168 { 169 170 atf_tc_set_md_var(tc, "descr", "saslc__hmac_md5_hex() tests"); 171 } 172 ATF_TC_BODY(t_crypto_md5, tc) 173 { 174 const char *digest; 175 int i; 176 177 for (i = 0; i < MD5_TEST_CASES; i++) { 178 digest = saslc__crypto_md5_hex(md5_test_cases[i].in, 179 strlen(md5_test_cases[i].in)); 180 ATF_CHECK_STREQ_MSG(digest, md5_test_cases[i].out, 181 "saslc__crypto_md5_hex() failed on %s got %s should be: %s", 182 md5_test_cases[i].in, digest, md5_test_cases[i].out); 183 free((void *)digest); 184 } 185 } 186 187 ATF_TC(t_crypto_base64); 188 ATF_TC_HEAD(t_crypto_base64, tc) 189 { 190 atf_tc_set_md_var(tc, "descr", "saslc__crypto_nonce() tests"); 191 } 192 ATF_TC_BODY(t_crypto_base64, tc) 193 { 194 char *enc; 195 size_t enclen; 196 int i; 197 198 for (i = 0; i < BASE64_TEST_CASES; i++) { 199 saslc__crypto_encode_base64(base64_test_cases[i].in, 200 base64_test_cases[i].len, &enc, &enclen); 201 ATF_CHECK_STREQ_MSG(enc, base64_test_cases[i].out, 202 "saslc__crypto_encode_base64() failed on %s got %s should be: %s", 203 base64_test_cases[i].in, enc, base64_test_cases[i].out); 204 free((void *)enc); 205 } 206 } 207 208 ATF_TC(t_crypto_nonce); 209 ATF_TC_HEAD(t_crypto_nonce, tc) 210 { 211 212 atf_tc_set_md_var(tc, "descr", "saslc__crypto_nonce() tests"); 213 } 214 ATF_TC_BODY(t_crypto_nonce, tc) 215 { 216 unsigned char *x, *y; 217 218 /* Any better ideas how to test that? ... */ 219 220 x = saslc__crypto_nonce(1024); 221 y = saslc__crypto_nonce(1024); 222 223 ATF_CHECK_EQ(((strncmp(x, y, 1024) == 0) ? 1 : 0), 0); 224 225 free(x); 226 free(y); 227 } 228 229 ATF_TP_ADD_TCS(tp) 230 { 231 232 ATF_TP_ADD_TC(tp, t_crypto_hmac_md5); 233 ATF_TP_ADD_TC(tp, t_crypto_md5); 234 ATF_TP_ADD_TC(tp, t_crypto_base64); 235 ATF_TP_ADD_TC(tp, t_crypto_nonce); 236 return atf_no_error(); 237 } 238