summaryrefslogtreecommitdiff
path: root/src/manager/crypto/tz-backend/internals.cpp
blob: 71038e0a553665631d942a130518d16500dd058f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 *  Copyright (c) 2017 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file       internals.h
 * @author     Krzysztof Dynowski (k.dynowski@samsung.com)
 * @author     Lukasz Kostyra (l.kostyra@samsung.com)
 * @version    1.0
 */

#include <generic-backend/exception.h>
#include <generic-backend/algo-validation.h>
#include <generic-backend/crypto-params.h>
#include <dpl/log/log.h>

#include <tz-backend/internals.h>
#include <tz-backend/tz-context.h>
#include <km_ta_defines.h>


namespace CKM {
namespace Crypto {
namespace TZ {
namespace Internals {

tz_algo_type getGenKeyType(AlgoType type)
{
	switch (type)
	{
	case AlgoType::AES_GEN:	return ALGO_AES_GEN;
	default: ThrowErr(Exc::Crypto::OperationNotSupported, "Requested algorithm is not supported");
	}
}

tz_algo_type getAlgType(AlgoType type)
{
	switch (type)
	{
	case AlgoType::AES_CBC: return ALGO_AES_CBC;
	case AlgoType::AES_CTR: return ALGO_AES_CTR;
	case AlgoType::AES_CFB: return ALGO_AES_CFB;
	case AlgoType::AES_GCM: return ALGO_AES_GCM;
	default: ThrowErr(Exc::Crypto::OperationNotSupported, "Requested algorithm is not supported");
	};
}

tz_algo_type getAlgType(KeyType keyType)
{
	switch (keyType)
	{
	case KeyType::KEY_AES:
		return ALGO_AES_GEN;
	case KeyType::KEY_RSA_PUBLIC:
	case KeyType::KEY_RSA_PRIVATE:
		return ALGO_RSA_GEN;
	default:
		ThrowErr(Exc::Crypto::OperationNotSupported, "Requested algorithm is not supported");
	};
}

RawBuffer generateIV()
{
	RawBuffer result;
	TrustZoneContext::Instance().generateIV(result);
	return result;
}

Data generateSKey(const CryptoAlgorithm &alg,
				const Password &pwd,
				const RawBuffer &iv,
				RawBuffer &tag)
{
	AlgoType keyType = unpack<AlgoType>(alg, ParamName::ALGO_TYPE);
	int keyBits = unpack<int>(alg, ParamName::GEN_KEY_LEN);

	Data keyData;
	keyData.type = DataType(KeyType::KEY_AES);

	if (!pwd.empty()) {
		if (iv.empty()) {
			ThrowErr(Exc::InputParam, "Key generation with password encryption requires an IV");
		}

		RawBuffer pwdBuf(pwd.begin(), pwd.end());
		TrustZoneContext::Instance().generateSKeyPwd(getGenKeyType(keyType),
													pwdBuf, iv, keyBits,
													keyData.data, tag);
	} else {
		TrustZoneContext::Instance().generateSKey(getGenKeyType(keyType), keyBits,
												keyData.data);
	}

	return keyData;
}

DataPair generateAKey(const CryptoAlgorithm &,
					const Password &,
					const RawBuffer &)
{
	ThrowErr(Exc::Crypto::OperationNotSupported,
				"AKeys are not yet implemented in TrustZone backend");
}

void destroyKey(const RawBuffer &key)
{
	TrustZoneContext::Instance().executeDestroy(key);
}

RawBuffer importData(const Data &data,
					 const EncryptionParams &encData,
					 const Password &pwd,
					 const RawBuffer &pwdIV,
					 RawBuffer &tag)
{

	uint32_t dataType;

	if (data.type.isSKey()) {
		dataType = TYPE_SKEY;
	} else if (data.type.isBinaryData()) {
		dataType = TYPE_GENERIC_SECRET;
	} else {
		ThrowErr(Exc::Crypto::DataTypeNotSupported,
			"Data type could not be impoted by tz-backend");
	}

	RawBuffer result;

	RawBuffer pwdBuf(pwd.begin(), pwd.end());
	uint32_t keySizeBits = data.data.size() * 8;
	TrustZoneContext::Instance().importData(dataType,
										data.data,
										encData,
										pwdBuf,
										pwdIV,
										keySizeBits,
										Params::DERIVED_KEY_LENGTH_BITS,
										result,
										tag);
	return result;
}

RawBuffer getData(const RawBuffer &dataId,
				  const Pwd &pwd)
{
	RawBuffer result;
	TrustZoneContext::Instance().getData(dataId,
				 pwd,
				 result);
	return result;
}

void destroyData(const RawBuffer &dataId)
{
	TrustZoneContext::Instance().destroyData(dataId);
}

BufferPair encryptDataAesGcm(const RawBuffer &key,
							const Pwd &pwd,
							const RawBuffer &iv,
							int tagSize,
							const RawBuffer &data,
							const RawBuffer &aad)
{
	RawBuffer result;
	RawBuffer tag;

	TrustZoneContext::Instance().executeEncryptAE(key, pwd, iv, tagSize,
												aad, data, result, tag);

	return std::make_pair(result, tag);
}

RawBuffer encryptDataAesGcmPacked(const RawBuffer &key,
								const Pwd &pwd,
								const RawBuffer &iv,
								int tagSize,
								const RawBuffer &data,
								const RawBuffer &aad)
{
	auto pair = encryptDataAesGcm(key, pwd, iv, tagSize, data, aad);
	std::copy(pair.second.begin(), pair.second.end(),
			std::back_inserter(pair.first));
	return pair.first;
}

RawBuffer decryptDataAesGcm(const RawBuffer &key,
							const Pwd &pwd,
							const RawBuffer &iv,
							int tagSizeBits,
							const RawBuffer &tag,
							const RawBuffer &data,
							const RawBuffer &aad)
{
	RawBuffer result;

	TrustZoneContext::Instance().executeDecryptAE(key, pwd, iv, tagSizeBits,
												tag, aad, data, result);

	return result;
}

RawBuffer decryptDataAesGcmPacked(const RawBuffer &key,
								const Pwd &pwd,
								const RawBuffer &iv,
								int tagSizeBits,
								const RawBuffer &data,
								const RawBuffer &aad)
{
	int tagSizeBytes = tagSizeBits / 8;
	if (tagSizeBytes > static_cast<int>(data.size()))
		ThrowErr(Exc::Crypto::InputParam, "Wrong size of tag");

	auto tagPos = data.data() + data.size() - tagSizeBytes;
	return decryptDataAesGcm(key,
							pwd,
							iv,
							tagSizeBits,
							RawBuffer(tagPos, data.data() + data.size()),
							RawBuffer(data.data(), tagPos),
							aad);
}


RawBuffer symmetricEncrypt(const RawBuffer &key,
						const Pwd &pwd,
						const CryptoAlgorithm &alg,
						const RawBuffer &data)
{
	AlgoType algo = unpack<AlgoType>(alg, ParamName::ALGO_TYPE);
	uint64_t ctrLen = 0;

	switch (algo) {
		case AlgoType::AES_CTR: {
			ctrLen = unpack<uint64_t>(alg, ParamName::ED_CTR_LEN);
			// counter length is in bits
			if (ctrLen != Params::DEFAULT_AES_IV_LEN * 8) {
				LogError("CTR length invalid: " << std::to_string(ctrLen));
				ThrowErr(Exc::Crypto::InputParam, "Invalid CTR length");
			}
			// no break here, we still need to slide down to executeCrypt
		}
		case AlgoType::AES_CBC:
		case AlgoType::AES_CFB: {
			RawBuffer result;
			TrustZoneContext::Instance().executeCrypt(CMD_ENCRYPT,
													getAlgType(algo),
													key,
													pwd,
													unpack<RawBuffer>(alg, ParamName::ED_IV),
													data,
													result);
			return result;
		}
		case AlgoType::AES_GCM: {
			int tagLenBits = Params::DEFAULT_AES_GCM_TAG_LEN_BITS;
			alg.getParam(ParamName::ED_TAG_LEN, tagLenBits);
			RawBuffer aad;
			alg.getParam(ParamName::ED_AAD, aad);
			return encryptDataAesGcmPacked(key,
										pwd,
										unpack<RawBuffer>(alg, ParamName::ED_IV),
										tagLenBits,
										data,
										aad);
		}
		default:
			break;
	}

	ThrowErr(Exc::Crypto::OperationNotSupported,
				"Incorrect algorithm provided for symmetric crypto operation");
}

RawBuffer symmetricDecrypt(const RawBuffer &key,
						const Pwd &pwd,
						const CryptoAlgorithm &alg,
						const RawBuffer &data)
{
	AlgoType algo = unpack<AlgoType>(alg, ParamName::ALGO_TYPE);
	uint64_t ctrLen = 0;

	switch (algo) {
		case AlgoType::AES_CTR: {
			ctrLen = unpack<uint64_t>(alg, ParamName::ED_CTR_LEN);
			// counter length is in bits
			if (ctrLen != Params::DEFAULT_AES_IV_LEN * 8) {
				LogError("CTR length invalid: " << std::to_string(ctrLen));
				ThrowErr(Exc::Crypto::InputParam, "Invalid CTR length");
			}
			// no break here, we still need to slide down to executeCrypt
		}
		case AlgoType::AES_CBC:
		case AlgoType::AES_CFB: {
			RawBuffer result;
			TrustZoneContext::Instance().executeCrypt(CMD_DECRYPT,
													getAlgType(algo),
													key,
													pwd,
													unpack<RawBuffer>(alg, ParamName::ED_IV),
													data,
													result);
			return result;
		}
		case AlgoType::AES_GCM: {
			int tagSizeBits = Params::DEFAULT_AES_GCM_TAG_LEN_BITS;
			alg.getParam(ParamName::ED_TAG_LEN, tagSizeBits);
			RawBuffer aad;
			alg.getParam(ParamName::ED_AAD, aad);
			return decryptDataAesGcmPacked(key,
										pwd,
										unpack<RawBuffer>(alg, ParamName::ED_IV),
										tagSizeBits,
										data,
										aad);
		}
		default:
			break;
	}

	ThrowErr(Exc::Crypto::OperationNotSupported,
				"Incorrect algorithm provided for symmetric crypto operation");
}

RawBuffer asymmetricEncrypt(const RawBuffer &,
							const Pwd &,
							const CryptoAlgorithm &,
							const RawBuffer &)
{
	ThrowErr(Exc::Crypto::OperationNotSupported,
				"Asymmetric encryption is not yet supported on TrustZone backend");
}

RawBuffer asymmetricDecrypt(const RawBuffer &,
							const Pwd &,
							const CryptoAlgorithm &,
							const RawBuffer &)
{
	ThrowErr(Exc::Crypto::OperationNotSupported,
				"Asymmetric encryption is not yet supported on TrustZone backend");
}

RawBuffer sign(const RawBuffer &,
			const Pwd &,
			const CryptoAlgorithm &,
			const RawBuffer &)
{
	ThrowErr(Exc::Crypto::OperationNotSupported,
				"Certificate signing is not yet supported on TrustZone backend");
}

int verify(const RawBuffer &,
		const Pwd &,
		const CryptoAlgorithm &,
		const RawBuffer &,
		const RawBuffer &)
{
	ThrowErr(Exc::Crypto::OperationNotSupported,
				"Certificate signing is not yet supported on TrustZone backend");
}

} // namespace Internals
} // namespace TZ
} // namespace Crypto
} // namespace CKM