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