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.
30 #include <openssl/evp.h>
31 #include <openssl/rand.h>
33 #include <ckm/ckm-error.h>
35 #include <dpl/log/log.h>
39 #include <crypto-logic.h>
41 #include <generic-backend/exception.h>
42 #include <sw-backend/internals.h>
46 const static int AES_CBC_KEY_SIZE = 32;
47 const static int AES_GCM_TAG_SIZE = 16;
49 } // anonymous namespace
53 CryptoLogic::CryptoLogic() {}
55 CryptoLogic::CryptoLogic(CryptoLogic &&second) {
56 m_keyMap = std::move(second.m_keyMap);
59 CryptoLogic& CryptoLogic::operator=(CryptoLogic &&second) {
62 m_keyMap = std::move(second.m_keyMap);
66 bool CryptoLogic::haveKey(const Label &smackLabel)
68 return (m_keyMap.count(smackLabel) > 0);
71 void CryptoLogic::pushKey(const Label &smackLabel,
72 const RawBuffer &applicationKey)
74 if (smackLabel.length() == 0) {
75 ThrowErr(Exc::InternalError, "Empty smack label.");
77 if (applicationKey.size() == 0) {
78 ThrowErr(Exc::InternalError, "Empty application key.");
80 if (haveKey(smackLabel)) {
81 ThrowErr(Exc::InternalError, "Application key for ", smackLabel,
82 "label already exists.");
85 m_keyMap[smackLabel] = applicationKey;
88 void CryptoLogic::removeKey(const Label &smackLabel)
90 m_keyMap.erase(smackLabel);
93 RawBuffer CryptoLogic::passwordToKey(
94 const Password &password,
95 const RawBuffer &salt,
98 RawBuffer result(keySize);
100 if (1 != PKCS5_PBKDF2_HMAC_SHA1(
109 ThrowErr(Exc::InternalError, "PCKS5_PKKDF_HMAC_SHA1 failed.");
115 RawBuffer CryptoLogic::generateRandIV() const {
116 RawBuffer civ(EVP_MAX_IV_LENGTH);
118 if (1 != RAND_bytes(civ.data(), civ.size())) {
119 ThrowErr(Exc::InternalError, "RAND_bytes failed to generate IV.");
125 void CryptoLogic::encryptRow(const Password &password, DB::Row &row)
133 crow.algorithmType = DBCMAlgType::AES_GCM_256;
134 crow.dataSize = crow.data.size();
136 if (crow.dataSize <= 0) {
137 ThrowErr(Exc::InternalError, "Invalid dataSize.");
140 if (!haveKey(row.ownerLabel)) {
141 ThrowErr(Exc::InternalError, "Missing application key for ",
142 row.ownerLabel, " label.");
145 if (crow.iv.empty()) {
146 crow.iv = generateRandIV();
149 key = m_keyMap[row.ownerLabel];
150 crow.encryptionScheme = ENCR_APPKEY;
152 auto dataPair = Crypto::SW::Internals::encryptDataAesGcm(key, crow.data, crow.iv, AES_GCM_TAG_SIZE);
153 crow.data = dataPair.first;
155 crow.tag = dataPair.second;
157 if (!password.empty()) {
158 key = passwordToKey(password, crow.iv, AES_CBC_KEY_SIZE);
160 crow.data = Crypto::SW::Internals::encryptDataAes(AlgoType::AES_CBC, key, crow.data, crow.iv);
161 crow.encryptionScheme |= ENCR_PASSWORD;
164 encBase64(crow.data);
165 crow.encryptionScheme |= ENCR_BASE64;
168 row = std::move(crow);
169 } catch(const CKM::Base64Encoder::Exception::Base &e) {
170 ThrowErr(Exc::InternalError, e.GetMessage());
171 } catch(const CKM::Base64Decoder::Exception::Base &e) {
172 ThrowErr(Exc::InternalError, e.GetMessage());
176 void CryptoLogic::decryptRow(const Password &password, DB::Row &row)
181 RawBuffer digest, dataDigest;
183 if (row.algorithmType != DBCMAlgType::AES_GCM_256) {
184 ThrowErr(Exc::AuthenticationFailed, "Invalid algorithm type.");
187 if ((row.encryptionScheme & ENCR_PASSWORD) && password.empty()) {
188 ThrowErr(Exc::AuthenticationFailed,
189 "DB row is password protected, but given password is "
193 if ((row.encryptionScheme & ENCR_APPKEY) && !haveKey(row.ownerLabel)) {
194 ThrowErr(Exc::AuthenticationFailed, "Missing application key for ",
195 row.ownerLabel, " label.");
199 if (crow.encryptionScheme & ENCR_BASE64) {
200 decBase64(crow.data);
203 if (crow.encryptionScheme & ENCR_PASSWORD) {
204 key = passwordToKey(password, crow.iv, AES_CBC_KEY_SIZE);
205 crow.data = Crypto::SW::Internals::decryptDataAes(AlgoType::AES_CBC, key, crow.data, crow.iv);
208 if (crow.encryptionScheme & ENCR_APPKEY) {
209 key = m_keyMap[crow.ownerLabel];
210 crow.data = Crypto::SW::Internals::decryptDataAesGcm(key, crow.data, crow.iv, crow.tag);
213 if (static_cast<int>(crow.data.size()) < crow.dataSize) {
214 ThrowErr(Exc::AuthenticationFailed, "Decrypted row size mismatch");
217 if (static_cast<int>(crow.data.size()) > crow.dataSize) {
218 crow.data.resize(crow.dataSize);
221 row = std::move(crow);
222 } catch(const CKM::Base64Encoder::Exception::Base &e) {
223 ThrowErr(Exc::InternalError, e.GetMessage());
224 } catch(const CKM::Base64Decoder::Exception::Base &e) {
225 ThrowErr(Exc::InternalError, e.GetMessage());
226 } catch(const Exc::Exception &e) {
227 ThrowErr(Exc::AuthenticationFailed, e.message());
231 void CryptoLogic::encBase64(RawBuffer &data)
238 encdata = benc.get();
240 if (encdata.size() == 0) {
241 ThrowErr(Exc::InternalError, "Base64Encoder returned empty data.");
244 data = std::move(encdata);
247 void CryptoLogic::decBase64(RawBuffer &data)
254 if (!bdec.finalize()) {
255 ThrowErr(Exc::InternalError, "Failed in Base64Decoder.finalize.");
258 decdata = bdec.get();
260 if (decdata.size() == 0) {
261 ThrowErr(Exc::InternalError, "Base64Decoder returned empty data.");
264 data = std::move(decdata);
267 bool CryptoLogic::equalDigests(RawBuffer &dig1, RawBuffer &dig2)
269 unsigned int dlen = Digest().length();
271 if ((dig1.size() != dlen) || (dig2.size() != dlen))
273 return (dig1 == dig2);