| 1 |
/* |
| 2 |
* COPYRIGHT (c) International Business Machines Corp. 2005-2017 |
| 3 |
* |
| 4 |
* This program is provided under the terms of the Common Public License, |
| 5 |
* version 1.0 (CPL-1.0). Any use, reproduction or distribution for this |
| 6 |
* software constitutes recipient's acceptance of CPL-1.0 terms which can be |
| 7 |
* found in the file LICENSE file or at |
| 8 |
* https://opensource.org/licenses/cpl1.0.php |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef _REGRESS_H |
| 12 |
#define _REGRESS_H |
| 13 |
|
| 14 |
#if !defined(TRUE) |
| 15 |
#define TRUE 1 |
| 16 |
#endif |
| 17 |
|
| 18 |
#if !defined(FALSE) |
| 19 |
#define FALSE 0 |
| 20 |
#endif |
| 21 |
|
| 22 |
#define DES_BLOCK_SIZE 8 |
| 23 |
#define DES_KEY_LEN 8 |
| 24 |
|
| 25 |
#define SHA1_HASH_LEN 20 |
| 26 |
#define MD2_HASH_LEN 16 |
| 27 |
#define MD5_HASH_LEN 16 |
| 28 |
|
| 29 |
#define BIG_REQUEST 4096 |
| 30 |
|
| 31 |
#define MIN(a, b) ( (a) < (b) ? (a) : (b) ) |
| 32 |
|
| 33 |
#include <sys/time.h> |
| 34 |
#define SYSTEMTIME struct timeval |
| 35 |
#define GetSystemTime(x) gettimeofday((x), NULL) |
| 36 |
|
| 37 |
#include <sys/time.h> |
| 38 |
#if !defined(_AIX) |
| 39 |
#pragma GCC system_header |
| 40 |
#endif |
| 41 |
static struct timeval timev1; |
| 42 |
static struct timeval timev2; |
| 43 |
static struct timeval timev3; |
| 44 |
static struct timeval timev4; |
| 45 |
static struct timeval timevr; |
| 46 |
|
| 47 |
#ifndef timersub |
| 48 |
/* We just need timersub, so instead of requiring _BSD_SOURCE, * |
| 49 |
* define it just like glibc does */ |
| 50 |
#define timersub(t1, t2, tr) \ |
| 51 |
do { \ |
| 52 |
(tr)->tv_sec = (t1)->tv_sec - (t2)->tv_sec; \ |
| 53 |
(tr)->tv_usec = (t1)->tv_usec - (t2)->tv_usec; \ |
| 54 |
if ((tr)->tv_usec < 0) { \ |
| 55 |
--(tr)->tv_sec; \ |
| 56 |
(tr)->tv_usec += 1000000; \ |
| 57 |
} \ |
| 58 |
} while (0) |
| 59 |
#endif |
| 60 |
|
| 61 |
#include "p11util.h" |
| 62 |
|
| 63 |
extern CK_ULONG t_total; // total test assertions |
| 64 |
extern CK_ULONG t_ran; // number of assertions ran |
| 65 |
extern CK_ULONG t_passed; // number of assertions passed |
| 66 |
extern CK_ULONG t_failed; // number of assertions failed |
| 67 |
extern CK_ULONG t_skipped; // number of assertions skipped |
| 68 |
extern CK_ULONG t_errors; // number of errors |
| 69 |
|
| 70 |
void process_time(SYSTEMTIME t1, SYSTEMTIME t2); |
| 71 |
void show_error(char *str, CK_RV rc); |
| 72 |
void print_hex(CK_BYTE * buf, CK_ULONG len); |
| 73 |
|
| 74 |
CK_BBOOL do_GetFunctionList(void); |
| 75 |
|
| 76 |
void init_coprocessor(void); |
| 77 |
|
| 78 |
CK_RV C_GetFunctionList(CK_FUNCTION_LIST **); |
| 79 |
CK_RV DummyFunction(CK_SLOT_ID id); |
| 80 |
|
| 81 |
int digest_functions(void); |
| 82 |
|
| 83 |
extern CK_FUNCTION_LIST *funcs; |
| 84 |
extern CK_FUNCTION_LIST_3_0 *funcs3; |
| 85 |
extern CK_INTERFACE *ifs; |
| 86 |
extern CK_SLOT_ID SLOT_ID; |
| 87 |
|
| 88 |
void usage(char *fct); |
| 89 |
int do_ParseArgs(int argc, char **argv); |
| 90 |
|
| 91 |
// these values are required when generating a PKCS DSA value. they were |
| 92 |
// obtained by generating a DSA key pair on the 4758 with the default (random) |
| 93 |
// values. these values are in big-endian format |
| 94 |
// |
| 95 |
extern CK_BYTE DSA_PUBL_PRIME[128]; |
| 96 |
extern CK_BYTE DSA_PUBL_SUBPRIME[20]; |
| 97 |
extern CK_BYTE DSA_PUBL_BASE[128]; |
| 98 |
|
| 99 |
extern CK_BBOOL skip_token_obj; |
| 100 |
extern CK_BBOOL no_stop; |
| 101 |
extern CK_BBOOL no_init; |
| 102 |
extern CK_BBOOL securekey; |
| 103 |
|
| 104 |
int get_so_pin(CK_BYTE_PTR); |
| 105 |
int get_user_pin(CK_BYTE_PTR); |
| 106 |
|
| 107 |
#define PKCS11_MAX_PIN_LEN 128 |
| 108 |
#define PKCS11_SO_PIN_ENV_VAR "PKCS11_SO_PIN" |
| 109 |
#define PKCS11_USER_PIN_ENV_VAR "PKCS11_USER_PIN" |
| 110 |
|
| 111 |
#define PRINT_ERR(fmt, ...) \ |
| 112 |
fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ## __VA_ARGS__) |
| 113 |
#define PRINT(fmt, ...) \ |
| 114 |
printf("%s:%d " fmt "\n", __FILE__, __LINE__, ## __VA_ARGS__) |
| 115 |
|
| 116 |
|
| 117 |
/* show_error(char *_str, unsigned long _rc); */ |
| 118 |
#define show_error(_str, _rc) \ |
| 119 |
fprintf(stderr, "%s:%d: %s returned %lu (0x%lx) %s\n", \ |
| 120 |
__FILE__, __LINE__, _str, _rc, _rc, \ |
| 121 |
p11_get_ckr(_rc)) |
| 122 |
|
| 123 |
#define testcase_setup() \ |
| 124 |
do { \ |
| 125 |
t_total = 0; \ |
| 126 |
t_errors = 0; \ |
| 127 |
gettimeofday(&timev3, NULL); \ |
| 128 |
} while (0) |
| 129 |
|
| 130 |
#define testsuite_begin(_fmt, ...) \ |
| 131 |
do { \ |
| 132 |
printf("------\n* TESTSUITE %s BEGIN " _fmt "\n", \ |
| 133 |
__func__, ## __VA_ARGS__); \ |
| 134 |
} while (0) |
| 135 |
|
| 136 |
#define testcase_begin(_fmt, ...) \ |
| 137 |
do { \ |
| 138 |
printf("------\n* TESTCASE %s BEGIN " _fmt "\n", \ |
| 139 |
__func__, ## __VA_ARGS__); \ |
| 140 |
gettimeofday(&timev1, NULL); \ |
| 141 |
} while (0) |
| 142 |
|
| 143 |
#define testcase_begin_f(_func, _fmt, ...) \ |
| 144 |
do { \ |
| 145 |
printf("------\n* TESTCASE %s BEGIN " _fmt "\n", \ |
| 146 |
_func, ## __VA_ARGS__); \ |
| 147 |
gettimeofday(&timev1, NULL); \ |
| 148 |
} while (0) |
| 149 |
|
| 150 |
#define testcase_new_assertion() \ |
| 151 |
t_ran++; |
| 152 |
|
| 153 |
#define testcase_pass(_fmt, ...) \ |
| 154 |
do { \ |
| 155 |
gettimeofday(&timev2, NULL); \ |
| 156 |
timersub(&timev2, &timev1, &timevr); \ |
| 157 |
printf("* TESTCASE %s PASS (elapsed time %lds %ldus) " _fmt "\n\n", \ |
| 158 |
__func__, timevr.tv_sec, timevr.tv_usec, ## __VA_ARGS__); \ |
| 159 |
t_passed++; \ |
| 160 |
} while (0) |
| 161 |
|
| 162 |
#define testcase_pass_f(_func, _fmt, ...) \ |
| 163 |
do { \ |
| 164 |
gettimeofday(&timev2, NULL); \ |
| 165 |
timersub(&timev2, &timev1, &timevr); \ |
| 166 |
printf("* TESTCASE %s PASS (elapsed time %lds %ldus) " _fmt "\n\n", \ |
| 167 |
_func, timevr.tv_sec, timevr.tv_usec, ## __VA_ARGS__); \ |
| 168 |
t_passed++; \ |
| 169 |
} while (0) |
| 170 |
|
| 171 |
#define testsuite_skip(_n,_fmt, ...) \ |
| 172 |
do { \ |
| 173 |
printf("* TESTSUITE %s SKIP " _fmt "\n\n", \ |
| 174 |
__func__, ## __VA_ARGS__); \ |
| 175 |
t_skipped+= _n; \ |
| 176 |
} while (0) |
| 177 |
|
| 178 |
#define testcase_skip(_fmt, ...) \ |
| 179 |
do { \ |
| 180 |
printf("* TESTCASE %s SKIP " _fmt "\n\n", \ |
| 181 |
__func__, ## __VA_ARGS__); \ |
| 182 |
t_skipped++; \ |
| 183 |
} while (0) |
| 184 |
|
| 185 |
#define testcase_skip_f(_func, _fmt, ...) \ |
| 186 |
do { \ |
| 187 |
printf("* TESTCASE %s SKIP " _fmt "\n\n", \ |
| 188 |
_func, ## __VA_ARGS__); \ |
| 189 |
t_skipped++; \ |
| 190 |
} while (0) |
| 191 |
|
| 192 |
#define testcase_notice(_fmt, ...) \ |
| 193 |
do { \ |
| 194 |
printf("* TESTCASE %s NOTICE " _fmt "\n", \ |
| 195 |
__func__, ## __VA_ARGS__); \ |
| 196 |
} while (0) |
| 197 |
|
| 198 |
#define testcase_notice_f(_func, _fmt, ...) \ |
| 199 |
do { \ |
| 200 |
printf("* TESTCASE %s NOTICE " _fmt "\n", \ |
| 201 |
__func, ## __VA_ARGS__); \ |
| 202 |
} while (0) |
| 203 |
|
| 204 |
#define testcase_fail(_fmt, ...) \ |
| 205 |
do { \ |
| 206 |
printf("* TESTCASE %s FAIL (%s:%d) " _fmt "\n", \ |
| 207 |
__func__, __FILE__, __LINE__, ## __VA_ARGS__); \ |
| 208 |
t_failed++; \ |
| 209 |
} while (0) |
| 210 |
|
| 211 |
#define testcase_fail_f(_func, _fmt, ...) \ |
| 212 |
do { \ |
| 213 |
printf("* TESTCASE %s FAIL (%s:%d) " _fmt "\n", \ |
| 214 |
_func, __FILE__, __LINE__, ## __VA_ARGS__); \ |
| 215 |
t_failed++; \ |
| 216 |
} while (0) |
| 217 |
|
| 218 |
#define testcase_error(_fmt, ...) \ |
| 219 |
do { \ |
| 220 |
printf("* TESTCASE %s ERROR (%s:%d)) " _fmt "\n", \ |
| 221 |
__func__, __FILE__, __LINE__, ## __VA_ARGS__); \ |
| 222 |
t_errors++; \ |
| 223 |
} while (0) |
| 224 |
|
| 225 |
#define testcase_error_f(_func, _fmt, ...) \ |
| 226 |
do { \ |
| 227 |
printf("* TESTCASE %s ERROR (%s:%d)) " _fmt "\n", \ |
| 228 |
_func, __FILE__, __LINE__, ## __VA_ARGS__); \ |
| 229 |
t_errors++; \ |
| 230 |
} while (0) |
| 231 |
|
| 232 |
#define testcase_print_result() \ |
| 233 |
do { \ |
| 234 |
gettimeofday(&timev4, NULL); \ |
| 235 |
timersub(&timev4, &timev3, &timevr); \ |
| 236 |
printf("Total=%lu, Ran=%lu, Passed=%lu, Failed=%lu, " \ |
| 237 |
"Skipped=%lu, Errors=%lu (total elapsed time " \ |
| 238 |
"%lds %ldus)\n", \ |
| 239 |
(t_ran + t_skipped), t_ran, t_passed, t_failed, \ |
| 240 |
t_skipped, t_errors, timevr.tv_sec, \ |
| 241 |
timevr.tv_usec); \ |
| 242 |
} while (0) |
| 243 |
|
| 244 |
#define testcase_rw_session() \ |
| 245 |
do { \ |
| 246 |
flags = CKF_SERIAL_SESSION | CKF_RW_SESSION; \ |
| 247 |
rc = funcs->C_OpenSession(SLOT_ID, flags, NULL, NULL, &session ); \ |
| 248 |
if (rc != CKR_OK) { \ |
| 249 |
testcase_error("C_OpenSession() rc = %s", p11_get_ckr(rc)); \ |
| 250 |
session = CK_INVALID_HANDLE; \ |
| 251 |
goto testcase_cleanup; \ |
| 252 |
} \ |
| 253 |
} while (0) |
| 254 |
|
| 255 |
#define testcase_ro_session() \ |
| 256 |
do { \ |
| 257 |
flags = CKF_SERIAL_SESSION; \ |
| 258 |
rc = funcs->C_OpenSession(SLOT_ID, flags, NULL, NULL, &session ); \ |
| 259 |
if (rc != CKR_OK) { \ |
| 260 |
testcase_error("C_OpenSession() rc = %s", p11_get_ckr(rc)); \ |
| 261 |
session = CK_INVALID_HANDLE; \ |
| 262 |
goto testcase_cleanup; \ |
| 263 |
} \ |
| 264 |
} while (0) |
| 265 |
|
| 266 |
#define testcase_close_session() \ |
| 267 |
do { \ |
| 268 |
if (session != CK_INVALID_HANDLE) { \ |
| 269 |
rc = funcs->C_CloseSession(session); \ |
| 270 |
if (rc != CKR_OK) { \ |
| 271 |
testcase_error("C_CloseSession() rc = %s", p11_get_ckr(rc));\ |
| 272 |
} \ |
| 273 |
} \ |
| 274 |
} while (0) |
| 275 |
|
| 276 |
#define testcase_closeall_session() \ |
| 277 |
do { \ |
| 278 |
rc = funcs->C_CloseAllSessions(SLOT_ID); \ |
| 279 |
if (rc != CKR_OK) { \ |
| 280 |
testcase_error("C_CloseAllSessions() rc = %s", p11_get_ckr(rc));\ |
| 281 |
} \ |
| 282 |
} while (0) |
| 283 |
|
| 284 |
|
| 285 |
#define testcase_user_login() \ |
| 286 |
do { \ |
| 287 |
if (get_user_pin(user_pin)) { \ |
| 288 |
testcase_error("get_user_pin() failed"); \ |
| 289 |
testcase_closeall_session(); \ |
| 290 |
exit(-1); \ |
| 291 |
} \ |
| 292 |
user_pin_len = (CK_ULONG) strlen( (char *) user_pin); \ |
| 293 |
rc = funcs->C_Login(session, CKU_USER, user_pin, user_pin_len); \ |
| 294 |
if (rc != CKR_OK) { \ |
| 295 |
testcase_error("C_Login() rc = %s", p11_get_ckr(rc)); \ |
| 296 |
goto testcase_cleanup; \ |
| 297 |
} \ |
| 298 |
} while (0) |
| 299 |
|
| 300 |
#define testcase_user_logout() \ |
| 301 |
do { \ |
| 302 |
if (session != CK_INVALID_HANDLE) { \ |
| 303 |
rc = funcs->C_Logout(session); \ |
| 304 |
if (rc != CKR_OK) { \ |
| 305 |
testcase_error("C_Logout() rc = %s", p11_get_ckr(rc)); \ |
| 306 |
} \ |
| 307 |
} \ |
| 308 |
} while (0) |
| 309 |
|
| 310 |
|
| 311 |
#define testcase_so_login() \ |
| 312 |
do { \ |
| 313 |
if (get_so_pin(so_pin)) { \ |
| 314 |
testcase_error("get_so_pin() failed"); \ |
| 315 |
rc = -1; \ |
| 316 |
goto testcase_cleanup; \ |
| 317 |
} \ |
| 318 |
so_pin_len = (CK_ULONG) strlen( (char *) so_pin); \ |
| 319 |
rc = funcs->C_Login(session, CKU_SO, so_pin, so_pin_len); \ |
| 320 |
if (rc != CKR_OK) { \ |
| 321 |
testcase_error("C_Login() rc = %s", p11_get_ckr(rc)); \ |
| 322 |
goto testcase_cleanup; \ |
| 323 |
} \ |
| 324 |
} while (0) |
| 325 |
|
| 326 |
#define testcase_return(rc) \ |
| 327 |
(((rc) != 0 || t_failed != 0 || t_errors != 0) ? 1 : 0) |
| 328 |
|
| 329 |
#endif |