1272343Sngie/* $NetBSD: t_algorithms.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $ */
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2000, 2008, 2010 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/cdefs.h>
30272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008, 2010\
31272343Sngie The NetBSD Foundation, inc. All rights reserved.");
32272343Sngie__RCSID("$NetBSD: t_algorithms.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $");
33272343Sngie
34272343Sngie#include <stdio.h>
35272343Sngie#include <strings.h>
36272343Sngie#include <skey.h>
37272343Sngie
38272343Sngie#include <atf-c.h>
39272343Sngie
40272343Sngie#define H_REQUIRE(x, y) \
41272343Sngie	ATF_REQUIRE_MSG(strcasecmp((x), (y)) == 0, "\"%s\" != \"%s\"", (x), (y))
42272343Sngie
43272343Sngiestatic void
44272343Sngieh_check(const char *pass, const char *seed,
45272343Sngie        const char *algo, const char *zero,
46272343Sngie        const char *one,  const char *nine)
47272343Sngie{
48272343Sngie	char prn[64];
49272343Sngie	char data[16];
50272343Sngie	int i;
51272343Sngie
52272343Sngie	skey_set_algorithm(algo);
53272343Sngie
54272343Sngie	keycrunch(data, seed, pass);
55272343Sngie	btoa8(prn, data);
56272343Sngie	H_REQUIRE(prn, zero);
57272343Sngie
58272343Sngie	f(data);
59272343Sngie	btoa8(prn, data);
60272343Sngie	H_REQUIRE(prn, one);
61272343Sngie
62272343Sngie	for(i = 1; i < 99; ++i)
63272343Sngie		f(data);
64272343Sngie	btoa8(prn, data);
65272343Sngie	H_REQUIRE(prn, nine);
66272343Sngie}
67272343Sngie
68272343SngieATF_TC(md4);
69272343SngieATF_TC_HEAD(md4, tc)
70272343Sngie{
71272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks MD4 algorithm");
72272343Sngie}
73272343SngieATF_TC_BODY(md4, tc)
74272343Sngie{
75272343Sngie	h_check("This is a test.", "TeSt", "md4", "D1854218EBBB0B51",
76272343Sngie		"63473EF01CD0B444", "C5E612776E6C237A");
77272343Sngie	h_check("AbCdEfGhIjK", "alpha1", "md4", "50076F47EB1ADE4E",
78272343Sngie		"65D20D1949B5F7AB", "D150C82CCE6F62D1");
79272343Sngie	h_check("OTP's are good", "correct", "md4", "849C79D4F6F55388",
80272343Sngie		"8C0992FB250847B1", "3F3BF4B4145FD74B");
81272343Sngie}
82272343Sngie
83272343SngieATF_TC(md5);
84272343SngieATF_TC_HEAD(md5, tc)
85272343Sngie{
86272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks MD5 algorithm");
87272343Sngie}
88272343SngieATF_TC_BODY(md5, tc)
89272343Sngie{
90272343Sngie	h_check("This is a test.", "TeSt", "md5", "9E876134D90499DD",
91272343Sngie		"7965E05436F5029F", "50FE1962C4965880");
92272343Sngie	h_check("AbCdEfGhIjK", "alpha1", "md5", "87066DD9644BF206",
93272343Sngie		"7CD34C1040ADD14B", "5AA37A81F212146C");
94272343Sngie	h_check("OTP's are good", "correct", "md5", "F205753943DE4CF9",
95272343Sngie		"DDCDAC956F234937", "B203E28FA525BE47");
96272343Sngie}
97272343Sngie
98272343SngieATF_TC(sha1);
99272343SngieATF_TC_HEAD(sha1, tc)
100272343Sngie{
101272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks SHA1 algorithm");
102272343Sngie}
103272343SngieATF_TC_BODY(sha1, tc)
104272343Sngie{
105272343Sngie	h_check("This is a test.", "TeSt", "sha1","BB9E6AE1979D8FF4",
106272343Sngie		"63D936639734385B", "87FEC7768B73CCF9");
107272343Sngie	h_check("AbCdEfGhIjK", "alpha1", "sha1","AD85F658EBE383C9",
108272343Sngie		"D07CE229B5CF119B", "27BC71035AAF3DC6");
109272343Sngie	h_check("OTP's are good", "correct", "sha1","D51F3E99BF8E6F0B",
110272343Sngie		"82AEB52D943774E4", "4F296A74FE1567EC");
111272343Sngie}
112272343Sngie
113272343SngieATF_TP_ADD_TCS(tp)
114272343Sngie{
115272343Sngie
116272343Sngie	ATF_TP_ADD_TC(tp, md4);
117272343Sngie	ATF_TP_ADD_TC(tp, md5);
118272343Sngie	ATF_TP_ADD_TC(tp, sha1);
119272343Sngie
120272343Sngie	return atf_no_error();
121272343Sngie}
122