a0a6852f29e808d4d262f655a987e0ff0c08e44c
[platform/core/security/tef-simulator.git] / ssflib / src / ssf_crypto.cpp
1 /**
2  * Copyright (c) 2015-2017 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 /**
18  * @file
19  * @author Krishna Raghottam Devale (k.devale@samsung.com)
20  * @brief  SSF crypto functions
21  */
22
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include <crypto_internal.h>
31 #include <error.h>
32 #include <log.h>
33 #include <permission.h>
34 #include <array>
35 #include <algorithm>
36
37 #include "CC_API.h"
38 #include "ssf_crypto_openssl.h"
39
40 #define TAG "TEE:Crypto"
41
42 #define CRYPTO_PANIC    do{LOGE(MODULE_SSF_LIB, "This Line!");TEE_Panic(0);}while(0)
43
44 #define MAX_ATTRIBUTE_NUMBER 35 // Maximum number of attributes for each object
45
46 #ifdef _LOGGING
47 #define CRYPTO_INTERNAL_LOG(_f, _a...)  printf("[%s]%d: " _f "\n", __func__ , __LINE__ , ## _a)
48 #define CRYPTO_INTERNAL_LOG_BYTE(msg, Data, DataLen) {          \
49         int idx;                                                                                        \
50         printf("%10s =", msg);                                                          \
51         printf("\n");                                                                           \
52         for( idx = 0; idx < (int)DataLen; idx++) {                              \
53                 if( (idx != 0) && ((idx%16) == 0) ) printf("\n");               \
54                 if((idx % 16) == 0)     printf("\t\"");                 \
55                 printf("%.2X", Data[idx]);                                              \
56                 if( (idx != 0) && ((idx%16) == 15) ) printf("\"");      \
57         }                                                                                               \
58         printf("\n");                                                                           \
59 }
60 #else
61 #define CRYPTO_INTERNAL_LOG(_f, _a...)
62 #define CRYPTO_INTERNAL_LOG_BYTE(msg, Data, DataLen)
63 #endif
64
65 struct __TEE_Attributees
66 {
67         int attr_number;
68         TEE_Attribute attr_array[MAX_ATTRIBUTE_NUMBER];
69 };
70
71 struct TransientObject
72 {
73         TEE_ObjectInfo info;
74         struct __TEE_Attributees attr;
75 };
76
77 struct __TEE_ObjectHandle
78 {
79         struct TransientObject tr;
80     int drv_hndl;
81 };
82
83 struct __TEE_OperationHandle
84 {
85         TEE_OperationInfo info;
86 };
87
88 static int sw_crypto_ioctl_init(crypto_internal_operation *operation, crypto_internal_keystruct *key, unsigned char *ivec, unsigned int ivec_len)
89 {
90         (void)ivec_len; /* actually always==16 */
91         int rc = 0;
92         int mode;
93         unsigned int padding = ID_NO_PADDING;
94         CryptoCoreContainer *handle = (CryptoCoreContainer *)operation->crypto;
95
96         switch(operation->info.algorithm)
97         {
98                 /* TEE_OPERATION_CIPHER */
99                 case TEE_ALG_AES_ECB_NOPAD:
100                         if(operation->info.mode == TEE_MODE_ENCRYPT) mode = ID_ENC_ECB;
101                         else mode = ID_DEC_ECB;
102                         padding = ID_NO_PADDING;
103                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
104                         break;
105
106                 case TEE_ALG_AES_ECB_PKCS5:
107                 case TEE_ALG_AES_ECB_PKCS7:
108                         if(operation->info.mode == TEE_MODE_ENCRYPT) mode = ID_ENC_ECB;
109                         else mode = ID_DEC_ECB;
110                         padding = ID_NO_PADDING /* ID_PKCS5 */;
111                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
112                         break;
113
114                 case TEE_ALG_AES_ECB_ISO9797_M1:
115                 case TEE_ALG_AES_ECB_ISO9797_M2:
116                         if(operation->info.mode == TEE_MODE_ENCRYPT) mode = ID_ENC_ECB;
117                         else mode = ID_DEC_ECB;
118                         padding = ID_NO_PADDING /* ID_PKCS5 */;
119                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
120                         break;
121
122                 case TEE_ALG_AES_CBC_NOPAD:
123                         if(operation->info.mode == TEE_MODE_ENCRYPT) mode = ID_ENC_CBC;
124                         else mode = ID_DEC_CBC;
125                         padding = ID_NO_PADDING;
126                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
127                         break;
128
129                 case TEE_ALG_AES_CBC_PKCS5:
130                 case TEE_ALG_AES_CBC_PKCS7:
131                         if(operation->info.mode == TEE_MODE_ENCRYPT) mode = ID_ENC_CBC;
132                         else mode = ID_DEC_CBC;
133                         padding = ID_NO_PADDING/* ID_PKCS5 */;
134                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
135                         break;
136
137                 case TEE_ALG_AES_CBC_ISO9797_M1:
138                 case TEE_ALG_AES_CBC_ISO9797_M2:
139                         if(operation->info.mode == TEE_MODE_ENCRYPT) mode = ID_ENC_CBC;
140                         else mode = ID_DEC_CBC;
141                         padding = ID_NO_PADDING /* ID_PKCS5 */;
142                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
143                         break;
144
145                 case TEE_ALG_AES_CTR:
146                 case TEE_ALG_AES_CTR_NOPAD:
147                         if(operation->info.mode == TEE_MODE_ENCRYPT) mode = ID_ENC_CTR;
148                         else mode = ID_DEC_CTR;
149                         padding = ID_NO_PADDING;
150                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
151                         break;
152
153                 case TEE_ALG_AES_CTS:
154                 case TEE_ALG_AES_XTS:
155                         break;
156
157                 case TEE_ALG_DES_ECB_NOPAD:
158                 case TEE_ALG_DES3_ECB_NOPAD:
159                 if(operation->info.mode == TEE_MODE_ENCRYPT) {
160                         mode = ID_ENC_ECB;
161                 } else {
162                         mode = ID_DEC_ECB;
163                 }
164                         padding = ID_NO_PADDING;
165                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
166                         break;
167
168                 case TEE_ALG_DES_CBC_NOPAD:
169                 case TEE_ALG_DES3_CBC_NOPAD:
170                 if(operation->info.mode == TEE_MODE_ENCRYPT) {
171                         mode = ID_ENC_CBC;
172                 } else {
173                         mode = ID_DEC_CBC;
174                 }
175                         padding = ID_NO_PADDING;
176                         rc = handle->SE_init(handle, mode, padding, key->secret.buffer, key->secret.size, ivec);
177                         break;
178
179                 case TEE_ALG_HMAC_MD5:
180                 case TEE_ALG_HMAC_SHA1:
181                 case TEE_ALG_HMAC_SHA224:
182                 case TEE_ALG_HMAC_SHA256:
183                 case TEE_ALG_HMAC_SHA384:
184                 case TEE_ALG_HMAC_SHA512:
185                 case TEE_ALG_AES_CBC_MAC_NOPAD:
186                 case TEE_ALG_AES_CBC_MAC_PKCS5:
187                 case TEE_ALG_DES_CBC_MAC_NOPAD:
188                 case TEE_ALG_DES_CBC_MAC_PKCS5:
189                 case TEE_ALG_AES_CMAC:
190                 case TEE_ALG_DES3_CBC_MAC_NOPAD:
191                 case TEE_ALG_DES3_CBC_MAC_PKCS5:
192                         rc = handle->MAC_init(handle, key->secret.buffer, key->secret.size);
193                         break;
194
195                 case TEE_ALG_AES_CCM:
196                 case TEE_ALG_AES_GCM:
197                         break;
198
199                 case TEE_ALG_MD5:
200                 case TEE_ALG_SHA1:
201                 case TEE_ALG_SHA224:
202                 case TEE_ALG_SHA256:
203                 case TEE_ALG_SHA384:
204                 case TEE_ALG_SHA512:
205                         rc = handle->MD_init(handle);
206                         break;
207
208                 case TEE_ALG_RSA_NOPAD:
209                         padding = ID_NO_PADDING;
210                         rc = handle->RSA_setKeypairForCRT(handle, padding,
211                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
212                                                 key->rsa_public.buffer, key->rsa_public.size,
213                                                 key->rsa_private.buffer, key->rsa_private.size,
214                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
215                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
216                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
217                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
218                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
219                         break;
220
221                 case TEE_ALG_RSAES_PKCS1_V1_5:
222                         padding = ID_RSAES_PKCS15;
223                         rc = handle->RSA_setKeypairForCRT(handle, padding,
224                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
225                                                 key->rsa_public.buffer, key->rsa_public.size,
226                                                 key->rsa_private.buffer, key->rsa_private.size,
227                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
228                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
229                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
230                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
231                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
232                         break;
233
234                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
235                         padding = ID_RSAES_OAEP_SHA1;
236                         rc = handle->RSA_setKeypairForCRT(handle, padding,
237                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
238                                                 key->rsa_public.buffer, key->rsa_public.size,
239                                                 key->rsa_private.buffer, key->rsa_private.size,
240                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
241                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
242                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
243                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
244                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
245                         break;
246
247                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
248                         padding = ID_RSAES_OAEP_SHA224;
249                         rc = handle->RSA_setKeypairForCRT(handle, padding,
250                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
251                                                 key->rsa_public.buffer, key->rsa_public.size,
252                                                 key->rsa_private.buffer, key->rsa_private.size,
253                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
254                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
255                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
256                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
257                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
258                         break;
259
260                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
261                         padding = ID_RSAES_OAEP_SHA256;
262                         rc = handle->RSA_setKeypairForCRT(handle, padding,
263                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
264                                                 key->rsa_public.buffer, key->rsa_public.size,
265                                                 key->rsa_private.buffer, key->rsa_private.size,
266                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
267                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
268                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
269                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
270                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
271                         break;
272
273                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
274                         padding = ID_RSAES_OAEP_SHA384;
275                         rc = handle->RSA_setKeypairForCRT(handle, padding,
276                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
277                                                 key->rsa_public.buffer, key->rsa_public.size,
278                                                 key->rsa_private.buffer, key->rsa_private.size,
279                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
280                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
281                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
282                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
283                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
284                         break;
285
286                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
287                         padding = ID_RSAES_OAEP_SHA512;
288                         rc = handle->RSA_setKeypairForCRT(handle, padding,
289                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
290                                                 key->rsa_public.buffer, key->rsa_public.size,
291                                                 key->rsa_private.buffer, key->rsa_private.size,
292                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
293                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
294                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
295                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
296                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
297                         break;
298
299                 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
300                         padding = ID_RSASSA_PKCS15_MD5;
301                         rc = handle->RSA_setKeypairForCRT(handle, padding,
302                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
303                                                 key->rsa_public.buffer, key->rsa_public.size,
304                                                 key->rsa_private.buffer, key->rsa_private.size,
305                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
306                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
307                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
308                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
309                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
310                         break;
311
312                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
313                         padding = ID_RSASSA_PKCS15_SHA1;
314                         rc = handle->RSA_setKeypairForCRT(handle, padding,
315                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
316                                                 key->rsa_public.buffer, key->rsa_public.size,
317                                                 key->rsa_private.buffer, key->rsa_private.size,
318                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
319                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
320                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
321                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
322                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
323                         break;
324
325                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
326                         padding = ID_RSASSA_PKCS15_SHA224;
327                         rc = handle->RSA_setKeypairForCRT(handle, padding,
328                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
329                                                 key->rsa_public.buffer, key->rsa_public.size,
330                                                 key->rsa_private.buffer, key->rsa_private.size,
331                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
332                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
333                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
334                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
335                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
336                         break;
337
338                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
339                         padding = ID_RSASSA_PKCS15_SHA256;
340                         rc = handle->RSA_setKeypairForCRT(handle, padding,
341                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
342                                                 key->rsa_public.buffer, key->rsa_public.size,
343                                                 key->rsa_private.buffer, key->rsa_private.size,
344                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
345                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
346                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
347                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
348                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
349                         break;
350
351                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
352                         padding = ID_RSASSA_PKCS15_SHA384;
353                         rc = handle->RSA_setKeypairForCRT(handle, padding,
354                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
355                                                 key->rsa_public.buffer, key->rsa_public.size,
356                                                 key->rsa_private.buffer, key->rsa_private.size,
357                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
358                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
359                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
360                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
361                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
362                         break;
363
364                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
365                         padding = ID_RSASSA_PKCS15_SHA512;
366                         rc = handle->RSA_setKeypairForCRT(handle, padding,
367                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
368                                                 key->rsa_public.buffer, key->rsa_public.size,
369                                                 key->rsa_private.buffer, key->rsa_private.size,
370                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
371                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
372                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
373                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
374                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
375                         break;
376
377                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
378                         padding = ID_RSASSA_PSS_SHA1;
379                         rc = handle->RSA_setKeypairForCRT(handle, padding,
380                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
381                                                 key->rsa_public.buffer, key->rsa_public.size,
382                                                 key->rsa_private.buffer, key->rsa_private.size,
383                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
384                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
385                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
386                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
387                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
388                         break;
389
390                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
391                         padding = ID_RSASSA_PSS_SHA224;
392                         rc = handle->RSA_setKeypairForCRT(handle, padding,
393                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
394                                                 key->rsa_public.buffer, key->rsa_public.size,
395                                                 key->rsa_private.buffer, key->rsa_private.size,
396                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
397                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
398                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
399                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
400                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
401                         break;
402
403                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
404                         padding = ID_RSASSA_PSS_SHA256;
405                         rc = handle->RSA_setKeypairForCRT(handle, padding,
406                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
407                                                 key->rsa_public.buffer, key->rsa_public.size,
408                                                 key->rsa_private.buffer, key->rsa_private.size,
409                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
410                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
411                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
412                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
413                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
414                         break;
415
416                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
417                         padding = ID_RSASSA_PSS_SHA384;
418                         rc = handle->RSA_setKeypairForCRT(handle, padding,
419                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
420                                                 key->rsa_public.buffer, key->rsa_public.size,
421                                                 key->rsa_private.buffer, key->rsa_private.size,
422                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
423                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
424                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
425                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
426                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
427                         break;
428
429                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
430                         padding = ID_RSASSA_PSS_SHA512;
431                         rc = handle->RSA_setKeypairForCRT(handle, padding,
432                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
433                                                 key->rsa_public.buffer, key->rsa_public.size,
434                                                 key->rsa_private.buffer, key->rsa_private.size,
435                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
436                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
437                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
438                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
439                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
440                         break;
441
442                 case TEE_ALG_DSA_SHA1:
443                         padding = 0;
444                         rc = handle->RSA_setKeypairForCRT(handle, padding,
445                                                 key->rsa_modulus.buffer, key->rsa_modulus.size,
446                                                 key->rsa_public.buffer, key->rsa_public.size,
447                                                 key->rsa_private.buffer, key->rsa_private.size,
448                                                 key->rsa_prime1.buffer, key->rsa_prime1.size,
449                                                 key->rsa_prime2.buffer, key->rsa_prime2.size,
450                                                 key->rsa_exponent1.buffer, key->rsa_exponent1.size,
451                                                 key->rsa_exponent2.buffer, key->rsa_exponent2.size,
452                                                 key->rsa_coefficient.buffer, key->rsa_coefficient.size);
453                         break;
454
455                 case TEE_ALG_GENERATE_SECRET_KEY:
456                         rc = handle->PRNG_get(handle, key->secret.size, key->secret.buffer);
457                         /* Ignore return value to avoid CRYPTO_PANIC. Only SDRM_X931_ConditionalTest() can return TEE_ERROR.*/
458                         rc = TEE_SUCCESS;
459                         break;
460
461                 case TEE_ALG_GENERATE_RSA_KEY:
462                 {
463                         unsigned char E[3] = {0x01, 0x00, 0x01};
464                         unsigned int ELen = 3;
465
466                         rc = handle->RSA_genKeypairWithEforCRT(handle, padding,
467                                                 E, ELen,
468                                 key->rsa_modulus.buffer, &key->rsa_modulus.size,
469                                                 key->rsa_private.buffer, &key->rsa_private.size,
470                                                 key->rsa_prime1.buffer, &key->rsa_prime1.size,
471                                                 key->rsa_prime2.buffer, &key->rsa_prime2.size,
472                                                 key->rsa_exponent1.buffer, &key->rsa_exponent1.size,
473                                                 key->rsa_exponent2.buffer, &key->rsa_exponent2.size,
474                                                 key->rsa_coefficient.buffer, &key->rsa_coefficient.size);
475
476                         memcpy(key->rsa_public.buffer, E, ELen);
477                         key->rsa_public.size = ELen;
478                 }
479                 break;
480
481                 default:
482                         LOGE(MODULE_SSF_LIB, "Not Support Algorithm : %X ", operation->info.algorithm);
483                         break;
484         }
485
486         CRYPTO_INTERNAL_LOG("rc=%d ", rc);
487         return rc;
488 }
489
490 static int sw_crypto_ioctl_update(crypto_internal_operation *operation, unsigned char* src_addr, unsigned int src_size, unsigned char* dst_addr, unsigned int* dst_size)
491 {
492         int rc;
493         CryptoCoreContainer *handle = (CryptoCoreContainer *)operation->crypto;
494
495         switch(operation->info.algorithm)
496         {
497                 /* TEE_OPERATION_CIPHER */
498                 case TEE_ALG_AES_ECB_NOPAD:
499                 case TEE_ALG_AES_ECB_PKCS5:
500                 case TEE_ALG_AES_ECB_PKCS7:
501                 case TEE_ALG_AES_ECB_ISO9797_M1:
502                 case TEE_ALG_AES_ECB_ISO9797_M2:
503                 case TEE_ALG_AES_CBC_NOPAD:
504                 case TEE_ALG_AES_CBC_PKCS5:
505                 case TEE_ALG_AES_CBC_PKCS7:
506                 case TEE_ALG_AES_CBC_ISO9797_M1:
507                 case TEE_ALG_AES_CBC_ISO9797_M2:
508                 case TEE_ALG_AES_CTR:
509                 case TEE_ALG_AES_CTR_NOPAD:
510                 case TEE_ALG_DES_ECB_NOPAD:
511                 case TEE_ALG_DES3_ECB_NOPAD:
512                 case TEE_ALG_DES_CBC_NOPAD:
513                 case TEE_ALG_DES3_CBC_NOPAD:
514                         rc = handle->SE_process(handle, src_addr, src_size, dst_addr, dst_size);
515                         break;
516
517                 case TEE_ALG_HMAC_MD5:
518                 case TEE_ALG_HMAC_SHA1:
519                 case TEE_ALG_HMAC_SHA224:
520                 case TEE_ALG_HMAC_SHA256:
521                 case TEE_ALG_HMAC_SHA384:
522                 case TEE_ALG_HMAC_SHA512:
523                 case TEE_ALG_AES_CBC_MAC_NOPAD:
524                 case TEE_ALG_AES_CBC_MAC_PKCS5:
525                 case TEE_ALG_DES_CBC_MAC_NOPAD:
526                 case TEE_ALG_DES_CBC_MAC_PKCS5:
527                 case TEE_ALG_AES_CMAC:
528                 case TEE_ALG_DES3_CBC_MAC_NOPAD:
529                 case TEE_ALG_DES3_CBC_MAC_PKCS5:
530                         rc = handle->MAC_update(handle, src_addr, src_size);
531                         break;
532
533                 case TEE_ALG_MD5:
534                 case TEE_ALG_SHA1:
535                 case TEE_ALG_SHA224:
536                 case TEE_ALG_SHA256:
537                 case TEE_ALG_SHA384:
538                 case TEE_ALG_SHA512:
539                         rc = handle->MD_update(handle, src_addr, src_size);
540                         break;
541
542                 default:
543                         LOGE(MODULE_SSF_LIB, "Not Support Algorithm : %X", operation->info.algorithm);
544                         rc = -1;
545                         break;
546         }
547
548         if(src_size && dst_size) {
549                 CRYPTO_INTERNAL_LOG("rc=%d src_size=%d dst_size=%d", rc, src_size, *dst_size);
550         } else {
551                 CRYPTO_INTERNAL_LOG("rc=%d", rc);
552         }
553         return rc;
554 }
555
556 static int sw_crypto_ioctl_final(crypto_internal_operation *operation, unsigned char* src_addr, unsigned int src_size, unsigned char* dst_addr, unsigned int* dst_size)
557 {
558         int rc = -1;
559         int result = 0;
560         CryptoCoreContainer *handle = (CryptoCoreContainer *)operation->crypto;
561
562         switch(operation->info.algorithm)
563         {
564                 /* TEE_OPERATION_CIPHER */
565                 case TEE_ALG_AES_ECB_NOPAD:
566                 case TEE_ALG_AES_ECB_PKCS5:
567                 case TEE_ALG_AES_ECB_PKCS7:
568                 case TEE_ALG_AES_ECB_ISO9797_M1:
569                 case TEE_ALG_AES_ECB_ISO9797_M2:
570                 case TEE_ALG_AES_CBC_NOPAD:
571                 case TEE_ALG_AES_CBC_PKCS5:
572                 case TEE_ALG_AES_CBC_PKCS7:
573                 case TEE_ALG_AES_CBC_ISO9797_M1:
574                 case TEE_ALG_AES_CBC_ISO9797_M2:
575                 case TEE_ALG_AES_CTR_NOPAD:
576                 case TEE_ALG_AES_CTR:
577                         rc = handle->SE_final(handle, src_addr, src_size, dst_addr, dst_size);
578                         break;
579
580                 case TEE_ALG_AES_CTS:
581                 case TEE_ALG_AES_XTS:
582                         break;
583
584                 case TEE_ALG_DES_ECB_NOPAD:
585                 case TEE_ALG_DES3_ECB_NOPAD:
586                 case TEE_ALG_DES_CBC_NOPAD:
587                 case TEE_ALG_DES3_CBC_NOPAD:
588                         rc = handle->SE_final(handle, src_addr, src_size, dst_addr, dst_size);
589                         break;
590
591                 /* TEE_OPERATION_MAC */
592                 case TEE_ALG_HMAC_MD5:
593                 case TEE_ALG_HMAC_SHA1:
594                 case TEE_ALG_HMAC_SHA224:
595                 case TEE_ALG_HMAC_SHA256:
596                 case TEE_ALG_HMAC_SHA384:
597                 case TEE_ALG_HMAC_SHA512:
598                 case TEE_ALG_AES_CBC_MAC_NOPAD:
599                 case TEE_ALG_AES_CBC_MAC_PKCS5:
600                 case TEE_ALG_DES_CBC_MAC_NOPAD:
601                 case TEE_ALG_DES_CBC_MAC_PKCS5:
602                 case TEE_ALG_AES_CMAC:
603                 case TEE_ALG_DES3_CBC_MAC_NOPAD:
604                 case TEE_ALG_DES3_CBC_MAC_PKCS5:
605                 if(src_addr && src_size != 0) {
606                         handle->MAC_update(handle, src_addr, src_size);
607                 }
608                         rc = handle->MAC_final(handle, dst_addr, dst_size);
609                         break;
610
611                 /* TEE_OPERATION_AE */
612                 case TEE_ALG_AES_CCM:
613                 case TEE_ALG_AES_GCM:
614                         break;
615
616                 /* TEE_OPERATION_DIGEST */
617                 case TEE_ALG_MD5:
618                 case TEE_ALG_SHA1:
619                 case TEE_ALG_SHA224:
620                 case TEE_ALG_SHA256:
621                 case TEE_ALG_SHA384:
622                 case TEE_ALG_SHA512:
623                 if(src_addr && src_size != 0) {
624                         handle->MD_update(handle, src_addr, src_size);
625                 }
626                         rc = handle->MD_final(handle, dst_addr);
627                         *dst_size = operation->info.digestLength;
628                         break;
629
630                 /* TEE_OPERATION_ASYMMETRIC_CIPHER */
631                 case TEE_ALG_RSA_NOPAD:
632                 case TEE_ALG_RSAES_PKCS1_V1_5:
633                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
634                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
635                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
636                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
637                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
638                         if (operation->info.mode == TEE_MODE_ENCRYPT ) {
639                                 rc = handle->AE_encrypt(handle, src_addr, src_size, dst_addr, dst_size);
640                         } else {
641                                 rc = handle->AE_decrypt(handle, src_addr, src_size, dst_addr, dst_size);
642                         }
643                         break;
644
645                 /* TEE_OPERATION_ASYMMETRIC_SIGNATURE */
646                 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
647                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
648                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
649                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
650                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
651                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
652                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
653                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
654                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
655                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
656                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
657                 if (operation->info.mode == TEE_MODE_SIGN ) {
658                         rc = handle->DS_sign(handle, src_addr, src_size, dst_addr, dst_size);
659                 } else {
660                         rc = handle->DS_verify(handle, src_addr, src_size, dst_addr, *dst_size, &result);
661                         if(result != rc) {
662                                 rc = result;
663                         }
664                 }
665                         break;
666
667                 case TEE_ALG_GENERATE_SECRET_KEY:
668                         rc = 0;
669                         break;
670
671                 case TEE_ALG_GENERATE_RSA_KEY:
672                         rc = 0;
673                         break;
674
675                 default:
676                         LOGE(MODULE_SSF_LIB, "Not Support Algorithm : %X", operation->info.algorithm);
677                         break;
678         }
679
680         if(src_size && dst_size) {
681                 CRYPTO_INTERNAL_LOG("rc=%d src_size=%d dst_size=%d", rc, src_size, *dst_size);
682         } else {
683                 CRYPTO_INTERNAL_LOG("rc=%d", rc);
684         }
685         return rc;
686 }
687
688 static int sw_crypto_open(crypto_internal_operation *operation)
689 {
690         unsigned int alg;
691
692         switch(operation->info.algorithm)
693         {
694                 /* TEE_OPERATION_CIPHER */
695                 case TEE_ALG_AES_ECB_NOPAD:
696                 case TEE_ALG_AES_CBC_NOPAD:
697                 case TEE_ALG_AES_CTR:
698                 case TEE_ALG_AES_CTR_NOPAD:
699                 case TEE_ALG_AES_ECB_PKCS5:
700                 case TEE_ALG_AES_ECB_PKCS7:
701                 case TEE_ALG_AES_ECB_ISO9797_M1:
702                 case TEE_ALG_AES_ECB_ISO9797_M2:
703                 case TEE_ALG_AES_CBC_PKCS5:
704                 case TEE_ALG_AES_CBC_PKCS7:
705                 case TEE_ALG_AES_CBC_ISO9797_M1:
706                 case TEE_ALG_AES_CBC_ISO9797_M2:
707                 if (operation->info.keySize == 128) {
708                         alg = ID_AES128;
709                 } else if (operation->info.keySize == 192) {
710                         alg = ID_AES192;
711                 } else if (operation->info.keySize == 256) {
712                         alg = ID_AES256;
713                 } else {
714                         goto error;
715                 }
716                         break;
717                 case TEE_ALG_AES_XTS:
718                 case TEE_ALG_AES_CTS:
719                         goto error;
720                         break;
721                 case TEE_ALG_DES_ECB_NOPAD:
722                 case TEE_ALG_DES_CBC_NOPAD:
723                         alg = ID_DES;
724                         break;
725                 case TEE_ALG_DES3_ECB_NOPAD:
726                 case TEE_ALG_DES3_CBC_NOPAD:
727                         alg = ID_TDES;
728                         break;
729
730                 /* TEE_OPERATION_MAC */
731                 case TEE_ALG_AES_CBC_MAC_NOPAD:
732                 case TEE_ALG_AES_CBC_MAC_PKCS5:
733                 case TEE_ALG_AES_CMAC:
734                 case TEE_ALG_DES_CBC_MAC_NOPAD:
735                 case TEE_ALG_DES_CBC_MAC_PKCS5:
736                 case TEE_ALG_DES3_CBC_MAC_NOPAD:
737                 case TEE_ALG_DES3_CBC_MAC_PKCS5:
738                         goto error;
739                         break;
740                 case TEE_ALG_HMAC_MD5:
741                         alg = ID_HMD5;
742                         break;
743                 case TEE_ALG_HMAC_SHA1:
744                         alg = ID_HSHA1;
745                         break;
746                 case TEE_ALG_HMAC_SHA224:
747                         alg = ID_HSHA224;
748                         break;
749                 case TEE_ALG_HMAC_SHA256:
750                         alg = ID_HSHA256;
751                         break;
752                 case TEE_ALG_HMAC_SHA384:
753                         alg = ID_HSHA384;
754                         break;
755                 case TEE_ALG_HMAC_SHA512:
756                         alg = ID_HSHA512;
757                         break;
758
759                 /* TEE_OPERATION_AE */
760                 case TEE_ALG_AES_CCM:
761                 case TEE_ALG_AES_GCM:
762                         goto error;
763                         break;
764
765                 /* TEE_OPERATION_DIGEST */
766                 case TEE_ALG_MD5:
767                         alg = ID_MD5;
768                         break;
769                 case TEE_ALG_SHA1:
770                         alg = ID_SHA1;
771                         break;
772                 case TEE_ALG_SHA224:
773                         alg = ID_SHA224;
774                         break;
775                 case TEE_ALG_SHA256:
776                         alg = ID_SHA256;
777                         break;
778                 case TEE_ALG_SHA384:
779                         alg = ID_SHA384;
780                         break;
781                 case TEE_ALG_SHA512:
782                         alg = ID_SHA512;
783                         break;
784
785                 /* TEE_OPERATION_ASYMMETRIC_CIPHER */
786                 case TEE_ALG_RSA_NOPAD:
787                 case TEE_ALG_RSAES_PKCS1_V1_5:
788                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
789                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
790                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
791                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
792                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
793                         if (operation->info.keySize == 512) {
794                                 alg = ID_RSA512;
795                         } else if (operation->info.keySize == 1024) {
796                                 alg = ID_RSA1024;
797                         } else if (operation->info.keySize == 2048) {
798                                 alg = ID_RSA2048;
799                         } else if (operation->info.keySize == 3072) {
800                                 alg = ID_RSA3072;
801                         } else if (operation->info.keySize == 4096) {
802                                 alg = ID_RSA4096;
803                         } else {
804                                 goto error;
805                         }
806                         break;
807
808                 /* TEE_OPERATION_ASYMMETRIC_SIGNATURE */
809                 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
810                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
811                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
812                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
813                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
814                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
815                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
816                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
817                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
818                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
819                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
820                         if (operation->info.keySize == 512) {
821                                 alg = ID_RSA512;
822                         } else if (operation->info.keySize == 1024) {
823                                 alg = ID_RSA1024;
824                         } else if (operation->info.keySize == 2048) {
825                                 alg = ID_RSA2048;
826                         } else if (operation->info.keySize == 3072) {
827                                 alg = ID_RSA3072;
828                         } else if (operation->info.keySize == 4096) {
829                                 alg = ID_RSA4096;
830                         } else {
831                                 goto error;
832                         }
833                         break;
834
835                 case TEE_ALG_DSA_SHA1:
836                         goto error;
837                         break;
838
839                 case TEE_ALG_ECDSA_P160:
840                 case TEE_ALG_ECDSA_P192:
841                 case TEE_ALG_ECDSA_P224:
842                 case TEE_ALG_ECDSA_P256:
843                 case TEE_ALG_ECDSA_P384:
844                 case TEE_ALG_ECDSA_P521:
845                         goto error;
846                         break;
847
848                 /* TEE_OPERATION_KEY_DERIVATION */
849                 case TEE_ALG_DH_DERIVE_SHARED_SECRET:
850                         goto error;
851                         break;
852
853                 case TEE_ALG_ECDH_P192:
854                 case TEE_ALG_ECDH_P224:
855                 case TEE_ALG_ECDH_P256:
856                 case TEE_ALG_ECDH_P384:
857                 case TEE_ALG_ECDH_P521:
858                         goto error;
859                         break;
860
861                 case TEE_ALG_GENERATE_SECRET_KEY:
862                         alg = ID_X931;
863                         break;
864
865                 case TEE_ALG_GENERATE_RSA_KEY:
866                         if (operation->info.keySize == 512) {
867                                 alg = ID_RSA512;
868                         } else if (operation->info.keySize == 1024) {
869                                 alg = ID_RSA1024;
870                         } else if (operation->info.keySize == 2048) {
871                                 alg = ID_RSA2048;
872                         } else if (operation->info.keySize == 3072) {
873                                 alg = ID_RSA3072;
874                         } else if (operation->info.keySize == 4096) {
875                                 alg = ID_RSA4096;
876                         } else {
877                                 goto error;
878                         }
879                         break;
880
881                 default:
882                         LOGE(MODULE_SSF_LIB, "Not Support Algorithm : %X ", operation->info.algorithm);
883                         goto error;
884                         break;
885         }
886
887         operation->crypto = (void*)create_CryptoCoreContainer(alg);
888
889         if(operation->crypto == NULL) {
890                 goto error;
891         }
892         return 0;
893
894 error:
895         return -1;
896 }
897
898 static int sw_crypto_close(crypto_internal_operation *operation)
899 {
900         int rc = 0;
901         if(operation->crypto) {
902                 destroy_CryptoCoreContainer((CryptoCoreContainer*)operation->crypto);
903         }
904         operation->crypto = NULL;
905         return rc;
906 }
907
908 int crypto_internal_open(crypto_internal_operation *operation)
909 {
910         if (operation->info.algorithm == TEE_ALG_AES_GCM) {
911                 return ossl_crypto_open(operation);
912         } else {
913                 return sw_crypto_open(operation);
914         }
915 }
916
917 int crypto_internal_close(crypto_internal_operation *operation)
918 {
919         if (operation->info.algorithm == TEE_ALG_AES_GCM) {
920                 ossl_crypto_close(operation);
921                 return 0;
922         } else {
923                 return sw_crypto_close(operation);
924         }
925 }
926
927 int crypto_internal_init(crypto_internal_operation *operation, crypto_internal_keystruct *key, unsigned char *ivec, size_t ivec_len)
928 {
929         return sw_crypto_ioctl_init(operation, key, ivec, ivec_len);
930 }
931
932 int crypto_internal_update(crypto_internal_operation *operation, unsigned char *src_data, size_t src_len, unsigned char *dst_data, size_t *dst_len)
933 {
934         unsigned char* in_data = NULL;
935         unsigned char* out_data = NULL;
936         unsigned int in_size = 0;
937         unsigned int out_size = 0;
938         unsigned int num = 0;
939         unsigned int processing_len = 0;
940         unsigned int total_processing_len = 0;
941         int (*crypto_update_engine)(crypto_internal_operation *, unsigned char *, unsigned int, unsigned char *, unsigned int*);
942
943         crypto_update_engine = sw_crypto_ioctl_update;
944
945         if(src_data) {
946                 in_data = (unsigned char*)src_data;
947         }
948         if(dst_data) {
949                 out_data = (unsigned char*)dst_data;
950         }
951         if(src_len) {
952                 in_size = (unsigned int)src_len;
953         }
954         if(dst_len) {
955                 out_size = (unsigned int)*dst_len;
956         }
957
958         CRYPTO_INTERNAL_LOG("--------------------------------------------------------------");
959         CRYPTO_INTERNAL_LOG("in_size=%d out_size=%d op->data_len=%d, processed=%d", in_size, out_size, operation->data_len, total_processing_len);
960
961         if(operation->info.operationClass == TEE_OPERATION_CIPHER)
962         {
963                 if (operation->data_len != 0)
964                 {
965                         if (in_size < (size_t)(operation->block_len - operation->data_len)) {
966                                 num = in_size;
967                         } else {
968                                 num = (size_t)(operation->block_len - operation->data_len);
969                         }
970
971                         CRYPTO_INTERNAL_LOG("num=%d in_size=%d out_size=%d processed=%d", num, in_size, out_size, total_processing_len);
972                         if(num != 0) {
973                                 memcpy(operation->data + operation->data_len, in_data, num);
974
975                                 operation->data_len += num;
976                                 in_size -= num;
977                                 in_data = (unsigned char*)((unsigned long)in_data + num);
978
979                                 /* accumulated data is full */
980                                 if (operation->data_len == operation->block_len)
981                                 {
982                                         processing_len = out_size;
983                                         if (crypto_update_engine(operation, operation->data, operation->data_len, out_data, &processing_len)) {
984                                                 goto error;
985                                         }
986                                         total_processing_len += processing_len;
987                                         out_size -= processing_len;
988                                         out_data = (unsigned char*)((unsigned long) out_data + processing_len);
989                                         operation->data_len = 0;
990                                 }
991                         }
992                         CRYPTO_INTERNAL_LOG("num=%d in_size=%d out_size=%d processed=%d", num, in_size, out_size, total_processing_len);
993                 }
994
995                 if (in_size != 0)
996                 {
997                         size_t should_be_processed_of_bytes = (size_t)in_size/operation->block_len*operation->block_len;
998                         size_t remaining_number_of_bytes = in_size-should_be_processed_of_bytes;
999
1000                         CRYPTO_INTERNAL_LOG("should_be_processed_of_bytes=%zd remaining_number_of_bytes=%zd processed=%d", should_be_processed_of_bytes, remaining_number_of_bytes, total_processing_len);
1001                         if (should_be_processed_of_bytes != 0)
1002                         {
1003                                 processing_len = out_size-total_processing_len;
1004                                 if (crypto_update_engine(operation, in_data, should_be_processed_of_bytes, out_data, &processing_len)) {
1005                                         goto error;
1006                                 }
1007                                 total_processing_len += processing_len;
1008                                 in_size -= processing_len;
1009                                 in_data = (unsigned char*)((unsigned long) in_data + processing_len);
1010                         }
1011
1012                         if(remaining_number_of_bytes != 0) {
1013                                 memcpy(operation->data, in_data, remaining_number_of_bytes);
1014                                 operation->data_len = remaining_number_of_bytes;
1015                         }
1016                 }
1017         } else if (operation->info.operationClass == TEE_OPERATION_MAC || operation->info.operationClass == TEE_OPERATION_DIGEST) {
1018                 if (operation->data_len != 0)
1019                 {
1020                         if (in_size < (size_t)(operation->block_len - operation->data_len)) {
1021                                 num = in_size;
1022                         } else {
1023                                 num = (size_t)(operation->block_len - operation->data_len);
1024                         }
1025
1026                         CRYPTO_INTERNAL_LOG("num=%d in_size=%d processed=%d", num, in_size, total_processing_len);
1027                         if(num != 0) {
1028                                 memcpy(operation->data + operation->data_len, in_data, num);
1029
1030                                 operation->data_len += num;
1031                                 in_size -= num;
1032                                 in_data = (unsigned char*)((unsigned long)in_data + num);
1033
1034                                 /* accumulated data is full */
1035                                 if (operation->data_len == operation->block_len)
1036                                 {
1037                                         if (crypto_update_engine(operation, operation->data, operation->data_len, NULL, NULL)) {
1038                                                 goto error;
1039                                         }
1040                                         operation->data_len = 0;
1041                                 }
1042
1043                                 total_processing_len += num;
1044                         }
1045                         CRYPTO_INTERNAL_LOG("num=%d in_size=%d processed=%d", num, in_size, total_processing_len);
1046                 }
1047
1048                 if (in_size != 0)
1049                 {
1050                         size_t should_be_processed_of_bytes = (size_t)in_size/operation->block_len*operation->block_len;
1051                         size_t remaining_number_of_bytes = in_size-should_be_processed_of_bytes;
1052
1053                         CRYPTO_INTERNAL_LOG("should_be_processed_of_bytes=%zd remaining_number_of_bytes=%zd processed=%d", should_be_processed_of_bytes, remaining_number_of_bytes, total_processing_len);
1054                         if (should_be_processed_of_bytes != 0)
1055                         {
1056                                 if (crypto_update_engine(operation, in_data, should_be_processed_of_bytes, NULL, NULL)) {
1057                                         goto error;
1058                                 }
1059                                 total_processing_len += should_be_processed_of_bytes;
1060                                 in_size -= should_be_processed_of_bytes;
1061                                 in_data = (unsigned char*)((unsigned long) in_data + should_be_processed_of_bytes);
1062                         }
1063
1064                         if(remaining_number_of_bytes != 0) {
1065                                 memcpy(operation->data, in_data, remaining_number_of_bytes);
1066                                 total_processing_len += remaining_number_of_bytes;
1067                                 operation->data_len = remaining_number_of_bytes;
1068                                 in_size -= remaining_number_of_bytes;
1069                         }
1070                 }
1071         } else {
1072                 if(crypto_update_engine(operation, in_data, in_size, out_data, &out_size)) {
1073                         goto error;
1074                 }
1075         }
1076
1077         CRYPTO_INTERNAL_LOG("in_size=%d processed=%d", in_size, total_processing_len);
1078         CRYPTO_INTERNAL_LOG("--------------------------------------------------------------");
1079         if(operation->info.operationClass == TEE_OPERATION_CIPHER && dst_len) {
1080                         *dst_len = total_processing_len;
1081         }
1082         return 0;
1083 error:
1084         return -1;
1085 }
1086
1087 int crypto_internal_final(crypto_internal_operation *operation, unsigned char *src_data, size_t src_len, unsigned char *dst_data, size_t *dst_len)
1088 {
1089         unsigned char* in_data = NULL;
1090         unsigned char* out_data = NULL;
1091         unsigned int in_size = 0;
1092         unsigned int out_size = 0;
1093         unsigned int num = 0;
1094         unsigned int processing_len = 0;
1095         unsigned int total_processing_len = 0;
1096         int (*crypto_update_engine)(crypto_internal_operation *, unsigned char *, unsigned int, unsigned char *, unsigned int*);
1097         int (*crypto_final_engine)(crypto_internal_operation *, unsigned char *, unsigned int, unsigned char *, unsigned int*);
1098
1099         crypto_update_engine = sw_crypto_ioctl_update;
1100         crypto_final_engine = sw_crypto_ioctl_final;
1101
1102         if(src_data) {
1103                 in_data = (unsigned char*)src_data;
1104         }
1105         if(dst_data) {
1106                 out_data = (unsigned char*)dst_data;
1107         }
1108         if(src_len) {
1109                 in_size = (unsigned int)src_len;
1110         }
1111         if(dst_len) {
1112                 out_size = (unsigned int)*dst_len;
1113         }
1114
1115         CRYPTO_INTERNAL_LOG("--------------------------------------------------------------");
1116         CRYPTO_INTERNAL_LOG("in_size=%d out_size=%d op->data_len=%d processed=%d", in_size, out_size, operation->data_len, total_processing_len);
1117
1118         if(operation->info.operationClass == TEE_OPERATION_CIPHER)
1119         {
1120                 if (operation->data_len != 0)
1121                 {
1122                         if (in_size < (size_t)(operation->block_len - operation->data_len)) {
1123                                 num = in_size;
1124                         } else {
1125                                 num = (size_t)(operation->block_len - operation->data_len);
1126                         }
1127
1128                         CRYPTO_INTERNAL_LOG("num=%d in_size=%d out_size=%d processed=%d", num, in_size, out_size, total_processing_len);
1129                         if(num != 0) {
1130                                 memcpy(operation->data + operation->data_len, in_data, num);
1131
1132                                 operation->data_len += num;
1133                                 in_size -= num;
1134                                 in_data = (unsigned char*)((unsigned long)in_data + num);
1135
1136                                 /* accumulated data is full */
1137                                 if (operation->data_len == operation->block_len)
1138                                 {
1139                                         processing_len = out_size;
1140                                         if (crypto_update_engine(operation, operation->data, operation->data_len, out_data, &processing_len)) {
1141                                                 goto error;
1142                                         }
1143                                         total_processing_len += processing_len;
1144                                         out_size -= processing_len;
1145                                         if (out_data) {
1146                                                 out_data = (unsigned char*)((unsigned long) out_data + processing_len);
1147                                         }
1148                                         operation->data_len = 0;
1149                                 }
1150                         }
1151
1152                         if (in_size == 0 && operation->data_len != 0) {
1153                                 in_size = operation->data_len;
1154                                 in_data = operation->data;
1155                                 operation->data_len = 0;
1156                         }
1157                         CRYPTO_INTERNAL_LOG("num=%d in_size=%d out_size=%d processed=%d", num, in_size, out_size, total_processing_len);
1158                 }
1159
1160                 // process remaining data
1161                 {
1162                         size_t should_be_processed_of_bytes = (size_t)in_size/operation->block_len*operation->block_len;
1163                         size_t remaining_number_of_bytes = in_size-should_be_processed_of_bytes;
1164
1165                         CRYPTO_INTERNAL_LOG("should_be_processed_of_bytes=%zd remaining_number_of_bytes=%zd processed=%d", should_be_processed_of_bytes, remaining_number_of_bytes, total_processing_len);
1166                         if (should_be_processed_of_bytes != 0)
1167                         {
1168                                 processing_len = out_size-total_processing_len;
1169                                 if (crypto_update_engine(operation, in_data, should_be_processed_of_bytes, out_data, &processing_len)) {
1170                                         goto error;
1171                                 }
1172                                 total_processing_len += processing_len;
1173                                 in_size -= processing_len;
1174                                 in_data = (unsigned char*)((unsigned long) in_data + processing_len);
1175                                 if (out_data) {
1176                                         out_data = (unsigned char*)((unsigned long) out_data + processing_len);
1177                                 }
1178                         }
1179
1180                         if (operation->info.mode == TEE_MODE_ENCRYPT)
1181                         {
1182                                 unsigned int pad_byte;
1183                                 size_t should_be_processed_of_pad_bytes = 0;
1184
1185                                 /* NOPAD */
1186                                 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1187                                         operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1188                                         operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD)
1189                                 {
1190                                         CRYPTO_INTERNAL_LOG("ENC NOPAD : Ignore remaining_number_of_bytes=%zd !!", remaining_number_of_bytes);
1191                                         goto exit;
1192                                 }
1193
1194                                 memcpy(operation->data, in_data, remaining_number_of_bytes);
1195                                 operation->data_len += remaining_number_of_bytes;
1196
1197                                 if (dst_len && *dst_len < total_processing_len+operation->block_len) {
1198                                         return TEE_ERROR_SHORT_BUFFER;
1199                                 }
1200
1201                                 pad_byte = operation->block_len - remaining_number_of_bytes;
1202
1203                                 if (operation->info.algorithm == TEE_ALG_AES_ECB_PKCS5 || operation->info.algorithm == TEE_ALG_AES_ECB_PKCS7 ||
1204                                         operation->info.algorithm == TEE_ALG_AES_CBC_PKCS5 || operation->info.algorithm == TEE_ALG_AES_CBC_PKCS7) {
1205                                         should_be_processed_of_pad_bytes = operation->block_len;
1206
1207                                         memset(operation->data + operation->data_len, pad_byte, pad_byte);
1208                                         CRYPTO_INTERNAL_LOG("ENC PKCS : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[0], operation->data[1], operation->data[2], operation->data[3], operation->data[4], operation->data[5], operation->data[6], operation->data[7]);
1209                                         CRYPTO_INTERNAL_LOG("ENC PKCS : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[8], operation->data[9], operation->data[10], operation->data[11], operation->data[12], operation->data[13], operation->data[14], operation->data[15]);
1210                                 } else if (operation->info.algorithm == TEE_ALG_AES_ECB_ISO9797_M1 || operation->info.algorithm == TEE_ALG_AES_CBC_ISO9797_M1) {
1211                                         if (pad_byte != 0 && (operation->block_len != pad_byte)) {
1212                                                 should_be_processed_of_pad_bytes = operation->block_len;
1213
1214                                                 memset(operation->data + operation->data_len, 0x00, pad_byte);
1215                                                 CRYPTO_INTERNAL_LOG("ENC ZERO : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[0], operation->data[1], operation->data[2], operation->data[3], operation->data[4], operation->data[5], operation->data[6], operation->data[7]);
1216                                                 CRYPTO_INTERNAL_LOG("ENC ZERO : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[8], operation->data[9], operation->data[10], operation->data[11], operation->data[12], operation->data[13], operation->data[14], operation->data[15]);
1217                                         } else {
1218                                                 should_be_processed_of_pad_bytes = 0;
1219                                         }
1220                                 } else if (operation->info.algorithm == TEE_ALG_AES_ECB_ISO9797_M2 || operation->info.algorithm == TEE_ALG_AES_CBC_ISO9797_M2) {
1221                                         should_be_processed_of_pad_bytes = operation->block_len;
1222
1223                                         memset(operation->data + operation->data_len, 0x00, pad_byte);
1224                                         CRYPTO_INTERNAL_LOG("ENC ZERO : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[0], operation->data[1], operation->data[2], operation->data[3], operation->data[4], operation->data[5], operation->data[6], operation->data[7]);
1225                                         CRYPTO_INTERNAL_LOG("ENC ZERO : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[8], operation->data[9], operation->data[10], operation->data[11], operation->data[12], operation->data[13], operation->data[14], operation->data[15]);
1226
1227                                         operation->data[operation->data_len] = 0x80;
1228                                         CRYPTO_INTERNAL_LOG("ENC ISO9797 : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[0], operation->data[1], operation->data[2], operation->data[3], operation->data[4], operation->data[5], operation->data[6], operation->data[7]);
1229                                         CRYPTO_INTERNAL_LOG("ENC ISO9797 : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[8], operation->data[9], operation->data[10], operation->data[11], operation->data[12], operation->data[13], operation->data[14], operation->data[15]);
1230                                 } else if (operation->info.algorithm == TEE_ALG_AES_CTR || operation->info.algorithm == TEE_ALG_AES_CTR_NOPAD) {
1231                                         should_be_processed_of_pad_bytes = remaining_number_of_bytes;
1232                                 }
1233
1234                                 if (crypto_final_engine(operation, operation->data, should_be_processed_of_pad_bytes, out_data, &processing_len)) {
1235                                         goto error;
1236                                 }
1237
1238                                 total_processing_len += processing_len;
1239                         } else if (operation->info.mode == TEE_MODE_DECRYPT) {
1240                                 unsigned char * pad = out_data;
1241                                 unsigned int npad = 0;
1242                                 if (pad == NULL) {
1243                                         goto exit;
1244                                 }
1245
1246                                 if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1247                                         operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1248                                         operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD || operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
1249                                         CRYPTO_INTERNAL_LOG("DEC NOPAD : Ignore remaining_number_of_bytes=%zd !!", remaining_number_of_bytes);
1250                                         goto exit;
1251                                 /* PAD */
1252                                 } else if (operation->info.algorithm == TEE_ALG_AES_ECB_PKCS5 || operation->info.algorithm == TEE_ALG_AES_ECB_PKCS7 ||
1253                                                    operation->info.algorithm == TEE_ALG_AES_CBC_PKCS5 || operation->info.algorithm == TEE_ALG_AES_CBC_PKCS7) {
1254                                         memcpy(operation->data, pad-operation->block_len, operation->block_len);
1255                                         CRYPTO_INTERNAL_LOG("DEC PKCS : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[0], operation->data[1], operation->data[2], operation->data[3], operation->data[4], operation->data[5], operation->data[6], operation->data[7]);
1256                                         CRYPTO_INTERNAL_LOG("DEC PKCS : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[8], operation->data[9], operation->data[10], operation->data[11], operation->data[12], operation->data[13], operation->data[14], operation->data[15]);
1257
1258                                         pad--; //last byte
1259                                         npad = *pad;
1260
1261                                         if (npad <= operation->block_len) { // can't be more than block length
1262                                                 unsigned int i;
1263                                                 int ok = 1;
1264                                                 for(i = 0; i < npad; i++, pad--) {
1265                                                         if (*pad != npad) {
1266                                                                 ok = 0;
1267                                                                 break;
1268                                                         }
1269                                                 }
1270
1271                                                 if (ok) {
1272                                                         if (total_processing_len >= npad)
1273                                                                 total_processing_len -= npad;        // padding OK. Othewise padding will not be removed
1274                                                         else
1275                                                                 total_processing_len = 0;
1276                                                 }
1277                                         }
1278                                 } else if (operation->info.algorithm == TEE_ALG_AES_ECB_ISO9797_M1 ||operation->info.algorithm == TEE_ALG_AES_CBC_ISO9797_M1) {
1279                                         CRYPTO_INTERNAL_LOG("DEC ISO9797 M1 : Ignore remaining_number_of_bytes=%zd !!", remaining_number_of_bytes);
1280                                         goto exit;
1281                                 } else if (operation->info.algorithm == TEE_ALG_AES_ECB_ISO9797_M2 || operation->info.algorithm == TEE_ALG_AES_CBC_ISO9797_M2) {
1282                                         memcpy(operation->data, pad-operation->block_len, operation->block_len);
1283                                         CRYPTO_INTERNAL_LOG("DEC ISO9797 M2 : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[0], operation->data[1], operation->data[2], operation->data[3], operation->data[4], operation->data[5], operation->data[6], operation->data[7]);
1284                                         CRYPTO_INTERNAL_LOG("DEC ISO9797 M2 : op->data=%2X%2X%2X%2X%2X%2X%2X%2X", operation->data[8], operation->data[9], operation->data[10], operation->data[11], operation->data[12], operation->data[13], operation->data[14], operation->data[15]);
1285
1286                                         pad--; //last byte
1287                                         npad = 0;
1288
1289                                         if (*pad == 0x00) // remove 0s
1290                                                 for (; npad < operation->block_len-1 && *pad == 0x00; npad++, pad--);
1291
1292                                         if (*pad == 0x80) { // correct M2 padding
1293                                                 npad++;        // remove 1st PAD byte 0x80
1294                                         } else { // M2 padding error
1295                                                 npad = 0;        // don't remove any padding
1296                                         }
1297
1298                                         if (total_processing_len >= npad)
1299                                                 total_processing_len -= npad;
1300                                         else
1301                                                 total_processing_len = 0;
1302                                 } else if (operation->info.algorithm == TEE_ALG_AES_CTR || operation->info.algorithm == TEE_ALG_AES_CTR_NOPAD) {
1303                                         memcpy(operation->data, in_data, remaining_number_of_bytes);
1304                                         operation->data_len += remaining_number_of_bytes;
1305
1306                                         if (crypto_final_engine(operation, operation->data, remaining_number_of_bytes, out_data, &processing_len)) {
1307                                                 goto error;
1308                                         }
1309                                         total_processing_len += remaining_number_of_bytes;
1310                                 }
1311                         } else {
1312                                 goto error;
1313                         }
1314                 }
1315         } else if (operation->info.operationClass == TEE_OPERATION_MAC || operation->info.operationClass == TEE_OPERATION_DIGEST) {
1316                 if (operation->data_len != 0) {
1317                         if (in_size < (size_t)(operation->block_len - operation->data_len)) {
1318                                 num = in_size;
1319                         } else {
1320                                 num = (size_t)(operation->block_len - operation->data_len);
1321                         }
1322
1323                         CRYPTO_INTERNAL_LOG("num=%d in_size=%d processed=%d", num, in_size, total_processing_len);
1324                         if (num != 0) {
1325                                 memcpy(operation->data + operation->data_len, in_data, num);
1326
1327                                 operation->data_len += num;
1328                                 in_size -= num;
1329                                 in_data = (unsigned char*)((unsigned long)in_data + num);
1330
1331                                 /* accumulated data is full */
1332                                 if (operation->data_len == operation->block_len) {
1333                                         if (crypto_update_engine(operation, operation->data, operation->data_len, NULL, NULL)) {
1334                                                 goto error;
1335                                         }
1336                                         operation->data_len = 0;
1337                                 }
1338                         }
1339
1340                         if (in_size == 0 && operation->data_len != 0) {
1341                                 in_size = operation->data_len;
1342                                 in_data = operation->data;
1343                                 operation->data_len = 0;
1344                         }
1345                         CRYPTO_INTERNAL_LOG("num=%d in_size=%d op->data_len=%d", num, in_size, operation->data_len);
1346                 }
1347
1348                 if (in_size != 0) {
1349                         if (crypto_final_engine(operation, in_data, in_size, out_data, &out_size)) {
1350                                 goto error;
1351                         }
1352                         total_processing_len += in_size;
1353                 }
1354         } else {
1355                 if (crypto_final_engine(operation, in_data, in_size, out_data, &out_size)) {
1356                         goto error;
1357                 }
1358                 total_processing_len += in_size;
1359         }
1360 exit:
1361         CRYPTO_INTERNAL_LOG("in_size=%d out_size=%d processed=%d", in_size, out_size, total_processing_len);
1362         CRYPTO_INTERNAL_LOG("--------------------------------------------------------------");
1363         if (operation->info.operationClass == TEE_OPERATION_CIPHER && dst_len) {
1364                 *dst_len = total_processing_len;
1365         } else if (operation->info.operationClass == TEE_OPERATION_MAC && dst_len) {
1366                 *dst_len = out_size;
1367         } else if (operation->info.operationClass == TEE_OPERATION_AE && dst_len) {
1368                 *dst_len = total_processing_len;
1369         } else if (operation->info.operationClass == TEE_OPERATION_DIGEST && dst_len) {
1370                 *dst_len = out_size;
1371         } else if (operation->info.operationClass == TEE_OPERATION_ASYMMETRIC_CIPHER && dst_len) {
1372                 *dst_len = out_size;
1373         } else if (operation->info.operationClass == TEE_OPERATION_ASYMMETRIC_SIGNATURE && dst_len) {
1374                 *dst_len = out_size;
1375         }
1376         return 0;
1377 error:
1378         LOGE(MODULE_SSF_LIB, "THIS HERE!!!");
1379         CRYPTO_INTERNAL_LOG("--------------------------------------------------------------");
1380         return -1;
1381 }
1382
1383
1384 void TEE_DigestInit(TEE_OperationHandle operation);
1385
1386 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation, uint32_t algorithm, uint32_t mode, uint32_t maxKeySize)
1387 {
1388         PERMISSION_CHECK(PERM_CRYPTO);
1389         crypto_internal_operation * op;
1390         TEE_Result rc = TEE_SUCCESS;
1391         uint32_t alg_class = 0;
1392         uint32_t key_object_type = 0;
1393         uint32_t digest_len = 0;
1394         uint32_t block_len = 0;
1395         TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
1396         TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
1397
1398         // check parameters compatibility
1399         switch(algorithm)
1400         {
1401                 /* Algorithm Class is SYMMETRIC CIPHER */
1402                 case TEE_ALG_AES_ECB_NOPAD:
1403                 case TEE_ALG_AES_CBC_NOPAD:
1404                 case TEE_ALG_AES_CTR:
1405                 case TEE_ALG_AES_CTR_NOPAD:
1406                 case TEE_ALG_AES_ECB_PKCS5:
1407                 case TEE_ALG_AES_ECB_PKCS7:
1408                 case TEE_ALG_AES_ECB_ISO9797_M1:
1409                 case TEE_ALG_AES_ECB_ISO9797_M2:
1410                 case TEE_ALG_AES_CBC_PKCS5:
1411                 case TEE_ALG_AES_CBC_PKCS7:
1412                 case TEE_ALG_AES_CBC_ISO9797_M1:
1413                 case TEE_ALG_AES_CBC_ISO9797_M2:
1414                 if (mode != TEE_MODE_ENCRYPT && mode != TEE_MODE_DECRYPT) {
1415                                 return TEE_ERROR_NOT_SUPPORTED;
1416                 }
1417
1418                         alg_class = TEE_OPERATION_CIPHER;
1419                         key_object_type = TEE_TYPE_AES;
1420                         block_len = 16;
1421                         digest_len = 0;
1422                         break;
1423
1424                 case TEE_ALG_AES_XTS:
1425                 case TEE_ALG_AES_CTS:
1426                 if (mode != TEE_MODE_ENCRYPT && mode != TEE_MODE_DECRYPT) {
1427                                 return TEE_ERROR_NOT_SUPPORTED;
1428                 }
1429
1430                         alg_class = TEE_OPERATION_CIPHER;
1431                         key_object_type = TEE_TYPE_AES;
1432                         block_len = 32; // for CTS & XTS need 2 AES blocks
1433                         digest_len = 0;
1434                         break;
1435
1436                 case TEE_ALG_DES_ECB_NOPAD:
1437                 case TEE_ALG_DES_CBC_NOPAD:
1438
1439                 if (mode != TEE_MODE_ENCRYPT && mode != TEE_MODE_DECRYPT) {
1440                                 return TEE_ERROR_NOT_SUPPORTED;
1441                 }
1442
1443                         alg_class = TEE_OPERATION_CIPHER;
1444                         key_object_type = TEE_TYPE_DES;
1445                         block_len = 8;
1446                         digest_len = 0;
1447                         break;
1448
1449                 case TEE_ALG_DES3_ECB_NOPAD:
1450                 case TEE_ALG_DES3_CBC_NOPAD:
1451                 if (mode != TEE_MODE_ENCRYPT && mode != TEE_MODE_DECRYPT) {
1452                                 return TEE_ERROR_NOT_SUPPORTED;
1453                 }
1454
1455                         alg_class = TEE_OPERATION_CIPHER;
1456                         key_object_type = TEE_TYPE_DES3;
1457                         block_len = 8;
1458                         digest_len = 0;
1459                         break;
1460
1461                 /* Algorithm Class is AE */
1462                 case TEE_ALG_AES_CCM:
1463                 if (mode != TEE_MODE_ENCRYPT && mode != TEE_MODE_DECRYPT) {
1464                                 return TEE_ERROR_NOT_SUPPORTED;
1465                 }
1466
1467                         alg_class = TEE_OPERATION_AE;
1468                         key_object_type = TEE_TYPE_AES;
1469                         block_len = 16;
1470                         digest_len = 0;
1471                         break;
1472
1473                 case TEE_ALG_AES_GCM:
1474                 if (mode != TEE_MODE_ENCRYPT && mode != TEE_MODE_DECRYPT) {
1475                                 return TEE_ERROR_NOT_SUPPORTED;
1476                 }
1477
1478                         alg_class = TEE_OPERATION_AE;
1479                         key_object_type = TEE_TYPE_AES;
1480                         block_len = 16;
1481                         digest_len = 0;
1482                         break;
1483
1484                 /* Algorithm Class is MAC */
1485                 case TEE_ALG_AES_CBC_MAC_NOPAD:
1486                 case TEE_ALG_AES_CBC_MAC_PKCS5:
1487                 case TEE_ALG_AES_CMAC:
1488                 if (mode != TEE_MODE_MAC) {
1489                                 return TEE_ERROR_NOT_SUPPORTED;
1490                 }
1491
1492                         alg_class = TEE_OPERATION_MAC;
1493                         key_object_type = TEE_TYPE_AES;
1494                         block_len = 16;
1495                         digest_len = 16;
1496                         break;
1497
1498                 case TEE_ALG_DES_CBC_MAC_NOPAD:
1499                 case TEE_ALG_DES_CBC_MAC_PKCS5:
1500                 if (mode != TEE_MODE_MAC) {
1501                                 return TEE_ERROR_NOT_SUPPORTED;
1502                 }
1503
1504                         alg_class = TEE_OPERATION_MAC;
1505                         key_object_type = TEE_TYPE_DES;
1506                         block_len = 8;
1507                         digest_len = 8;
1508                         break;
1509
1510                 case TEE_ALG_DES3_CBC_MAC_NOPAD:
1511                 case TEE_ALG_DES3_CBC_MAC_PKCS5:
1512                 if (mode != TEE_MODE_MAC) {
1513                                 return TEE_ERROR_NOT_SUPPORTED;
1514                 }
1515
1516                         alg_class = TEE_OPERATION_MAC;
1517                         key_object_type = TEE_TYPE_DES3;
1518                         block_len = 8;
1519                         digest_len = 8;
1520                         break;
1521
1522                 case TEE_ALG_HMAC_MD5:
1523                 if (mode != TEE_MODE_MAC) {
1524                                 return TEE_ERROR_NOT_SUPPORTED;
1525                 }
1526
1527                         alg_class = TEE_OPERATION_MAC;
1528                         key_object_type = TEE_TYPE_HMAC_MD5;
1529                         block_len = 64;
1530                         digest_len =    16;
1531                         break;
1532
1533                 case TEE_ALG_HMAC_SHA1:
1534                 if (mode != TEE_MODE_MAC) {
1535                                 return TEE_ERROR_NOT_SUPPORTED;
1536                 }
1537
1538                         alg_class = TEE_OPERATION_MAC;
1539                         key_object_type = TEE_TYPE_HMAC_SHA1;
1540                         block_len = 64;
1541                         digest_len =    20;
1542                         break;
1543
1544                 case TEE_ALG_HMAC_SHA224:
1545                 if (mode != TEE_MODE_MAC) {
1546                                 return TEE_ERROR_NOT_SUPPORTED;
1547                 }
1548
1549                         alg_class = TEE_OPERATION_MAC;
1550                         key_object_type = TEE_TYPE_HMAC_SHA224;
1551                         block_len = 64;
1552                         digest_len =    28;
1553                         break;
1554
1555                 case TEE_ALG_HMAC_SHA256:
1556                 if (mode != TEE_MODE_MAC) {
1557                                 return TEE_ERROR_NOT_SUPPORTED;
1558                 }
1559
1560                         alg_class = TEE_OPERATION_MAC;
1561                         key_object_type = TEE_TYPE_HMAC_SHA256;
1562                         block_len = 64;
1563                         digest_len =    32;
1564                         break;
1565
1566                 case TEE_ALG_HMAC_SHA384:
1567                 if (mode != TEE_MODE_MAC) {
1568                                 return TEE_ERROR_NOT_SUPPORTED;
1569                 }
1570
1571                         alg_class = TEE_OPERATION_MAC;
1572                         key_object_type = TEE_TYPE_HMAC_SHA384;
1573                         block_len = 64;
1574                         digest_len =    48;
1575                         break;
1576
1577                 case TEE_ALG_HMAC_SHA512:
1578                 if (mode != TEE_MODE_MAC) {
1579                                 return TEE_ERROR_NOT_SUPPORTED;
1580                 }
1581
1582                         alg_class = TEE_OPERATION_MAC;
1583                         key_object_type = TEE_TYPE_HMAC_SHA512;
1584                         block_len = 64;
1585                         digest_len =    64;
1586                         break;
1587
1588                 /* Algorithm Class is DIGIT */
1589                 case TEE_ALG_MD5:
1590                 if (mode != TEE_MODE_DIGEST) {
1591                                 return TEE_ERROR_NOT_SUPPORTED;
1592                 }
1593
1594                         alg_class = TEE_OPERATION_DIGEST;
1595                         key_object_type = 0;
1596                         digest_len = 16;
1597                         block_len = 64;
1598                         break;
1599
1600                 case TEE_ALG_SHA1:
1601                 if (mode != TEE_MODE_DIGEST) {
1602                                 return TEE_ERROR_NOT_SUPPORTED;
1603                 }
1604
1605                         alg_class = TEE_OPERATION_DIGEST;
1606                         key_object_type = 0;
1607                         digest_len = 20;
1608                         block_len = 64;
1609                         break;
1610
1611                 case TEE_ALG_SHA224:
1612                 if (mode != TEE_MODE_DIGEST) {
1613                                 return TEE_ERROR_NOT_SUPPORTED;
1614                 }
1615
1616                         alg_class = TEE_OPERATION_DIGEST;
1617                         key_object_type = 0;
1618                         digest_len = 28;
1619                         block_len = 64;
1620                         break;
1621
1622                 case TEE_ALG_SHA256:
1623                 if (mode != TEE_MODE_DIGEST) {
1624                                 return TEE_ERROR_NOT_SUPPORTED;
1625                 }
1626
1627                         alg_class = TEE_OPERATION_DIGEST;
1628                         key_object_type = 0;
1629                         digest_len = 32;
1630                         block_len = 64;
1631                         break;
1632
1633                 case TEE_ALG_SHA384:
1634                 if (mode != TEE_MODE_DIGEST) {
1635                                 return TEE_ERROR_NOT_SUPPORTED;
1636                 }
1637
1638                         alg_class = TEE_OPERATION_DIGEST;
1639                         key_object_type = 0;
1640                         digest_len = 48;
1641                         block_len = 64;
1642                         break;
1643
1644                 case TEE_ALG_SHA512:
1645                 if (mode != TEE_MODE_DIGEST) {
1646                                 return TEE_ERROR_NOT_SUPPORTED;
1647                 }
1648
1649                         alg_class = TEE_OPERATION_DIGEST;
1650                         key_object_type = 0;
1651                         digest_len = 64;
1652                         block_len = 64;
1653                         break;
1654
1655                 /* Algorithm Class is ASYMMETRIC CIPHER */
1656                 case TEE_ALG_RSAES_PKCS1_V1_5:
1657                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
1658                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
1659                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
1660                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
1661                 case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
1662                 case TEE_ALG_RSA_NOPAD:
1663                 if (mode != TEE_MODE_ENCRYPT && mode != TEE_MODE_DECRYPT) {
1664                                 return TEE_ERROR_NOT_SUPPORTED;
1665                 }
1666
1667                         alg_class = TEE_OPERATION_ASYMMETRIC_CIPHER;
1668                         key_object_type = TEE_TYPE_RSA_KEYPAIR;
1669                         block_len = 0;
1670                         digest_len =    0;
1671                         break;
1672
1673                 /* Algorithm Class is SIGNATURE */
1674                 case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
1675                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
1676                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
1677                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
1678                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
1679                 case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
1680                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
1681                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
1682                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
1683                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
1684                 case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
1685                 if (mode != TEE_MODE_SIGN && mode != TEE_MODE_VERIFY) {
1686                                 return TEE_ERROR_NOT_SUPPORTED;
1687                 }
1688
1689                         alg_class = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
1690                         key_object_type = TEE_TYPE_RSA_KEYPAIR;
1691                         break;
1692
1693                 case TEE_ALG_ECDSA_P160:
1694                 case TEE_ALG_ECDSA_P192:
1695                 case TEE_ALG_ECDSA_P224:
1696                 case TEE_ALG_ECDSA_P256:
1697                 case TEE_ALG_ECDSA_P384:
1698                 case TEE_ALG_ECDSA_P521:
1699                 if (mode != TEE_MODE_SIGN && mode != TEE_MODE_VERIFY) {
1700                                 return TEE_ERROR_NOT_SUPPORTED;
1701                 }
1702
1703                         alg_class = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
1704                         key_object_type = TEE_TYPE_RSA_KEYPAIR;
1705                         break;
1706
1707                 case TEE_ALG_DSA_SHA1:
1708                 if (mode != TEE_MODE_SIGN && mode != TEE_MODE_VERIFY) {
1709                                 return TEE_ERROR_NOT_SUPPORTED;
1710                 }
1711
1712                         alg_class = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
1713                         key_object_type = TEE_TYPE_DSA_KEYPAIR;
1714                         break;
1715
1716                 case TEE_ALG_ECDH_P192:
1717                 case TEE_ALG_ECDH_P224:
1718                 case TEE_ALG_ECDH_P256:
1719                 case TEE_ALG_ECDH_P384:
1720                 case TEE_ALG_ECDH_P521:
1721                 if (mode != TEE_MODE_SIGN && mode != TEE_MODE_VERIFY) {
1722                                 return TEE_ERROR_NOT_SUPPORTED;
1723                 }
1724
1725                         alg_class = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
1726                         key_object_type = TEE_TYPE_ECDH_KEYPAIR;
1727                         break;
1728
1729                 /* Algorithm Class is KEY DERIVATION */
1730                 case TEE_ALG_DH_DERIVE_SHARED_SECRET:
1731                 if (mode != TEE_MODE_DERIVE) {
1732                                 return TEE_ERROR_NOT_SUPPORTED;
1733                 }
1734
1735                         alg_class = TEE_OPERATION_KEY_DERIVATION;
1736                         key_object_type = TEE_TYPE_DH_KEYPAIR;
1737                         break;
1738
1739                 default:
1740                         LOGE(MODULE_SSF_LIB, "Not Support Algorithm : %X", algorithm);
1741                         rc =  TEE_ERROR_NOT_SUPPORTED;
1742                         goto exit;
1743                         break;
1744         }
1745
1746         /* first malloc for crypto operation */
1747         op = (crypto_internal_operation *)malloc(sizeof (crypto_internal_operation));
1748         if (!op) {
1749                 rc = TEE_ERROR_OUT_OF_MEMORY;
1750                 goto exit;
1751         }
1752
1753         memset(op, 0, sizeof (crypto_internal_operation));
1754
1755         /* Set TEE_OperationInfo */
1756         op->info.algorithm = algorithm;
1757         op->info.operationClass = alg_class;
1758         op->info.mode = mode;
1759         op->info.digestLength = digest_len;
1760         op->info.maxKeySize = maxKeySize;
1761         op->info.keySize = maxKeySize;
1762
1763         if (mode == TEE_MODE_ENCRYPT) {
1764                 op->info.requiredKeyUsage |= TEE_USAGE_ENCRYPT;
1765         }
1766         if (mode == TEE_MODE_DECRYPT) {
1767                 op->info.requiredKeyUsage |= TEE_USAGE_DECRYPT;
1768         }
1769         if (mode == TEE_MODE_MAC) {
1770                 op->info.requiredKeyUsage |= TEE_USAGE_MAC;
1771         }
1772         if (mode == TEE_MODE_DERIVE) {
1773                 op->info.requiredKeyUsage |= TEE_USAGE_DERIVE;
1774         }
1775         if (mode == TEE_MODE_SIGN) {
1776                 op->info.requiredKeyUsage |= TEE_USAGE_SIGN;
1777         }
1778         if (mode == TEE_MODE_VERIFY) {
1779                 op->info.requiredKeyUsage |= TEE_USAGE_VERIFY;
1780         }
1781         if (algorithm == TEE_ALG_RSA_NOPAD)
1782         {
1783                 if (mode == TEE_MODE_ENCRYPT) {
1784                         op->info.requiredKeyUsage |= TEE_USAGE_VERIFY;
1785                 } else if (mode == TEE_MODE_DECRYPT) {
1786                         op->info.requiredKeyUsage |= TEE_USAGE_SIGN;
1787                 }
1788         }
1789
1790         if (algorithm == TEE_ALG_AES_XTS) {
1791                 op->info.handleState |= TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
1792         }
1793
1794         /* get handle */
1795         if (crypto_internal_open(op) != 0) {
1796                 rc = TEE_ERROR_NOT_SUPPORTED;
1797                 goto error;
1798         }
1799
1800         /* key1 alloc */
1801         if (key_object_type) {
1802                 if (TEE_AllocateTransientObject(key_object_type, maxKeySize, &key1) != TEE_SUCCESS) {
1803                         rc = TEE_ERROR_OUT_OF_MEMORY;
1804                         goto error;
1805                 }
1806         }
1807
1808         /* key2 alloc for XTS */
1809         if (algorithm == TEE_ALG_AES_XTS) {
1810                 if (TEE_AllocateTransientObject(key_object_type, maxKeySize, &key2) != TEE_SUCCESS) {
1811                         rc = TEE_ERROR_OUT_OF_MEMORY;
1812                         goto error;
1813                 }
1814         }
1815
1816         /* key map for crypto operation */
1817         op->key1 = key1;
1818         op->key2 = key2;
1819         op->block_len = block_len;
1820
1821         *operation = (TEE_OperationHandle)op;
1822
1823         if (alg_class == TEE_OPERATION_DIGEST) {
1824                 TEE_DigestInit(*operation);
1825         }
1826
1827         return TEE_SUCCESS;
1828
1829 error:
1830         crypto_internal_close(op);
1831         if (key1) {
1832                 TEE_CloseObject(key1);
1833         }
1834         if (key2) {
1835                 TEE_CloseObject(key2);
1836         }
1837         if (op) {
1838                 free(op);
1839         }
1840 exit:
1841         *operation = TEE_HANDLE_NULL;
1842         LOGE(MODULE_SSF_LIB, "Error : %X", rc);
1843         return rc;
1844 }
1845
1846 void TEE_FreeOperation(TEE_OperationHandle operation)
1847 {
1848         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
1849
1850         crypto_internal_operation * op;
1851
1852         if (operation == TEE_HANDLE_NULL) {
1853                 return;
1854         }
1855         op = (crypto_internal_operation*)operation;
1856         if (op->key1) {
1857                 TEE_CloseObject(op->key1);
1858         }
1859         if (op->key2) {
1860                 TEE_CloseObject(op->key2);
1861         }
1862         crypto_internal_close(op);
1863         free(op);
1864         return;
1865 }
1866
1867 void TEE_GetOperationInfo(TEE_OperationHandle operation, TEE_OperationInfo* operationInfo)
1868 {
1869         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
1870
1871         crypto_internal_operation * op = (crypto_internal_operation*) operation;
1872
1873         operationInfo->algorithm = op->info.algorithm;
1874         operationInfo->digestLength = op->info.digestLength;
1875         operationInfo->handleState = op->info.handleState;
1876         operationInfo->keySize = op->info.keySize;
1877         operationInfo->maxKeySize = op->info.maxKeySize;
1878         operationInfo->mode = op->info.mode;
1879         operationInfo->operationClass = op->info.operationClass;
1880         operationInfo->requiredKeyUsage = op->info.requiredKeyUsage;
1881 }
1882
1883 void TEE_ResetOperation(TEE_OperationHandle operation)
1884 {
1885         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
1886
1887         crypto_internal_operation * op = (crypto_internal_operation*) operation;
1888         op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1889         return;
1890 }
1891
1892 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation, TEE_ObjectHandle key)
1893 {
1894         PERMISSION_CHECK(PERM_CRYPTO);
1895
1896         crypto_internal_operation * op = (crypto_internal_operation*) operation;
1897         if (!op || op->info.operationClass == TEE_OPERATION_DIGEST || op->info.algorithm == TEE_ALG_AES_XTS)
1898     {
1899         LOGE(MODULE_SSF_LIB, "op->info.operationClass == TEE_OPERATION_DIGEST\n");
1900         return TEE_ERROR_BAD_PARAMETERS;
1901     };
1902
1903         if (key == TEE_HANDLE_NULL)
1904         {
1905                 TEE_CloseObject(op->key1);
1906                 op->key1 = TEE_HANDLE_NULL;
1907                 return TEE_SUCCESS;
1908         }
1909
1910         if ((key->tr.info.objectUsage | ~(op->info.requiredKeyUsage)) != 0xffffffff)
1911     {
1912         LOGE(MODULE_SSF_LIB, "(key->tr.info.objectUsage | ~(op->info.requiredKeyUsage)) != 0xffffffff\n");
1913         return TEE_ERROR_BAD_PARAMETERS;
1914     };
1915
1916         TEE_CopyObjectAttributes(op->key1, key);
1917
1918         op->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
1919         return TEE_SUCCESS;
1920 }
1921
1922 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation, TEE_ObjectHandle key1, TEE_ObjectHandle key2)
1923 {
1924         PERMISSION_CHECK(PERM_CRYPTO);
1925
1926         crypto_internal_operation * op = (crypto_internal_operation*) operation;
1927
1928         if ( (key1 && !key2) || (!key1 && key2)) {
1929                 CRYPTO_PANIC;
1930         }
1931         if (!op || op->info.algorithm != TEE_ALG_AES_XTS) {
1932                 CRYPTO_PANIC;
1933         }
1934
1935         if (!key1 && !key2)
1936         {
1937                 TEE_CloseObject(op->key1);
1938                 TEE_CloseObject(op->key2);
1939                 op->key1 = TEE_HANDLE_NULL;
1940                 op->key2 = TEE_HANDLE_NULL;
1941                 return TEE_SUCCESS;
1942         }
1943
1944         if (key1 && (key1->tr.info.objectUsage | ~op->info.requiredKeyUsage) != 0xffffffff) {
1945                 CRYPTO_PANIC;
1946         }
1947         if (key2 && (key2->tr.info.objectUsage | ~op->info.requiredKeyUsage) != 0xffffffff) {
1948                 CRYPTO_PANIC;
1949         }
1950
1951         if(key1) {
1952                 TEE_CopyObjectAttributes(op->key1, key1);
1953         }
1954
1955         if(key2) {
1956                 TEE_CopyObjectAttributes(op->key2, key2);
1957         }
1958
1959         op->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
1960         return TEE_SUCCESS;
1961 }
1962
1963
1964 void TEE_CopyOperation(TEE_OperationHandle dstOperation, TEE_OperationHandle srcOperation)
1965 {
1966         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
1967
1968         crypto_internal_operation * dstOp = (crypto_internal_operation*) dstOperation;
1969         crypto_internal_operation * srcOp = (crypto_internal_operation*) srcOperation;
1970
1971         if (dstOp->info.mode != srcOp->info.mode || dstOp->info.algorithm != srcOp->info.algorithm) {
1972                 CRYPTO_PANIC;
1973         }
1974         if (dstOp->info.maxKeySize < srcOp->info.maxKeySize) {
1975                 CRYPTO_PANIC;
1976         }
1977
1978         dstOp->info.algorithm = srcOp->info.algorithm;
1979         dstOp->info.digestLength = srcOp->info.digestLength;
1980         dstOp->info.handleState = srcOp->info.handleState;
1981         dstOp->info.keySize = srcOp->info.keySize;
1982         dstOp->info.maxKeySize = srcOp->info.maxKeySize;
1983         dstOp->info.mode = srcOp->info.mode;
1984         dstOp->info.operationClass = srcOp->info.operationClass;
1985         dstOp->info.requiredKeyUsage = srcOp->info.requiredKeyUsage;
1986
1987         if (dstOp->key1) {
1988                 TEE_CopyObjectAttributes(dstOp->key1, srcOp->key1);
1989         }
1990         if (dstOp->key2) {
1991                 TEE_CopyObjectAttributes(dstOp->key2, srcOp->key2);
1992         }
1993         if (srcOp->crypto) {
1994                 if (crypto_internal_open(dstOp) != 0) {
1995                         CRYPTO_PANIC;
1996                 }
1997         } else {
1998                 dstOp->crypto = NULL;
1999         }
2000         return;
2001 }
2002
2003 // Message Digest Functions
2004 /*
2005 This is not GP Spec function. but I used this
2006 */
2007 void TEE_DigestInit(TEE_OperationHandle operation)
2008 {
2009         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2010
2011         if (crypto_internal_init(op, NULL, NULL, 0)) {
2012                 CRYPTO_PANIC;
2013         }
2014         op->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
2015         op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
2016         return;
2017 }
2018
2019 void TEE_DigestUpdate(TEE_OperationHandle operation, const void* chunk, size_t chunkSize)
2020 {
2021         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2022         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2023
2024         if (!op || !chunk || !chunkSize) {
2025                 return;
2026         }
2027         if (op->info.operationClass != TEE_OPERATION_DIGEST) {
2028                 CRYPTO_PANIC;
2029         }
2030         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2031                 TEE_DigestInit(operation);
2032         }
2033         if (crypto_internal_update(op, (unsigned char*)chunk, chunkSize, NULL, NULL)) {
2034                 CRYPTO_PANIC;
2035         }
2036         return;
2037 }
2038
2039 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void* chunk, size_t chunkLen, void* hash, size_t *hashLen)
2040 {
2041         PERMISSION_CHECK(PERM_CRYPTO);
2042         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2043
2044         if (!hash || *hashLen < op->info.digestLength) {
2045                 return TEE_ERROR_SHORT_BUFFER;
2046         }
2047         if (op->info.operationClass != TEE_OPERATION_DIGEST) {
2048                 CRYPTO_PANIC;
2049         }
2050         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2051                 TEE_DigestInit(operation);
2052         }
2053         if(crypto_internal_final(op, (unsigned char*)chunk, chunkLen, (unsigned char*)hash, hashLen)) {
2054                 CRYPTO_PANIC;
2055         }
2056         return TEE_SUCCESS;
2057 }
2058
2059 // Symmetric Cipher Functions
2060 void TEE_CipherInit(TEE_OperationHandle operation, const void* IV, size_t IVLen)
2061 {
2062         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2063         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2064         crypto_internal_keystruct key;
2065         unsigned char key_buf[32] = {0x0, };
2066
2067         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2068         key.secret.size = sizeof(key_buf);
2069         key.secret.buffer = key_buf;
2070
2071         if (op->info.operationClass != TEE_OPERATION_CIPHER) {
2072                 CRYPTO_PANIC;
2073         }
2074         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2075                 CRYPTO_PANIC;
2076         }
2077         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_SECRET_VALUE,
2078                 (void*)key.secret.buffer, (size_t*)&key.secret.size) != TEE_SUCCESS) {
2079                 CRYPTO_PANIC;
2080         }
2081         if (!key.secret.buffer) {
2082                 CRYPTO_PANIC;
2083         }
2084         if (crypto_internal_init(op, &key, (unsigned char*)IV, IVLen)) {
2085                 CRYPTO_PANIC;
2086         }
2087         op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
2088         return;
2089 }
2090
2091 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void* srcData, size_t srcLen, void* destData, size_t *destLen)
2092 {
2093         PERMISSION_CHECK(PERM_CRYPTO);
2094         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2095
2096         if (*destLen < srcLen) {
2097                 return TEE_ERROR_SHORT_BUFFER;
2098         }
2099         if (op->info.operationClass != TEE_OPERATION_CIPHER) {
2100                 CRYPTO_PANIC;
2101         }
2102         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2103                 CRYPTO_PANIC;
2104         }
2105         if (crypto_internal_update(op, (unsigned char*)srcData, srcLen, (unsigned char*)destData, destLen)) {
2106                 CRYPTO_PANIC;
2107         }
2108         return TEE_SUCCESS;
2109 }
2110
2111 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation, const void* srcData, size_t srcLen, void* destData, size_t *destLen)
2112 {
2113         PERMISSION_CHECK(PERM_CRYPTO);
2114         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2115
2116         if (*destLen < srcLen) {
2117                 return TEE_ERROR_SHORT_BUFFER;
2118         }
2119         if (op->info.operationClass != TEE_OPERATION_CIPHER) {
2120                 CRYPTO_PANIC;
2121         }
2122         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2123                 CRYPTO_PANIC;
2124         }
2125         if (crypto_internal_final(op, (unsigned char*)srcData, srcLen, (unsigned char*)destData, destLen)) {
2126                 CRYPTO_PANIC;
2127         }
2128         return TEE_SUCCESS;
2129 }
2130
2131 // MAC Functions
2132 void TEE_MACInit(TEE_OperationHandle operation, const void* IV, size_t IVLen)
2133 {
2134         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2135         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2136         crypto_internal_keystruct key;
2137         unsigned char key_buf[128] = {0x0, };
2138
2139         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2140         key.secret.size = sizeof(key_buf);
2141         key.secret.buffer = key_buf;
2142
2143         if (op->info.operationClass != TEE_OPERATION_MAC) {
2144                 CRYPTO_PANIC;
2145         }
2146         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2147                 CRYPTO_PANIC;
2148         }
2149         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_SECRET_VALUE,
2150                 (void*)key.secret.buffer, (size_t*)&key.secret.size) != TEE_SUCCESS) {
2151                 CRYPTO_PANIC;
2152         }
2153         if (!key.secret.buffer) {
2154                 CRYPTO_PANIC;
2155         }
2156         if (crypto_internal_init(op, &key, (unsigned char*)IV, IVLen)) {
2157                 CRYPTO_PANIC;
2158         }
2159         op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
2160         return;
2161 }
2162
2163 void TEE_MACUpdate(TEE_OperationHandle operation, const void* chunk, size_t chunkSize)
2164 {
2165         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2166         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2167
2168         if (!chunk || !chunkSize) {
2169                 return;
2170         }
2171         if (op->info.operationClass != TEE_OPERATION_MAC) {
2172                 CRYPTO_PANIC;
2173         }
2174         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2175                 CRYPTO_PANIC;
2176         }
2177         if (crypto_internal_update(op, (unsigned char*)chunk, chunkSize, NULL, NULL)) {
2178                 CRYPTO_PANIC;
2179         }
2180         return;
2181 }
2182
2183 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation, const void* message, size_t messageLen, void* mac, size_t *macLen)
2184 {
2185         PERMISSION_CHECK(PERM_CRYPTO);
2186         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2187
2188         if (!mac || *macLen < op->info.digestLength) {
2189                 return TEE_ERROR_SHORT_BUFFER;
2190         }
2191         if (op->info.operationClass != TEE_OPERATION_MAC) {
2192                 CRYPTO_PANIC;
2193         }
2194         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2195                 CRYPTO_PANIC;
2196         }
2197         if(crypto_internal_final(op, (unsigned char*)message, messageLen, (unsigned char*)mac, macLen)) {
2198                 CRYPTO_PANIC;
2199         }
2200         return TEE_SUCCESS;
2201 }
2202
2203 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, void* message, size_t messageLen, void* mac, size_t *macLen)
2204 {
2205         PERMISSION_CHECK(PERM_CRYPTO);
2206         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2207         char result[64];
2208         size_t result_len = sizeof result;
2209
2210         if (!mac || !macLen || *macLen != op->info.digestLength) {
2211                 return TEE_ERROR_MAC_INVALID;
2212         }
2213         if (TEE_MACComputeFinal(operation, (unsigned char*)message, messageLen, result, &result_len) != TEE_SUCCESS) {
2214                 return TEE_ERROR_MAC_INVALID;
2215         }
2216         if (memcmp(mac, result, *macLen)) {
2217                 return TEE_ERROR_MAC_INVALID;
2218         }
2219
2220         return TEE_SUCCESS;
2221 }
2222
2223 // Authenticated Encryption Functions
2224 TEE_Result TEE_AEInit(TEE_OperationHandle operation, void* nonce, size_t nonceLen, uint32_t tagLen, uint32_t AADLen, uint32_t payloadLen)
2225 {
2226         PERMISSION_CHECK(PERM_CRYPTO);
2227
2228         crypto_internal_operation *op = (crypto_internal_operation*)operation;
2229         crypto_internal_keystruct key;
2230         unsigned char key_buf[32] = {0x0, };
2231
2232         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2233         key.secret.size = sizeof(key_buf);
2234         key.secret.buffer = key_buf;
2235
2236         // operation check
2237         if (op->info.operationClass != TEE_OPERATION_AE) {
2238                 LOGE(MODULE_SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2239                 CRYPTO_PANIC;
2240         }
2241         if (op->info.mode != TEE_MODE_ENCRYPT && op->info.mode != TEE_MODE_DECRYPT) {
2242                 LOGE(MODULE_SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2243                 CRYPTO_PANIC;
2244         }
2245         if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2246                 LOGE(MODULE_SSF_LIB, "Key not set in operation");
2247                 CRYPTO_PANIC;
2248         }
2249         // nonce check
2250         if (nonce == NULL || nonceLen < 12) {
2251                 LOGE(MODULE_SSF_LIB, "Incorrect nonce provided");
2252                 CRYPTO_PANIC;
2253         }
2254         // tagLen check
2255         std::array<uint32_t, 5> values_GCM = {128, 120, 112, 104, 96};
2256         std::array<uint32_t, 6> values_CCM = {128, 112, 96, 64, 48, 32};
2257         switch (op->info.algorithm) {
2258                 case TEE_ALG_AES_GCM: {
2259                         if (std::find(values_GCM.begin(), values_GCM.end(), tagLen) == values_GCM.end()) {
2260                                 LOGE(MODULE_SSF_LIB, "Incorrect tag length %u", tagLen);
2261                                 return TEE_ERROR_NOT_SUPPORTED;
2262                         }
2263                         break;
2264                 }
2265                 case TEE_ALG_AES_CCM: {
2266                         if (std::find(values_CCM.begin(), values_CCM.end(), tagLen) == values_CCM.end()) {
2267                                 LOGE(MODULE_SSF_LIB, "Incorrect tag length %u", tagLen);
2268                                 return TEE_ERROR_NOT_SUPPORTED;
2269                         }
2270                         break;
2271                 }
2272                 default: {
2273                         LOGE(MODULE_SSF_LIB, "Incorrect algorithm %x", op->info.algorithm);
2274                         CRYPTO_PANIC;
2275                 }
2276         };
2277         // CCM exclusive checks
2278         if (op->info.algorithm == TEE_ALG_AES_CCM) {
2279                 // TODO support CCM and check AAD/payload here
2280                 LOGE(MODULE_SSF_LIB, "CCM mode not supported");
2281                 return TEE_ERROR_NOT_SUPPORTED;
2282         }
2283         // key check
2284         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_SECRET_VALUE,
2285                 (void*)key.secret.buffer, (size_t*)&key.secret.size) != TEE_SUCCESS) {
2286                 LOGE(MODULE_SSF_LIB, "Cannot acquire key from operation");
2287                 CRYPTO_PANIC;
2288         }
2289         if (!key.secret.buffer) {
2290                 LOGE(MODULE_SSF_LIB, "Uninitialized operation key");
2291                 CRYPTO_PANIC;
2292         }
2293
2294         if (ossl_crypto_ae_init(op, &key,
2295                                                         (unsigned char*)nonce, nonceLen, tagLen)) {
2296                 LOGE(MODULE_SSF_LIB, "Failed to initialize AE algorithm");
2297                 CRYPTO_PANIC;
2298         }
2299
2300         return TEE_SUCCESS;
2301 }
2302
2303 void TEE_AEUpdateAAD(TEE_OperationHandle operation, void* AADdata, size_t AADdataLen)
2304 {
2305         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2306         crypto_internal_operation *op = (crypto_internal_operation*)operation;
2307
2308         // operation check
2309         if (op->info.operationClass != TEE_OPERATION_AE) {
2310                 LOGE(MODULE_SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2311                 CRYPTO_PANIC;
2312         }
2313         if (op->info.mode != TEE_MODE_ENCRYPT && op->info.mode != TEE_MODE_DECRYPT) {
2314                 LOGE(MODULE_SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2315                 CRYPTO_PANIC;
2316         }
2317         if (op->crypto == 0) {
2318                 LOGE(MODULE_SSF_LIB, "Uninitialized operation handle provided");
2319                 CRYPTO_PANIC;
2320         }
2321
2322         if (ossl_crypto_ae_update_aad(op, AADdata, AADdataLen)) {
2323                 LOGE(MODULE_SSF_LIB, "Failed to update AAD data");
2324                 CRYPTO_PANIC;
2325         }
2326
2327         return;
2328 }
2329
2330 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void* srcData, size_t srcLen, void* destData, size_t *destLen)
2331 {
2332         PERMISSION_CHECK(PERM_CRYPTO);
2333         crypto_internal_operation *op = (crypto_internal_operation*)operation;
2334
2335         if (op->info.operationClass != TEE_OPERATION_AE) {
2336                 LOGE(MODULE_SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2337                 CRYPTO_PANIC;
2338         }
2339         if (op->info.mode != TEE_MODE_ENCRYPT && op->info.mode != TEE_MODE_DECRYPT) {
2340                 LOGE(MODULE_SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2341                 CRYPTO_PANIC;
2342         }
2343         if (op->crypto == 0) {
2344                 LOGE(MODULE_SSF_LIB, "Uninitialized operation handle provided");
2345                 CRYPTO_PANIC;
2346         }
2347
2348         if (ossl_crypto_ae_update(op, srcData, srcLen, destData, destLen)) {
2349                 LOGE(MODULE_SSF_LIB, "Failed to update cipher data");
2350                 CRYPTO_PANIC;
2351         }
2352
2353         return TEE_SUCCESS;
2354 }
2355
2356 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, void* srcData, size_t srcLen, void* destData, size_t* destLen, void* tag, size_t* tagLen)
2357 {
2358         PERMISSION_CHECK(PERM_CRYPTO);
2359         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2360
2361         if (op->info.operationClass != TEE_OPERATION_AE) {
2362                 LOGE(MODULE_SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2363                 CRYPTO_PANIC;
2364         }
2365         if (op->info.mode != TEE_MODE_ENCRYPT) {
2366                 LOGE(MODULE_SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2367                 CRYPTO_PANIC;
2368         }
2369         if (op->crypto == 0) {
2370                 LOGE(MODULE_SSF_LIB, "Uninitialized operation handle provided");
2371                 CRYPTO_PANIC;
2372         }
2373
2374         if (ossl_crypto_ae_enc_final(op, srcData, srcLen, destData, destLen, tag, tagLen)) {
2375                 LOGE(MODULE_SSF_LIB, "Failed to finalize AE encryption");
2376                 CRYPTO_PANIC;
2377         }
2378
2379         return TEE_SUCCESS;
2380 }
2381
2382 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, void* srcData, size_t srcLen, void* destData, size_t *destLen, void* tag, size_t tagLen)
2383 {
2384         PERMISSION_CHECK(PERM_CRYPTO);
2385         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2386         TEE_Result ret = TEE_SUCCESS;
2387
2388         if (op->info.operationClass != TEE_OPERATION_AE) {
2389                 LOGE(MODULE_SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2390                 CRYPTO_PANIC;
2391         }
2392         if (op->info.mode != TEE_MODE_DECRYPT) {
2393                 LOGE(MODULE_SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2394                 CRYPTO_PANIC;
2395         }
2396         if (op->crypto == 0) {
2397                 LOGE(MODULE_SSF_LIB, "Uninitialized operation handle provided");
2398                 CRYPTO_PANIC;
2399         }
2400
2401         ret = ossl_crypto_ae_dec_final(op, srcData, srcLen, destData, destLen, tag, tagLen);
2402         if (ret != TEE_SUCCESS && ret != TEE_ERROR_MAC_INVALID) {
2403                 LOGE(MODULE_SSF_LIB, "Failed to finalize AE decryption");
2404                 CRYPTO_PANIC;
2405         }
2406
2407         return ret;
2408 }
2409
2410 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation, const TEE_Attribute* params, uint32_t paramCount, const void* srcData, size_t srcLen, void* destData, size_t *destLen)
2411 {
2412         PERMISSION_CHECK(PERM_CRYPTO);
2413         (void)params;
2414         (void)paramCount;
2415         crypto_internal_operation *op = (crypto_internal_operation*) operation;
2416         crypto_internal_keystruct key;
2417         unsigned char module_buf[512] = {0x0, };
2418         unsigned char pub_buf[512] = {0x0, };
2419
2420         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2421         key.rsa_modulus.size = sizeof(module_buf);
2422         key.rsa_modulus.buffer = module_buf;
2423         key.rsa_public.size = sizeof(pub_buf);
2424         key.rsa_public.buffer = pub_buf;
2425
2426         if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) {
2427                 CRYPTO_PANIC;
2428         }
2429         if (op->info.mode != TEE_MODE_ENCRYPT ) {
2430                 CRYPTO_PANIC;
2431         }
2432         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2433                 CRYPTO_PANIC;
2434         }
2435         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_MODULUS,
2436                 (void*)key.rsa_modulus.buffer, (size_t*)&key.rsa_modulus.size) != TEE_SUCCESS) {
2437                 CRYPTO_PANIC;
2438         }
2439         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PUBLIC_EXPONENT,
2440                 (void*)key.rsa_public.buffer, (size_t*)&key.rsa_public.size) != TEE_SUCCESS) {
2441                 CRYPTO_PANIC;
2442         }
2443         if(!key.rsa_modulus.buffer || !key.rsa_public.buffer ) {
2444                 CRYPTO_PANIC;
2445         }
2446         if (crypto_internal_init(op, &key, NULL, 0)) {
2447                 CRYPTO_PANIC;
2448         }
2449         if (crypto_internal_final(op, (unsigned char*)srcData, srcLen, (unsigned char*)destData, destLen)) {
2450                 return TEE_ERROR_SIGNATURE_INVALID;
2451         }
2452         return TEE_SUCCESS;
2453 }
2454
2455 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation, const TEE_Attribute* params, uint32_t paramCount, const void* srcData, size_t srcLen, void* destData, size_t *destLen)
2456 {
2457         PERMISSION_CHECK(PERM_CRYPTO);
2458
2459         (void)params;
2460         (void)paramCount;
2461         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2462         crypto_internal_keystruct key;
2463
2464         unsigned char module_buf[512] = {0x0, };
2465         unsigned char pub_buf[512] = {0x0, };
2466         unsigned char priv_buf[512] = {0x0, };
2467
2468         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2469         key.rsa_modulus.size = sizeof(module_buf);
2470         key.rsa_modulus.buffer = module_buf;
2471         key.rsa_public.size = sizeof(pub_buf);
2472         key.rsa_public.buffer = pub_buf;
2473         key.rsa_private.size = sizeof(priv_buf);
2474         key.rsa_private.buffer = priv_buf;
2475
2476         if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) {
2477                 CRYPTO_PANIC;
2478         }
2479         if (op->info.mode != TEE_MODE_DECRYPT) {
2480                 CRYPTO_PANIC;
2481         }
2482         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2483                 CRYPTO_PANIC;
2484         }
2485         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_MODULUS,
2486                 (void*)key.rsa_modulus.buffer, (size_t*)&key.rsa_modulus.size) != TEE_SUCCESS) {
2487                 CRYPTO_PANIC;
2488         }
2489         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PUBLIC_EXPONENT,
2490                 (void*)key.rsa_public.buffer, (size_t*)&key.rsa_public.size) != TEE_SUCCESS) {
2491                 CRYPTO_PANIC;
2492         }
2493         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIVATE_EXPONENT,
2494                 (void*)key.rsa_private.buffer, (size_t*)&key.rsa_private.size) != TEE_SUCCESS) {
2495                 CRYPTO_PANIC;
2496         }
2497 #if 0 /* Not Support */
2498         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIME1,
2499                 (void*)key.rsa_prime1.buffer, (size_t*)&key.rsa_prime1.size) != TEE_SUCCESS) {
2500                 CRYPTO_PANIC;
2501         }
2502         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIME2,
2503                 (void*)key.rsa_prime2.buffer, (size_t*)&key.rsa_prime2.size) != TEE_SUCCESS) {
2504                 CRYPTO_PANIC;
2505         }
2506         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_EXPONENT1,
2507                 (void*)key.rsa_exponent1.buffer, (size_t*)&key.rsa_exponent1.size) != TEE_SUCCESS) {
2508                 CRYPTO_PANIC;
2509         }
2510         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_EXPONENT2,
2511                 (void*)key.rsa_exponent2.buffer, (size_t*)&key.rsa_exponent2.size) != TEE_SUCCESS) {
2512                 CRYPTO_PANIC;
2513         }
2514         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_COEFFICIENT,
2515                 (void*)key.rsa_coefficient.buffer, (size_t*)&key.rsa_coefficient.size) != TEE_SUCCESS) {
2516                 CRYPTO_PANIC;
2517         }
2518 #endif
2519         if(!key.rsa_modulus.buffer || !key.rsa_public.buffer || !key.rsa_private.buffer
2520                 /*|| !key.rsa_prime1.buffer || !key.rsa_prime2.buffer || !key.rsa_exponent1.buffer
2521                 || !key.rsa_exponent2.buffer || !key.rsa_coefficient.buffer*/) {
2522                 CRYPTO_PANIC;
2523         }
2524         if (crypto_internal_init(op, &key, NULL, 0)) {
2525                 CRYPTO_PANIC;
2526         }
2527         if (crypto_internal_final(op, (unsigned char*)srcData, srcLen, (unsigned char*)destData, destLen)) {
2528                 CRYPTO_PANIC;
2529         }
2530         return TEE_SUCCESS;
2531 }
2532
2533 TEE_Result TEE_AsymmetricSignDigest( TEE_OperationHandle operation, const TEE_Attribute* params, uint32_t paramCount, const void* digest, size_t digestLen, void* signature, size_t *signatureLen)
2534 {
2535         PERMISSION_CHECK(PERM_CRYPTO);
2536         (void)params;
2537         (void)paramCount;
2538         crypto_internal_operation *op = (crypto_internal_operation*) operation;
2539         crypto_internal_keystruct key;
2540
2541         unsigned char module_buf[384] = {0x0, };
2542         unsigned char pub_buf[384] = {0x0, };
2543         unsigned char priv_buf[384] = {0x0, };
2544
2545         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2546         key.rsa_modulus.size = sizeof(module_buf);
2547         key.rsa_modulus.buffer = module_buf;
2548         key.rsa_public.size = sizeof(pub_buf);
2549         key.rsa_public.buffer = pub_buf;
2550         key.rsa_private.size = sizeof(priv_buf);
2551         key.rsa_private.buffer = priv_buf;
2552
2553         if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) {
2554                 CRYPTO_PANIC;
2555         }
2556         if (op->info.mode != TEE_MODE_SIGN ) {
2557                 CRYPTO_PANIC;
2558         }
2559         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2560                 CRYPTO_PANIC;
2561         }
2562         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_MODULUS,
2563                 (void*)key.rsa_modulus.buffer, (size_t*)&key.rsa_modulus.size) != TEE_SUCCESS) {
2564                 CRYPTO_PANIC;
2565         }
2566         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PUBLIC_EXPONENT,
2567                 (void*)key.rsa_public.buffer, (size_t*)&key.rsa_public.size) != TEE_SUCCESS) {
2568                 CRYPTO_PANIC;
2569         }
2570         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIVATE_EXPONENT,
2571                 (void*)key.rsa_private.buffer, (size_t*)&key.rsa_private.size) != TEE_SUCCESS) {
2572                 CRYPTO_PANIC;
2573         }
2574 #if 0 /* Not Support */
2575         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIME1,
2576                 (void*)key.rsa_prime1.buffer, (size_t*)&key.rsa_prime1.size) != TEE_SUCCESS) {
2577                 CRYPTO_PANIC;
2578         }
2579         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIME2,
2580                 (void*)key.rsa_prime2.buffer, (size_t*)&key.rsa_prime2.size) != TEE_SUCCESS) {
2581                 CRYPTO_PANIC;
2582         }
2583         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_EXPONENT1,
2584                 (void*)key.rsa_exponent1.buffer, (size_t*)&key.rsa_exponent1.size) != TEE_SUCCESS) {
2585                 CRYPTO_PANIC;
2586         }
2587         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_EXPONENT2,
2588                 (void*)key.rsa_exponent2.buffer, (size_t*)&key.rsa_exponent2.size) != TEE_SUCCESS) {
2589                 CRYPTO_PANIC;
2590         }
2591         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_COEFFICIENT,
2592                 (void*)key.rsa_coefficient.buffer, (size_t*)&key.rsa_coefficient.size) != TEE_SUCCESS) {
2593                 CRYPTO_PANIC;
2594         }
2595 #endif
2596         if(!key.rsa_modulus.buffer || !key.rsa_public.buffer || !key.rsa_private.buffer
2597                 /*|| !key.rsa_prime1.buffer || !key.rsa_prime2.buffer || !key.rsa_exponent1.buffer
2598                 || !key.rsa_exponent2.buffer || !key.rsa_coefficient.buffer*/) {
2599                 CRYPTO_PANIC;
2600         }
2601         if (crypto_internal_init(op, &key, NULL, 0)) {
2602                 CRYPTO_PANIC;
2603         }
2604         if (crypto_internal_final(op, (unsigned char*)digest, digestLen, (unsigned char*)signature, signatureLen)) {
2605                 return TEE_ERROR_SHORT_BUFFER;
2606         }
2607         return TEE_SUCCESS;
2608 }
2609
2610 TEE_Result TEE_AsymmetricVerifyDigest( TEE_OperationHandle operation, const TEE_Attribute* params, uint32_t paramCount, const void* digest, size_t digestLen, void* signature, size_t signatureLen)
2611 {
2612         PERMISSION_CHECK(PERM_CRYPTO);
2613         (void)params;
2614         (void)paramCount;
2615         crypto_internal_operation *op = (crypto_internal_operation*) operation;
2616         crypto_internal_keystruct key;
2617         size_t sign_len=signatureLen;
2618
2619         unsigned char module_buf[384] = {0x0, };
2620         unsigned char pub_buf[384] = {0x0, };
2621
2622         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2623         key.rsa_modulus.size = sizeof(module_buf);
2624         key.rsa_modulus.buffer = module_buf;
2625         key.rsa_public.size = sizeof(pub_buf);
2626         key.rsa_public.buffer = pub_buf;
2627
2628         if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) {
2629                 CRYPTO_PANIC;
2630         }
2631         if (op->info.mode != TEE_MODE_VERIFY ) {
2632                 CRYPTO_PANIC;
2633         }
2634         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2635                 CRYPTO_PANIC;
2636         }
2637         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_MODULUS,
2638                 (void*)key.rsa_modulus.buffer, (size_t*)&key.rsa_modulus.size) != TEE_SUCCESS) {
2639                 CRYPTO_PANIC;
2640         }
2641         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PUBLIC_EXPONENT,
2642                 (void*)key.rsa_public.buffer, (size_t*)&key.rsa_public.size) != TEE_SUCCESS) {
2643                 CRYPTO_PANIC;
2644         }
2645         if(!key.rsa_modulus.buffer || !key.rsa_public.buffer ) {
2646                 CRYPTO_PANIC;
2647         }
2648         if (crypto_internal_init(op, &key, NULL, 0)) {
2649                 CRYPTO_PANIC;
2650         }
2651         if (crypto_internal_final(op, (unsigned char*)digest, digestLen, (unsigned char*)signature, &sign_len)) {
2652                 return TEE_ERROR_SIGNATURE_INVALID;
2653         }
2654         return TEE_SUCCESS;
2655 }
2656
2657 // Key Derivation Functions
2658 void TEE_DeriveKey( TEE_OperationHandle operation, TEE_Attribute* params, uint32_t paramCount, TEE_ObjectHandle derivedKey)
2659 {
2660         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2661         (void)operation;
2662         (void)params;
2663         (void)paramCount;
2664         (void)derivedKey;
2665         return;
2666 }
2667
2668 void TEE_GenerateRandom(void* randomBuffer, size_t randomBufferLen)
2669 {
2670         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2671         crypto_internal_operation op;
2672         crypto_internal_keystruct key;
2673         unsigned char random[512] = {0};
2674         size_t random_len=512;
2675         memset((void *)&op,0,sizeof(op));
2676         if(randomBufferLen > 512)
2677         {
2678                 LOGE(MODULE_SSF_LIB, "currently only support less than 512 byte random data");
2679                 return;
2680         }
2681         op.info.algorithm = TEE_ALG_GENERATE_SECRET_KEY;
2682         op.info.keySize = randomBufferLen;
2683         /*cryptocore need bit_length*/
2684         key.secret.buffer = random;
2685         key.secret.size = random_len*8;
2686
2687         if (crypto_internal_open(&op) != 0) {
2688                 CRYPTO_PANIC;
2689         }
2690         if (crypto_internal_init(&op, &key, NULL, 0)) {
2691                 CRYPTO_PANIC;
2692         }
2693         if (crypto_internal_final(&op, NULL, 0, NULL, NULL)) {
2694                 CRYPTO_PANIC;
2695         }
2696         if (crypto_internal_close(&op)) {
2697                 CRYPTO_PANIC;
2698         }
2699         memcpy(randomBuffer, random, randomBufferLen);
2700         return;
2701 }