Implement public key extraction in TZ backend
[platform/core/security/key-manager.git] / src / manager / crypto / tz-backend / internals.h
1 /*
2  *  Copyright (c) 2017 - 2019 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 /*
17  * @file       internals.h
18  * @author     Krzysztof Dynowski (k.dynowski@samsung.com)
19  * @author     Lukasz Kostyra (l.kostyra@samsung.com)
20  * @version    1.0
21  */
22 #pragma once
23
24 #include <ckm/ckm-type.h>
25 #include <data-type.h>
26 #include <tz-backend/obj.h>
27 #include <generic-backend/gstore.h>
28
29 namespace CKM {
30 namespace Crypto {
31 namespace TZ {
32 namespace Internals {
33
34 using DataPair = std::pair<Data, Data>;
35 using BufferPair = std::pair<RawBuffer, RawBuffer>;
36
37 // encryption schema + buffer pair
38 using KeyIdPair = std::pair<int, RawBuffer>;
39
40 RawBuffer generateIV();
41
42 AlgoType generateAKey(const CryptoAlgorithm &alg,
43                                         const Password &pubPwd,
44                                         const Password &privPwd,
45                                         const RawBuffer &pubPwdIv,
46                                         const RawBuffer &privPwdIv,
47                                         RawBuffer &pubTag,
48                                         RawBuffer &privTag,
49                                         const RawBuffer &hashPriv,
50                                         const RawBuffer &hashPub);
51
52 void generateSKey(const CryptoAlgorithm &alg,
53                                 const Password &pwd,
54                                 const RawBuffer &iv,
55                                 RawBuffer &tag,
56                                 const RawBuffer &hash);
57
58 void importData(const Data &key,
59                                          const EncryptionParams &encData,
60                                          const Password &pwd,
61                                          const RawBuffer &pwdIV,
62                                          RawBuffer &tag,
63                                          const RawBuffer &hash);
64
65 void importWrappedKey(const RawBuffer &wrappingKeyId,
66                                           const Pwd &wrappingKeyPwd,
67                                           const CryptoAlgorithm &alg,
68                                           const Data &encryptedKey,
69                                           const Password &encryptedKeyPassword,
70                                           const RawBuffer &encryptedKeyIV,
71                                           RawBuffer &encryptedKeyTag,
72                                           const RawBuffer &encryptedKeyId);
73
74 RawBuffer exportWrappedKey(const RawBuffer &wrappingKeyId,
75                                                    const Pwd &wrappingKeyPwd,
76                                                    const CryptoAlgorithm &alg,
77                                                    const RawBuffer &keyToWrapId,
78                                                    const Pwd &keyToWrapPwd);
79
80 RawBuffer getData(const RawBuffer &dataId,
81                                   const Pwd &pwd);
82
83 void destroyData(const RawBuffer &dataId);
84
85 void destroyKey(const RawBuffer &keyId);
86
87 RawBuffer symmetricEncrypt(
88         const RawBuffer &keyId,
89         const Pwd &pwd,
90         const CryptoAlgorithm &alg,
91         const RawBuffer &data);
92
93 RawBuffer symmetricDecrypt(
94         const RawBuffer &keyId,
95         const Pwd &pwd,
96         const CryptoAlgorithm &alg,
97         const RawBuffer &cipher);
98
99 RawBuffer asymmetricEncrypt(
100         const RawBuffer &keyId,
101         const Pwd &pwd,
102         const CryptoAlgorithm &alg,
103         const RawBuffer &data);
104
105 RawBuffer asymmetricDecrypt(
106         const RawBuffer &keyId,
107         const Pwd &pwd,
108         const CryptoAlgorithm &alg,
109         const RawBuffer &cipher);
110
111 BufferPair encryptDataAesGcm(const RawBuffer &keyId,
112                                                         const Pwd &pwd,
113                                                         const RawBuffer &iv,
114                                                         int tagSize,
115                                                         const RawBuffer &data,
116                                                         const RawBuffer &aad = RawBuffer());
117
118 RawBuffer decryptDataAesGcm(const RawBuffer &keyId,
119                                                         const Pwd &pwd,
120                                                         const RawBuffer &iv,
121                                                         const RawBuffer &tag,
122                                                         const RawBuffer &data,
123                                                         const RawBuffer &aad = RawBuffer());
124
125 uint32_t initCipher(const RawBuffer &keyId,
126                                         const Pwd &pwd,
127                                         const CryptoAlgorithm &alg,
128                                         bool encrypt);
129
130 void addAAD(uint32_t opId,
131                         const RawBuffer &aad);
132
133 RawBuffer updateCipher(uint32_t opId,
134                                            const RawBuffer &data);
135
136 RawBuffer finalizeCipher(uint32_t opId,
137                                                  const RawBuffer &data);
138
139 RawBuffer sign(const RawBuffer &pkeyId,
140                         const Pwd &pwd,
141                         const CryptoAlgorithm &alg,
142                         const RawBuffer &message);
143
144 int verify(const RawBuffer &pkeyId,
145                 const Pwd &pwd,
146                 const CryptoAlgorithm &alg,
147                 const RawBuffer &message,
148                 const RawBuffer &signature);
149
150 void deriveECDH(const RawBuffer &prvKeyId,
151                                 const Pwd &prvKeyPwd,
152                                 const RawBuffer &pubKey,
153                                 const Password &secretPwd,
154                                 const RawBuffer &secretPwdIV,
155                                 RawBuffer &secretTag,
156                                 const RawBuffer &secretHash);
157
158 void deriveKBKDF(const RawBuffer &secretId,
159                                  const CryptoAlgorithm &alg,
160                                  const Password &keyPwd,
161                                  const RawBuffer &keyPwdIV,
162                                  RawBuffer &keyTag,
163                                  const RawBuffer &keyHash);
164 } // namespace Internals
165 } // namespace TZ
166 } // namespace Crypto
167 } // namespace CKM