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