96f948965421d92ab7731ecb992a9e7e9bd97cd5
[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->tr.info.objectUsage | ~op->info.requiredKeyUsage) != 0xffffffff) {
2000                 CRYPTO_PANIC;
2001         }
2002         if ((key2->tr.info.objectUsage | ~op->info.requiredKeyUsage) != 0xffffffff) {
2003                 CRYPTO_PANIC;
2004         }
2005
2006         TEE_CopyObjectAttributes(op->key1, key1);
2007         TEE_CopyObjectAttributes(op->key2, key2);
2008
2009         op->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
2010         return TEE_SUCCESS;
2011 }
2012
2013
2014 void TEE_CopyOperation( TEE_OperationHandle dstOperation, TEE_OperationHandle srcOperation)
2015 {
2016         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2017
2018         crypto_internal_operation * dstOp = (crypto_internal_operation*) dstOperation;
2019         crypto_internal_operation * srcOp = (crypto_internal_operation*) srcOperation;
2020
2021         if (dstOp->info.mode != srcOp->info.mode || dstOp->info.algorithm != srcOp->info.algorithm) {
2022                 CRYPTO_PANIC;
2023         }
2024         if (dstOp->info.maxKeySize < srcOp->info.maxKeySize) {
2025                 CRYPTO_PANIC;
2026         }
2027
2028         dstOp->info.algorithm = srcOp->info.algorithm;
2029         dstOp->info.digestLength = srcOp->info.digestLength;
2030         dstOp->info.handleState = srcOp->info.handleState;
2031         dstOp->info.keySize = srcOp->info.keySize;
2032         dstOp->info.maxKeySize = srcOp->info.maxKeySize;
2033         dstOp->info.mode = srcOp->info.mode;
2034         dstOp->info.operationClass = srcOp->info.operationClass;
2035         dstOp->info.requiredKeyUsage = srcOp->info.requiredKeyUsage;
2036
2037         if (dstOp->key1) {
2038                 TEE_CopyObjectAttributes(dstOp->key1, srcOp->key1);
2039         }
2040         if (dstOp->key2) {
2041                 TEE_CopyObjectAttributes(dstOp->key2, srcOp->key2);
2042         }
2043         if (srcOp->crypto) {
2044                 if (crypto_internal_open(dstOp) != 0) {
2045                         CRYPTO_PANIC;
2046                 }
2047         }
2048         else {
2049                 dstOp->crypto = NULL;
2050         }
2051         return;
2052 }
2053
2054 // Message Digest Functions
2055 /*
2056 This is not GP Spec function. but I used this
2057 */
2058 void TEE_DigestInit(TEE_OperationHandle operation)
2059 {
2060         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2061
2062         if (crypto_internal_init(op, NULL, NULL, 0)) {
2063                 CRYPTO_PANIC;
2064         }
2065         op->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
2066         op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
2067         return;
2068 }
2069
2070 void TEE_DigestUpdate( TEE_OperationHandle operation, const void* chunk, size_t chunkSize)
2071 {
2072         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2073         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2074
2075         if (!op || !chunk || !chunkSize) {
2076                 return;
2077         }
2078         if (op->info.operationClass != TEE_OPERATION_DIGEST) {
2079                 CRYPTO_PANIC;
2080         }
2081         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2082                 TEE_DigestInit(operation);
2083         }
2084         if (crypto_internal_update(op, (unsigned char*)chunk, chunkSize, NULL, NULL)) {
2085                 CRYPTO_PANIC;
2086         }
2087         return;
2088 }
2089
2090 TEE_Result TEE_DigestDoFinal( TEE_OperationHandle operation, const void* chunk, size_t chunkLen, void* hash, size_t *hashLen)
2091 {
2092         PERMISSION_CHECK(PERM_CRYPTO);
2093         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2094
2095         if (!hash || *hashLen < op->info.digestLength) {
2096                 return TEE_ERROR_SHORT_BUFFER;
2097         }
2098         if (op->info.operationClass != TEE_OPERATION_DIGEST) {
2099                 CRYPTO_PANIC;
2100         }
2101         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2102                 TEE_DigestInit(operation);
2103         }
2104         if(crypto_internal_final(op, (unsigned char*)chunk, chunkLen, (unsigned char*)hash, hashLen)) {
2105                 CRYPTO_PANIC;
2106         }
2107         return TEE_SUCCESS;
2108 }
2109
2110 // Symmetric Cipher Functions
2111 void TEE_CipherInit( TEE_OperationHandle operation, const void* IV, size_t IVLen)
2112 {
2113         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2114         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2115         crypto_internal_keystruct key;
2116         unsigned char key_buf[32] = {0x0, };
2117
2118         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2119         key.secret.size = sizeof(key_buf);
2120         key.secret.buffer = key_buf;
2121
2122         if (op->info.operationClass != TEE_OPERATION_CIPHER) {
2123                 CRYPTO_PANIC;
2124         }
2125         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2126                 CRYPTO_PANIC;
2127         }
2128         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_SECRET_VALUE,
2129                 (void*)key.secret.buffer, (size_t*)&key.secret.size) != TEE_SUCCESS) {
2130                 CRYPTO_PANIC;
2131         }
2132         if (!key.secret.buffer) {
2133                 CRYPTO_PANIC;
2134         }
2135         if (crypto_internal_init(op, &key, (unsigned char*)IV, IVLen)) {
2136                 CRYPTO_PANIC;
2137         }
2138         op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
2139         return;
2140 }
2141
2142 TEE_Result TEE_CipherUpdate( TEE_OperationHandle operation, const void* srcData, size_t srcLen, void* destData, size_t *destLen)
2143 {
2144         PERMISSION_CHECK(PERM_CRYPTO);
2145         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2146
2147         if (*destLen < srcLen) {
2148                 return TEE_ERROR_SHORT_BUFFER;
2149         }
2150         if (op->info.operationClass != TEE_OPERATION_CIPHER) {
2151                 CRYPTO_PANIC;
2152         }
2153         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2154                 CRYPTO_PANIC;
2155         }
2156         if (crypto_internal_update(op, (unsigned char*)srcData, srcLen, (unsigned char*)destData, destLen)) {
2157                 CRYPTO_PANIC;
2158         }
2159         return TEE_SUCCESS;
2160 }
2161
2162 TEE_Result TEE_CipherDoFinal( TEE_OperationHandle operation, const void* srcData, size_t srcLen, void* destData, size_t *destLen)
2163 {
2164         PERMISSION_CHECK(PERM_CRYPTO);
2165         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2166
2167         if (*destLen < srcLen) {
2168                 return TEE_ERROR_SHORT_BUFFER;
2169         }
2170         if (op->info.operationClass != TEE_OPERATION_CIPHER) {
2171                 CRYPTO_PANIC;
2172         }
2173         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2174                 CRYPTO_PANIC;
2175         }
2176         if (crypto_internal_final(op, (unsigned char*)srcData, srcLen, (unsigned char*)destData, destLen)) {
2177                 CRYPTO_PANIC;
2178         }
2179         return TEE_SUCCESS;
2180 }
2181
2182 // MAC Functions
2183 void TEE_MACInit( TEE_OperationHandle operation, const void* IV, size_t IVLen)
2184 {
2185         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2186         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2187         crypto_internal_keystruct key;
2188         unsigned char key_buf[128] = {0x0, };
2189
2190         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2191         key.secret.size = sizeof(key_buf);
2192         key.secret.buffer = key_buf;
2193
2194         if (op->info.operationClass != TEE_OPERATION_MAC) {
2195                 CRYPTO_PANIC;
2196         }
2197         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2198                 CRYPTO_PANIC;
2199         }
2200         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_SECRET_VALUE,
2201                 (void*)key.secret.buffer, (size_t*)&key.secret.size) != TEE_SUCCESS) {
2202                 CRYPTO_PANIC;
2203         }
2204         if (!key.secret.buffer) {
2205                 CRYPTO_PANIC;
2206         }
2207         if (crypto_internal_init(op, &key, (unsigned char*)IV, IVLen)) {
2208                 CRYPTO_PANIC;
2209         }
2210         op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
2211         return;
2212 }
2213
2214 void TEE_MACUpdate( TEE_OperationHandle operation, const void* chunk, size_t chunkSize)
2215 {
2216         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2217         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2218
2219         if (!chunk || !chunkSize) {
2220                 return;
2221         }
2222         if (op->info.operationClass != TEE_OPERATION_MAC) {
2223                 CRYPTO_PANIC;
2224         }
2225         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2226                 CRYPTO_PANIC;
2227         }
2228         if (crypto_internal_update(op, (unsigned char*)chunk, chunkSize, NULL, NULL)) {
2229                 CRYPTO_PANIC;
2230         }
2231         return;
2232 }
2233
2234 TEE_Result TEE_MACComputeFinal( TEE_OperationHandle operation, const void* message, size_t messageLen, void* mac, size_t *macLen)
2235 {
2236         PERMISSION_CHECK(PERM_CRYPTO);
2237         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2238
2239         if (!mac || *macLen < op->info.digestLength) {
2240                 return TEE_ERROR_SHORT_BUFFER;
2241         }
2242         if (op->info.operationClass != TEE_OPERATION_MAC) {
2243                 CRYPTO_PANIC;
2244         }
2245         if (!(op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED)) {
2246                 CRYPTO_PANIC;
2247         }
2248         if(crypto_internal_final(op, (unsigned char*)message, messageLen, (unsigned char*)mac, macLen)) {
2249                 CRYPTO_PANIC;
2250         }
2251         return TEE_SUCCESS;
2252 }
2253
2254 TEE_Result TEE_MACCompareFinal( TEE_OperationHandle operation, void* message, size_t messageLen, void* mac, size_t *macLen)
2255 {
2256         PERMISSION_CHECK(PERM_CRYPTO);
2257         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2258         char result[64];
2259         size_t result_len = sizeof result;
2260
2261         if (!mac || !macLen || *macLen != op->info.digestLength) {
2262                 return TEE_ERROR_MAC_INVALID;
2263         }
2264         if (TEE_MACComputeFinal(operation, (unsigned char*)message, messageLen, result, &result_len) != TEE_SUCCESS) {
2265                 return TEE_ERROR_MAC_INVALID;
2266         }
2267         if (memcmp(mac, result, *macLen)) {
2268                 return TEE_ERROR_MAC_INVALID;
2269         }
2270
2271         return TEE_SUCCESS;
2272 }
2273
2274 // Authenticated Encryption Functions
2275 TEE_Result TEE_AEInit(TEE_OperationHandle operation, void* nonce, size_t nonceLen, uint32_t tagLen, uint32_t AADLen, uint32_t payloadLen)
2276 {
2277         PERMISSION_CHECK(PERM_CRYPTO);
2278
2279         crypto_internal_operation *op = (crypto_internal_operation*)operation;
2280         crypto_internal_keystruct key;
2281         unsigned char key_buf[32] = {0x0, };
2282
2283         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2284         key.secret.size = sizeof(key_buf);
2285         key.secret.buffer = key_buf;
2286
2287         // operation check
2288         if (op->info.operationClass != TEE_OPERATION_AE) {
2289                 LOGE(SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2290                 CRYPTO_PANIC;
2291         }
2292         if (op->info.mode != TEE_MODE_ENCRYPT && op->info.mode != TEE_MODE_DECRYPT) {
2293                 LOGE(SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2294                 CRYPTO_PANIC;
2295         }
2296         if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2297                 LOGE(SSF_LIB, "Key not set in operation");
2298                 CRYPTO_PANIC;
2299         }
2300         // nonce check
2301         if (nonce == NULL || nonceLen < 12) {
2302                 LOGE(SSF_LIB, "Incorrect nonce provided");
2303                 CRYPTO_PANIC;
2304         }
2305         // tagLen check
2306         switch (op->info.algorithm) {
2307                 case TEE_ALG_AES_GCM: {
2308                         switch (tagLen) {
2309                                 case 128:
2310                                 case 120:
2311                                 case 112:
2312                                 case 104:
2313                                 case 96:
2314                                         break;
2315                                 default:
2316                                         LOGE(SSF_LIB, "Incorrect tag length %u", tagLen);
2317                                         return TEE_ERROR_NOT_SUPPORTED;
2318                         };
2319                         break;
2320                 }
2321                 case TEE_ALG_AES_CCM: {
2322                         switch (tagLen) {
2323                                 case 128:
2324                                 case 112:
2325                                 case 96:
2326                                 case 64:
2327                                 case 48:
2328                                 case 32:
2329                                         break;
2330                                 default:
2331                                         LOGE(SSF_LIB, "Incorrect tag length %u", tagLen);
2332                                         return TEE_ERROR_NOT_SUPPORTED;
2333                         };
2334                         break;
2335                 }
2336                 default: {
2337                         LOGE(SSF_LIB, "Incorrect algorithm %x", op->info.algorithm);
2338                         CRYPTO_PANIC;
2339                 }
2340         };
2341         // CCM exclusive checks
2342         if (op->info.algorithm == TEE_ALG_AES_CCM) {
2343                 // TODO support CCM and check AAD/payload here
2344                 LOGE(SSF_LIB, "CCM mode not supported");
2345                 return TEE_ERROR_NOT_SUPPORTED;
2346         }
2347         // key check
2348         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_SECRET_VALUE,
2349                 (void*)key.secret.buffer, (size_t*)&key.secret.size) != TEE_SUCCESS) {
2350                 LOGE(SSF_LIB, "Cannot acquire key from operation");
2351                 CRYPTO_PANIC;
2352         }
2353         if (!key.secret.buffer) {
2354                 LOGE(SSF_LIB, "Uninitialized operation key");
2355                 CRYPTO_PANIC;
2356         }
2357
2358         if (ossl_crypto_ae_init(op, &key,
2359                                                         (unsigned char*)nonce, nonceLen, tagLen)) {
2360                 LOGE(SSF_LIB, "Failed to initialize AE algorithm");
2361                 CRYPTO_PANIC;
2362         }
2363
2364         return TEE_SUCCESS;
2365 }
2366
2367 void TEE_AEUpdateAAD(TEE_OperationHandle operation, void* AADdata, size_t AADdataLen)
2368 {
2369         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2370         crypto_internal_operation *op = (crypto_internal_operation*)operation;
2371
2372         // operation check
2373         if (op->info.operationClass != TEE_OPERATION_AE) {
2374                 LOGE(SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2375                 CRYPTO_PANIC;
2376         }
2377         if (op->info.mode != TEE_MODE_ENCRYPT && op->info.mode != TEE_MODE_DECRYPT) {
2378                 LOGE(SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2379                 CRYPTO_PANIC;
2380         }
2381         if (op->crypto == 0) {
2382                 LOGE(SSF_LIB, "Uninitialized operation handle provided");
2383                 CRYPTO_PANIC;
2384         }
2385
2386         if (ossl_crypto_ae_update_aad(op, AADdata, AADdataLen)) {
2387                 LOGE(SSF_LIB, "Failed to update AAD data");
2388                 CRYPTO_PANIC;
2389         }
2390
2391         return;
2392 }
2393
2394 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void* srcData, size_t srcLen, void* destData, size_t *destLen)
2395 {
2396         PERMISSION_CHECK(PERM_CRYPTO);
2397         crypto_internal_operation *op = (crypto_internal_operation*)operation;
2398
2399         if (op->info.operationClass != TEE_OPERATION_AE) {
2400                 LOGE(SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2401                 CRYPTO_PANIC;
2402         }
2403         if (op->info.mode != TEE_MODE_ENCRYPT && op->info.mode != TEE_MODE_DECRYPT) {
2404                 LOGE(SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2405                 CRYPTO_PANIC;
2406         }
2407         if (op->crypto == 0) {
2408                 LOGE(SSF_LIB, "Uninitialized operation handle provided");
2409                 CRYPTO_PANIC;
2410         }
2411
2412         if (ossl_crypto_ae_update(op, srcData, srcLen, destData, destLen)) {
2413                 LOGE(SSF_LIB, "Failed to update cipher data");
2414                 CRYPTO_PANIC;
2415         }
2416
2417         return TEE_SUCCESS;
2418 }
2419
2420 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation, void* srcData, size_t srcLen, void* destData, size_t* destLen, void* tag, size_t* tagLen)
2421 {
2422         PERMISSION_CHECK(PERM_CRYPTO);
2423         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2424
2425         if (op->info.operationClass != TEE_OPERATION_AE) {
2426                 LOGE(SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2427                 CRYPTO_PANIC;
2428         }
2429         if (op->info.mode != TEE_MODE_ENCRYPT) {
2430                 LOGE(SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2431                 CRYPTO_PANIC;
2432         }
2433         if (op->crypto == 0) {
2434                 LOGE(SSF_LIB, "Uninitialized operation handle provided");
2435                 CRYPTO_PANIC;
2436         }
2437
2438         if (ossl_crypto_ae_enc_final(op, srcData, srcLen, destData, destLen, tag, tagLen)) {
2439                 LOGE(SSF_LIB, "Failed to finalize AE encryption");
2440                 CRYPTO_PANIC;
2441         }
2442
2443         return TEE_SUCCESS;
2444 }
2445
2446 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation, void* srcData, size_t srcLen, void* destData, size_t *destLen, void* tag, size_t tagLen)
2447 {
2448         PERMISSION_CHECK(PERM_CRYPTO);
2449         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2450         TEE_Result ret = TEE_SUCCESS;
2451
2452         if (op->info.operationClass != TEE_OPERATION_AE) {
2453                 LOGE(SSF_LIB, "Incorrect operation class %x", op->info.operationClass);
2454                 CRYPTO_PANIC;
2455         }
2456         if (op->info.mode != TEE_MODE_DECRYPT) {
2457                 LOGE(SSF_LIB, "Incorrect operation mode %x", op->info.mode);
2458                 CRYPTO_PANIC;
2459         }
2460         if (op->crypto == 0) {
2461                 LOGE(SSF_LIB, "Uninitialized operation handle provided");
2462                 CRYPTO_PANIC;
2463         }
2464
2465         ret = ossl_crypto_ae_dec_final(op, srcData, srcLen, destData, destLen, tag, tagLen);
2466         if (ret != TEE_SUCCESS && ret != TEE_ERROR_MAC_INVALID) {
2467                 LOGE(SSF_LIB, "Failed to finalize AE decryption");
2468                 CRYPTO_PANIC;
2469         }
2470
2471         return ret;
2472 }
2473
2474 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)
2475 {
2476         PERMISSION_CHECK(PERM_CRYPTO);
2477         (void)params;
2478         (void)paramCount;
2479         crypto_internal_operation *op = (crypto_internal_operation*) operation;
2480         crypto_internal_keystruct key;
2481         unsigned char module_buf[512] = {0x0, };
2482         unsigned char pub_buf[512] = {0x0, };
2483
2484         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2485         key.rsa_modulus.size = sizeof(module_buf);
2486         key.rsa_modulus.buffer = module_buf;
2487         key.rsa_public.size = sizeof(pub_buf);
2488         key.rsa_public.buffer = pub_buf;
2489
2490         if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) {
2491                 CRYPTO_PANIC;
2492         }
2493         if (op->info.mode != TEE_MODE_ENCRYPT ) {
2494                 CRYPTO_PANIC;
2495         }
2496         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2497                 CRYPTO_PANIC;
2498         }
2499         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_MODULUS,
2500                 (void*)key.rsa_modulus.buffer, (size_t*)&key.rsa_modulus.size) != TEE_SUCCESS) {
2501                 CRYPTO_PANIC;
2502         }
2503         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PUBLIC_EXPONENT,
2504                 (void*)key.rsa_public.buffer, (size_t*)&key.rsa_public.size) != TEE_SUCCESS) {
2505                 CRYPTO_PANIC;
2506         }
2507         if(!key.rsa_modulus.buffer || !key.rsa_public.buffer ) {
2508                 CRYPTO_PANIC;
2509         }
2510         if (crypto_internal_init(op, &key, NULL, 0)) {
2511                 CRYPTO_PANIC;
2512         }
2513         if (crypto_internal_final(op, (unsigned char*)srcData, srcLen, (unsigned char*)destData, destLen)) {
2514                 return TEE_ERROR_SIGNATURE_INVALID;
2515         }
2516         return TEE_SUCCESS;
2517 }
2518
2519 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)
2520 {
2521         PERMISSION_CHECK(PERM_CRYPTO);
2522
2523         (void)params;
2524         (void)paramCount;
2525         crypto_internal_operation * op = (crypto_internal_operation*) operation;
2526         crypto_internal_keystruct key;
2527
2528         unsigned char module_buf[512] = {0x0, };
2529         unsigned char pub_buf[512] = {0x0, };
2530         unsigned char priv_buf[512] = {0x0, };
2531
2532         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2533         key.rsa_modulus.size = sizeof(module_buf);
2534         key.rsa_modulus.buffer = module_buf;
2535         key.rsa_public.size = sizeof(pub_buf);
2536         key.rsa_public.buffer = pub_buf;
2537         key.rsa_private.size = sizeof(priv_buf);
2538         key.rsa_private.buffer = priv_buf;
2539
2540         if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER) {
2541                 CRYPTO_PANIC;
2542         }
2543         if (op->info.mode != TEE_MODE_DECRYPT) {
2544                 CRYPTO_PANIC;
2545         }
2546         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2547                 CRYPTO_PANIC;
2548         }
2549         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_MODULUS,
2550                 (void*)key.rsa_modulus.buffer, (size_t*)&key.rsa_modulus.size) != TEE_SUCCESS) {
2551                 CRYPTO_PANIC;
2552         }
2553         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PUBLIC_EXPONENT,
2554                 (void*)key.rsa_public.buffer, (size_t*)&key.rsa_public.size) != TEE_SUCCESS) {
2555                 CRYPTO_PANIC;
2556         }
2557         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIVATE_EXPONENT,
2558                 (void*)key.rsa_private.buffer, (size_t*)&key.rsa_private.size) != TEE_SUCCESS) {
2559                 CRYPTO_PANIC;
2560         }
2561 #if 0 /* Not Support */
2562         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIME1,
2563                 (void*)key.rsa_prime1.buffer, (size_t*)&key.rsa_prime1.size) != TEE_SUCCESS) {
2564                 CRYPTO_PANIC;
2565         }
2566         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIME2,
2567                 (void*)key.rsa_prime2.buffer, (size_t*)&key.rsa_prime2.size) != TEE_SUCCESS) {
2568                 CRYPTO_PANIC;
2569         }
2570         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_EXPONENT1,
2571                 (void*)key.rsa_exponent1.buffer, (size_t*)&key.rsa_exponent1.size) != TEE_SUCCESS) {
2572                 CRYPTO_PANIC;
2573         }
2574         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_EXPONENT2,
2575                 (void*)key.rsa_exponent2.buffer, (size_t*)&key.rsa_exponent2.size) != TEE_SUCCESS) {
2576                 CRYPTO_PANIC;
2577         }
2578         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_COEFFICIENT,
2579                 (void*)key.rsa_coefficient.buffer, (size_t*)&key.rsa_coefficient.size) != TEE_SUCCESS) {
2580                 CRYPTO_PANIC;
2581         }
2582 #endif
2583         if(!key.rsa_modulus.buffer || !key.rsa_public.buffer || !key.rsa_private.buffer
2584                 /*|| !key.rsa_prime1.buffer || !key.rsa_prime2.buffer || !key.rsa_exponent1.buffer
2585                 || !key.rsa_exponent2.buffer || !key.rsa_coefficient.buffer*/) {
2586                 CRYPTO_PANIC;
2587         }
2588         if (crypto_internal_init(op, &key, NULL, 0)) {
2589                 CRYPTO_PANIC;
2590         }
2591         if (crypto_internal_final(op, (unsigned char*)srcData, srcLen, (unsigned char*)destData, destLen)) {
2592                 CRYPTO_PANIC;
2593         }
2594         return TEE_SUCCESS;
2595 }
2596
2597 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)
2598 {
2599         PERMISSION_CHECK(PERM_CRYPTO);
2600         (void)params;
2601         (void)paramCount;
2602         crypto_internal_operation *op = (crypto_internal_operation*) operation;
2603         crypto_internal_keystruct key;
2604
2605         unsigned char module_buf[384] = {0x0, };
2606         unsigned char pub_buf[384] = {0x0, };
2607         unsigned char priv_buf[384] = {0x0, };
2608
2609         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2610         key.rsa_modulus.size = sizeof(module_buf);
2611         key.rsa_modulus.buffer = module_buf;
2612         key.rsa_public.size = sizeof(pub_buf);
2613         key.rsa_public.buffer = pub_buf;
2614         key.rsa_private.size = sizeof(priv_buf);
2615         key.rsa_private.buffer = priv_buf;
2616
2617         if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) {
2618                 CRYPTO_PANIC;
2619         }
2620         if (op->info.mode != TEE_MODE_SIGN ) {
2621                 CRYPTO_PANIC;
2622         }
2623         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2624                 CRYPTO_PANIC;
2625         }
2626         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_MODULUS,
2627                 (void*)key.rsa_modulus.buffer, (size_t*)&key.rsa_modulus.size) != TEE_SUCCESS) {
2628                 CRYPTO_PANIC;
2629         }
2630         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PUBLIC_EXPONENT,
2631                 (void*)key.rsa_public.buffer, (size_t*)&key.rsa_public.size) != TEE_SUCCESS) {
2632                 CRYPTO_PANIC;
2633         }
2634         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIVATE_EXPONENT,
2635                 (void*)key.rsa_private.buffer, (size_t*)&key.rsa_private.size) != TEE_SUCCESS) {
2636                 CRYPTO_PANIC;
2637         }
2638 #if 0 /* Not Support */
2639         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIME1,
2640                 (void*)key.rsa_prime1.buffer, (size_t*)&key.rsa_prime1.size) != TEE_SUCCESS) {
2641                 CRYPTO_PANIC;
2642         }
2643         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PRIME2,
2644                 (void*)key.rsa_prime2.buffer, (size_t*)&key.rsa_prime2.size) != TEE_SUCCESS) {
2645                 CRYPTO_PANIC;
2646         }
2647         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_EXPONENT1,
2648                 (void*)key.rsa_exponent1.buffer, (size_t*)&key.rsa_exponent1.size) != TEE_SUCCESS) {
2649                 CRYPTO_PANIC;
2650         }
2651         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_EXPONENT2,
2652                 (void*)key.rsa_exponent2.buffer, (size_t*)&key.rsa_exponent2.size) != TEE_SUCCESS) {
2653                 CRYPTO_PANIC;
2654         }
2655         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_COEFFICIENT,
2656                 (void*)key.rsa_coefficient.buffer, (size_t*)&key.rsa_coefficient.size) != TEE_SUCCESS) {
2657                 CRYPTO_PANIC;
2658         }
2659 #endif
2660         if(!key.rsa_modulus.buffer || !key.rsa_public.buffer || !key.rsa_private.buffer
2661                 /*|| !key.rsa_prime1.buffer || !key.rsa_prime2.buffer || !key.rsa_exponent1.buffer
2662                 || !key.rsa_exponent2.buffer || !key.rsa_coefficient.buffer*/) {
2663                 CRYPTO_PANIC;
2664         }
2665         if (crypto_internal_init(op, &key, NULL, 0)) {
2666                 CRYPTO_PANIC;
2667         }
2668         if (crypto_internal_final(op, (unsigned char*)digest, digestLen, (unsigned char*)signature, signatureLen)) {
2669                 return TEE_ERROR_SHORT_BUFFER;
2670         }
2671         return TEE_SUCCESS;
2672 }
2673
2674 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)
2675 {
2676         PERMISSION_CHECK(PERM_CRYPTO);
2677         (void)params;
2678         (void)paramCount;
2679         crypto_internal_operation *op = (crypto_internal_operation*) operation;
2680         crypto_internal_keystruct key;
2681         size_t sign_len=signatureLen;
2682
2683         unsigned char module_buf[384] = {0x0, };
2684         unsigned char pub_buf[384] = {0x0, };
2685
2686         memset(&key, 0x00, sizeof(crypto_internal_keystruct));
2687         key.rsa_modulus.size = sizeof(module_buf);
2688         key.rsa_modulus.buffer = module_buf;
2689         key.rsa_public.size = sizeof(pub_buf);
2690         key.rsa_public.buffer = pub_buf;
2691
2692         if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE) {
2693                 CRYPTO_PANIC;
2694         }
2695         if (op->info.mode != TEE_MODE_VERIFY ) {
2696                 CRYPTO_PANIC;
2697         }
2698         if (!(op->info.handleState & TEE_HANDLE_FLAG_KEY_SET)) {
2699                 CRYPTO_PANIC;
2700         }
2701         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_MODULUS,
2702                 (void*)key.rsa_modulus.buffer, (size_t*)&key.rsa_modulus.size) != TEE_SUCCESS) {
2703                 CRYPTO_PANIC;
2704         }
2705         if (TEE_GetObjectBufferAttribute(op->key1, TEE_ATTR_RSA_PUBLIC_EXPONENT,
2706                 (void*)key.rsa_public.buffer, (size_t*)&key.rsa_public.size) != TEE_SUCCESS) {
2707                 CRYPTO_PANIC;
2708         }
2709         if(!key.rsa_modulus.buffer || !key.rsa_public.buffer ) {
2710                 CRYPTO_PANIC;
2711         }
2712         if (crypto_internal_init(op, &key, NULL, 0)) {
2713                 CRYPTO_PANIC;
2714         }
2715         if (crypto_internal_final(op, (unsigned char*)digest, digestLen, (unsigned char*)signature, &sign_len)) {
2716                 return TEE_ERROR_SIGNATURE_INVALID;
2717         }
2718         return TEE_SUCCESS;
2719 }
2720
2721 // Key Derivation Functions
2722 void TEE_DeriveKey( TEE_OperationHandle operation, TEE_Attribute* params, uint32_t paramCount, TEE_ObjectHandle derivedKey)
2723 {
2724         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2725         (void)operation;
2726         (void)params;
2727         (void)paramCount;
2728         (void)derivedKey;
2729         return;
2730 }
2731
2732 void TEE_GenerateRandom(void* randomBuffer, size_t randomBufferLen)
2733 {
2734         PERMISSION_CHECK_RETURN_VOID(PERM_CRYPTO);
2735         crypto_internal_operation op;
2736         crypto_internal_keystruct key;
2737         unsigned char random[512] = {0};
2738         size_t random_len=512;
2739         memset((void *)&op,0,sizeof(op));
2740         if(randomBufferLen > 512)
2741         {
2742                 LOGE(SSF_LIB, "currently only support less than 512 byte random data");
2743                 return;
2744         }
2745         op.info.algorithm = TEE_ALG_GENERATE_SECRET_KEY;
2746         op.info.keySize = randomBufferLen;
2747         /*cryptocore need bit_length*/
2748         key.secret.buffer = random;
2749         key.secret.size = random_len*8;
2750
2751         if (crypto_internal_open(&op)!=0) {
2752                 CRYPTO_PANIC;
2753         }
2754         if (crypto_internal_init(&op, &key, NULL, 0)) {
2755                 CRYPTO_PANIC;
2756         }
2757         if (crypto_internal_final(&op, NULL, 0, NULL, NULL)) {
2758                 CRYPTO_PANIC;
2759         }
2760         if (crypto_internal_close(&op)) {
2761                 CRYPTO_PANIC;
2762         }
2763         memcpy(randomBuffer, random, randomBufferLen);
2764         return;
2765 }