57071f0197d3f521da2cf660ac286f86dc5d08b9
[platform/core/security/key-manager.git] / src / manager / client-capi / ckmc-type.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  *
16  *
17  * @file        ckmc-type.cpp
18  * @author      Yuseok Jeon(yuseok.jeon@samsung.com)
19  * @version     1.0
20  * @brief       new and free methods for the struct of CAPI
21  */
22
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ckm/ckm-type.h>
28 #include <ckmc/ckmc-type.h>
29 #include <ckmc/ckmc-error.h>
30 #include <ckmc-type-converter.h>
31 #include <protocols.h>
32 #include <openssl/x509v3.h>
33 #include <openssl/pkcs12.h>
34 #include <openssl/evp.h>
35 #include <openssl/pem.h>
36 #include <fstream>
37 #include <crypto-init.h>
38
39 namespace {
40
41 const size_t DEFAULT_IV_LEN = 16;
42 const size_t DEFAULT_IV_LEN_BITS = 8*DEFAULT_IV_LEN;
43 const size_t DEFAULT_KEY_LEN_BITS = 4096;
44
45 int _ckmc_load_cert_from_x509(X509 *xCert, ckmc_cert_s **cert)
46 {
47     if(xCert == NULL) {
48         return CKMC_ERROR_INVALID_FORMAT;
49     }
50
51     BIO *bcert = BIO_new(BIO_s_mem());
52
53     i2d_X509_bio(bcert, xCert);
54
55     CKM::RawBuffer output(8196);
56     int size = BIO_read(bcert, output.data(), output.size());
57     BIO_free_all(bcert);
58     if (size <= 0) {
59         return CKMC_ERROR_INVALID_FORMAT;
60     }
61     output.resize(size);
62
63     return ckmc_cert_new(output.data(), output.size(), CKMC_FORM_DER, cert);
64 }
65
66 } // namespace anonymous
67
68
69 const char * const ckmc_label_name_separator    = CKM::LABEL_NAME_SEPARATOR;
70 const char * const ckmc_owner_id_separator      = CKM::LABEL_NAME_SEPARATOR;
71 const char * const ckmc_owner_id_system         = CKM::OWNER_ID_SYSTEM;
72
73 KEY_MANAGER_CAPI
74 int ckmc_key_new(unsigned char *raw_key, size_t key_size, ckmc_key_type_e key_type, char *password, ckmc_key_s **ppkey)
75 {
76     ckmc_key_s *pkey;
77
78     if(raw_key == NULL || key_size <= 0 || ppkey == NULL) {
79         return CKMC_ERROR_INVALID_PARAMETER;
80     }
81
82     pkey = static_cast<ckmc_key_s*>(malloc(sizeof(ckmc_key_s)));
83     if(pkey == NULL) {
84         return CKMC_ERROR_OUT_OF_MEMORY;
85     }
86     pkey->raw_key = reinterpret_cast<unsigned char*>(malloc(key_size));
87     if(pkey->raw_key == NULL) {
88         free(pkey);
89         return CKMC_ERROR_OUT_OF_MEMORY;
90     }
91     memcpy(pkey->raw_key, raw_key, key_size);
92
93     pkey->key_size = key_size;
94     pkey->key_type = key_type;
95
96     if(password != NULL) {
97         pkey->password = reinterpret_cast<char*>(malloc(strlen(password) +1));
98         if(pkey->password == NULL) {
99             free(pkey->raw_key);
100             free(pkey);
101             return CKMC_ERROR_OUT_OF_MEMORY;
102         }
103         memset(pkey->password, 0, strlen(password) +1);
104         strncpy(pkey->password, password, strlen(password));
105     }else {
106         pkey->password = NULL;
107     }
108
109     *ppkey = pkey;
110
111     return CKMC_ERROR_NONE;
112 }
113
114 KEY_MANAGER_CAPI
115 void ckmc_key_free(ckmc_key_s *key)
116 {
117     if(key == NULL)
118         return;
119
120     if(key->password != NULL)
121         free(key->password);
122     if(key->raw_key != NULL) {
123         memset(key->raw_key, 0, key->key_size);
124         free(key->raw_key);
125     }
126
127     free(key);
128 }
129
130 KEY_MANAGER_CAPI
131 int ckmc_buffer_new(unsigned char *data, size_t size,ckmc_raw_buffer_s **ppbuffer)
132 {
133     ckmc_raw_buffer_s *pbuff;
134
135     if(data == NULL || size <= 0 || ppbuffer == NULL) {
136         return CKMC_ERROR_INVALID_PARAMETER;
137     }
138
139     pbuff = static_cast<ckmc_raw_buffer_s*>(malloc(sizeof(ckmc_raw_buffer_s)));
140     if(pbuff == NULL)
141             return CKMC_ERROR_OUT_OF_MEMORY;
142
143     pbuff->data = reinterpret_cast<unsigned char*>(malloc(size));
144     if(pbuff->data == NULL) {
145         free(pbuff);
146         return CKMC_ERROR_OUT_OF_MEMORY;
147     }
148     memcpy(pbuff->data, data, size);
149
150     pbuff->size = size;
151     *ppbuffer = pbuff;
152
153     return CKMC_ERROR_NONE;
154 }
155
156 KEY_MANAGER_CAPI
157 void ckmc_buffer_free(ckmc_raw_buffer_s *buffer)
158 {
159     if(buffer == NULL)
160         return;
161
162     if(buffer->data != NULL) {
163         memset(buffer->data, 0, buffer->size);
164         free(buffer->data);
165     }
166     free(buffer);
167 }
168
169 KEY_MANAGER_CAPI
170 int ckmc_cert_new(unsigned char *raw_cert, size_t cert_size, ckmc_data_format_e data_format, ckmc_cert_s **ppcert)
171 {
172     ckmc_cert_s *pcert;
173
174     if(raw_cert == NULL || cert_size <= 0 || ppcert == NULL) {
175         return CKMC_ERROR_INVALID_PARAMETER;
176     }
177
178     pcert = static_cast<ckmc_cert_s*>(malloc(sizeof(ckmc_cert_s)));
179     if(pcert == NULL) {
180         return CKMC_ERROR_OUT_OF_MEMORY;
181     }
182     pcert->raw_cert = reinterpret_cast<unsigned char*>(malloc(cert_size));
183     if(pcert->raw_cert == NULL) {
184         free(pcert);
185         return CKMC_ERROR_OUT_OF_MEMORY;
186     }
187     memcpy(pcert->raw_cert, raw_cert, cert_size);
188
189     pcert->cert_size = cert_size;
190     pcert->data_format = data_format;
191
192     *ppcert = pcert;
193     return CKMC_ERROR_NONE;
194 }
195
196 KEY_MANAGER_CAPI
197 int ckmc_load_cert_from_file(const char *file_path, ckmc_cert_s **cert)
198 {
199     CKM::initOpenSslOnce();
200
201     FILE *fp = fopen(file_path, "r");
202     if(fp == NULL)
203         return CKMC_ERROR_FILE_ACCESS_DENIED;
204     X509 *pcert = NULL;
205     if(!(pcert = d2i_X509_fp(fp, NULL))) {
206         fseek(fp, 0, SEEK_SET);
207         pcert = PEM_read_X509(fp, NULL, NULL, NULL);
208     }
209     fclose(fp);
210     if(pcert == NULL) {
211         return CKMC_ERROR_INVALID_FORMAT;
212     }
213
214     int ret = _ckmc_load_cert_from_x509(pcert, cert);
215     if(ret != CKMC_ERROR_NONE) {
216         X509_free(pcert);
217     }
218     return ret;
219 }
220
221 KEY_MANAGER_CAPI
222 void ckmc_cert_free(ckmc_cert_s *cert)
223 {
224     if(cert == NULL)
225         return;
226
227     if(cert->raw_cert != NULL) {
228         memset(cert->raw_cert, 0, cert->cert_size);
229         free(cert->raw_cert);
230     }
231     free(cert);
232 }
233
234 KEY_MANAGER_CAPI
235 int ckmc_pkcs12_new(ckmc_key_s *private_key, ckmc_cert_s *cert,
236         ckmc_cert_list_s *ca_cert_list, ckmc_pkcs12_s **pkcs12_bundle)
237 {
238     ckmc_pkcs12_s *pkcs12;
239
240     if(!pkcs12_bundle ||
241        (private_key==NULL && cert==NULL && (ca_cert_list==NULL || ca_cert_list->cert==NULL))) {
242         return CKMC_ERROR_INVALID_PARAMETER;
243     }
244
245     pkcs12 = static_cast<ckmc_pkcs12_s*>(malloc(sizeof(ckmc_pkcs12_s)));
246     if(pkcs12 == NULL) {
247         return CKMC_ERROR_OUT_OF_MEMORY;
248     }
249     // ownership is transferred into pkcs12 - mentioned in the docs
250     pkcs12->priv_key = private_key;
251     pkcs12->cert = cert;
252     pkcs12->ca_chain = ca_cert_list;
253
254     *pkcs12_bundle = pkcs12;
255     return CKMC_ERROR_NONE;
256 }
257
258 KEY_MANAGER_CAPI
259 int ckmc_load_from_pkcs12_file(const char *file_path, const char *passphrase, ckmc_key_s **private_key, ckmc_cert_s **ckmcert, ckmc_cert_list_s **ca_cert_list)
260 {
261     class Pkcs12Converter {
262     private:
263         FILE* fp_in;
264         PKCS12* p12;
265         EVP_PKEY* pkey;
266         X509* x509Cert;
267         STACK_OF(X509)* ca;
268
269         int ret;
270     public:
271         ckmc_key_s *retPrivateKey;
272         ckmc_cert_s *retCkmCert;
273         ckmc_cert_list_s *retCaCertList;
274
275         Pkcs12Converter(){
276             fp_in = NULL;
277             p12 = NULL;
278             pkey = NULL;
279             x509Cert = NULL;
280             ca = NULL;
281             ret = CKMC_ERROR_NONE;
282             retPrivateKey = NULL;
283             retCkmCert = NULL;
284             retCaCertList = NULL;
285         };
286         ~Pkcs12Converter(){
287             if(fp_in != NULL)
288                 fclose(fp_in);
289             if(p12 != NULL)
290                 PKCS12_free(p12);
291             if(x509Cert != NULL)
292                 X509_free(x509Cert);
293             if(pkey != NULL)
294                 EVP_PKEY_free(pkey);
295             if(ca != NULL)
296                 sk_X509_pop_free(ca, X509_free);
297
298             if(ret != CKMC_ERROR_NONE) {
299                 if(retPrivateKey != NULL){
300                     ckmc_key_free(retPrivateKey);
301                     retPrivateKey = NULL;
302                 }
303                 if(retCkmCert != NULL) {
304                     ckmc_cert_free(retCkmCert);
305                     retCkmCert = NULL;
306                 }
307                 if(retCaCertList != NULL) {
308                     ckmc_cert_list_all_free(retCaCertList);
309                     retCaCertList = NULL;
310                 }
311             }
312         };
313
314         int parsePkcs12(const char *filePath, const char *pass) {
315             fp_in = NULL;
316             if(!(fp_in = fopen(filePath, "rb"))) {
317                 return CKMC_ERROR_FILE_ACCESS_DENIED;
318             }
319
320             if(!(p12 = d2i_PKCS12_fp(fp_in, NULL))) {
321                 return CKMC_ERROR_INVALID_FORMAT;
322             }
323
324             /* parse PKCS#12 certificate */
325             if((ret = PKCS12_parse(p12, pass, &pkey, &x509Cert, &ca)) != 1) {
326                 return CKMC_ERROR_INVALID_FORMAT;
327             }
328             return CKMC_ERROR_NONE;
329         }
330
331         int toCkmCert() {
332             if( (ret =_ckmc_load_cert_from_x509(x509Cert,&retCkmCert)) != CKMC_ERROR_NONE) {
333                 return ret;
334             }
335             return CKMC_ERROR_NONE;
336         }
337
338         int toCkmKey() {
339             BIO *bkey = BIO_new(BIO_s_mem());
340
341             i2d_PrivateKey_bio(bkey, pkey);
342
343             CKM::RawBuffer output(8196);
344             int size = BIO_read(bkey, output.data(), output.size());
345             BIO_free_all(bkey);
346             if (size <= 0) {
347                 return CKMC_ERROR_INVALID_FORMAT;
348             }
349             output.resize(size);
350
351             int type = EVP_PKEY_type(pkey->type);
352             ckmc_key_type_e key_type = CKMC_KEY_NONE;
353             switch(type) {
354             case EVP_PKEY_RSA :
355                 key_type = CKMC_KEY_RSA_PRIVATE;
356                 break;
357             case EVP_PKEY_DSA :
358                 key_type = CKMC_KEY_DSA_PRIVATE;
359                 break;
360             case EVP_PKEY_EC :
361                 key_type = CKMC_KEY_ECDSA_PRIVATE;
362                 break;
363             }
364             if(key_type == CKMC_KEY_NONE) {
365                 return CKMC_ERROR_INVALID_FORMAT;
366             }
367
368             char *nullPassword = NULL;
369
370             return ckmc_key_new(output.data(), size, key_type, nullPassword, &retPrivateKey);
371         }
372
373         int toCaCkmCertList() {
374             int tmpRet;
375             X509* popedCert = NULL;
376             ckmc_cert_s *popedCkmCert = NULL;
377             ckmc_cert_list_s *tmpCertList = NULL;
378             while((popedCert = sk_X509_pop(ca)) != NULL) {
379                 if( (tmpRet =_ckmc_load_cert_from_x509(popedCert, &popedCkmCert)) != CKMC_ERROR_NONE) {
380                     return CKMC_ERROR_OUT_OF_MEMORY;
381                 }
382                 if(tmpCertList == NULL) { // first
383                     tmpRet = ckmc_cert_list_new(popedCkmCert, &tmpCertList);
384                     retCaCertList = tmpCertList;
385                 }else {
386                     tmpRet = ckmc_cert_list_add(tmpCertList, popedCkmCert, &tmpCertList);
387                 }
388                 if(tmpRet != CKMC_ERROR_NONE) {
389                     ckmc_cert_list_all_free(retCaCertList);
390                     retCaCertList = NULL;
391                     return tmpRet;
392                 }
393             }
394             return CKMC_ERROR_NONE;
395         }
396
397     };
398
399     CKM::initOpenSslOnce();
400
401     int ret = CKMC_ERROR_NONE;
402
403     Pkcs12Converter converter;
404     if((ret = converter.parsePkcs12(file_path, passphrase)) != CKMC_ERROR_NONE) {
405         return ret;
406     }
407     if((ret = converter.toCkmCert()) != CKMC_ERROR_NONE) {
408         return ret;
409     }
410     if((ret = converter.toCkmKey()) != CKMC_ERROR_NONE) {
411         return ret;
412     }
413     if((ret = converter.toCaCkmCertList()) != CKMC_ERROR_NONE) {
414         return ret;
415     }
416
417     *private_key = converter.retPrivateKey;
418     *ckmcert = converter.retCkmCert;
419     *ca_cert_list = converter.retCaCertList;
420
421     return CKMC_ERROR_NONE;
422 }
423
424 KEY_MANAGER_CAPI
425 int ckmc_pkcs12_load(const char *file_path, const char *passphrase, ckmc_pkcs12_s **pkcs12_bundle)
426 {
427     int ec;
428     ckmc_key_s *private_key = 0;
429     ckmc_cert_s *cert = 0;
430     ckmc_cert_list_s *ca_cert_list = 0;
431
432     if(!file_path || !pkcs12_bundle)
433         return CKMC_ERROR_INVALID_PARAMETER;
434
435     ec = ckmc_load_from_pkcs12_file(file_path, passphrase, &private_key, &cert, &ca_cert_list);
436     if(ec != CKMC_ERROR_NONE)
437         return ec;
438
439     ec = ckmc_pkcs12_new(private_key, cert, ca_cert_list, pkcs12_bundle);
440     if(ec != CKMC_ERROR_NONE)
441     {
442         ckmc_key_free(private_key);
443         ckmc_cert_free(cert);
444         ckmc_cert_list_free(ca_cert_list);
445         return ec;
446     }
447
448     return CKMC_ERROR_NONE;
449 }
450
451 KEY_MANAGER_CAPI
452 void ckmc_pkcs12_free(ckmc_pkcs12_s *pkcs12)
453 {
454     if(pkcs12 == NULL)
455         return;
456
457     ckmc_key_free(pkcs12->priv_key);
458     ckmc_cert_free(pkcs12->cert);
459     ckmc_cert_list_free(pkcs12->ca_chain);
460     free(pkcs12);
461 }
462
463 KEY_MANAGER_CAPI
464 int ckmc_alias_list_new(char *alias, ckmc_alias_list_s **ppalias_list)
465 {
466     ckmc_alias_list_s *previous = NULL;
467     return ckmc_alias_list_add(previous, alias, ppalias_list);
468 }
469
470 KEY_MANAGER_CAPI
471 int ckmc_alias_list_add(ckmc_alias_list_s *previous, char *alias, ckmc_alias_list_s **pplast)
472 {
473     ckmc_alias_list_s *plist;
474
475     if(alias == NULL || pplast == NULL) {
476         return CKMC_ERROR_INVALID_PARAMETER;
477     }
478
479     plist = static_cast<ckmc_alias_list_s*>(malloc(sizeof(ckmc_alias_list_s)));
480     if(plist == NULL) {
481         return CKMC_ERROR_OUT_OF_MEMORY;
482     }
483
484     plist->alias = alias;
485     plist->next = NULL;
486
487     if(previous != NULL) {
488         previous->next = plist;
489     }
490     *pplast = plist;
491
492     return CKMC_ERROR_NONE;
493 }
494
495 KEY_MANAGER_CAPI
496 void ckmc_alias_list_free(ckmc_alias_list_s *first)
497 {
498     ckmc_alias_list_s *next = first;
499     while (next) {
500         ckmc_alias_list_s *current = next;
501         next = current->next;
502         free(current);
503     }
504 }
505
506 KEY_MANAGER_CAPI
507 void ckmc_alias_list_all_free(ckmc_alias_list_s *first)
508 {
509     ckmc_alias_list_s *next = first;
510     while (next) {
511         ckmc_alias_list_s *current = next;
512         next = current->next;
513         free(current->alias);
514         free(current);
515     }
516 }
517
518 KEY_MANAGER_CAPI
519 int ckmc_cert_list_new(ckmc_cert_s *cert, ckmc_cert_list_s **ppalias_list)
520 {
521     ckmc_cert_list_s *previous = NULL;
522     return ckmc_cert_list_add(previous, cert, ppalias_list);
523 }
524
525 KEY_MANAGER_CAPI
526 int ckmc_cert_list_add(ckmc_cert_list_s *previous, ckmc_cert_s *cert, ckmc_cert_list_s **pplast)
527 {
528     ckmc_cert_list_s *plist;
529
530     if(cert == NULL || pplast == NULL) {
531         return CKMC_ERROR_INVALID_PARAMETER;
532     }
533
534     plist = static_cast<ckmc_cert_list_s*>(malloc(sizeof(ckmc_cert_list_s)));
535     if(plist == NULL) {
536         return CKMC_ERROR_OUT_OF_MEMORY;
537     }
538     plist->cert = cert;
539     plist->next = NULL;
540
541     if(previous != NULL) {
542         previous->next = plist;
543     }
544
545     *pplast = plist;
546
547     return CKMC_ERROR_NONE;
548 }
549
550 KEY_MANAGER_CAPI
551 void ckmc_cert_list_free(ckmc_cert_list_s *first)
552 {
553     ckmc_cert_list_s *next = first;
554     while (next) {
555         ckmc_cert_list_s *current = next;
556         next = current->next;
557         free(current);
558     }
559 }
560
561 KEY_MANAGER_CAPI
562 void ckmc_cert_list_all_free(ckmc_cert_list_s *first)
563 {
564     ckmc_cert_list_s *next = first;
565     while (next) {
566         ckmc_cert_list_s *current = next;
567         next = current->next;
568         ckmc_cert_free(current->cert);
569         free(current);
570     }
571 }
572
573 KEY_MANAGER_CAPI
574 int ckmc_param_list_new(ckmc_param_list_h *pparams)
575 {
576     if (!pparams || *pparams)
577         return CKMC_ERROR_INVALID_PARAMETER;
578
579     *pparams = reinterpret_cast<ckmc_param_list_h>(new(std::nothrow)(CKM::CryptoAlgorithm));
580     if (!*pparams)
581         return CKMC_ERROR_OUT_OF_MEMORY;
582     return CKMC_ERROR_NONE;
583 }
584
585 KEY_MANAGER_CAPI
586 int ckmc_param_list_set_integer(ckmc_param_list_h params,
587                                 ckmc_param_name_e name,
588                                 uint64_t value)
589 {
590     if (!params)
591         return CKMC_ERROR_INVALID_PARAMETER;
592
593     CKM::CryptoAlgorithm* algo = reinterpret_cast<CKM::CryptoAlgorithm*>(params);
594     bool ret = algo->setParam(static_cast<CKM::ParamName>(name), value);
595     return (ret ? CKMC_ERROR_NONE : CKMC_ERROR_INVALID_PARAMETER);
596 }
597
598 KEY_MANAGER_CAPI
599 int ckmc_param_list_set_buffer(ckmc_param_list_h params,
600                                ckmc_param_name_e name,
601                                const ckmc_raw_buffer_s *buffer)
602 {
603     if (!params || !buffer || !buffer->data || buffer->size == 0)
604         return CKMC_ERROR_INVALID_PARAMETER;
605
606     CKM::CryptoAlgorithm* algo = reinterpret_cast<CKM::CryptoAlgorithm*>(params);
607     CKM::RawBuffer b(buffer->data, buffer->data + buffer->size);
608     bool ret =  algo->setParam(static_cast<CKM::ParamName>(name), b);
609     return (ret ? CKMC_ERROR_NONE : CKMC_ERROR_INVALID_PARAMETER);
610 }
611
612 KEY_MANAGER_CAPI
613 int ckmc_param_list_get_integer(ckmc_param_list_h params,
614                                 ckmc_param_name_e name,
615                                 uint64_t *pvalue)
616 {
617     if (!params || !pvalue)
618         return CKMC_ERROR_INVALID_PARAMETER;
619
620     const CKM::CryptoAlgorithm* algo = reinterpret_cast<const CKM::CryptoAlgorithm*>(params);
621     if (!algo->getParam(static_cast<CKM::ParamName>(name), *pvalue))
622         return CKMC_ERROR_INVALID_PARAMETER;
623
624     return CKMC_ERROR_NONE;
625 }
626
627 KEY_MANAGER_CAPI
628 int ckmc_param_list_get_buffer(ckmc_param_list_h params,
629                                ckmc_param_name_e name,
630                                ckmc_raw_buffer_s **ppbuffer)
631 {
632     if (!params || !ppbuffer || *ppbuffer)
633         return CKMC_ERROR_INVALID_PARAMETER;
634
635     const CKM::CryptoAlgorithm* algo = reinterpret_cast<const CKM::CryptoAlgorithm*>(params);
636     CKM::RawBuffer value;
637     if (!algo->getParam(static_cast<CKM::ParamName>(name),value))
638         return CKMC_ERROR_INVALID_PARAMETER;
639
640     return ckmc_buffer_new(value.data(), value.size(), ppbuffer);
641 }
642
643 KEY_MANAGER_CAPI
644 void ckmc_param_list_free(ckmc_param_list_h params)
645 {
646     CKM::CryptoAlgorithm* algo = reinterpret_cast<CKM::CryptoAlgorithm*>(params);
647     delete algo;
648 }
649
650 KEY_MANAGER_CAPI
651 int ckmc_generate_new_params(ckmc_algo_type_e type, ckmc_param_list_h *pparams)
652 {
653     if (!pparams || *pparams)
654         return CKMC_ERROR_INVALID_PARAMETER;
655
656     ckmc_param_list_h params = NULL;
657     int ret = ckmc_param_list_new(&params);
658     if (ret != CKMC_ERROR_NONE)
659         return ret;
660
661     switch (type) {
662     case CKMC_ALGO_AES_CTR:
663         ret = ckmc_param_list_set_integer(params, CKMC_PARAM_ED_CTR_LEN, DEFAULT_IV_LEN_BITS);
664         break;
665     case CKMC_ALGO_AES_CBC:
666     case CKMC_ALGO_AES_GCM:
667     case CKMC_ALGO_AES_CFB:
668     case CKMC_ALGO_RSA_OAEP:
669         // no iv by default
670         break;
671     default:
672         ret = CKMC_ERROR_INVALID_PARAMETER;
673         break;
674     }
675
676     if (ret != CKMC_ERROR_NONE) {
677         ckmc_param_list_free(params);
678         return ret;
679     }
680
681     ret = ckmc_param_list_set_integer(params, CKMC_PARAM_ALGO_TYPE, type);
682     if (ret != CKMC_ERROR_NONE) {
683         ckmc_param_list_free(params);
684         return ret;
685     }
686
687     *pparams = params;
688
689     return CKMC_ERROR_NONE;
690 }