| 1 |
/* |
| 2 |
* Copyright (c) 2012 SURFnet |
| 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 |
SymmetricAlgorithmTests.cpp |
| 29 |
|
| 30 |
Contains test cases for symmetrical algorithms (i.e., AES and DES) |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
#include <climits> |
| 37 |
//#include <iomanip> |
| 38 |
#include "SymmetricAlgorithmTests.h" |
| 39 |
|
| 40 |
// CKA_TOKEN |
| 41 |
const CK_BBOOL ON_TOKEN = CK_TRUE; |
| 42 |
const CK_BBOOL IN_SESSION = CK_FALSE; |
| 43 |
|
| 44 |
// CKA_PRIVATE |
| 45 |
const CK_BBOOL IS_PRIVATE = CK_TRUE; |
| 46 |
const CK_BBOOL IS_PUBLIC = CK_FALSE; |
| 47 |
|
| 48 |
#define NR_OF_BLOCKS_IN_TEST 0x10001 |
| 49 |
|
| 50 |
CPPUNIT_TEST_SUITE_REGISTRATION(SymmetricAlgorithmTests); |
| 51 |
|
| 52 |
CK_RV SymmetricAlgorithmTests::generateGenericKey(CK_SESSION_HANDLE hSession, CK_BBOOL bToken, CK_BBOOL bPrivate, CK_OBJECT_HANDLE &hKey) |
| 53 |
{ |
| 54 |
CK_MECHANISM mechanism = { CKM_GENERIC_SECRET_KEY_GEN, NULL_PTR, 0 }; |
| 55 |
CK_ULONG bytes = 16; |
| 56 |
// CK_BBOOL bFalse = CK_FALSE; |
| 57 |
CK_BBOOL bTrue = CK_TRUE; |
| 58 |
CK_ATTRIBUTE keyAttribs[] = { |
| 59 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 60 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 61 |
{ CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, |
| 62 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 63 |
{ CKA_WRAP, &bTrue, sizeof(bTrue) }, |
| 64 |
{ CKA_UNWRAP, &bTrue, sizeof(bTrue) }, |
| 65 |
{ CKA_VALUE_LEN, &bytes, sizeof(bytes) }, |
| 66 |
}; |
| 67 |
|
| 68 |
hKey = CK_INVALID_HANDLE; |
| 69 |
return CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 70 |
keyAttribs, sizeof(keyAttribs)/sizeof(CK_ATTRIBUTE), |
| 71 |
&hKey) ); |
| 72 |
} |
| 73 |
|
| 74 |
CK_RV SymmetricAlgorithmTests::generateAesKey(CK_SESSION_HANDLE hSession, CK_BBOOL bToken, CK_BBOOL bPrivate, CK_OBJECT_HANDLE &hKey) |
| 75 |
{ |
| 76 |
CK_MECHANISM mechanism = { CKM_AES_KEY_GEN, NULL_PTR, 0 }; |
| 77 |
CK_ULONG bytes = 16; |
| 78 |
// CK_BBOOL bFalse = CK_FALSE; |
| 79 |
CK_BBOOL bTrue = CK_TRUE; |
| 80 |
CK_ATTRIBUTE keyAttribs[] = { |
| 81 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 82 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 83 |
{ CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, |
| 84 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 85 |
{ CKA_WRAP, &bTrue, sizeof(bTrue) }, |
| 86 |
{ CKA_UNWRAP, &bTrue, sizeof(bTrue) }, |
| 87 |
{ CKA_VALUE_LEN, &bytes, sizeof(bytes) }, |
| 88 |
}; |
| 89 |
|
| 90 |
hKey = CK_INVALID_HANDLE; |
| 91 |
return CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 92 |
keyAttribs, sizeof(keyAttribs)/sizeof(CK_ATTRIBUTE), |
| 93 |
&hKey) ); |
| 94 |
} |
| 95 |
|
| 96 |
#ifndef WITH_FIPS |
| 97 |
CK_RV SymmetricAlgorithmTests::generateDesKey(CK_SESSION_HANDLE hSession, CK_BBOOL bToken, CK_BBOOL bPrivate, CK_OBJECT_HANDLE &hKey) |
| 98 |
{ |
| 99 |
CK_MECHANISM mechanism = { CKM_DES_KEY_GEN, NULL_PTR, 0 }; |
| 100 |
// CK_BBOOL bFalse = CK_FALSE; |
| 101 |
CK_BBOOL bTrue = CK_TRUE; |
| 102 |
CK_ATTRIBUTE keyAttribs[] = { |
| 103 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 104 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 105 |
{ CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, |
| 106 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 107 |
}; |
| 108 |
|
| 109 |
hKey = CK_INVALID_HANDLE; |
| 110 |
return CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 111 |
keyAttribs, sizeof(keyAttribs)/sizeof(CK_ATTRIBUTE), |
| 112 |
&hKey) ); |
| 113 |
} |
| 114 |
|
| 115 |
CK_RV SymmetricAlgorithmTests::generateDes2Key(CK_SESSION_HANDLE hSession, CK_BBOOL bToken, CK_BBOOL bPrivate, CK_OBJECT_HANDLE &hKey) |
| 116 |
{ |
| 117 |
CK_MECHANISM mechanism = { CKM_DES2_KEY_GEN, NULL_PTR, 0 }; |
| 118 |
// CK_BBOOL bFalse = CK_FALSE; |
| 119 |
CK_BBOOL bTrue = CK_TRUE; |
| 120 |
CK_ATTRIBUTE keyAttribs[] = { |
| 121 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 122 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 123 |
{ CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, |
| 124 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 125 |
}; |
| 126 |
|
| 127 |
hKey = CK_INVALID_HANDLE; |
| 128 |
return CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 129 |
keyAttribs, sizeof(keyAttribs)/sizeof(CK_ATTRIBUTE), |
| 130 |
&hKey) ); |
| 131 |
} |
| 132 |
#endif |
| 133 |
|
| 134 |
CK_RV SymmetricAlgorithmTests::generateDes3Key(CK_SESSION_HANDLE hSession, CK_BBOOL bToken, CK_BBOOL bPrivate, CK_OBJECT_HANDLE &hKey) |
| 135 |
{ |
| 136 |
CK_MECHANISM mechanism = { CKM_DES3_KEY_GEN, NULL_PTR, 0 }; |
| 137 |
// CK_BBOOL bFalse = CK_FALSE; |
| 138 |
CK_BBOOL bTrue = CK_TRUE; |
| 139 |
CK_ATTRIBUTE keyAttribs[] = { |
| 140 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 141 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 142 |
{ CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, |
| 143 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 144 |
}; |
| 145 |
|
| 146 |
hKey = CK_INVALID_HANDLE; |
| 147 |
return CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 148 |
keyAttribs, sizeof(keyAttribs)/sizeof(CK_ATTRIBUTE), |
| 149 |
&hKey) ); |
| 150 |
} |
| 151 |
|
| 152 |
void SymmetricAlgorithmTests::encryptDecrypt( |
| 153 |
const CK_MECHANISM_TYPE mechanismType, |
| 154 |
const size_t blockSize, |
| 155 |
const CK_SESSION_HANDLE hSession, |
| 156 |
const CK_OBJECT_HANDLE hKey, |
| 157 |
const size_t messageSize, |
| 158 |
const bool isSizeOK) |
| 159 |
{ |
| 160 |
class PartSize {// class to get random size for part |
| 161 |
private: // we want to know for sure that no part length is causing any problem. |
| 162 |
const int blockSize; |
| 163 |
const unsigned* pRandom;// point to memory with random data. We are using the data to be encrypted. |
| 164 |
const unsigned* pBack;// point to memory where random data ends. |
| 165 |
int current;// the current size. |
| 166 |
public: |
| 167 |
PartSize( |
| 168 |
const int _blockSize, |
| 169 |
const std::vector<CK_BYTE>* pvData) : |
| 170 |
blockSize(_blockSize), |
| 171 |
pRandom((const unsigned*)&pvData->front()), |
| 172 |
pBack((const unsigned*)&pvData->back()), |
| 173 |
current(blockSize*4){}; |
| 174 |
int getCurrent() {// current part size |
| 175 |
return current; |
| 176 |
} |
| 177 |
int getNext() {// get next part size. |
| 178 |
// Check if we do not have more random data |
| 179 |
if ((pRandom+sizeof(unsigned)-1) > pBack) { |
| 180 |
current = blockSize*4; |
| 181 |
return current; |
| 182 |
} |
| 183 |
const unsigned random(*(pRandom++)); |
| 184 |
// Bit shift to handle 32- and 64-bit systems. |
| 185 |
// Just want a simple random part length, |
| 186 |
// not a perfect random number (bit shifting will |
| 187 |
// give some loss of precision). |
| 188 |
current = ((unsigned long)random >> 20)*blockSize*0x100/(UINT_MAX >> 20) + 1; |
| 189 |
//std::cout << "New random " << std::hex << random << " current " << std::hex << std::setfill('0') << std::setw(4) << current << " block size " << std::hex << blockSize << std::endl; |
| 190 |
return current; |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
const std::vector<CK_BYTE> vData(messageSize); |
| 195 |
std::vector<CK_BYTE> vEncryptedData; |
| 196 |
std::vector<CK_BYTE> vEncryptedDataParted; |
| 197 |
PartSize partSize(blockSize, &vData); |
| •198 |
CK_BBOOL oldMechs = CK_FALSE; |
| 199 |
CK_RV rv = CKR_OK; |
| 200 |
|
| 201 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_GenerateRandom(hSession, (CK_BYTE_PTR)&vData.front(), messageSize) ) ); |
| 202 |
|
| 203 |
const CK_MECHANISM mechanism = { mechanismType, NULL_PTR, 0 }; |
| 204 |
CK_MECHANISM_PTR pMechanism((CK_MECHANISM_PTR)&mechanism); |
| 205 |
CK_AES_CTR_PARAMS ctrParams = |
| 206 |
{ |
| 207 |
32, |
| 208 |
{ |
| 209 |
0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, |
| 210 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 |
| 211 |
} |
| 212 |
}; |
| 213 |
CK_BYTE gcmIV[] = { |
| 214 |
0xCA, 0xFE, 0xBA, 0xBE, 0xFA, 0xCE, |
| 215 |
0xDB, 0xAD, 0xDE, 0xCA, 0xF8, 0x88 |
| 216 |
}; |
| 217 |
CK_BYTE gcmAAD[] = { |
| 218 |
0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF, |
| 219 |
0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF, |
| 220 |
0xAB, 0xAD, 0xDA, 0xD2 |
| 221 |
}; |
| 222 |
CK_GCM_PARAMS gcmParams = |
| 223 |
{ |
| 224 |
&gcmIV[0], |
| 225 |
sizeof(gcmIV), |
| 226 |
sizeof(gcmIV)*8, |
| 227 |
&gcmAAD[0], |
| 228 |
sizeof(gcmAAD), |
| 229 |
16*8 |
| 230 |
}; |
| 231 |
|
| 232 |
switch (mechanismType) |
| 233 |
{ |
| 234 |
case CKM_DES_CBC: |
| 235 |
case CKM_DES_CBC_PAD: |
| 236 |
case CKM_DES3_CBC: |
| 237 |
case CKM_DES3_CBC_PAD: |
| •238 |
oldMechs = CK_TRUE; |
| 239 |
/* fall-through */ |
| 240 |
case CKM_AES_CBC: |
| 241 |
case CKM_AES_CBC_PAD: |
| 242 |
pMechanism->pParameter = (CK_VOID_PTR)&vData.front(); |
| 243 |
pMechanism->ulParameterLen = blockSize; |
| 244 |
break; |
| 245 |
case CKM_AES_CTR: |
| 246 |
pMechanism->pParameter = &ctrParams; |
| 247 |
pMechanism->ulParameterLen = sizeof(ctrParams); |
| 248 |
break; |
| 249 |
case CKM_AES_GCM: |
| 250 |
pMechanism->pParameter = &gcmParams; |
| 251 |
pMechanism->ulParameterLen = sizeof(gcmParams); |
| 252 |
break; |
| •253 |
case CKM_DES_ECB: |
| 254 |
case CKM_DES3_ECB: |
| 255 |
oldMechs = CK_TRUE; |
| 256 |
break; |
| 257 |
default: |
| 258 |
break; |
| 259 |
} |
| 260 |
|
| 261 |
// Single-part encryption |
| •262 |
rv = CRYPTOKI_F_PTR( C_EncryptInit(hSession,pMechanism,hKey) ); |
| 263 |
CPPUNIT_ASSERT_EQUAL( (CK_BBOOL) CK_FALSE, (CK_BBOOL) ((rv != CKR_OK) && (oldMechs == CK_FALSE)) ); |
| 264 |
if (oldMechs == CK_FALSE) |
| 265 |
{ |
| 266 |
CK_ULONG ulEncryptedDataLen; |
| 267 |
const CK_RV rv( CRYPTOKI_F_PTR( C_Encrypt(hSession,(CK_BYTE_PTR)&vData.front(),messageSize,NULL_PTR,&ulEncryptedDataLen) ) ); |
| 268 |
if ( isSizeOK ) { |
| 269 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 270 |
vEncryptedData.resize(ulEncryptedDataLen); |
| 271 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_Encrypt(hSession,(CK_BYTE_PTR)&vData.front(),messageSize,&vEncryptedData.front(),&ulEncryptedDataLen) ) ); |
| 272 |
vEncryptedData.resize(ulEncryptedDataLen); |
| 273 |
} else { |
| 274 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("C_Encrypt should fail with C_CKR_DATA_LEN_RANGE", (CK_RV)CKR_DATA_LEN_RANGE, rv); |
| 275 |
vEncryptedData = vData; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// Multi-part encryption |
| •280 |
rv = CRYPTOKI_F_PTR( C_EncryptInit(hSession,pMechanism,hKey) ); |
| 281 |
CPPUNIT_ASSERT_EQUAL( (CK_BBOOL) CK_FALSE, (CK_BBOOL) ((rv != CKR_OK) && (oldMechs == CK_FALSE)) ); |
| 282 |
if (oldMechs == CK_FALSE) { |
| 283 |
for ( std::vector<CK_BYTE>::const_iterator i(vData.begin()); i<vData.end(); i+=partSize.getCurrent() ) { |
| 284 |
const CK_ULONG lPartLen( i+partSize.getNext()<vData.end() ? partSize.getCurrent() : vData.end()-i ); |
| 285 |
CK_ULONG ulEncryptedPartLen; |
| 286 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_EncryptUpdate(hSession,(CK_BYTE_PTR)&(*i),lPartLen,NULL_PTR,&ulEncryptedPartLen) ) ); |
| 287 |
const size_t oldSize( vEncryptedDataParted.size() ); |
| •288 |
vEncryptedDataParted.resize(oldSize+ulEncryptedPartLen); |
| 289 |
CK_BYTE dummy; |
| •290 |
const CK_BYTE_PTR pEncryptedPart( ulEncryptedPartLen>0 ? &vEncryptedDataParted.at(oldSize) : &dummy ); |
| 291 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_EncryptUpdate(hSession,(CK_BYTE_PTR)&(*i),lPartLen,pEncryptedPart,&ulEncryptedPartLen) ) ); |
| 292 |
vEncryptedDataParted.resize(oldSize+ulEncryptedPartLen); |
| 293 |
} |
| 294 |
{ |
| 295 |
CK_ULONG ulLastEncryptedPartLen; |
| 296 |
const CK_RV rv( CRYPTOKI_F_PTR( C_EncryptFinal(hSession,NULL_PTR,&ulLastEncryptedPartLen) ) ); |
| 297 |
if ( isSizeOK ) { |
| 298 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 299 |
const size_t oldSize( vEncryptedDataParted.size() ); |
| 300 |
CK_BYTE dummy; |
| 301 |
vEncryptedDataParted.resize(oldSize+ulLastEncryptedPartLen); |
| 302 |
const CK_BYTE_PTR pLastEncryptedPart( ulLastEncryptedPartLen>0 ? &vEncryptedDataParted.at(oldSize) : &dummy ); |
| 303 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_EncryptFinal(hSession,pLastEncryptedPart,&ulLastEncryptedPartLen) ) ); |
| 304 |
vEncryptedDataParted.resize(oldSize+ulLastEncryptedPartLen); |
| 305 |
} else { |
| 306 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("C_EncryptFinal should fail with C_CKR_DATA_LEN_RANGE", (CK_RV)CKR_DATA_LEN_RANGE, rv); |
| 307 |
vEncryptedDataParted = vData; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// Single-part decryption |
| •313 |
rv = CRYPTOKI_F_PTR( C_DecryptInit(hSession,pMechanism,hKey) ); |
| 314 |
CPPUNIT_ASSERT_EQUAL( (CK_BBOOL) CK_FALSE, (CK_BBOOL) ((rv != CKR_OK) && (oldMechs == CK_FALSE)) ); |
| 315 |
if (oldMechs == CK_FALSE) { |
| 316 |
CK_ULONG ulDataLen; |
| 317 |
const CK_RV rv( CRYPTOKI_F_PTR( C_Decrypt(hSession,&vEncryptedData.front(),vEncryptedData.size(),NULL_PTR,&ulDataLen) ) ); |
| 318 |
if ( isSizeOK ) { |
| 319 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 320 |
std::vector<CK_BYTE> vDecryptedData(ulDataLen); |
| 321 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_Decrypt(hSession,&vEncryptedData.front(),vEncryptedData.size(),&vDecryptedData.front(),&ulDataLen) ) ); |
| 322 |
vDecryptedData.resize(ulDataLen); |
| 323 |
CPPUNIT_ASSERT_MESSAGE("C_Encrypt C_Decrypt does not give the original", vData==vDecryptedData); |
| 324 |
} else { |
| 325 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "C_Decrypt should fail with CKR_ENCRYPTED_DATA_LEN_RANGE", (CK_RV)CKR_ENCRYPTED_DATA_LEN_RANGE, rv ); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// Multi-part decryption |
| •330 |
rv = CRYPTOKI_F_PTR( C_DecryptInit(hSession,pMechanism,hKey) ); |
| 331 |
CPPUNIT_ASSERT_EQUAL( (CK_BBOOL) CK_FALSE, (CK_BBOOL) ((rv != CKR_OK) && (oldMechs == CK_FALSE)) ); |
| 332 |
if (oldMechs == CK_FALSE) { |
| 333 |
std::vector<CK_BYTE> vDecryptedData; |
| 334 |
CK_BYTE dummy; |
| 335 |
for ( std::vector<CK_BYTE>::iterator i(vEncryptedDataParted.begin()); i<vEncryptedDataParted.end(); i+=partSize.getCurrent()) { |
| 336 |
const CK_ULONG ulPartLen( i+partSize.getNext()<vEncryptedDataParted.end() ? partSize.getCurrent() : vEncryptedDataParted.end()-i ); |
| 337 |
CK_ULONG ulDecryptedPartLen; |
| 338 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_DecryptUpdate(hSession,&(*i),ulPartLen,NULL_PTR,&ulDecryptedPartLen) ) ); |
| 339 |
const size_t oldSize( vDecryptedData.size() ); |
| 340 |
vDecryptedData.resize(oldSize+ulDecryptedPartLen); |
| 341 |
const CK_BYTE_PTR pDecryptedPart( ulDecryptedPartLen>0 ? &vDecryptedData.at(oldSize) : &dummy ); |
| 342 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_DecryptUpdate(hSession,&(*i),ulPartLen,pDecryptedPart,&ulDecryptedPartLen) ) ); |
| 343 |
vDecryptedData.resize(oldSize+ulDecryptedPartLen); |
| 344 |
} |
| 345 |
{ |
| 346 |
CK_ULONG ulLastPartLen; |
| 347 |
const CK_RV rv( CRYPTOKI_F_PTR( C_DecryptFinal(hSession,NULL_PTR,&ulLastPartLen) ) ); |
| 348 |
if ( isSizeOK ) { |
| 349 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 350 |
const size_t oldSize( vDecryptedData.size() ); |
| 351 |
vDecryptedData.resize(oldSize+ulLastPartLen); |
| 352 |
const CK_BYTE_PTR pLastPart( ulLastPartLen>0 ? &vDecryptedData.at(oldSize) : &dummy ); |
| 353 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_DecryptFinal(hSession,pLastPart,&ulLastPartLen) ) ); |
| 354 |
vDecryptedData.resize(oldSize+ulLastPartLen); |
| 355 |
CPPUNIT_ASSERT_MESSAGE("C_EncryptUpdate/C_EncryptFinal C_DecryptUpdate/C_DecryptFinal does not give the original", vData==vDecryptedData); |
| 356 |
} else { |
| 357 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "C_EncryptFinal should fail with CKR_ENCRYPTED_DATA_LEN_RANGE", (CK_RV)CKR_ENCRYPTED_DATA_LEN_RANGE, rv ); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
CK_RV SymmetricAlgorithmTests::generateRsaPrivateKey(CK_SESSION_HANDLE hSession, CK_BBOOL bToken, CK_BBOOL bPrivate, CK_OBJECT_HANDLE &hKey) |
| 364 |
{ |
| 365 |
CK_MECHANISM mechanism = { CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0 }; |
| 366 |
CK_ULONG bits = 1536; |
| 367 |
CK_BYTE pubExp[] = {0x01, 0x00, 0x01}; |
| 368 |
CK_BYTE subject[] = { 0x12, 0x34 }; // dummy |
| 369 |
CK_BYTE id[] = { 123 } ; // dummy |
| 370 |
CK_BBOOL bFalse = CK_FALSE; |
| 371 |
CK_BBOOL bTrue = CK_TRUE; |
| 372 |
CK_ATTRIBUTE pubAttribs[] = { |
| 373 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 374 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 375 |
{ CKA_ENCRYPT, &bFalse, sizeof(bFalse) }, |
| 376 |
{ CKA_VERIFY, &bTrue, sizeof(bTrue) }, |
| 377 |
{ CKA_WRAP, &bFalse, sizeof(bFalse) }, |
| 378 |
{ CKA_MODULUS_BITS, &bits, sizeof(bits) }, |
| 379 |
{ CKA_PUBLIC_EXPONENT, &pubExp[0], sizeof(pubExp) } |
| 380 |
}; |
| 381 |
CK_ATTRIBUTE privAttribs[] = { |
| 382 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 383 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 384 |
{ CKA_SUBJECT, &subject[0], sizeof(subject) }, |
| 385 |
{ CKA_ID, &id[0], sizeof(id) }, |
| 386 |
{ CKA_SENSITIVE, &bTrue, sizeof(bTrue) }, |
| 387 |
{ CKA_DECRYPT, &bFalse, sizeof(bFalse) }, |
| 388 |
{ CKA_SIGN, &bTrue, sizeof(bTrue) }, |
| 389 |
{ CKA_UNWRAP, &bFalse, sizeof(bFalse) }, |
| 390 |
{ CKA_SENSITIVE, &bFalse, sizeof(bFalse) }, |
| 391 |
{ CKA_EXTRACTABLE, &bTrue, sizeof(bTrue) } |
| 392 |
}; |
| 393 |
|
| 394 |
CK_OBJECT_HANDLE hPub = CK_INVALID_HANDLE; |
| 395 |
hKey = CK_INVALID_HANDLE; |
| 396 |
CK_RV rv; |
| 397 |
rv = CRYPTOKI_F_PTR( C_GenerateKeyPair(hSession, &mechanism, |
| 398 |
pubAttribs, sizeof(pubAttribs)/sizeof(CK_ATTRIBUTE), |
| 399 |
privAttribs, sizeof(privAttribs)/sizeof(CK_ATTRIBUTE), |
| 400 |
&hPub, &hKey) ); |
| 401 |
if (hPub != CK_INVALID_HANDLE) |
| 402 |
{ |
| 403 |
CRYPTOKI_F_PTR( C_DestroyObject(hSession, hPub) ); |
| 404 |
} |
| 405 |
return rv; |
| 406 |
} |
| 407 |
|
| 408 |
#ifdef WITH_GOST |
| 409 |
CK_RV SymmetricAlgorithmTests::generateGostPrivateKey(CK_SESSION_HANDLE hSession, CK_BBOOL bToken, CK_BBOOL bPrivate, CK_OBJECT_HANDLE &hKey) |
| 410 |
{ |
| 411 |
CK_MECHANISM mechanism = { CKM_GOSTR3410_KEY_PAIR_GEN, NULL_PTR, 0 }; |
| 412 |
CK_BYTE param_a[] = { 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01 }; |
| 413 |
CK_BYTE param_b[] = { 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1e, 0x01 }; |
| 414 |
CK_BYTE subject[] = { 0x12, 0x34 }; // dummy |
| 415 |
CK_BYTE id[] = { 123 } ; // dummy |
| 416 |
CK_BBOOL bFalse = CK_FALSE; |
| 417 |
CK_BBOOL bTrue = CK_TRUE; |
| 418 |
CK_ATTRIBUTE pubAttribs[] = { |
| 419 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 420 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 421 |
{ CKA_ENCRYPT, &bFalse, sizeof(bFalse) }, |
| 422 |
{ CKA_VERIFY, &bTrue, sizeof(bTrue) }, |
| 423 |
{ CKA_WRAP, &bFalse, sizeof(bFalse) }, |
| 424 |
{ CKA_GOSTR3410_PARAMS, ¶m_a[0], sizeof(param_a) }, |
| 425 |
{ CKA_GOSTR3411_PARAMS, ¶m_b[0], sizeof(param_b) } |
| 426 |
}; |
| 427 |
CK_ATTRIBUTE privAttribs[] = { |
| 428 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 429 |
{ CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, |
| 430 |
{ CKA_SUBJECT, &subject[0], sizeof(subject) }, |
| 431 |
{ CKA_ID, &id[0], sizeof(id) }, |
| 432 |
{ CKA_SENSITIVE, &bTrue, sizeof(bTrue) }, |
| 433 |
{ CKA_DECRYPT, &bFalse, sizeof(bFalse) }, |
| 434 |
{ CKA_SIGN, &bTrue, sizeof(bTrue) }, |
| 435 |
{ CKA_UNWRAP, &bFalse, sizeof(bFalse) }, |
| 436 |
{ CKA_SENSITIVE, &bFalse, sizeof(bFalse) }, |
| 437 |
{ CKA_EXTRACTABLE, &bTrue, sizeof(bTrue) } |
| 438 |
}; |
| 439 |
|
| 440 |
CK_OBJECT_HANDLE hPub = CK_INVALID_HANDLE; |
| 441 |
hKey = CK_INVALID_HANDLE; |
| 442 |
CK_RV rv; |
| 443 |
rv = CRYPTOKI_F_PTR( C_GenerateKeyPair(hSession, &mechanism, |
| 444 |
pubAttribs, sizeof(pubAttribs)/sizeof(CK_ATTRIBUTE), |
| 445 |
privAttribs, sizeof(privAttribs)/sizeof(CK_ATTRIBUTE), |
| 446 |
&hPub, &hKey) ); |
| 447 |
if (hPub != CK_INVALID_HANDLE) |
| 448 |
{ |
| 449 |
CRYPTOKI_F_PTR( C_DestroyObject(hSession, hPub) ); |
| 450 |
} |
| 451 |
return rv; |
| 452 |
} |
| 453 |
#endif |
| 454 |
|
| 455 |
void SymmetricAlgorithmTests::aesWrapUnwrapGeneric(CK_MECHANISM_TYPE mechanismType, CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) |
| 456 |
{ |
| 457 |
CK_MECHANISM mechanism = { mechanismType, NULL_PTR, 0 }; |
| 458 |
CK_BBOOL bFalse = CK_FALSE; |
| 459 |
CK_BBOOL bTrue = CK_TRUE; |
| 460 |
CK_OBJECT_CLASS secretClass = CKO_SECRET_KEY; |
| 461 |
CK_KEY_TYPE genKeyType = CKK_GENERIC_SECRET; |
| 462 |
CK_BYTE keyPtr[128]; |
| 463 |
CK_ULONG keyLen = |
| 464 |
mechanismType == CKM_AES_KEY_WRAP_PAD ? 125UL : 128UL; |
| 465 |
CK_ATTRIBUTE attribs[] = { |
| 466 |
{ CKA_EXTRACTABLE, &bFalse, sizeof(bFalse) }, |
| 467 |
{ CKA_CLASS, &secretClass, sizeof(secretClass) }, |
| 468 |
{ CKA_KEY_TYPE, &genKeyType, sizeof(genKeyType) }, |
| 469 |
{ CKA_TOKEN, &bFalse, sizeof(bFalse) }, |
| 470 |
{ CKA_PRIVATE, &bTrue, sizeof(bTrue) }, |
| 471 |
{ CKA_SENSITIVE, &bTrue, sizeof(bTrue) }, // Wrapping is allowed even on sensitive objects |
| 472 |
{ CKA_VALUE, keyPtr, keyLen } |
| 473 |
}; |
| 474 |
CK_OBJECT_HANDLE hSecret; |
| 475 |
CK_RV rv; |
| 476 |
|
| 477 |
rv = CRYPTOKI_F_PTR( C_GenerateRandom(hSession, keyPtr, keyLen) ); |
| 478 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 479 |
|
| 480 |
hSecret = CK_INVALID_HANDLE; |
| 481 |
rv = CRYPTOKI_F_PTR( C_CreateObject(hSession, attribs, sizeof(attribs)/sizeof(CK_ATTRIBUTE), &hSecret) ); |
| 482 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 483 |
CPPUNIT_ASSERT(hSecret != CK_INVALID_HANDLE); |
| 484 |
|
| 485 |
CK_BYTE_PTR wrappedPtr = NULL_PTR; |
| 486 |
CK_ULONG wrappedLen = 0UL; |
| 487 |
CK_ULONG zero = 0UL; |
| 488 |
CK_ULONG rndKeyLen = keyLen; |
| 489 |
if (mechanismType == CKM_AES_KEY_WRAP_PAD) |
| 490 |
rndKeyLen = (keyLen + 7) & ~7; |
| 491 |
rv = CRYPTOKI_F_PTR( C_WrapKey(hSession, &mechanism, hKey, hSecret, wrappedPtr, &wrappedLen) ); |
| 492 |
CPPUNIT_ASSERT(rv == CKR_KEY_UNEXTRACTABLE); |
| 493 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hSecret) ); |
| 494 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 495 |
|
| 496 |
attribs[0].pValue = &bTrue; |
| 497 |
|
| 498 |
hSecret = CK_INVALID_HANDLE; |
| 499 |
rv = CRYPTOKI_F_PTR( C_CreateObject(hSession, attribs, sizeof(attribs)/sizeof(CK_ATTRIBUTE), &hSecret) ); |
| 500 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 501 |
CPPUNIT_ASSERT(hSecret != CK_INVALID_HANDLE); |
| 502 |
|
| 503 |
// Estimate wrapped length |
| 504 |
rv = CRYPTOKI_F_PTR( C_WrapKey(hSession, &mechanism, hKey, hSecret, wrappedPtr, &wrappedLen) ); |
| 505 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 506 |
CPPUNIT_ASSERT(wrappedLen == rndKeyLen + 8); |
| 507 |
|
| 508 |
wrappedPtr = (CK_BYTE_PTR) malloc(wrappedLen); |
| 509 |
CPPUNIT_ASSERT(wrappedPtr != NULL_PTR); |
| 510 |
rv = CRYPTOKI_F_PTR( C_WrapKey(hSession, &mechanism, hKey, hSecret, wrappedPtr, &wrappedLen) ); |
| 511 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 512 |
CPPUNIT_ASSERT(wrappedLen == rndKeyLen + 8); |
| 513 |
|
| 514 |
// This should always fail because wrapped data have to be longer than 0 bytes |
| 515 |
zero = 0; |
| 516 |
rv = CRYPTOKI_F_PTR( C_WrapKey(hSession, &mechanism, hKey, hSecret, wrappedPtr, &zero) ); |
| 517 |
CPPUNIT_ASSERT(rv == CKR_BUFFER_TOO_SMALL); |
| 518 |
|
| 519 |
CK_ATTRIBUTE nattribs[] = { |
| 520 |
{ CKA_CLASS, &secretClass, sizeof(secretClass) }, |
| 521 |
{ CKA_KEY_TYPE, &genKeyType, sizeof(genKeyType) }, |
| 522 |
{ CKA_TOKEN, &bFalse, sizeof(bFalse) }, |
| 523 |
{ CKA_PRIVATE, &bTrue, sizeof(bTrue) }, |
| 524 |
{ CKA_ENCRYPT, &bFalse, sizeof(bFalse) }, |
| 525 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 526 |
{ CKA_SIGN, &bFalse,sizeof(bFalse) }, |
| 527 |
{ CKA_VERIFY, &bTrue, sizeof(bTrue) } |
| 528 |
}; |
| 529 |
CK_OBJECT_HANDLE hNew; |
| 530 |
|
| 531 |
hNew = CK_INVALID_HANDLE; |
| 532 |
rv = CRYPTOKI_F_PTR( C_UnwrapKey(hSession, &mechanism, hKey, wrappedPtr, wrappedLen, nattribs, sizeof(nattribs)/sizeof(CK_ATTRIBUTE), &hNew) ); |
| 533 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 534 |
CPPUNIT_ASSERT(hNew != CK_INVALID_HANDLE); |
| 535 |
|
| 536 |
free(wrappedPtr); |
| 537 |
wrappedPtr = NULL_PTR; |
| 538 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hSecret) ); |
| 539 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 540 |
} |
| 541 |
|
| 542 |
void SymmetricAlgorithmTests::aesWrapUnwrapRsa(CK_MECHANISM_TYPE mechanismType, CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) |
| 543 |
{ |
| 544 |
CK_MECHANISM mechanism = { mechanismType, NULL_PTR, 0 }; |
| 545 |
CK_BBOOL bFalse = CK_FALSE; |
| 546 |
CK_BBOOL bTrue = CK_TRUE; |
| 547 |
CK_OBJECT_HANDLE hPrk = CK_INVALID_HANDLE; |
| 548 |
CK_RV rv = generateRsaPrivateKey(hSession, CK_TRUE, CK_TRUE, hPrk); |
| 549 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 550 |
CPPUNIT_ASSERT(hPrk != CK_INVALID_HANDLE); |
| 551 |
|
| 552 |
CK_OBJECT_CLASS privateClass = CKO_PRIVATE_KEY; |
| 553 |
CK_KEY_TYPE keyType = CKK_RSA; |
| 554 |
CK_BYTE_PTR prkAttrPtr = NULL_PTR; |
| 555 |
CK_ULONG prkAttrLen = 0UL; |
| 556 |
CK_ATTRIBUTE prkAttribs[] = { |
| 557 |
{ CKA_CLASS, &privateClass, sizeof(privateClass) }, |
| 558 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 559 |
{ CKA_PRIME_2, NULL_PTR, 0UL } |
| 560 |
}; |
| 561 |
|
| 562 |
rv = CRYPTOKI_F_PTR( C_GetAttributeValue(hSession, hPrk, prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE)) ); |
| 563 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 564 |
|
| 565 |
CPPUNIT_ASSERT(prkAttribs[0].ulValueLen == sizeof(CK_OBJECT_CLASS)); |
| 566 |
CPPUNIT_ASSERT(*(CK_OBJECT_CLASS*)prkAttribs[0].pValue == CKO_PRIVATE_KEY); |
| 567 |
CPPUNIT_ASSERT(prkAttribs[1].ulValueLen == sizeof(CK_KEY_TYPE)); |
| 568 |
CPPUNIT_ASSERT(*(CK_KEY_TYPE*)prkAttribs[1].pValue == CKK_RSA); |
| 569 |
|
| 570 |
prkAttrLen = prkAttribs[2].ulValueLen; |
| 571 |
prkAttrPtr = (CK_BYTE_PTR) malloc(2 * prkAttrLen); |
| 572 |
CPPUNIT_ASSERT(prkAttrPtr != NULL_PTR); |
| 573 |
prkAttribs[2].pValue = prkAttrPtr; |
| 574 |
prkAttribs[2].ulValueLen = prkAttrLen; |
| 575 |
|
| 576 |
rv = CRYPTOKI_F_PTR( C_GetAttributeValue(hSession, hPrk, prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE)) ); |
| 577 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 578 |
CPPUNIT_ASSERT(prkAttribs[2].ulValueLen == prkAttrLen); |
| 579 |
|
| 580 |
CK_BYTE_PTR wrappedPtr = NULL_PTR; |
| 581 |
CK_ULONG wrappedLen = 0UL; |
| 582 |
rv = CRYPTOKI_F_PTR( C_WrapKey(hSession, &mechanism, hKey, hPrk, wrappedPtr, &wrappedLen) ); |
| 583 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 584 |
wrappedPtr = (CK_BYTE_PTR) malloc(wrappedLen); |
| 585 |
CPPUNIT_ASSERT(wrappedPtr != NULL_PTR); |
| 586 |
rv = CRYPTOKI_F_PTR( C_WrapKey(hSession, &mechanism, hKey, hPrk, wrappedPtr, &wrappedLen) ); |
| 587 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 588 |
|
| 589 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hPrk) ); |
| 590 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 591 |
|
| 592 |
CK_ATTRIBUTE nPrkAttribs[] = { |
| 593 |
{ CKA_CLASS, &privateClass, sizeof(privateClass) }, |
| 594 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 595 |
{ CKA_TOKEN, &bFalse, sizeof(bFalse) }, |
| 596 |
{ CKA_PRIVATE, &bTrue, sizeof(bTrue) }, |
| 597 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 598 |
{ CKA_SIGN, &bFalse,sizeof(bFalse) }, |
| 599 |
{ CKA_UNWRAP, &bTrue, sizeof(bTrue) }, |
| 600 |
{ CKA_SENSITIVE, &bFalse, sizeof(bFalse) }, |
| 601 |
{ CKA_EXTRACTABLE, &bTrue, sizeof(bTrue) } |
| 602 |
}; |
| 603 |
|
| 604 |
hPrk = CK_INVALID_HANDLE; |
| 605 |
rv = CRYPTOKI_F_PTR( C_UnwrapKey(hSession, &mechanism, hKey, wrappedPtr, wrappedLen, nPrkAttribs, sizeof(nPrkAttribs)/sizeof(CK_ATTRIBUTE), &hPrk) ); |
| 606 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 607 |
CPPUNIT_ASSERT(hPrk != CK_INVALID_HANDLE); |
| 608 |
|
| 609 |
prkAttribs[2].pValue = prkAttrPtr + prkAttrLen; |
| 610 |
rv = CRYPTOKI_F_PTR( C_GetAttributeValue(hSession, hPrk, prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE)) ); |
| 611 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 612 |
|
| 613 |
CPPUNIT_ASSERT(prkAttribs[0].ulValueLen == sizeof(CK_OBJECT_CLASS)); |
| 614 |
CPPUNIT_ASSERT(*(CK_OBJECT_CLASS*)prkAttribs[0].pValue == CKO_PRIVATE_KEY); |
| 615 |
CPPUNIT_ASSERT(prkAttribs[1].ulValueLen == sizeof(CK_KEY_TYPE)); |
| 616 |
CPPUNIT_ASSERT(*(CK_KEY_TYPE*)prkAttribs[1].pValue == CKK_RSA); |
| 617 |
CPPUNIT_ASSERT(prkAttribs[2].ulValueLen == prkAttrLen); |
| 618 |
CPPUNIT_ASSERT(memcmp(prkAttrPtr, prkAttrPtr + prkAttrLen, prkAttrLen) == 0); |
| 619 |
|
| 620 |
free(wrappedPtr); |
| 621 |
free(prkAttrPtr); |
| 622 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hPrk) ); |
| 623 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 624 |
} |
| 625 |
|
| 626 |
#ifdef WITH_GOST |
| 627 |
void SymmetricAlgorithmTests::aesWrapUnwrapGost(CK_MECHANISM_TYPE mechanismType, CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) |
| 628 |
{ |
| 629 |
CK_MECHANISM mechanism = { mechanismType, NULL_PTR, 0 }; |
| 630 |
CK_BBOOL bFalse = CK_FALSE; |
| 631 |
CK_BBOOL bTrue = CK_TRUE; |
| 632 |
CK_OBJECT_HANDLE hPrk = CK_INVALID_HANDLE; |
| 633 |
CK_RV rv = generateGostPrivateKey(hSession, CK_TRUE, CK_TRUE, hPrk); |
| 634 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 635 |
CPPUNIT_ASSERT(hPrk != CK_INVALID_HANDLE); |
| 636 |
|
| 637 |
CK_OBJECT_CLASS privateClass = CKO_PRIVATE_KEY; |
| 638 |
CK_KEY_TYPE keyType = CKK_GOSTR3410; |
| 639 |
CK_BYTE_PTR prkAttrPtr = NULL_PTR; |
| 640 |
CK_ULONG prkAttrLen = 0UL; |
| 641 |
CK_ATTRIBUTE prkAttribs[] = { |
| 642 |
{ CKA_CLASS, &privateClass, sizeof(privateClass) }, |
| 643 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 644 |
{ CKA_VALUE, NULL_PTR, 0UL } |
| 645 |
}; |
| 646 |
|
| 647 |
rv = CRYPTOKI_F_PTR( C_GetAttributeValue(hSession, hPrk, prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE)) ); |
| 648 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 649 |
|
| 650 |
CPPUNIT_ASSERT(prkAttribs[0].ulValueLen == sizeof(CK_OBJECT_CLASS)); |
| 651 |
CPPUNIT_ASSERT(*(CK_OBJECT_CLASS*)prkAttribs[0].pValue == CKO_PRIVATE_KEY); |
| 652 |
CPPUNIT_ASSERT(prkAttribs[1].ulValueLen == sizeof(CK_KEY_TYPE)); |
| 653 |
CPPUNIT_ASSERT(*(CK_KEY_TYPE*)prkAttribs[1].pValue == CKK_GOSTR3410); |
| 654 |
|
| 655 |
prkAttrLen = prkAttribs[2].ulValueLen; |
| 656 |
prkAttrPtr = (CK_BYTE_PTR) malloc(2 * prkAttrLen); |
| 657 |
CPPUNIT_ASSERT(prkAttrPtr != NULL_PTR); |
| 658 |
prkAttribs[2].pValue = prkAttrPtr; |
| 659 |
prkAttribs[2].ulValueLen = prkAttrLen; |
| 660 |
|
| 661 |
rv = CRYPTOKI_F_PTR( C_GetAttributeValue(hSession, hPrk, prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE)) ); |
| 662 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 663 |
CPPUNIT_ASSERT(prkAttribs[2].ulValueLen == prkAttrLen); |
| 664 |
|
| 665 |
CK_BYTE_PTR wrappedPtr = NULL_PTR; |
| 666 |
CK_ULONG wrappedLen = 0UL; |
| 667 |
rv = CRYPTOKI_F_PTR( C_WrapKey(hSession, &mechanism, hKey, hPrk, wrappedPtr, &wrappedLen) ); |
| 668 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 669 |
wrappedPtr = (CK_BYTE_PTR) malloc(wrappedLen); |
| 670 |
CPPUNIT_ASSERT(wrappedPtr != NULL_PTR); |
| 671 |
rv = CRYPTOKI_F_PTR( C_WrapKey(hSession, &mechanism, hKey, hPrk, wrappedPtr, &wrappedLen) ); |
| 672 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 673 |
|
| 674 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hPrk) ); |
| 675 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 676 |
|
| 677 |
CK_ATTRIBUTE nPrkAttribs[] = { |
| 678 |
{ CKA_CLASS, &privateClass, sizeof(privateClass) }, |
| 679 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 680 |
{ CKA_TOKEN, &bFalse, sizeof(bFalse) }, |
| 681 |
{ CKA_PRIVATE, &bTrue, sizeof(bTrue) }, |
| 682 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 683 |
{ CKA_SIGN, &bFalse,sizeof(bFalse) }, |
| 684 |
{ CKA_UNWRAP, &bTrue, sizeof(bTrue) }, |
| 685 |
{ CKA_SENSITIVE, &bFalse, sizeof(bFalse) }, |
| 686 |
{ CKA_EXTRACTABLE, &bTrue, sizeof(bTrue) } |
| 687 |
}; |
| 688 |
|
| 689 |
hPrk = CK_INVALID_HANDLE; |
| 690 |
rv = CRYPTOKI_F_PTR( C_UnwrapKey(hSession, &mechanism, hKey, wrappedPtr, wrappedLen, nPrkAttribs, sizeof(nPrkAttribs)/sizeof(CK_ATTRIBUTE), &hPrk) ); |
| 691 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 692 |
CPPUNIT_ASSERT(hPrk != CK_INVALID_HANDLE); |
| 693 |
|
| 694 |
prkAttribs[2].pValue = prkAttrPtr + prkAttrLen; |
| 695 |
rv = CRYPTOKI_F_PTR( C_GetAttributeValue(hSession, hPrk, prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE)) ); |
| 696 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 697 |
|
| 698 |
CPPUNIT_ASSERT(prkAttribs[0].ulValueLen == sizeof(CK_OBJECT_CLASS)); |
| 699 |
CPPUNIT_ASSERT(*(CK_OBJECT_CLASS*)prkAttribs[0].pValue == CKO_PRIVATE_KEY); |
| 700 |
CPPUNIT_ASSERT(prkAttribs[1].ulValueLen == sizeof(CK_KEY_TYPE)); |
| 701 |
CPPUNIT_ASSERT(*(CK_KEY_TYPE*)prkAttribs[1].pValue == CKK_GOSTR3410); |
| 702 |
CPPUNIT_ASSERT(prkAttribs[2].ulValueLen == prkAttrLen); |
| 703 |
CPPUNIT_ASSERT(memcmp(prkAttrPtr, prkAttrPtr + prkAttrLen, prkAttrLen) == 0); |
| 704 |
|
| 705 |
free(wrappedPtr); |
| 706 |
free(prkAttrPtr); |
| 707 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hPrk) ); |
| 708 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 709 |
} |
| 710 |
#endif |
| 711 |
|
| 712 |
void SymmetricAlgorithmTests::testAesEncryptDecrypt() |
| 713 |
{ |
| 714 |
CK_RV rv; |
| 715 |
// CK_UTF8CHAR sopin[] = SLOT_0_SO1_PIN; |
| 716 |
// CK_ULONG sopinLength = sizeof(sopin) - 1; |
| 717 |
CK_SESSION_HANDLE hSessionRO; |
| 718 |
CK_SESSION_HANDLE hSessionRW; |
| 719 |
|
| 720 |
// Just make sure that we finalize any previous tests |
| 721 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 722 |
|
| 723 |
// Open read-only session on when the token is not initialized should fail |
| 724 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSessionRO) ); |
| 725 |
CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 726 |
|
| 727 |
// Initialize the library and start the test. |
| 728 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 729 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 730 |
|
| 731 |
// Open read-only session |
| 732 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSessionRO) ); |
| 733 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 734 |
|
| 735 |
// Open read-write session |
| 736 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSessionRW) ); |
| 737 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 738 |
|
| 739 |
// Login USER into the sessions so we can create a private objects |
| 740 |
rv = CRYPTOKI_F_PTR( C_Login(hSessionRO,CKU_USER,m_userPin1,m_userPin1Length) ); |
| 741 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 742 |
|
| 743 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 744 |
|
| 745 |
// Generate all combinations of session/token keys. |
| 746 |
rv = generateAesKey(hSessionRW,IN_SESSION,IS_PUBLIC,hKey); |
| 747 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 748 |
|
| 749 |
// AES allways have the block size of 128 bits (0x80 bits 0x10 bytes). |
| 750 |
// with padding all message sizes could be encrypted-decrypted. |
| 751 |
// without padding the message size must be a multiple of the block size. |
| 752 |
const int blockSize(0x10); |
| 753 |
encryptDecrypt(CKM_AES_CBC_PAD,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST-1); |
| 754 |
encryptDecrypt(CKM_AES_CBC_PAD,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST+1); |
| 755 |
encryptDecrypt(CKM_AES_CBC_PAD,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 756 |
encryptDecrypt(CKM_AES_CBC,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 757 |
encryptDecrypt(CKM_AES_CBC,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST+1, false); |
| 758 |
encryptDecrypt(CKM_AES_ECB,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 759 |
encryptDecrypt(CKM_AES_ECB,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST+1, false); |
| 760 |
encryptDecrypt(CKM_AES_CTR,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST-1); |
| 761 |
encryptDecrypt(CKM_AES_CTR,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST+1); |
| 762 |
encryptDecrypt(CKM_AES_CTR,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 763 |
encryptDecrypt(CKM_AES_GCM,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST-1); |
| 764 |
encryptDecrypt(CKM_AES_GCM,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST+1); |
| 765 |
encryptDecrypt(CKM_AES_GCM,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 766 |
} |
| 767 |
|
| 768 |
void SymmetricAlgorithmTests::testAesWrapUnwrap() |
| 769 |
{ |
| 770 |
CK_RV rv; |
| 771 |
// CK_UTF8CHAR sopin[] = SLOT_0_SO1_PIN; |
| 772 |
// CK_ULONG sopinLength = sizeof(sopin) - 1; |
| 773 |
CK_SESSION_HANDLE hSession; |
| 774 |
|
| 775 |
// Just make sure that we finalize any previous tests |
| 776 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 777 |
|
| 778 |
// Initialize the library and start the test. |
| 779 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 780 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 781 |
|
| 782 |
// Open session |
| 783 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 784 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 785 |
|
| 786 |
// Login USER into the session so we can create a private object |
| 787 |
rv = CRYPTOKI_F_PTR( C_Login(hSession,CKU_USER,m_userPin1,m_userPin1Length) ); |
| 788 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 789 |
|
| 790 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 791 |
|
| 792 |
// Generate a wrapping session public key |
| 793 |
rv = generateAesKey(hSession,IN_SESSION,IS_PUBLIC,hKey); |
| 794 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 795 |
|
| 796 |
aesWrapUnwrapGeneric(CKM_AES_KEY_WRAP, hSession, hKey); |
| 797 |
aesWrapUnwrapRsa(CKM_AES_KEY_WRAP, hSession, hKey); |
| 798 |
#ifdef WITH_GOST |
| 799 |
aesWrapUnwrapGost(CKM_AES_KEY_WRAP, hSession, hKey); |
| 800 |
#endif |
| 801 |
|
| 802 |
#ifdef HAVE_AES_KEY_WRAP_PAD |
| 803 |
aesWrapUnwrapGeneric(CKM_AES_KEY_WRAP_PAD, hSession, hKey); |
| 804 |
aesWrapUnwrapRsa(CKM_AES_KEY_WRAP_PAD, hSession, hKey); |
| 805 |
#ifdef WITH_GOST |
| 806 |
aesWrapUnwrapGost(CKM_AES_KEY_WRAP_PAD, hSession, hKey); |
| 807 |
#endif |
| 808 |
#endif |
| 809 |
} |
| 810 |
|
| 811 |
void SymmetricAlgorithmTests::testDesEncryptDecrypt() |
| 812 |
{ |
| 813 |
CK_RV rv; |
| 814 |
// CK_UTF8CHAR sopin[] = SLOT_0_SO1_PIN; |
| 815 |
// CK_ULONG sopinLength = sizeof(sopin) - 1; |
| 816 |
CK_SESSION_HANDLE hSessionRO; |
| 817 |
CK_SESSION_HANDLE hSessionRW; |
| 818 |
|
| 819 |
// Just make sure that we finalize any previous tests |
| 820 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 821 |
|
| 822 |
// Open read-only session on when the token is not initialized should fail |
| 823 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSessionRO) ); |
| 824 |
CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_NOT_INITIALIZED); |
| 825 |
|
| 826 |
// Initialize the library and start the test. |
| 827 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 828 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 829 |
|
| 830 |
// Open read-only session |
| 831 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSessionRO) ); |
| 832 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 833 |
|
| 834 |
// Open read-write session |
| 835 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSessionRW) ); |
| 836 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 837 |
|
| 838 |
// Login USER into the sessions so we can create a private objects |
| 839 |
rv = CRYPTOKI_F_PTR( C_Login(hSessionRO,CKU_USER,m_userPin1,m_userPin1Length) ); |
| 840 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 841 |
|
| 842 |
// 3DES and DES always have the block size of 64 bits (0x40 bits 0x8 bytes). |
| 843 |
// with padding all message sizes could be encrypted-decrypted. |
| 844 |
// without padding the message size must be a multiple of the block size. |
| 845 |
const int blockSize(0x8); |
| 846 |
|
| 847 |
#ifndef WITH_FIPS |
| 848 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 849 |
|
| 850 |
// Generate all combinations of session/token keys. |
| 851 |
rv = generateDesKey(hSessionRW,IN_SESSION,IS_PUBLIC,hKey); |
| •852 |
if (rv == CKR_OK) { |
| 853 |
encryptDecrypt(CKM_DES_CBC_PAD,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST-1); |
| 854 |
encryptDecrypt(CKM_DES_CBC_PAD,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST+1); |
| 855 |
encryptDecrypt(CKM_DES_CBC_PAD,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 856 |
encryptDecrypt(CKM_DES_CBC,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 857 |
encryptDecrypt(CKM_DES_CBC,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST+1, false); |
| 858 |
encryptDecrypt(CKM_DES_ECB,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 859 |
encryptDecrypt(CKM_DES_ECB,blockSize,hSessionRO,hKey,blockSize*NR_OF_BLOCKS_IN_TEST+1, false); |
| 860 |
} |
| 861 |
|
| 862 |
CK_OBJECT_HANDLE hKey2 = CK_INVALID_HANDLE; |
| 863 |
|
| 864 |
// Generate all combinations of session/token keys. |
| 865 |
rv = generateDes2Key(hSessionRW,IN_SESSION,IS_PUBLIC,hKey2); |
| •866 |
if (rv == CKR_OK) { |
| 867 |
encryptDecrypt(CKM_DES3_CBC_PAD,blockSize,hSessionRO,hKey2,blockSize*NR_OF_BLOCKS_IN_TEST-1); |
| 868 |
encryptDecrypt(CKM_DES3_CBC_PAD,blockSize,hSessionRO,hKey2,blockSize*NR_OF_BLOCKS_IN_TEST+1); |
| 869 |
encryptDecrypt(CKM_DES3_CBC_PAD,blockSize,hSessionRO,hKey2,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 870 |
encryptDecrypt(CKM_DES3_CBC,blockSize,hSessionRO,hKey2,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 871 |
encryptDecrypt(CKM_DES3_CBC,blockSize,hSessionRO,hKey2,blockSize*NR_OF_BLOCKS_IN_TEST+1, false); |
| 872 |
encryptDecrypt(CKM_DES3_ECB,blockSize,hSessionRO,hKey2,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 873 |
encryptDecrypt(CKM_DES3_ECB,blockSize,hSessionRO,hKey2,blockSize*NR_OF_BLOCKS_IN_TEST+1, false); |
| 874 |
} |
| 875 |
#endif |
| 876 |
|
| 877 |
CK_OBJECT_HANDLE hKey3 = CK_INVALID_HANDLE; |
| 878 |
|
| 879 |
// Generate all combinations of session/token keys. |
| 880 |
rv = generateDes3Key(hSessionRW,IN_SESSION,IS_PUBLIC,hKey3); |
| •881 |
if (rv == CKR_OK) { |
| 882 |
encryptDecrypt(CKM_DES3_CBC_PAD,blockSize,hSessionRO,hKey3,blockSize*NR_OF_BLOCKS_IN_TEST-1); |
| 883 |
encryptDecrypt(CKM_DES3_CBC_PAD,blockSize,hSessionRO,hKey3,blockSize*NR_OF_BLOCKS_IN_TEST+1); |
| 884 |
encryptDecrypt(CKM_DES3_CBC_PAD,blockSize,hSessionRO,hKey3,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 885 |
encryptDecrypt(CKM_DES3_CBC,blockSize,hSessionRO,hKey3,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 886 |
encryptDecrypt(CKM_DES3_CBC,blockSize,hSessionRO,hKey3,blockSize*NR_OF_BLOCKS_IN_TEST+1, false); |
| 887 |
encryptDecrypt(CKM_DES3_ECB,blockSize,hSessionRO,hKey3,blockSize*NR_OF_BLOCKS_IN_TEST); |
| 888 |
encryptDecrypt(CKM_DES3_ECB,blockSize,hSessionRO,hKey3,blockSize*NR_OF_BLOCKS_IN_TEST+1, false); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
void SymmetricAlgorithmTests::testNullTemplate() |
| 893 |
{ |
| 894 |
CK_RV rv; |
| 895 |
CK_SESSION_HANDLE hSession; |
| 896 |
CK_MECHANISM mechanism1 = { CKM_DES3_KEY_GEN, NULL_PTR, 0 }; |
| 897 |
CK_MECHANISM mechanism2 = { CKM_AES_KEY_GEN, NULL_PTR, 0 }; |
| 898 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 899 |
|
| 900 |
// Just make sure that we finalize any previous tests |
| 901 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 902 |
|
| 903 |
// Initialize the library and start the test. |
| 904 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 905 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 906 |
|
| 907 |
// Open read-write session |
| 908 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 909 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 910 |
|
| 911 |
// Login USER into the sessions so we can create a private objects |
| 912 |
rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_USER, m_userPin1, m_userPin1Length) ); |
| 913 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 914 |
|
| 915 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism1, NULL_PTR, 0, &hKey) ); |
| 916 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 917 |
|
| 918 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism1, NULL_PTR, 1, &hKey) ); |
| 919 |
CPPUNIT_ASSERT(rv == CKR_ARGUMENTS_BAD); |
| 920 |
|
| 921 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hKey) ); |
| 922 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 923 |
|
| 924 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism2, NULL_PTR, 0, &hKey) ); |
| 925 |
CPPUNIT_ASSERT(rv == CKR_TEMPLATE_INCOMPLETE); |
| 926 |
} |
| 927 |
|
| 928 |
void SymmetricAlgorithmTests::testNonModifiableDesKeyGeneration() |
| 929 |
{ |
| 930 |
CK_RV rv; |
| 931 |
CK_SESSION_HANDLE hSession; |
| 932 |
CK_MECHANISM mechanism = { CKM_DES3_KEY_GEN, NULL_PTR, 0 }; |
| 933 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 934 |
CK_BBOOL bFalse = CK_FALSE; |
| 935 |
CK_BBOOL bTrue = CK_TRUE; |
| 936 |
CK_BBOOL bToken = IN_SESSION; |
| 937 |
|
| 938 |
CK_ATTRIBUTE keyAttribs[] = |
| 939 |
{ |
| 940 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 941 |
{ CKA_PRIVATE, &bTrue, sizeof(bTrue) }, |
| 942 |
{ CKA_MODIFIABLE, &bTrue, sizeof(bTrue) }, |
| 943 |
{ CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, |
| 944 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 945 |
{ CKA_WRAP, &bTrue, sizeof(bTrue) } |
| 946 |
}; |
| 947 |
|
| 948 |
// Just make sure that we finalize any previous tests |
| 949 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 950 |
|
| 951 |
// Initialize the library and start the test. |
| 952 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 953 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 954 |
|
| 955 |
// Open read-write session |
| 956 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 957 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 958 |
|
| 959 |
// Login USER into the sessions so we can create a private objects |
| 960 |
rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_USER, m_userPin1, m_userPin1Length) ); |
| 961 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 962 |
|
| 963 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 964 |
keyAttribs, sizeof(keyAttribs)/sizeof(CK_ATTRIBUTE), |
| 965 |
&hKey) ); |
| 966 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 967 |
|
| 968 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hKey) ); |
| 969 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 970 |
|
| 971 |
// The C_GenerateKey call failed if CKA_MODIFIABLE was bFalse |
| 972 |
// This was a bug in the SoftHSM implementation |
| 973 |
keyAttribs[2].pValue = &bFalse; |
| 974 |
keyAttribs[2].ulValueLen = sizeof(bFalse); |
| 975 |
|
| 976 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 977 |
keyAttribs, sizeof(keyAttribs) / sizeof(CK_ATTRIBUTE), |
| 978 |
&hKey) ); |
| 979 |
// The call would fail with CKR_ATTRIBUTE_READ_ONLY |
| 980 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 981 |
|
| 982 |
// Now create a template where the CKA_MODIFIABLE attribute is last in the list |
| 983 |
CK_ATTRIBUTE keyAttribs1[] = |
| 984 |
{ |
| 985 |
{ CKA_TOKEN, &bToken, sizeof(bToken) }, |
| 986 |
{ CKA_PRIVATE, &bTrue, sizeof(bTrue) }, |
| 987 |
{ CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, |
| 988 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 989 |
{ CKA_WRAP, &bTrue, sizeof(bTrue) }, |
| 990 |
{ CKA_MODIFIABLE, &bTrue, sizeof(bTrue) } |
| 991 |
}; |
| 992 |
|
| 993 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 994 |
keyAttribs1, sizeof(keyAttribs1) / sizeof(CK_ATTRIBUTE), |
| 995 |
&hKey) ); |
| 996 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 997 |
|
| 998 |
// Now when CKA_MODIFIABLE is bFalse the key generation succeeds |
| 999 |
keyAttribs1[2].pValue = &bFalse; |
| 1000 |
keyAttribs1[2].ulValueLen = sizeof(bFalse); |
| 1001 |
|
| 1002 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 1003 |
keyAttribs1, sizeof(keyAttribs1) / sizeof(CK_ATTRIBUTE), |
| 1004 |
&hKey) ); |
| 1005 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1006 |
} |
| 1007 |
|
| 1008 |
void SymmetricAlgorithmTests::testCheckValue() |
| 1009 |
{ |
| 1010 |
CK_RV rv; |
| 1011 |
CK_SESSION_HANDLE hSession; |
| 1012 |
CK_MECHANISM mechanism = { CKM_AES_KEY_GEN, NULL_PTR, 0 }; |
| 1013 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 1014 |
|
| 1015 |
// Just make sure that we finalize any previous tests |
| 1016 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 1017 |
|
| 1018 |
// Initialize the library and start the test. |
| 1019 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 1020 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1021 |
|
| 1022 |
// Open read-write session |
| 1023 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 1024 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1025 |
|
| 1026 |
// Login USER into the sessions so we can create a private objects |
| 1027 |
rv = CRYPTOKI_F_PTR( C_Login(hSession, CKU_USER, m_userPin1, m_userPin1Length) ); |
| 1028 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 1029 |
|
| 1030 |
CK_ULONG bytes = 16; |
| 1031 |
CK_BYTE pCheckValue[] = { 0x2b, 0x84, 0xf6 }; |
| 1032 |
CK_BBOOL bFalse = CK_FALSE; |
| 1033 |
CK_BBOOL bTrue = CK_TRUE; |
| 1034 |
CK_ATTRIBUTE keyAttribs[] = { |
| 1035 |
{ CKA_TOKEN, &bFalse, sizeof(bFalse) }, |
| 1036 |
{ CKA_PRIVATE, &bTrue, sizeof(bTrue) }, |
| 1037 |
{ CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, |
| 1038 |
{ CKA_DECRYPT, &bTrue, sizeof(bTrue) }, |
| 1039 |
{ CKA_WRAP, &bTrue, sizeof(bTrue) }, |
| 1040 |
{ CKA_UNWRAP, &bTrue, sizeof(bTrue) }, |
| 1041 |
{ CKA_VALUE_LEN, &bytes, sizeof(bytes) }, |
| 1042 |
{ CKA_CHECK_VALUE, &pCheckValue, sizeof(pCheckValue) } |
| 1043 |
}; |
| 1044 |
|
| 1045 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 1046 |
keyAttribs, 8, |
| 1047 |
&hKey) ); |
| 1048 |
CPPUNIT_ASSERT(rv == CKR_ATTRIBUTE_VALUE_INVALID); |
| 1049 |
|
| 1050 |
keyAttribs[7].ulValueLen = 0; |
| 1051 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 1052 |
keyAttribs, 8, |
| 1053 |
&hKey) ); |
| 1054 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1055 |
|
| 1056 |
CK_ATTRIBUTE checkAttrib[] = { |
| 1057 |
{ CKA_CHECK_VALUE, &pCheckValue, sizeof(pCheckValue) } |
| 1058 |
}; |
| 1059 |
|
| 1060 |
rv = CRYPTOKI_F_PTR( C_GetAttributeValue(hSession, hKey, checkAttrib, 1) ); |
| 1061 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1062 |
CPPUNIT_ASSERT(checkAttrib[0].ulValueLen == 0); |
| 1063 |
|
| 1064 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hKey) ); |
| 1065 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1066 |
|
| 1067 |
rv = CRYPTOKI_F_PTR( C_GenerateKey(hSession, &mechanism, |
| 1068 |
keyAttribs, 7, |
| 1069 |
&hKey) ); |
| 1070 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1071 |
|
| 1072 |
checkAttrib[0].ulValueLen = sizeof(pCheckValue); |
| 1073 |
rv = CRYPTOKI_F_PTR( C_GetAttributeValue(hSession, hKey, checkAttrib, 1) ); |
| 1074 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1075 |
CPPUNIT_ASSERT(checkAttrib[0].ulValueLen == 3); |
| 1076 |
|
| 1077 |
rv = CRYPTOKI_F_PTR( C_DestroyObject(hSession, hKey) ); |
| 1078 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1079 |
} |
| 1080 |
|
| 1081 |
void SymmetricAlgorithmTests::testAesCtrOverflow() |
| 1082 |
{ |
| 1083 |
CK_RV rv; |
| 1084 |
CK_SESSION_HANDLE hSession; |
| 1085 |
|
| 1086 |
// Just make sure that we finalize any previous tests |
| 1087 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 1088 |
|
| 1089 |
// Initialize the library and start the test. |
| 1090 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 1091 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1092 |
|
| 1093 |
// Open read-write session |
| 1094 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 1095 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1096 |
|
| 1097 |
// Login USER into the session so we can create a private objects |
| 1098 |
rv = CRYPTOKI_F_PTR( C_Login(hSession,CKU_USER,m_userPin1,m_userPin1Length) ); |
| 1099 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 1100 |
|
| 1101 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 1102 |
|
| 1103 |
// Generate a session keys. |
| 1104 |
rv = generateAesKey(hSession,IN_SESSION,IS_PUBLIC,hKey); |
| 1105 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1106 |
|
| 1107 |
CK_MECHANISM mechanism = { CKM_AES_CTR, NULL_PTR, 0 }; |
| 1108 |
CK_AES_CTR_PARAMS ctrParams = |
| 1109 |
{ |
| 1110 |
2, |
| 1111 |
{ |
| 1112 |
0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, |
| 1113 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 |
| 1114 |
} |
| 1115 |
}; |
| 1116 |
mechanism.pParameter = &ctrParams; |
| 1117 |
mechanism.ulParameterLen = sizeof(ctrParams); |
| 1118 |
|
| 1119 |
CK_BYTE plainText[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1120 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1121 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1122 |
0x00 }; |
| 1123 |
std::vector<CK_BYTE> vEncryptedData; |
| 1124 |
std::vector<CK_BYTE> vEncryptedDataParted; |
| 1125 |
std::vector<CK_BYTE> vDecryptedData; |
| 1126 |
std::vector<CK_BYTE> vDecryptedDataParted; |
| 1127 |
CK_ULONG ulEncryptedDataLen; |
| 1128 |
CK_ULONG ulEncryptedPartLen; |
| 1129 |
CK_ULONG ulDataLen; |
| 1130 |
CK_ULONG ulDataPartLen; |
| 1131 |
|
| 1132 |
// Single-part encryption |
| 1133 |
rv = CRYPTOKI_F_PTR( C_EncryptInit(hSession,&mechanism,hKey) ); |
| 1134 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1135 |
rv = CRYPTOKI_F_PTR( C_Encrypt(hSession,plainText,sizeof(plainText),NULL_PTR,&ulEncryptedDataLen) ); |
| 1136 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_DATA_LEN_RANGE, rv ); |
| 1137 |
rv = CRYPTOKI_F_PTR( C_EncryptInit(hSession,&mechanism,hKey) ); |
| 1138 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1139 |
rv = CRYPTOKI_F_PTR( C_Encrypt(hSession,plainText,sizeof(plainText)-1,NULL_PTR,&ulEncryptedDataLen) ); |
| 1140 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1141 |
vEncryptedData.resize(ulEncryptedDataLen); |
| 1142 |
rv = CRYPTOKI_F_PTR( C_Encrypt(hSession,plainText,sizeof(plainText)-1,&vEncryptedData.front(),&ulEncryptedDataLen) ); |
| 1143 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1144 |
vEncryptedData.resize(ulEncryptedDataLen); |
| 1145 |
|
| 1146 |
// Multi-part encryption |
| 1147 |
rv = CRYPTOKI_F_PTR( C_EncryptInit(hSession,&mechanism,hKey) ); |
| 1148 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1149 |
rv = CRYPTOKI_F_PTR( C_EncryptUpdate(hSession,plainText,sizeof(plainText)-1,NULL_PTR,&ulEncryptedPartLen) ); |
| 1150 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1151 |
vEncryptedDataParted.resize(ulEncryptedPartLen); |
| 1152 |
rv = CRYPTOKI_F_PTR( C_EncryptUpdate(hSession,plainText,sizeof(plainText)-1,&vEncryptedDataParted.front(),&ulEncryptedPartLen) ); |
| 1153 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1154 |
vEncryptedDataParted.resize(ulEncryptedPartLen); |
| 1155 |
rv = CRYPTOKI_F_PTR( C_EncryptUpdate(hSession,plainText,1,NULL_PTR,&ulEncryptedPartLen) ); |
| 1156 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_DATA_LEN_RANGE, rv ); |
| 1157 |
|
| 1158 |
// Single-part decryption |
| 1159 |
rv = CRYPTOKI_F_PTR( C_DecryptInit(hSession,&mechanism,hKey) ); |
| 1160 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1161 |
rv = CRYPTOKI_F_PTR( C_Decrypt(hSession,&vEncryptedData.front(),vEncryptedData.size()+1,NULL_PTR,&ulDataLen) ); |
| 1162 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_ENCRYPTED_DATA_LEN_RANGE, rv ); |
| 1163 |
rv = CRYPTOKI_F_PTR( C_DecryptInit(hSession,&mechanism,hKey) ); |
| 1164 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1165 |
rv = CRYPTOKI_F_PTR( C_Decrypt(hSession,&vEncryptedData.front(),vEncryptedData.size(),NULL_PTR,&ulDataLen) ); |
| 1166 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1167 |
vDecryptedData.resize(ulDataLen); |
| 1168 |
rv = CRYPTOKI_F_PTR( C_Decrypt(hSession,&vEncryptedData.front(),vEncryptedData.size(),&vDecryptedData.front(),&ulDataLen) ); |
| 1169 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1170 |
vDecryptedData.resize(ulDataLen); |
| 1171 |
|
| 1172 |
// Multi-part decryption |
| 1173 |
rv = CRYPTOKI_F_PTR( C_DecryptInit(hSession,&mechanism,hKey) ); |
| 1174 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1175 |
rv = CRYPTOKI_F_PTR( C_DecryptUpdate(hSession,&vEncryptedData.front(),vEncryptedData.size(),NULL_PTR,&ulDataPartLen) ); |
| 1176 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1177 |
vDecryptedDataParted.resize(ulDataPartLen); |
| 1178 |
rv = CRYPTOKI_F_PTR( C_DecryptUpdate(hSession,&vEncryptedData.front(),vEncryptedData.size(),&vDecryptedDataParted.front(),&ulDataPartLen) ); |
| 1179 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1180 |
vDecryptedDataParted.resize(ulDataPartLen); |
| 1181 |
rv = CRYPTOKI_F_PTR( C_DecryptUpdate(hSession,&vEncryptedData.front(),1,NULL_PTR,&ulDataPartLen) ); |
| 1182 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_ENCRYPTED_DATA_LEN_RANGE, rv ); |
| 1183 |
} |
| 1184 |
|
| 1185 |
void SymmetricAlgorithmTests::testGenericKey() |
| 1186 |
{ |
| 1187 |
CK_RV rv; |
| 1188 |
CK_SESSION_HANDLE hSession; |
| 1189 |
|
| 1190 |
// Just make sure that we finalize any previous tests |
| 1191 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 1192 |
|
| 1193 |
// Initialize the library and start the test. |
| 1194 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 1195 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1196 |
|
| 1197 |
// Open read-write session |
| 1198 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 1199 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1200 |
|
| 1201 |
// Login USER into the session so we can create a private objects |
| 1202 |
rv = CRYPTOKI_F_PTR( C_Login(hSession,CKU_USER,m_userPin1,m_userPin1Length) ); |
| 1203 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 1204 |
|
| 1205 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 1206 |
|
| 1207 |
// Generate a session key. |
| 1208 |
rv = generateGenericKey(hSession,IN_SESSION,IS_PUBLIC,hKey); |
| 1209 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1210 |
} |
| 1211 |
|
| 1212 |
void SymmetricAlgorithmTests::testEncDecFinalNULLValidation() |
| 1213 |
{ |
| 1214 |
CK_RV rv; |
| 1215 |
CK_SESSION_HANDLE hSession; |
| 1216 |
|
| 1217 |
// Just make sure that we finalize any previous tests |
| 1218 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 1219 |
|
| 1220 |
// Initialize the library and start the test. |
| 1221 |
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ); |
| 1222 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1223 |
|
| 1224 |
// Open read-write session |
| 1225 |
rv = CRYPTOKI_F_PTR( C_OpenSession(m_initializedTokenSlotID, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, &hSession) ); |
| 1226 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1227 |
|
| 1228 |
// Login USER into the sessions so we can create a private objects |
| 1229 |
rv = CRYPTOKI_F_PTR( C_Login(hSession,CKU_USER,m_userPin1,m_userPin1Length) ); |
| 1230 |
CPPUNIT_ASSERT(rv==CKR_OK); |
| 1231 |
|
| 1232 |
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE; |
| 1233 |
|
| 1234 |
// Generate all combinations of session/token keys. |
| 1235 |
rv = generateAesKey(hSession,IN_SESSION,IS_PUBLIC,hKey); |
| 1236 |
|
| 1237 |
CPPUNIT_ASSERT(rv == CKR_OK); |
| 1238 |
|
| 1239 |
CK_MECHANISM mechanism = { CKM_AES_CTR, NULL_PTR, 0 }; |
| 1240 |
CK_AES_CTR_PARAMS ctrParams = |
| 1241 |
{ |
| 1242 |
2, |
| 1243 |
{ |
| 1244 |
0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, |
| 1245 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 |
| 1246 |
} |
| 1247 |
}; |
| 1248 |
mechanism.pParameter = &ctrParams; |
| 1249 |
mechanism.ulParameterLen = sizeof(ctrParams); |
| 1250 |
CK_BYTE plainText[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1251 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1252 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1253 |
0x00 }; |
| 1254 |
std::vector<CK_BYTE> vEncryptedData; |
| 1255 |
std::vector<CK_BYTE> vEncryptedDataParted; |
| 1256 |
std::vector<CK_BYTE> vDecryptedData; |
| 1257 |
std::vector<CK_BYTE> vDecryptedDataParted; |
| 1258 |
CK_ULONG ulEncryptedDataLen; |
| 1259 |
CK_ULONG ulEncryptedPartLen; |
| 1260 |
CK_ULONG ulDecryptedPartLen; |
| 1261 |
CK_ULONG ulDataPartLen; |
| 1262 |
|
| 1263 |
// Single-part encryption |
| 1264 |
rv = CRYPTOKI_F_PTR( C_EncryptInit(hSession,&mechanism,hKey) ); |
| 1265 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1266 |
rv = CRYPTOKI_F_PTR( C_Encrypt(hSession,plainText,sizeof(plainText)-1,NULL_PTR,&ulEncryptedDataLen) ); |
| 1267 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1268 |
vEncryptedData.resize(ulEncryptedDataLen); |
| 1269 |
rv = CRYPTOKI_F_PTR( C_Encrypt(hSession,plainText,sizeof(plainText)-1,&vEncryptedData.front(),&ulEncryptedDataLen) ); |
| 1270 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1271 |
vEncryptedData.resize(ulEncryptedDataLen); |
| 1272 |
|
| 1273 |
// Multi-part encryption |
| 1274 |
rv = CRYPTOKI_F_PTR( C_EncryptInit(hSession,&mechanism,hKey) ); |
| 1275 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1276 |
rv = CRYPTOKI_F_PTR( C_EncryptUpdate(hSession,plainText,sizeof(plainText)-1,NULL_PTR,&ulEncryptedPartLen) ); |
| 1277 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1278 |
vEncryptedDataParted.resize(ulEncryptedPartLen); |
| 1279 |
rv = CRYPTOKI_F_PTR( C_EncryptUpdate(hSession,plainText,sizeof(plainText)-1,&vEncryptedDataParted.front(),&ulEncryptedPartLen) ); |
| 1280 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1281 |
|
| 1282 |
// Test input validation |
| 1283 |
rv = CRYPTOKI_F_PTR( C_EncryptFinal(hSession, NULL_PTR, NULL_PTR) ); |
| 1284 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_ARGUMENTS_BAD, rv ); |
| 1285 |
ulEncryptedPartLen = 0; |
| 1286 |
rv = CRYPTOKI_F_PTR( C_EncryptFinal(hSession, NULL_PTR, &ulEncryptedPartLen) ); |
| 1287 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OPERATION_NOT_INITIALIZED, rv ); |
| 1288 |
|
| 1289 |
// Multi-part decryption |
| 1290 |
rv = CRYPTOKI_F_PTR( C_DecryptInit(hSession,&mechanism,hKey) ); |
| 1291 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1292 |
rv = CRYPTOKI_F_PTR( C_DecryptUpdate(hSession,&vEncryptedData.front(),vEncryptedData.size(),NULL_PTR,&ulDataPartLen) ); |
| 1293 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1294 |
vDecryptedDataParted.resize(ulDataPartLen); |
| 1295 |
rv = CRYPTOKI_F_PTR( C_DecryptUpdate(hSession,&vEncryptedData.front(),vEncryptedData.size(),&vDecryptedDataParted.front(),&ulDataPartLen) ); |
| 1296 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, rv ); |
| 1297 |
|
| 1298 |
// Test input validation |
| 1299 |
rv = CRYPTOKI_F_PTR( C_DecryptFinal(hSession, NULL_PTR, NULL_PTR) ); |
| 1300 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_ARGUMENTS_BAD, rv ); |
| 1301 |
ulDecryptedPartLen = 0; |
| 1302 |
rv = CRYPTOKI_F_PTR( C_DecryptFinal(hSession, NULL_PTR, &ulDecryptedPartLen) ); |
| 1303 |
CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OPERATION_NOT_INITIALIZED, rv ); |
| 1304 |
|
| 1305 |
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ); |
| 1306 |
} |