1 /* ****************************************************************************
2
3 * eID Middleware Project.
4 * Copyright (C) 2014 FedICT.
5 *
6 * This is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License version
8 * 3.0 as published by the Free Software Foundation.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this software; if not, see
17 * http://www.gnu.org/licenses/.
18
19 **************************************************************************** */
20 #ifdef WIN32
21 #include <win32.h>
22 #pragma pack(push, cryptoki, 1)
23 #include "pkcs11.h"
24 #pragma pack(pop, cryptoki)
25 #else
26 #include <unix.h>
27 #include <pkcs11.h>
28 #endif
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include "testlib.h"
34
35 CK_BYTE digest_results[4][512] = {
36 {
37 0x2c, 0x26, 0xb4, 0x6b,
38 0x68, 0xff, 0xc6, 0x8f,
39 0xf9, 0x9b, 0x45, 0x3c,
40 0x1d, 0x30, 0x41, 0x34,
41 0x13, 0x42, 0x2d, 0x70,
42 0x64, 0x83, 0xbf, 0xa0,
43 0xf9, 0x8a, 0x5e, 0x88,
44 0x62, 0x66, 0xe7, 0xae
45 },
46 {
47 0x98, 0xc1, 0x1f, 0xfd,
48 0xfd, 0xd5, 0x40, 0x67,
49 0x6b, 0x1a, 0x13, 0x7c,
50 0xb1, 0xa2, 0x2b, 0x2a,
51 0x70, 0x35, 0x0c, 0x9a,
52 0x44, 0x17, 0x1d, 0x6b,
53 0x11, 0x80, 0xc6, 0xbe,
54 0x5c, 0xbb, 0x2e, 0xe3,
55 0xf7, 0x9d, 0x53, 0x2c,
56 0x8a, 0x1d, 0xd9, 0xef,
57 0x2e, 0x8e, 0x08, 0xe7,
58 0x52, 0xa3, 0xba, 0xbb
59 },
60 {
61 0xf7, 0xfb, 0xba, 0x6e,
62 0x06, 0x36, 0xf8, 0x90,
63 0xe5, 0x6f, 0xbb, 0xf3,
64 0x28, 0x3e, 0x52, 0x4c,
65 0x6f, 0xa3, 0x20, 0x4a,
66 0xe2, 0x98, 0x38, 0x2d,
67 0x62, 0x47, 0x41, 0xd0,
68 0xdc, 0x66, 0x38, 0x32,
69 0x6e, 0x28, 0x2c, 0x41,
70 0xbe, 0x5e, 0x42, 0x54,
71 0xd8, 0x82, 0x07, 0x72,
72 0xc5, 0x51, 0x8a, 0x2c,
73 0x5a, 0x8c, 0x0c, 0x7f,
74 0x7e, 0xda, 0x19, 0x59,
75 0x4a, 0x7e, 0xb5, 0x39,
76 0x45, 0x3e, 0x1e, 0xd7
77 },
78 {
79 0x42, 0xcf, 0xa2, 0x11,
80 0x01, 0x8e, 0xa4, 0x92,
81 0xfd, 0xee, 0x45, 0xac,
82 0x63, 0x7b, 0x79, 0x72,
83 0xa0, 0xad, 0x68, 0x73
84 },
85 };
86
87 CK_MECHANISM_TYPE digest_mechs[4] = {
88 CKM_SHA256,
89 CKM_SHA384,
90 CKM_SHA512,
91 CKM_RIPEMD160,
92 };
93
TEST_FUNC(digest)94 TEST_FUNC(digest) {
95 int ret;
96 CK_SESSION_HANDLE session;
97 CK_MECHANISM mech;
98 CK_BYTE data[] = { 'f', 'o', 'o' };
99 CK_SLOT_ID slot;
100 int i;
101 CK_ULONG len = 0;
102 CK_BYTE_PTR digest;
103
104 check_rv(C_Initialize(NULL_PTR));
105
106 if((ret = find_slot(CK_TRUE, &slot)) != TEST_RV_OK) {
107 check_rv(C_Finalize(NULL_PTR));
108 return ret;
109 }
110
111 check_rv(C_OpenSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &session));
112
113 for(i=0; i<4; i++) {
114 memset(&mech, 0, sizeof(mech));
115 mech.mechanism = digest_mechs[i];
116
117 printf("Testing mechanism %s\n", ckm_to_charp(digest_mechs[i]));
118
119 check_rv(C_DigestInit(session, &mech));
120
121 check_rv(C_DigestUpdate(session, data, sizeof(data)));
122
123 check_rv(C_DigestFinal(session, NULL_PTR, &len));
124
125 digest = malloc(len);
126
127 check_rv(C_DigestFinal(session, digest, &len));
128
129 verbose_assert(memcmp(digest, digest_results[i], len) == 0);
130
131 check_rv(C_DigestInit(session, &mech));
132
133 check_rv(C_Digest(session, data, sizeof(data), digest, &len));
134
135 verbose_assert(memcmp(digest, digest_results[i], len) == 0);
136 }
137
138 if(have_robot()) {
139 CK_ULONG len = 0;
140 ckrv_mod m_maybe_rmvd[] = {
141 { CKR_TOKEN_NOT_PRESENT, TEST_RV_OK },
142 { CKR_DEVICE_REMOVED, TEST_RV_OK },
143 };
144 ckrv_mod m_is_rmvd[] = {
145 { CKR_OK, TEST_RV_FAIL },
146 { CKR_TOKEN_NOT_PRESENT, TEST_RV_OK },
147 { CKR_DEVICE_REMOVED, TEST_RV_OK },
148 };
149 ckrv_mod m_inv[] = {
150 { CKR_OK, TEST_RV_FAIL },
151 { CKR_DEVICE_REMOVED, TEST_RV_OK },
152 { CKR_SESSION_HANDLE_INVALID, TEST_RV_OK },
153 };
154
155 check_rv(C_DigestInit(session, &mech));
156
157 robot_remove_card();
158
159 check_rv_long(C_DigestUpdate(session, data, sizeof(data)), m_maybe_rmvd);
160
161 check_rv_long(C_DigestFinal(session, NULL_PTR, &len), m_is_rmvd);
162
163 if((ret = find_slot(CK_TRUE, &slot)) != TEST_RV_OK) {
164 check_rv(C_Finalize(NULL_PTR));
165 return ret;
166 }
167
168 check_rv_long(C_DigestInit(session, &mech), m_inv);
169
170 check_rv(C_CloseSession(session));
171
172 check_rv(C_OpenSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &session));
173
174 check_rv(C_DigestInit(session, &mech));
175 } else {
176 printf("Robot not present, skipping manual removal/insertion part of test...\n");
177 }
178
179 check_rv(C_Finalize(NULL_PTR));
180
181 return TEST_RV_OK;
182 }
183