2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
25 #include <yaca/crypto.h>
26 #include <yaca/sign.h>
32 #define PADDING_IMPLEMENTED 0
34 // Signature creation and verification using advanced API
35 void sign_verify_rsa(void)
37 char* signature = NULL;
40 yaca_ctx_h ctx = YACA_CTX_NULL;
41 yaca_key_h prv = YACA_KEY_NULL;
42 yaca_key_h pub = YACA_KEY_NULL;
43 #if PADDING_IMPLEMENTED
44 yaca_padding_e padding = YACA_PADDING_PKCS1;
48 if (yaca_key_gen_pair(&prv, &pub, YACA_KEY_TYPE_PAIR_RSA, YACA_KEY_4096BIT) != 0)
52 if (yaca_sign_init(&ctx, YACA_DIGEST_SHA512, prv) != 0)
55 #if PADDING_IMPLEMENTED
56 // TODO: yaca_ctx_set_param should take void* not char*
57 if (yaca_ctx_set_param(ctx, YACA_PARAM_PADDING, (char*)(&padding), sizeof(padding)) != 0)
61 if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
64 if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
67 if ((signature = yaca_malloc(signature_len)) == NULL)
70 if (yaca_sign_final(ctx, signature, &signature_len) != 0)
73 dump_hex(signature, signature_len, "RSA Signature of lorem4096:");
80 if (yaca_verify_init(&ctx, YACA_DIGEST_SHA512, pub) != 0)
83 #if PADDING_IMPLEMENTED
84 if (yaca_ctx_set_param(ctx, YACA_PARAM_PADDING, (char*)(&padding), sizeof(padding)) != 0)
88 if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
91 if (yaca_verify_final(ctx, signature, signature_len) != 0)
92 printf("RSA verification failed\n");
94 printf("RSA verification succesful\n");
103 void sign_verify_hmac(void)
105 char* signature = NULL;
106 size_t signature_len;
108 yaca_ctx_h ctx = YACA_CTX_NULL;
109 yaca_key_h key = YACA_KEY_NULL;
112 if (yaca_key_gen(&key, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT) != 0)
116 if (yaca_sign_init(&ctx, YACA_DIGEST_SHA512, key) != 0)
119 if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
122 if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
125 if ((signature = yaca_malloc(signature_len)) == NULL)
128 if (yaca_sign_final(ctx, signature, &signature_len) != 0)
131 dump_hex(signature, signature_len, "HMAC Signature of lorem4096:");
138 if (yaca_verify_init(&ctx, YACA_DIGEST_SHA512, key) != 0)
141 if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
144 if (yaca_verify_final(ctx, signature, signature_len) != 0)
145 printf("HMAC verification failed\n");
147 printf("HMAC verification succesful\n");
150 yaca_free(signature);
155 void sign_verify_cmac(void)
157 char* signature = NULL;
158 size_t signature_len;
160 yaca_ctx_h ctx = YACA_CTX_NULL;
161 yaca_key_h key = YACA_KEY_NULL;
164 if (yaca_key_gen(&key, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT))
168 // TODO: CMAC must extract the key length to select the proper evp (EVP_aes_XXX_cbc()) it should be documented
169 if (yaca_sign_init(&ctx, YACA_DIGEST_CMAC, key) != 0)
172 if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE))
175 if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
178 if ((signature = yaca_malloc(signature_len)) == NULL)
181 if (yaca_sign_final(ctx, signature, &signature_len))
184 dump_hex(signature, signature_len, "CMAC Signature of lorem4096:");
191 if (yaca_verify_init(&ctx, YACA_DIGEST_CMAC, key) != 0)
194 if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
197 if (yaca_verify_final(ctx, signature, signature_len) != 0)
198 printf("CMAC verification failed\n");
200 printf("CMAC verification succesful\n");
203 yaca_free(signature);
210 int ret = yaca_init();
220 yaca_exit(); // TODO: what about handing of return value from exit??