11dbab8e161380725d44decb570b479f1de5bcfb
[platform/core/security/key-manager-se-backend.git] / srcs / km_se_backend.c
1 /*
2  *  Copyright (c) 2016 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        km_se_backend.c
18  * @author      Dongsun Lee (ds73.lee@samsung.com)
19  * @version     1.0
20  * @brief       provides fucntions for SE Backedn for Key Manager
21  */
22
23 //################################################################################
24 // WARNING
25 //   This is a dummy implementation of SE backend to show how to implement SE backend.
26 //   Do not use this dummpy implementation in commodity devices.
27 //################################################################################
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdbool.h>
32 #include <openssl/aes.h>
33 #include <openssl/ec.h>
34 #include <openssl/evp.h>
35 #include <openssl/rand.h>
36
37 #include "key-manager-se-backend.h"
38
39 //################################################################################
40 // Global variables and defines
41 //################################################################################
42 enum {
43     DBP_KEY_IDX = 0,
44     AES_KEY_IDX,
45     EC_P192_KEY_IDX,
46     EC_P256_KEY_IDX,
47     EC_S384_KEY_IDX,
48 } KEY_INDEX;
49
50 #define MAX_SLOT_INDEX  8
51
52 static unsigned char *KEY_ARRAY[MAX_SLOT_INDEX] = {NULL,};
53
54 const size_t DBP_KEY_SIZE = 32;
55 const size_t AES_KEY_SIZE = 32;
56 const size_t BLOCK_SIZE = 16;
57 const int32_t EVP_SUCCESS = 1;
58
59 #define FREE_OUT_CTX(ctx, buf, result) \
60     { \
61     EVP_CIPHER_CTX_free(ctx); \
62     if(buf) free(buf); \
63     return result; \
64     }
65 #define FREE_OUT(buf, result) \
66     { \
67     if(buf) free(buf); \
68     return result; \
69     }
70 //################################################################################
71 // For Supporting Key Manager DB Encryption with SE Key
72 //################################################################################
73 kmsb_error_e kmsb_generate_dbp_key(const bool delete_old)
74 {
75     if (!delete_old && KEY_ARRAY[DBP_KEY_IDX])
76         return KMSB_ERROR_NOT_PERMITTED;
77
78     if (!KEY_ARRAY[DBP_KEY_IDX]) {
79         KEY_ARRAY[DBP_KEY_IDX] = calloc(DBP_KEY_SIZE, sizeof(unsigned char));
80     } else {
81         memset(KEY_ARRAY[DBP_KEY_IDX], 0, DBP_KEY_SIZE);
82     }
83
84     // if (1 != RAND_bytes(KEY_ARRAY[DBP_KEY_IDX], DBP_KEY_SIZE))
85     //     FREE_OUT(KEY_ARRAY[DBP_KEY_IDX], KMSB_ERROR_OPERATION_FAILED)
86     memset(KEY_ARRAY[DBP_KEY_IDX], 01, DBP_KEY_SIZE);
87
88     return KMSB_ERROR_NONE;
89 }
90
91 kmsb_error_e kmsb_encrypt_with_dbp_key(const int dbp_scheme,
92     const unsigned char *input, const unsigned int input_len,
93     const unsigned char *iv, const unsigned int iv_len,
94     unsigned char **output, unsigned int *output_len)
95 {
96     unsigned int aes_block_size = 16;
97     if (dbp_scheme != (const int) SE_BACKEND_DBP_SCHEME_VERSION)
98         return KMSB_ERROR_INVALID_PARAMETER;
99     if (!KEY_ARRAY[DBP_KEY_IDX])
100         return KMSB_ERROR_NO_KEY;
101     if (input == NULL ||
102         input_len == 0 || input_len % aes_block_size != 0 ||
103         iv == NULL ||
104         iv_len != aes_block_size ||
105         output == NULL ||
106         output_len == NULL)
107         return KMSB_ERROR_INVALID_PARAMETER;
108
109     AES_KEY aes_key;
110     AES_set_encrypt_key(KEY_ARRAY[DBP_KEY_IDX], DBP_KEY_SIZE*8, &aes_key);
111
112     *output = malloc(input_len);
113     if (output == NULL)
114         return KMSB_ERROR_OUT_OF_MEMORY;
115     *output_len = input_len;
116
117     unsigned char *iv_temp = malloc(iv_len);
118     if (iv_temp == NULL)
119         return KMSB_ERROR_OUT_OF_MEMORY;
120     memcpy(iv_temp, iv, iv_len);
121
122     AES_cbc_encrypt(input,
123                     *output,
124                     input_len,
125                     &aes_key,
126                     iv_temp,
127                     AES_ENCRYPT);
128
129     return KMSB_ERROR_NONE;
130 }
131
132 //################################################################################
133 // For Supporting Preloaded SE Data
134 //################################################################################
135 static kmsb_error_e generate_aes_key(const unsigned int key_idx,
136                                         const bool delete_old)
137 {
138     if (!delete_old && KEY_ARRAY[key_idx])
139         return KMSB_ERROR_NOT_PERMITTED;
140
141     if (!KEY_ARRAY[key_idx]) {
142         KEY_ARRAY[key_idx] = calloc(AES_KEY_SIZE, sizeof(unsigned char));
143     } else {
144         memset(KEY_ARRAY[key_idx], 0, AES_KEY_SIZE);
145     }
146
147     if (1 != RAND_bytes(KEY_ARRAY[key_idx], AES_KEY_SIZE))
148         FREE_OUT(KEY_ARRAY[key_idx], KMSB_ERROR_OPERATION_FAILED)
149
150     return KMSB_ERROR_NONE;
151 }
152
153 static kmsb_error_e generate_ecdsa_key(const unsigned int key_idx, 
154                         kmsb_ec_type_e ec, const bool delete_old)
155 {
156     if (!delete_old && KEY_ARRAY[key_idx])
157         return KMSB_ERROR_NOT_PERMITTED;
158
159     /* Create the context for generating the parameters */
160     EVP_PKEY_CTX *pctx = NULL;
161     EVP_PKEY *pkey = NULL;
162     if (!(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)))
163         return KMSB_ERROR_OPERATION_FAILED;
164     if (!EVP_PKEY_paramgen_init(pctx))
165         return KMSB_ERROR_OPERATION_FAILED;
166
167     uint32_t idx = 0;
168     int32_t nid = 0;
169     switch (ec) {
170     case KMSB_EC_PRIME192V1:
171         nid = NID_X9_62_prime192v1;
172         idx = EC_P192_KEY_IDX;
173         break;
174     case KMSB_EC_PRIME256V1:
175         nid = NID_X9_62_prime256v1;
176         idx = EC_P256_KEY_IDX;
177         break;
178     case KMSB_EC_SECP384R1:
179         nid = NID_secp384r1;
180         idx = EC_S384_KEY_IDX;
181         break;
182     default:
183         return KMSB_ERROR_NOT_SUPPORTED;
184     }
185     if (idx != key_idx)
186         return KMSB_ERROR_INVALID_PARAMETER;
187
188     /* Use the NID_X9_62_prime256v1 named curve - defined in obj_mac.h */
189     if(!EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, nid))
190         return KMSB_ERROR_OPERATION_FAILED;
191
192     if(!EVP_PKEY_keygen_init(pctx))
193         return KMSB_ERROR_OPERATION_FAILED;
194     /* Generate the key */
195     if (!EVP_PKEY_keygen(pctx, &pkey))
196         return KMSB_ERROR_OPERATION_FAILED;
197     if (pkey == NULL)
198         return KMSB_ERROR_OPERATION_FAILED;
199
200     KEY_ARRAY[key_idx] = (unsigned char*)pkey;
201     return KMSB_ERROR_NONE;
202 }
203
204 static kmsb_error_e aes_gcm_encrypt(EVP_CIPHER_CTX *ctx, kmsb_aes_param_s *param, uint8_t *key,
205                      const unsigned char *input, const unsigned int input_len,
206                      unsigned char **output, unsigned int *output_len)
207  {
208     int clen = 0, flen = 0;
209     unsigned char* buf = NULL;
210
211     /* Initialise the encryption operation. */
212     if(EVP_SUCCESS != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
213         return KMSB_ERROR_OPERATION_FAILED;
214     /*
215      * Set IV length if default 12 bytes (96 bits) is not appropriate
216      */
217     if(EVP_SUCCESS != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, param->iv_len, NULL))
218         return KMSB_ERROR_OPERATION_FAILED;
219
220     /* Initialise key and IV */
221     if(EVP_SUCCESS != EVP_EncryptInit_ex(ctx, NULL, NULL, key, param->iv))
222         return KMSB_ERROR_OPERATION_FAILED;
223
224     clen = input_len + EVP_CIPHER_CTX_block_size(ctx);
225     buf = calloc(clen, sizeof(unsigned char));
226     if (!buf) return KMSB_ERROR_OUT_OF_MEMORY;
227
228     /*
229      * Set Padding zero forcely, with our concept, there should be no padding 
230      * to increase buffer size.
231      */
232     if(EVP_SUCCESS != EVP_CIPHER_CTX_set_padding(ctx, 0))
233         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
234     /*
235      * Provide any AAD data. This can be called zero or more times as
236      * required
237      */
238     if(EVP_SUCCESS != EVP_EncryptUpdate(ctx, NULL, &clen, param->aad, param->aad_len))
239         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
240     /*
241      * Provide the message to be encrypted, and obtain the encrypted output.
242      * EVP_EncryptUpdate can be called multiple times if necessary
243      */
244     if(EVP_SUCCESS != EVP_EncryptUpdate(ctx, buf, &clen, input, input_len))
245         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
246     /*
247      * Finalise the encryption. Normally ciphertext bytes may be written at
248      * this stage, but this does not occur in GCM mode
249      */
250     if(EVP_SUCCESS != EVP_EncryptFinal_ex(ctx, buf + clen, &flen))
251         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
252     *output_len = clen + flen;
253
254     /* Get the tag */
255     if(EVP_SUCCESS != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, param->tag))
256         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
257
258     *output = calloc(*output_len, sizeof(unsigned char));
259     if (output == NULL)
260         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OUT_OF_MEMORY)
261     memcpy(*output, buf, *output_len);
262
263     FREE_OUT_CTX(ctx, buf, KMSB_ERROR_NONE)
264 }
265
266 static kmsb_error_e aes_gcm_decrypt(EVP_CIPHER_CTX *ctx, kmsb_aes_param_s *param, uint8_t *key,
267                      const unsigned char *input, const unsigned int input_len,
268                      unsigned char **output, unsigned int *output_len)
269  {
270     int clen = 0, flen = 0;
271     unsigned char* buf = NULL;
272
273     /* Initialise the decryption operation. */
274     if(EVP_SUCCESS != EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
275         return KMSB_ERROR_OPERATION_FAILED;
276     /*
277      * Set IV length if default 12 bytes (96 bits) is not appropriate
278      */
279     if(EVP_SUCCESS != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, param->iv_len, NULL))
280         return KMSB_ERROR_OPERATION_FAILED;
281     /* Initialise key and IV */
282     if(EVP_SUCCESS != EVP_DecryptInit_ex(ctx, NULL, NULL, key, param->iv))
283         return KMSB_ERROR_OPERATION_FAILED;
284     clen = input_len + EVP_CIPHER_CTX_block_size(ctx);
285     buf = calloc(clen, sizeof(unsigned char));
286     if (!buf) return KMSB_ERROR_OUT_OF_MEMORY;
287     /*
288      * Set Padding zero forcely, with our concept, there should be no padding 
289      * to increase buffer size.
290      */
291     if(EVP_SUCCESS != EVP_CIPHER_CTX_set_padding(ctx, 0))
292         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
293     /*
294      * Provide any AAD data. This can be called zero or more times as
295      * required
296      */
297     if(EVP_SUCCESS != EVP_DecryptUpdate(ctx, NULL, &clen, param->aad, param->aad_len))
298         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
299     /*
300      * Provide the message to be decrypted, and obtain the plaintext output.
301      * EVP_DecryptUpdate can be called multiple times if necessary
302      */
303     if(EVP_SUCCESS != EVP_DecryptUpdate(ctx, buf, &clen, input, input_len))
304         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
305     /* Set expected tag value. Works in OpenSSL 1.0.1d and later */
306     if(EVP_SUCCESS != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, param->tag))
307         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
308     /*
309      * Finalise the decryption. A positive return value indicates success,
310      * anything else is a failure - the plaintext is not trustworthy.
311      */
312     if(EVP_SUCCESS != EVP_DecryptFinal_ex(ctx, buf + clen, &flen))
313         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
314     *output_len = clen + flen;
315
316     *output = calloc(*output_len, sizeof(unsigned char));
317     if (output == NULL)
318         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OUT_OF_MEMORY)
319     memcpy(*output, buf, *output_len);
320
321     FREE_OUT_CTX(ctx, buf, KMSB_ERROR_NONE)
322 }
323
324 kmsb_error_e kmsb_aes_encrypt(const unsigned int key_idx,
325                     kmsb_aes_param_s *param,
326                     const unsigned char *input, const unsigned int input_len,
327                     unsigned char **output, unsigned int *output_len)
328 {
329     // check the validate of key_idx
330     if (key_idx != AES_KEY_IDX) return KMSB_ERROR_INVALID_PARAMETER;
331     if (!KEY_ARRAY[key_idx]) {
332         if (KMSB_ERROR_NONE != generate_aes_key(key_idx, false))
333             return KMSB_ERROR_NO_KEY;
334     }
335     // check the input parameters
336     if (input == NULL ||
337         input_len == 0 ||
338         param->iv == NULL ||
339         param->iv_len == 0)
340         return KMSB_ERROR_INVALID_PARAMETER;
341
342     int ret = 0;
343     int clen = 0, flen = 0;
344     unsigned char* buf = NULL;
345     unsigned char* key = KEY_ARRAY[key_idx];
346     EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
347     
348     switch (param->mode) {
349     case KMSB_ALGO_AES_CTR:
350         ret = EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, key, param->iv);
351         break;
352     case KMSB_ALGO_AES_CBC:
353         ret = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, param->iv);
354         break;
355     case KMSB_ALGO_AES_GCM:
356         return aes_gcm_encrypt(ctx, param, key, input, input_len, output, output_len);
357     case KMSB_ALGO_AES_CFB:
358         ret = EVP_EncryptInit_ex(ctx, EVP_aes_256_cfb8(), NULL, key, param->iv);
359         break;
360     default:
361         return KMSB_ERROR_NOT_SUPPORTED;
362     }
363     if(EVP_SUCCESS != ret) return KMSB_ERROR_OPERATION_FAILED;
364
365     clen = input_len + EVP_CIPHER_CTX_block_size(ctx);
366     buf = calloc(clen, sizeof(unsigned char));
367     if (!buf) return KMSB_ERROR_OUT_OF_MEMORY;
368
369     /*
370      * Set Padding zero forcely, with our concept, there should be no padding 
371      * to increase buffer size.
372      */
373     if(EVP_SUCCESS != EVP_CIPHER_CTX_set_padding(ctx, 0))
374         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
375     /*
376      * Provide the message to be encrypted, and obtain the encrypted output.
377      * EVP_EncryptUpdate can be called multiple times if necessary
378      */
379     if(EVP_SUCCESS != EVP_EncryptUpdate(ctx, buf, &clen, input, input_len))
380         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
381     *output_len = clen;
382
383     /*
384      * Finalise the encryption. Further ciphertext bytes may be written at
385      * this stage.
386      */
387     if(EVP_SUCCESS != EVP_EncryptFinal_ex(ctx, buf + clen, &flen))
388         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
389     *output_len += flen;
390
391     *output = calloc(*output_len, sizeof(unsigned char));
392     if (output == NULL)
393         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OUT_OF_MEMORY)
394     memcpy(*output, buf, *output_len);
395
396     FREE_OUT_CTX(ctx, buf, KMSB_ERROR_NONE)
397 }
398
399 kmsb_error_e kmsb_aes_decrypt(const unsigned int key_idx,
400                     kmsb_aes_param_s *param,
401                     const unsigned char *input, const unsigned int input_len,
402                     unsigned char **output, unsigned int *output_len)
403 {
404     // check the validate of key_idx
405     if (key_idx != AES_KEY_IDX) return KMSB_ERROR_INVALID_PARAMETER;
406     if (!KEY_ARRAY[key_idx]) {
407         if (KMSB_ERROR_NONE != generate_aes_key(key_idx, false))
408             return KMSB_ERROR_NO_KEY;
409     }
410     // check the input parameters
411     if (input == NULL ||
412         input_len == 0 ||
413         output == NULL ||
414         output_len == NULL ||
415         param->iv == NULL ||
416         param->iv_len == 0)
417         return KMSB_ERROR_INVALID_PARAMETER;
418
419     int ret = 0;
420     int clen = 0, flen = 0;
421     unsigned char* buf = NULL;
422     unsigned char* key = KEY_ARRAY[key_idx];
423     EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
424
425     switch (param->mode) {
426     case KMSB_ALGO_AES_CTR:
427         ret = EVP_DecryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, key, param->iv);
428         break;
429     case KMSB_ALGO_AES_CBC:
430         ret = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, param->iv);
431         break;
432     case KMSB_ALGO_AES_GCM:
433         return aes_gcm_decrypt(ctx, param, key, input, input_len, output, output_len);
434     case KMSB_ALGO_AES_CFB:
435         ret = EVP_DecryptInit_ex(ctx, EVP_aes_256_cfb8(), NULL, key, param->iv);
436         break;
437     default:
438         return KMSB_ERROR_NOT_SUPPORTED;
439     }
440     if(EVP_SUCCESS != ret) return KMSB_ERROR_OPERATION_FAILED;
441
442     clen = input_len + EVP_CIPHER_CTX_block_size(ctx);
443     buf = calloc(clen, sizeof(unsigned char));
444     if (!buf) return KMSB_ERROR_OUT_OF_MEMORY;
445
446     /*
447      * Set Padding zero forcely, with our concept, there should be no padding 
448      * to increase buffer size.
449      */
450     if(EVP_SUCCESS != EVP_CIPHER_CTX_set_padding(ctx, 0))
451         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
452     /*
453      * Provide the message to be decrypted, and obtain the plaintext output.
454      * EVP_DecryptUpdate can be called multiple times if necessary
455      */
456     if(EVP_SUCCESS != EVP_DecryptUpdate(ctx, buf, &clen, input, input_len))
457        FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
458     /*
459      * Finalise the decryption. Further plaintext bytes may be written at
460      * this stage.
461      */
462     if(EVP_SUCCESS != EVP_DecryptFinal_ex(ctx, buf + clen, &flen))
463         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OPERATION_FAILED)
464     *output_len = clen + flen;
465
466     *output = calloc(*output_len, sizeof(unsigned char));
467     if (output == NULL)
468         FREE_OUT_CTX(ctx, buf, KMSB_ERROR_OUT_OF_MEMORY)
469
470     memcpy(*output, buf, *output_len);
471     FREE_OUT_CTX(ctx, buf, KMSB_ERROR_NONE)
472 }
473
474 kmsb_error_e kmsb_create_signature(const unsigned int key_idx,
475                 const kmsb_sign_param_s *param,
476                 const unsigned char *msg, const unsigned int msg_len,
477                 unsigned char **sig, unsigned int *sig_len)
478 {
479     kmsb_error_e ret = KMSB_ERROR_NONE;
480     if (!KEY_ARRAY[key_idx]) {
481         ret = generate_ecdsa_key(key_idx, param->ec_type, false);
482         if (KMSB_ERROR_NONE != ret)
483             return ret;
484     }
485
486     EVP_PKEY *pkey = (EVP_PKEY *)KEY_ARRAY[key_idx];
487     EVP_MD_CTX *mdctx = NULL;
488     /* Create the Message Digest Context */
489     if(!(mdctx = EVP_MD_CTX_create()))
490         return KMSB_ERROR_OPERATION_FAILED;
491
492     switch (param->hash_algo) {
493     case KMSB_HASH_SHA1:
494         if(EVP_SUCCESS != EVP_DigestSignInit(mdctx, NULL, EVP_sha1(), NULL, pkey))
495             return KMSB_ERROR_OPERATION_FAILED;
496         break;
497     case KMSB_HASH_SHA256:
498         if(EVP_SUCCESS != EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, pkey))
499             return KMSB_ERROR_OPERATION_FAILED;
500         break;
501     case KMSB_HASH_SHA384:
502         if(EVP_SUCCESS != EVP_DigestSignInit(mdctx, NULL, EVP_sha384(), NULL, pkey))
503             return KMSB_ERROR_OPERATION_FAILED;
504         break;
505     case KMSB_HASH_SHA512:
506         if(EVP_SUCCESS != EVP_DigestSignInit(mdctx, NULL, EVP_sha512(), NULL, pkey))
507             return KMSB_ERROR_OPERATION_FAILED;
508         break;
509     default:
510         return KMSB_ERROR_NOT_SUPPORTED;
511     }
512     /* Initialise the DigestSign operation - SHA-256 has been selected as the message digest function in this example */
513
514     /* Call update with the message */
515     if(EVP_SUCCESS != EVP_DigestSignUpdate(mdctx, msg, msg_len))
516         return KMSB_ERROR_OPERATION_FAILED;
517
518     /* Finalise the DigestSign operation */
519     /* First call EVP_DigestSignFinal with a NULL sig parameter to obtain the length of the
520     * signature. Length is returned in slen */
521     if(EVP_SUCCESS != EVP_DigestSignFinal(mdctx, NULL, sig_len))
522         return KMSB_ERROR_OPERATION_FAILED;
523     /* Allocate memory for the signature based on size in slen */
524     if(!(*sig = OPENSSL_malloc(sizeof(unsigned char) * (*sig_len))))
525         return KMSB_ERROR_OUT_OF_MEMORY;
526     /* Obtain the signature */
527     if(EVP_SUCCESS != EVP_DigestSignFinal(mdctx, *sig, sig_len))
528         return KMSB_ERROR_OPERATION_FAILED;
529
530     /* Clean up */
531     if(mdctx) EVP_MD_CTX_destroy(mdctx);
532     return KMSB_ERROR_NONE;
533 }
534
535 kmsb_error_e kmsb_verify_signature(const unsigned int key_idx,
536                 const kmsb_sign_param_s *param,
537                 const unsigned char *msg, const unsigned int msg_len,
538                 unsigned char *sig, unsigned int sig_len)
539 {
540     kmsb_error_e ret = KMSB_ERROR_NONE;
541     if (!KEY_ARRAY[key_idx]) {
542         ret = generate_ecdsa_key(key_idx, param->ec_type, false);
543         if (KMSB_ERROR_NONE != ret)
544             return ret;
545     }
546
547     EVP_PKEY *pkey = (EVP_PKEY *)KEY_ARRAY[key_idx];
548     EVP_MD_CTX *mdctx = NULL;
549     /* Create the Message Digest Context */
550     if(!(mdctx = EVP_MD_CTX_create()))
551         return KMSB_ERROR_OPERATION_FAILED;
552
553     switch (param->hash_algo) {
554     case KMSB_HASH_SHA1:
555         if(EVP_SUCCESS != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha1(), NULL, pkey))
556             return KMSB_ERROR_OPERATION_FAILED;
557         break;
558     case KMSB_HASH_SHA256:
559         if(EVP_SUCCESS != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey))
560             return KMSB_ERROR_OPERATION_FAILED;
561         break;
562     case KMSB_HASH_SHA384:
563         if(EVP_SUCCESS != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha384(), NULL, pkey))
564             return KMSB_ERROR_OPERATION_FAILED;
565         break;
566     case KMSB_HASH_SHA512:
567         if(EVP_SUCCESS != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha512(), NULL, pkey))
568             return KMSB_ERROR_OPERATION_FAILED;
569         break;
570     default:
571         return KMSB_ERROR_NOT_SUPPORTED;
572     }
573     /* Initialize `key` with a public key */
574     if(EVP_SUCCESS != EVP_DigestVerifyUpdate(mdctx, msg, msg_len))
575         return KMSB_ERROR_OPERATION_FAILED;
576     if(EVP_SUCCESS != EVP_DigestVerifyFinal(mdctx, sig, sig_len))
577         return KMSB_ERROR_VERIFICATION_FAILED;
578
579     return KMSB_ERROR_NONE;
580 }
581
582 int kmsb_get_key(const unsigned int key_idx, unsigned char **output, unsigned int *output_len)
583 {
584     (void) key_idx;
585     (void) output;
586     (void) output_len;
587     return KMSB_ERROR_NOT_SUPPORTED;
588 }
589
590 int kmsb_get_certificate(const unsigned int cert_idx, unsigned char **output, unsigned int *output_len)
591 {
592     (void) cert_idx;
593     (void) output;
594     (void) output_len;
595     return KMSB_ERROR_NOT_SUPPORTED;
596 }
597
598 int kmsb_get_data(const unsigned int data_idx, unsigned char **output, unsigned int *output_len)
599 {
600     (void) data_idx;
601     (void) output;
602     (void) output_len;
603     return KMSB_ERROR_NOT_SUPPORTED;
604 }