2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * @file crypto-logic.cpp
17 * @author Sebastian Grabowski (s.grabowski@samsung.com)
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Crypto module implementation.
28 #include <openssl/evp.h>
29 #include <openssl/rand.h>
31 #include <ckm/ckm-error.h>
33 #include <dpl/log/log.h>
37 #include <crypto-logic.h>
39 #include <generic-backend/exception.h>
40 #include <sw-backend/internals.h>
44 const static int AES_CBC_KEY_SIZE = 32;
45 const static int AES_GCM_TAG_SIZE = 16;
47 } // anonymous namespace
51 CryptoLogic::CryptoLogic() {}
53 CryptoLogic::CryptoLogic(CryptoLogic &&second) {
54 m_keyMap = std::move(second.m_keyMap);
57 CryptoLogic& CryptoLogic::operator=(CryptoLogic &&second) {
60 m_keyMap = std::move(second.m_keyMap);
64 bool CryptoLogic::haveKey(const Label &smackLabel)
66 return (m_keyMap.count(smackLabel) > 0);
69 void CryptoLogic::pushKey(const Label &smackLabel,
70 const RawBuffer &applicationKey)
72 if (smackLabel.length() == 0) {
73 ThrowErr(Exc::InternalError, "Empty smack label.");
75 if (applicationKey.size() == 0) {
76 ThrowErr(Exc::InternalError, "Empty application key.");
78 if (haveKey(smackLabel)) {
79 ThrowErr(Exc::InternalError, "Application key for ", smackLabel,
80 "label already exists.");
83 m_keyMap[smackLabel] = applicationKey;
86 void CryptoLogic::removeKey(const Label &smackLabel)
88 m_keyMap.erase(smackLabel);
91 RawBuffer CryptoLogic::passwordToKey(
92 const Password &password,
93 const RawBuffer &salt,
96 RawBuffer result(keySize);
98 if (1 != PKCS5_PBKDF2_HMAC_SHA1(
107 ThrowErr(Exc::InternalError, "PCKS5_PKKDF_HMAC_SHA1 failed.");
113 RawBuffer CryptoLogic::generateRandIV() const {
114 RawBuffer civ(EVP_MAX_IV_LENGTH);
116 if (1 != RAND_bytes(civ.data(), civ.size())) {
117 ThrowErr(Exc::InternalError, "RAND_bytes failed to generate IV.");
123 void CryptoLogic::encryptRow(const Password &password, DB::Row &row)
131 crow.algorithmType = DBCMAlgType::AES_GCM_256;
132 crow.dataSize = crow.data.size();
134 if (crow.dataSize <= 0) {
135 ThrowErr(Exc::InternalError, "Invalid dataSize.");
138 if (!haveKey(row.ownerLabel)) {
139 ThrowErr(Exc::InternalError, "Missing application key for ",
140 row.ownerLabel, " label.");
143 if (crow.iv.empty()) {
144 crow.iv = generateRandIV();
147 key = m_keyMap[row.ownerLabel];
148 crow.encryptionScheme = ENCR_APPKEY;
150 auto dataPair = Crypto::SW::Internals::encryptDataAesGcm(key, crow.data, crow.iv, AES_GCM_TAG_SIZE);
151 crow.data = dataPair.first;
153 crow.tag = dataPair.second;
155 #ifdef OPTIONAL_PASSWORD_ENABLE
156 if (!password.empty()) {
157 key = passwordToKey(password, crow.iv, AES_CBC_KEY_SIZE);
159 crow.data = Crypto::SW::Internals::encryptDataAes(AlgoType::AES_CBC, key, crow.data, crow.iv);
160 crow.encryptionScheme |= ENCR_PASSWORD;
166 encBase64(crow.data);
167 crow.encryptionScheme |= ENCR_BASE64;
171 } catch(const CKM::Base64Encoder::Exception::Base &e) {
172 ThrowErr(Exc::InternalError, e.GetMessage());
173 } catch(const CKM::Base64Decoder::Exception::Base &e) {
174 ThrowErr(Exc::InternalError, e.GetMessage());
178 void CryptoLogic::decryptRow(const Password &password, DB::Row &row)
183 RawBuffer digest, dataDigest;
185 if (row.algorithmType != DBCMAlgType::AES_GCM_256) {
186 ThrowErr(Exc::AuthenticationFailed, "Invalid algorithm type.");
189 if ((row.encryptionScheme & ENCR_PASSWORD) && password.empty()) {
190 ThrowErr(Exc::AuthenticationFailed,
191 "DB row is password protected, but given password is "
195 if ((row.encryptionScheme & ENCR_APPKEY) && !haveKey(row.ownerLabel)) {
196 ThrowErr(Exc::AuthenticationFailed, "Missing application key for ",
197 row.ownerLabel, " label.");
201 if (crow.encryptionScheme & ENCR_BASE64) {
202 decBase64(crow.data);
205 #ifdef OPTIONAL_PASSWORD_ENABLE
206 if (crow.encryptionScheme & ENCR_PASSWORD) {
207 key = passwordToKey(password, crow.iv, AES_CBC_KEY_SIZE);
208 crow.data = Crypto::SW::Internals::decryptDataAes(AlgoType::AES_CBC, key, crow.data, crow.iv);
212 if (crow.encryptionScheme & ENCR_APPKEY) {
213 key = m_keyMap[crow.ownerLabel];
214 crow.data = Crypto::SW::Internals::decryptDataAesGcm(key, crow.data, crow.iv, crow.tag);
217 if (static_cast<int>(crow.data.size()) < crow.dataSize) {
218 ThrowErr(Exc::AuthenticationFailed, "Decrypted row size mismatch");
221 if (static_cast<int>(crow.data.size()) > crow.dataSize) {
222 crow.data.resize(crow.dataSize);
226 } catch(const CKM::Base64Encoder::Exception::Base &e) {
227 ThrowErr(Exc::InternalError, e.GetMessage());
228 } catch(const CKM::Base64Decoder::Exception::Base &e) {
229 ThrowErr(Exc::InternalError, e.GetMessage());
230 } catch(const Exc::Exception &e) {
231 ThrowErr(Exc::AuthenticationFailed, e.message());
235 void CryptoLogic::encBase64(RawBuffer &data)
242 encdata = benc.get();
244 if (encdata.size() == 0) {
245 ThrowErr(Exc::InternalError, "Base64Encoder returned empty data.");
248 data = std::move(encdata);
251 void CryptoLogic::decBase64(RawBuffer &data)
258 if (!bdec.finalize()) {
259 ThrowErr(Exc::InternalError, "Failed in Base64Decoder.finalize.");
262 decdata = bdec.get();
264 if (decdata.size() == 0) {
265 ThrowErr(Exc::InternalError, "Base64Decoder returned empty data.");
268 data = std::move(decdata);
271 bool CryptoLogic::equalDigests(RawBuffer &dig1, RawBuffer &dig2)
273 unsigned int dlen = Digest().length();
275 if ((dig1.size() != dlen) || (dig2.size() != dlen))
277 return (dig1 == dig2);