From ce941b7dcc919153abd561bfcdf646e4e47ba446 Mon Sep 17 00:00:00 2001 From: BKSSM Venkateswarlu Date: Thu, 20 Aug 2020 01:30:42 +0530 Subject: [PATCH] For AES GCM Decryption operation hold last 16 bytes of data in update operation and send that in finish operation. In begin operation call getKeyCharacteristics to validate the keyblob and also to get keyCharacteristics. --- .../4.1/JavacardKeymaster4Device.cpp | 27 +++++++++++++------ .../4.1/JavacardOperationContext.cpp | 10 ++++--- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/HAL/keymaster/4.1/JavacardKeymaster4Device.cpp b/HAL/keymaster/4.1/JavacardKeymaster4Device.cpp index fa89ad8c..240da912 100644 --- a/HAL/keymaster/4.1/JavacardKeymaster4Device.cpp +++ b/HAL/keymaster/4.1/JavacardKeymaster4Device.cpp @@ -1085,16 +1085,27 @@ Return JavacardKeymaster4Device::begin(KeyPurpose purpose, const hidl_vec< cborConverter_.addHardwareAuthToken(array, authToken); std::vector cborData = array.encode(); - /* keyCharacteristics.hardwareEnforced is required to store algorithm, digest and padding values in operationInfo - * structure. To retrieve keyCharacteristics.hardwareEnforced, parse the keyBlob. - */ - /* TODO if keyBlob is corrupted it crashes in cbor */ - std::tie(blobItem, errorCode) = cborConverter_.decodeData(std::vector(keyBlob), false); + // keyCharacteristics.hardwareEnforced is required to store algorithm, digest and padding values in operationInfo + // structure. To retrieve keyCharacteristics.hardwareEnforced, call getKeyCharacateristics. + // By calling getKeyCharacateristics also helps in finding a corrupted keyblob. + hidl_vec applicationId; + hidl_vec applicationData; + if(getTag(inParams, Tag::APPLICATION_ID, param)) { + applicationId = param.blob; + } + if(getTag(inParams, Tag::APPLICATION_DATA, param)) { + applicationData = param.blob; + } + //Call to getKeyCharacteristics. + getKeyCharacteristics(keyBlob, applicationId, applicationData, + [&](ErrorCode error, KeyCharacteristics keyChars) { + errorCode = error; + keyCharacteristics = keyChars; + }); - if(blobItem != nullptr) { + if(errorCode == ErrorCode::OK) { errorCode = ErrorCode::UNKNOWN_ERROR; - if(cborConverter_.getKeyCharacteristics(blobItem, 3, keyCharacteristics) && - getTag(keyCharacteristics.hardwareEnforced, Tag::ALGORITHM, param)) { + if(getTag(keyCharacteristics.hardwareEnforced, Tag::ALGORITHM, param)) { errorCode = sendData(Instruction::INS_BEGIN_OPERATION_CMD, cborData, cborOutData); if((errorCode == ErrorCode::OK) && (cborOutData.size() > 2)) { //Skip last 2 bytes in cborData, it contains status. diff --git a/HAL/keymaster/4.1/JavacardOperationContext.cpp b/HAL/keymaster/4.1/JavacardOperationContext.cpp index 6b43790e..bea270c9 100644 --- a/HAL/keymaster/4.1/JavacardOperationContext.cpp +++ b/HAL/keymaster/4.1/JavacardOperationContext.cpp @@ -229,12 +229,14 @@ ErrorCode OperationContext::getBlockAlignedData(uint64_t operHandle, uint8_t* in /*Update */ //Calculate the block sized length on combined input of both buffered data and input data. uint32_t blockAlignedLen = ((data.buf_len + input_len)/blockSize) * blockSize; - //For symmetric ciphers, decryption operation and PKCS7 padding mode save last 16 bytes of block and send this - //block in finish operation. This is done to make sure that there will be always a 16 bytes data to finish - //operation so that javacard Applet may remove PKCS7 padding if any. + //For symmetric ciphers, decryption operation and PKCS7 padding mode or AES GCM operation save the last 16 bytes + //of block and send this block in finish operation. This is done to make sure that there will be always a 16 + //bytes of data left for finish operation so that javacard Applet may remove PKCS7 padding if any or get the tag + //data for AES GCM operation for authentication purpose. if(((operationTable[operHandle].info.alg == Algorithm::AES) || (operationTable[operHandle].info.alg == Algorithm::TRIPLE_DES)) && - (operationTable[operHandle].info.pad == PaddingMode::PKCS7) && + (operationTable[operHandle].info.pad == PaddingMode::PKCS7 || + operationTable[operHandle].info.mode == BlockMode::GCM) && (operationTable[operHandle].info.purpose == KeyPurpose::DECRYPT)) { if(blockAlignedLen >= blockSize) blockAlignedLen -= blockSize; }