NingSun | 0c89b3c | 2018-02-08 08:34:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | /***************************************************************************** |
| 28 | DigestTests.cpp |
| 29 | |
| 30 | Contains test cases to C_DigestInit, C_Digest, C_DigestUpdate, C_DigestFinal |
| 31 | *****************************************************************************/ |
| 32 | |
| 33 | #include <config.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include "DigestTests.h" |
| 37 | |
| 38 | CPPUNIT_TEST_SUITE_REGISTRATION(DigestTests); |
| 39 | |
| 40 | void DigestTests::testDigestInit() |
| 41 | { |
| 42 | CK_RV rv; |
| 43 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; |
| 44 | CK_MECHANISM mechanism = { |
| 45 | CKM_VENDOR_DEFINED, NULL_PTR, 0 |
| 46 | }; |
| 47 | |
| 48 | // Just make sure that we finalize any previous tests |
| 49 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 50 | |
| 51 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanism) ); |
| 52 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 53 | |
| 54 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 55 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 56 | |
| 57 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 58 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 59 | |
| 60 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, NULL_PTR) ); |
| 61 | CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 62 | |
| 63 | rv = CRYPTOKI_F_PTR( C_DigestInit(CK_INVALID_HANDLE, &mechanism) ); |
| 64 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 65 | |
| 66 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanism) ); |
| 67 | CPPUNIT_ASSERT(rv == CKR_MECHANISM_INVALID); |
| 68 | |
| 69 | mechanism.mechanism = CKM_SHA512; |
| 70 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanism) ); |
| 71 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 72 | |
| 73 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanism) ); |
| 74 | CPPUNIT_ASSERT(rv == CKR_OPERATION_ACTIVE); |
| 75 | } |
| 76 | |
| 77 | void DigestTests::testDigest() |
| 78 | { |
| 79 | CK_RV rv; |
| 80 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; |
| 81 | CK_MECHANISM mechanism = { |
| 82 | CKM_SHA512, NULL_PTR, 0 |
| 83 | }; |
| 84 | CK_ULONG digestLen; |
| 85 | CK_BYTE_PTR digest; |
| 86 | CK_BYTE data[] = {"Text to digest"}; |
| 87 | |
| 88 | // Just make sure that we finalize any previous tests |
| 89 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 90 | |
| 91 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, NULL_PTR, &digestLen) ); |
| 92 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 93 | |
| 94 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 95 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 96 | |
| 97 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 98 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 99 | |
| 100 | rv = CRYPTOKI_F_PTR( C_Digest(CK_INVALID_HANDLE, data, sizeof(data)-1, NULL_PTR, &digestLen) ); |
| 101 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 102 | |
| 103 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, NULL_PTR, &digestLen) ); |
| 104 | CPPUNIT_ASSERT(rv == CKR_OPERATION_NOT_INITIALIZED); |
| 105 | |
| 106 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanism) ); |
| 107 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 108 | |
| 109 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, NULL_PTR, sizeof(data)-1, NULL_PTR, &digestLen) ); |
| 110 | CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 111 | |
| 112 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, NULL_PTR, NULL_PTR) ); |
| 113 | CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 114 | |
| 115 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, NULL_PTR, &digestLen) ); |
| 116 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 117 | |
| 118 | digest = (CK_BYTE_PTR)malloc(digestLen); |
| 119 | digestLen = 0; |
| 120 | |
| 121 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, digest, &digestLen) ); |
| 122 | CPPUNIT_ASSERT(rv == CKR_BUFFER_TOO_SMALL); |
| 123 | |
| 124 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, digest, &digestLen) ); |
| 125 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 126 | |
| 127 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, digest, &digestLen) ); |
| 128 | CPPUNIT_ASSERT(rv == CKR_OPERATION_NOT_INITIALIZED); |
| 129 | free(digest); |
| 130 | } |
| 131 | |
| 132 | void DigestTests::testDigestUpdate() |
| 133 | { |
| 134 | CK_RV rv; |
| 135 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; |
| 136 | CK_MECHANISM mechanism = { |
| 137 | CKM_SHA512, NULL_PTR, 0 |
| 138 | }; |
| 139 | CK_BYTE data[] = {"Text to digest"}; |
| 140 | |
| 141 | // Just make sure that we finalize any previous tests |
| 142 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 143 | |
| 144 | rv = CRYPTOKI_F_PTR( C_DigestUpdate(hSession, data, sizeof(data)-1) ); |
| 145 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 146 | |
| 147 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 148 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 149 | |
| 150 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 151 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 152 | |
| 153 | rv = CRYPTOKI_F_PTR( C_DigestUpdate(CK_INVALID_HANDLE, data, sizeof(data)-1) ); |
| 154 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 155 | |
| 156 | rv = CRYPTOKI_F_PTR( C_DigestUpdate(hSession, data, sizeof(data)-1) ); |
| 157 | CPPUNIT_ASSERT(rv == CKR_OPERATION_NOT_INITIALIZED); |
| 158 | |
| 159 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanism) ); |
| 160 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 161 | |
| 162 | rv = CRYPTOKI_F_PTR( C_DigestUpdate(hSession, NULL_PTR, sizeof(data)-1) ); |
| 163 | CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 164 | |
| 165 | rv = CRYPTOKI_F_PTR( C_DigestUpdate(hSession, data, sizeof(data)-1) ); |
| 166 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 167 | } |
| 168 | |
| 169 | void DigestTests::testDigestKey() |
| 170 | { |
| 171 | CK_RV rv; |
| 172 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; |
| 173 | CK_MECHANISM mechanism = { |
| 174 | CKM_SHA512, NULL_PTR, 0 |
| 175 | }; |
| 176 | CK_BYTE data[] = {"Text to digest"}; |
| 177 | |
| 178 | // Just make sure that we finalize any previous tests |
| 179 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 180 | |
| 181 | rv = CRYPTOKI_F_PTR( C_DigestKey(hSession, (CK_OBJECT_HANDLE)123UL) ); |
| 182 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 183 | |
| 184 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 185 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 186 | |
| 187 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 188 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 189 | |
| 190 | // Create the generic secret key to digest |
| 191 | CK_BBOOL bFalse = CK_FALSE; |
| 192 | CK_BBOOL bTrue = CK_TRUE; |
| 193 | CK_OBJECT_CLASS secretClass = CKO_SECRET_KEY; |
| 194 | CK_KEY_TYPE genKeyType = CKK_GENERIC_SECRET; |
| 195 | CK_ATTRIBUTE attribs[] = { |
| 196 | { CKA_CLASS, &secretClass, sizeof(secretClass) }, |
| 197 | { CKA_KEY_TYPE, &genKeyType, sizeof(genKeyType) }, |
| 198 | { CKA_TOKEN, &bFalse, sizeof(bFalse) }, |
| 199 | { CKA_PRIVATE, &bFalse, sizeof(bFalse) }, |
| 200 | { CKA_EXTRACTABLE, &bTrue, sizeof(bTrue) }, |
| 201 | { CKA_SENSITIVE, &bFalse, sizeof(bFalse) }, |
| 202 | { CKA_VALUE, data, sizeof(data) - 1 } |
| 203 | }; |
| 204 | CK_OBJECT_HANDLE hKey; |
| 205 | |
| 206 | hKey = CK_INVALID_HANDLE; |
| 207 | rv = CRYPTOKI_F_PTR( C_CreateObject(hSession, attribs, sizeof(attribs)/sizeof(CK_ATTRIBUTE), &hKey) ); |
| 208 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 209 | CPPUNIT_ASSERT(hKey != CK_INVALID_HANDLE); |
| 210 | |
| 211 | rv = CRYPTOKI_F_PTR( C_DigestKey(CK_INVALID_HANDLE, hKey) ); |
| 212 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 213 | |
| 214 | rv = CRYPTOKI_F_PTR( C_DigestKey(hSession, hKey) ); |
| 215 | CPPUNIT_ASSERT(rv == CKR_OPERATION_NOT_INITIALIZED); |
| 216 | |
| 217 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanism) ); |
| 218 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 219 | |
| 220 | rv = CRYPTOKI_F_PTR( C_DigestKey(hSession, CK_INVALID_HANDLE) ); |
| 221 | CPPUNIT_ASSERT(rv == CKR_KEY_HANDLE_INVALID); |
| 222 | |
| 223 | rv = CRYPTOKI_F_PTR( C_DigestKey(hSession, hKey) ); |
| 224 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 225 | } |
| 226 | |
| 227 | void DigestTests::testDigestFinal() |
| 228 | { |
| 229 | CK_RV rv; |
| 230 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; |
| 231 | CK_MECHANISM mechanism = { |
| 232 | CKM_SHA512, NULL_PTR, 0 |
| 233 | }; |
| 234 | CK_BYTE data[] = {"Text to digest"}; |
| 235 | CK_ULONG digestLen; |
| 236 | CK_BYTE_PTR digest; |
| 237 | |
| 238 | // Just make sure that we finalize any previous tests |
| 239 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 240 | |
| 241 | rv = CRYPTOKI_F_PTR( C_DigestFinal(hSession, NULL_PTR, &digestLen) ); |
| 242 | CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 243 | |
| 244 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 245 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 246 | |
| 247 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 248 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 249 | |
| 250 | rv = CRYPTOKI_F_PTR( C_DigestFinal(CK_INVALID_HANDLE, NULL_PTR, &digestLen) ); |
| 251 | CPPUNIT_ASSERT(rv == CKR_SESSION_HANDLE_INVALID); |
| 252 | |
| 253 | rv = CRYPTOKI_F_PTR( C_DigestFinal(hSession, NULL_PTR, &digestLen) ); |
| 254 | CPPUNIT_ASSERT(rv == CKR_OPERATION_NOT_INITIALIZED); |
| 255 | |
| 256 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanism) ); |
| 257 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 258 | |
| 259 | rv = CRYPTOKI_F_PTR( C_DigestUpdate(hSession, data, sizeof(data)-1) ); |
| 260 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 261 | |
| 262 | rv = CRYPTOKI_F_PTR( C_DigestFinal(hSession, NULL_PTR, NULL_PTR) ); |
| 263 | CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 264 | |
| 265 | rv = CRYPTOKI_F_PTR( C_DigestFinal(hSession, NULL_PTR, &digestLen) ); |
| 266 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 267 | |
| 268 | digest = (CK_BYTE_PTR)malloc(digestLen); |
| 269 | digestLen = 0; |
| 270 | |
| 271 | rv = CRYPTOKI_F_PTR( C_DigestFinal(hSession, digest, &digestLen) ); |
| 272 | CPPUNIT_ASSERT(rv == CKR_BUFFER_TOO_SMALL); |
| 273 | |
| 274 | rv = CRYPTOKI_F_PTR( C_DigestFinal(hSession, digest, &digestLen) ); |
| 275 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 276 | free(digest); |
| 277 | |
| 278 | rv = CRYPTOKI_F_PTR( C_DigestFinal(hSession, NULL_PTR, &digestLen) ); |
| 279 | CPPUNIT_ASSERT(rv == CKR_OPERATION_NOT_INITIALIZED); |
| 280 | } |
| 281 | |
| 282 | void DigestTests::testDigestAll() |
| 283 | { |
| 284 | CK_RV rv; |
| 285 | CK_SESSION_HANDLE hSession; |
| 286 | CK_MECHANISM mechanisms[] = { |
| 287 | #ifndef WITH_FIPS |
| 288 | { CKM_MD5, NULL_PTR, 0 }, |
| 289 | #endif |
| 290 | { CKM_SHA_1, NULL_PTR, 0 }, |
| 291 | { CKM_SHA224, NULL_PTR, 0 }, |
| 292 | { CKM_SHA256, NULL_PTR, 0 }, |
| 293 | { CKM_SHA384, NULL_PTR, 0 }, |
| 294 | { CKM_SHA512, NULL_PTR, 0 }, |
| 295 | #ifdef WITH_GOST |
| 296 | { CKM_GOSTR3411, NULL_PTR, 0 }, |
| 297 | #endif |
| 298 | }; |
| 299 | CK_ULONG digestLen; |
| 300 | CK_BYTE_PTR digest; |
| 301 | CK_BYTE data[] = {"Text to digest"}; |
| 302 | |
| 303 | // Just make sure that we finalize any previous tests |
| 304 | CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 305 | |
| 306 | rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 307 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 308 | |
| 309 | rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 310 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 311 | |
| 312 | for (unsigned int i = 0; i < sizeof(mechanisms)/sizeof(CK_MECHANISM); i++) |
| 313 | { |
| 314 | rv = CRYPTOKI_F_PTR( C_DigestInit(hSession, &mechanisms[i]) ); |
| 315 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 316 | |
| 317 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, NULL_PTR, &digestLen) ); |
| 318 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 319 | |
| 320 | digest = (CK_BYTE_PTR)malloc(digestLen); |
| 321 | |
| 322 | rv = CRYPTOKI_F_PTR( C_Digest(hSession, data, sizeof(data)-1, digest, &digestLen) ); |
| 323 | CPPUNIT_ASSERT(rv == CKR_OK); |
| 324 | free(digest); |
| 325 | } |
| 326 | } |