Change AES CBC mode into AES GCM.
[platform/core/security/key-manager.git] / src / manager / service / crypto-logic.h
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  * @file        crypto-logic.h
17  * @author      Sebastian Grabowski (s.grabowski@samsung.com)
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Crypto module implementation.
21  */
22 #pragma once
23
24 #include <map>
25 #include <ckm/ckm-type.h>
26 #include <db-crypto.h>
27 #include <dpl/exception.h>
28
29 namespace CKM {
30
31 class CryptoLogic {
32 public:
33     class Exception
34     {
35         public:
36             DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
37             DECLARE_EXCEPTION_TYPE(Base, InternalError)
38             DECLARE_EXCEPTION_TYPE(Base, Base64EncoderError)
39             DECLARE_EXCEPTION_TYPE(Base, Base64DecoderError)
40             DECLARE_EXCEPTION_TYPE(Base, EncryptDBRowError)
41             DECLARE_EXCEPTION_TYPE(Base, DecryptDBRowError)
42     };
43     CryptoLogic();
44     CryptoLogic(const CryptoLogic &second) = delete;
45     CryptoLogic(CryptoLogic &&second);
46     CryptoLogic& operator=(CryptoLogic &&second);
47     CryptoLogic& operator=(const CryptoLogic &second) = delete;
48
49     virtual ~CryptoLogic(){}
50
51     void decryptRow(const Password &password, DBRow &row);
52     void encryptRow(const Password &password, DBRow &row);
53
54     bool haveKey(const std::string &smackLabel);
55     void pushKey(const std::string &smackLabel,
56                  const RawBuffer &applicationKey);
57
58 private:
59         static const int ENCR_BASE64 =   1 << 0;
60         static const int ENCR_APPKEY =   1 << 1;
61         static const int ENCR_PASSWORD = 1 << 2;
62
63         std::map<std::string, RawBuffer> m_keyMap;
64
65     RawBuffer generateRandIV() const;
66     RawBuffer passwordToKey(const Password &password,
67                             const RawBuffer &salt,
68                             size_t keySize) const;
69
70     RawBuffer encryptDataAesCbc(
71         const RawBuffer &data,
72         const RawBuffer &key,
73         const RawBuffer &iv) const;
74
75     RawBuffer decryptDataAesCbc(
76         const RawBuffer &data,
77         const RawBuffer &key,
78         const RawBuffer &iv) const;
79
80     std::pair<RawBuffer, RawBuffer> encryptDataAesGcm(
81         const RawBuffer &data,
82         const RawBuffer &key,
83         const RawBuffer &iv) const;
84
85     RawBuffer decryptDataAesGcm(
86         const RawBuffer &data,
87         const RawBuffer &key,
88         const RawBuffer &iv,
89         const RawBuffer &tag) const;
90
91     void decBase64(RawBuffer &data);
92     void encBase64(RawBuffer &data);
93     bool equalDigests(RawBuffer &dig1, RawBuffer &dig2);
94 };
95
96 } // namespace CKM
97