f8f7bec908da66cc44cf779c71d1bb2a577eb5d0
[platform/core/security/yaca.git] / examples / sign.c
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact:
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18
19 /**
20  * @file sign.c
21  * @brief
22  */
23
24 #include <stdio.h>
25 #include <yaca/crypto.h>
26 #include <yaca/sign.h>
27 #include <yaca/key.h>
28
29 #include "lorem.h"
30 #include "misc.h"
31
32 #define PADDING_IMPLEMENTED 0
33
34 // Signature creation and verification using advanced API
35 void sign_verify_rsa(void)
36 {
37         char* signature = NULL;
38         size_t signature_len;
39
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;
45 #endif
46
47         // GENERATE
48         if (yaca_key_gen_pair(&prv, &pub, YACA_KEY_TYPE_PAIR_RSA, YACA_KEY_4096BIT) != 0)
49                 return;
50
51         // SIGN
52         if (yaca_sign_init(&ctx, YACA_DIGEST_SHA512, prv) != 0)
53                 goto finish;
54
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)
58                 goto finish;
59 #endif
60
61         if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
62                 goto finish;
63
64         if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
65                 goto finish;
66
67         if ((signature = yaca_malloc(signature_len)) == NULL)
68                 goto finish;
69
70         if (yaca_sign_final(ctx, signature, &signature_len) != 0)
71                 goto finish;
72
73         dump_hex(signature, signature_len, "RSA Signature of lorem4096:");
74
75         // CLEANUP
76         yaca_ctx_free(ctx);
77         ctx = YACA_CTX_NULL;
78
79         // VERIFY
80         if (yaca_verify_init(&ctx, YACA_DIGEST_SHA512, pub) != 0)
81                 goto finish;
82
83 #if PADDING_IMPLEMENTED
84         if (yaca_ctx_set_param(ctx, YACA_PARAM_PADDING, (char*)(&padding), sizeof(padding)) != 0)
85                 goto finish;
86 #endif
87
88         if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
89                 goto finish;
90
91         if (yaca_verify_final(ctx, signature, signature_len) != 0)
92                 printf("RSA verification failed\n");
93         else
94                 printf("RSA verification succesful\n");
95
96 finish:
97         yaca_free(signature);
98         yaca_key_free(prv);
99         yaca_key_free(pub);
100         yaca_ctx_free(ctx);
101 }
102
103 void sign_verify_hmac(void)
104 {
105         char* signature = NULL;
106         size_t signature_len;
107
108         yaca_ctx_h ctx = YACA_CTX_NULL;
109         yaca_key_h key = YACA_KEY_NULL;
110
111         // GENERATE
112         if (yaca_key_gen(&key, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT) != 0)
113                 return;
114
115         // SIGN
116         if (yaca_sign_init(&ctx, YACA_DIGEST_SHA512, key) != 0)
117                 goto finish;
118
119         if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
120                 goto finish;
121
122         if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
123                 goto finish;
124
125         if ((signature = yaca_malloc(signature_len)) == NULL)
126                 goto finish;
127
128         if (yaca_sign_final(ctx, signature, &signature_len) != 0)
129                 goto finish;
130
131         dump_hex(signature, signature_len, "HMAC Signature of lorem4096:");
132
133         // CLEANUP
134         yaca_ctx_free(ctx);
135         ctx = YACA_CTX_NULL;
136
137         // VERIFY
138         if (yaca_verify_init(&ctx, YACA_DIGEST_SHA512, key) != 0)
139                 goto finish;
140
141         if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
142                 goto finish;
143
144         if (yaca_verify_final(ctx, signature, signature_len) != 0)
145                 printf("HMAC verification failed\n");
146         else
147                 printf("HMAC verification succesful\n");
148
149 finish:
150         yaca_free(signature);
151         yaca_key_free(key);
152         yaca_ctx_free(ctx);
153 }
154
155 void sign_verify_cmac(void)
156 {
157         char* signature = NULL;
158         size_t signature_len;
159
160         yaca_ctx_h ctx = YACA_CTX_NULL;
161         yaca_key_h key = YACA_KEY_NULL;
162
163         // GENERATE
164         if (yaca_key_gen(&key, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT))
165                 return;
166
167         // SIGN
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)
170                 goto finish;
171
172         if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE))
173                 goto finish;
174
175         if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
176                 goto finish;
177
178         if ((signature = yaca_malloc(signature_len)) == NULL)
179                 goto finish;
180
181         if (yaca_sign_final(ctx, signature, &signature_len))
182                 goto finish;
183
184         dump_hex(signature, signature_len, "CMAC Signature of lorem4096:");
185
186         // CLEANUP
187         yaca_ctx_free(ctx);
188         ctx = YACA_CTX_NULL;
189
190         // VERIFY
191         if (yaca_verify_init(&ctx, YACA_DIGEST_CMAC, key) != 0)
192                 goto finish;
193
194         if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
195                 goto finish;
196
197         if (yaca_verify_final(ctx, signature, signature_len) != 0)
198                 printf("CMAC verification failed\n");
199         else
200                 printf("CMAC verification succesful\n");
201
202 finish:
203         yaca_free(signature);
204         yaca_key_free(key);
205         yaca_ctx_free(ctx);
206 }
207
208 int main()
209 {
210         int ret = yaca_init();
211         if (ret < 0)
212                 return ret;
213
214         // TODO simple?
215
216         sign_verify_rsa();
217         sign_verify_hmac();
218         sign_verify_cmac();
219
220         yaca_exit(); // TODO: what about handing of return value from exit??
221         return ret;
222 }