1 /* safe.c Shishi SAFE self tests.
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Shishi 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 main (int argc
, char *argv
[])
38 if (strcmp (argv
[argc
- 1], "-v") == 0 ||
39 strcmp (argv
[argc
- 1], "--verbose") == 0)
41 else if (strcmp (argv
[argc
- 1], "-d") == 0 ||
42 strcmp (argv
[argc
- 1], "--debug") == 0)
44 else if (strcmp (argv
[argc
- 1], "-b") == 0 ||
45 strcmp (argv
[argc
- 1], "--break-on-error") == 0)
47 else if (strcmp (argv
[argc
- 1], "-h") == 0 ||
48 strcmp (argv
[argc
- 1], "-?") == 0 ||
49 strcmp (argv
[argc
- 1], "--help") == 0)
51 printf ("Usage: %s [-vdbh?] [--verbose] [--debug] "
52 "[--break-on-error] [--help]\n", argv
[0]);
60 fail ("Could not initialize shishi\n");
65 shishi_cfg (handle
, strdup ("verbose"));
67 escapeprint (NULL
, 0);
72 res
= shishi_safe (handle
, &safe
);
74 printf ("shishi_safe () => `%p'.\n", safe
);
76 success ("shishi_safe() OK\n");
78 fail ("shishi_safe() failed\n");
81 key
= shishi_safe_key (safe
);
83 success ("shishi_safe_key() OK\n");
85 fail ("shishi_safe_key() failed\n");
87 /* shishi_safe_safe */
88 asn1safe
= shishi_safe_safe (safe
);
90 success ("shishi_safe_safe() OK\n");
92 fail ("shishi_safe_safe() failed\n");
94 /* shishi_safe_set_user_data */
95 res
= shishi_safe_set_user_data (handle
, asn1safe
, "foo", 3);
97 success ("shishi_safe_set_user_data() OK\n");
99 fail ("shishi_safe_set_user_data() failed (%d)\n", res
);
101 /* shishi_safe_user_data */
102 res
= shishi_safe_user_data (handle
, asn1safe
, &p
, &l
);
105 if (res
== SHISHI_OK
&& l
== 3 && memcmp (p
, "foo", 3) == 0)
106 success ("shishi_safe_user_data() OK\n");
108 fail ("shishi_safe_user_data() failed (%d)\n", res
);
110 /* shishi_safe_set_cksum */
111 res
= shishi_safe_set_cksum (handle
, asn1safe
, 42, "bar", 3);
112 if (res
== SHISHI_OK
)
113 success ("shishi_safe_set_cksum() OK\n");
115 fail ("shishi_safe_set_cksum() failed (%d)\n", res
);
117 /* shishi_safe_cksum */
118 res
= shishi_safe_cksum (handle
, asn1safe
, &t
, &q
, &m
);
121 printf ("type=%d\n", t
);
124 if (res
== SHISHI_OK
&& t
== 42 && m
== 3 && memcmp (q
, "bar", 3) == 0)
125 success ("shishi_safe_cksum() OK\n");
127 fail ("shishi_safe_cksum() failed (%d)\n", res
);
129 /* shishi_safe_verify */
130 res
= shishi_safe_verify (safe
, key
);
131 if (res
== SHISHI_CRYPTO_ERROR
) /* t==42 unsupported cksumtype */
132 success ("shishi_safe_verify() OK\n");
134 fail ("shishi_safe_verify() failed (%d)\n", res
);
137 /* shishi_safe_set_cksum */
138 res
= shishi_safe_set_cksum (handle
, asn1safe
, SHISHI_RSA_MD5_DES
,
140 if (res
== SHISHI_OK
)
141 success ("shishi_safe_set_cksum() OK\n");
143 fail ("shishi_safe_set_cksum() failed (%d)\n", res
);
145 /* shishi_safe_verify */
146 res
= shishi_safe_verify (safe
, key
);
147 if (res
== SHISHI_VERIFY_FAILED
)
148 success ("shishi_safe_verify() OK\n");
150 fail ("shishi_safe_verify() failed (%d)\n", res
);
153 /* shishi_safe_safe_der() */
154 res
= shishi_safe_safe_der (safe
, &p
, &l
);
155 if (res
== SHISHI_OK
)
156 success ("shishi_safe_safe_der() OK\n");
158 fail ("shishi_safe_safe_der() failed\n");
160 /* shishi_safe_to_file() */
161 res
= shishi_safe_to_file (handle
, asn1safe
, SHISHI_FILETYPE_TEXT
,
163 if (res
== SHISHI_OK
)
164 success ("shishi_safe_to_file() OK\n");
166 fail ("shishi_safe_to_file() failed\n");
168 /* shishi_safe_done() */
169 shishi_safe_done (safe
);
170 success ("shishi_safe_done() OK\n");
172 /* shishi_authenticator_from_file() */
173 res
= shishi_safe_from_file (handle
, &asn1safe
, SHISHI_FILETYPE_TEXT
,
175 if (res
== SHISHI_OK
)
176 success ("shishi_safe_from_file() OK\n");
178 fail ("shishi_safe_from_file() failed\n");
182 /* shishi_safe_print() */
183 res
= shishi_safe_print (handle
, stdout
, asn1safe
);
184 if (res
== SHISHI_OK
)
185 success ("shishi_safe_print() OK\n");
187 fail ("shishi_safe_print() failed\n");
191 res
= shishi_new_a2d (handle
, asn1safe
, &q
, &m
);
192 if (res
== SHISHI_OK
)
193 success ("shishi_a2d() OK\n");
195 fail ("shishi_a2d() failed\n");
197 /* Compare DER encodings of authenticators */
198 if (l
> 0 && m
> 0 && l
== m
&& memcmp (p
, q
, l
) == 0)
199 success ("DER comparison OK\n");
201 fail ("DER comparison failed\n");
207 res
= unlink ("safe.tmp");
209 success ("unlink() OK\n");
211 fail ("unlink() failed\n");
213 shishi_done (handle
);
216 printf ("SAFE self tests done with %d errors\n", error_count
);
218 return error_count
? 1 : 0;