2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
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
26 #include <yaca/crypto.h>
27 #include <yaca/encrypt.h>
28 #include <yaca/simple.h>
33 // Symmetric encryption using simple API
34 void encrypt_simple(void)
37 yaca_key_h key = YACA_KEY_NULL;
38 yaca_key_h iv = YACA_KEY_NULL;
39 char *enc_data = NULL;
40 char *dec_data = NULL;
44 printf("Plain data (16 of %zu bytes): %.16s\n", (size_t)1024, lorem1024);
46 ret = yaca_key_derive_pbkdf2("foo bar", "123456789", 10,
47 1000, YACA_DIGEST_SHA256,
48 YACA_KEY_256BIT, &key);
52 ret = yaca_key_gen(&iv, YACA_KEY_TYPE_IV, YACA_KEY_IV_256BIT);
56 ret = yaca_encrypt(YACA_ENC_AES, YACA_BCM_CBC,
57 key, iv, lorem1024, 1024, &enc_data, &enc_len);
61 dump_hex(enc_data, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
63 ret = yaca_decrypt(YACA_ENC_AES, YACA_BCM_CBC,
70 printf("Decrypted data (16 of %zu bytes): %.16s\n", dec_len, dec_data);
76 if (iv != YACA_KEY_NULL)
81 // Symmetric encryption using advanced API
82 void encrypt_advanced(void)
86 yaca_key_h key = YACA_KEY_NULL;
87 yaca_key_h iv = YACA_KEY_NULL;
93 printf("Plain data (16 of %zu bytes): %.16s\n", (size_t)4096, lorem1024);
97 ret = yaca_key_derive_pbkdf2("foo bar", "123456789", 10,
98 1000, YACA_DIGEST_SHA256,
99 YACA_KEY_256BIT, &key);
103 ret = yaca_key_gen(&iv, YACA_KEY_IV_256BIT, YACA_KEY_TYPE_SYMMETRIC);
109 ret = yaca_encrypt_init(&ctx, YACA_ENC_AES, YACA_BCM_CBC,
114 ret = yaca_encrypt_update(ctx, lorem4096, 4096, NULL, &enc_size);
116 goto ex_ctx;// TODO: what error code?
118 ret = yaca_get_block_length(ctx);
122 enc_size += ret ; // Add block size for finalize
123 enc = yaca_malloc(enc_size);
127 size_t out_size = enc_size;
128 ret = yaca_encrypt_update(ctx, lorem4096, 4096, enc, &out_size);
132 size_t rem = enc_size - out_size;
133 ret = yaca_encrypt_final(ctx, enc + out_size, &rem);
137 enc_size = rem + out_size;
139 dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
141 yaca_ctx_free(ctx); // TODO: perhaps it should not return value
146 ret = yaca_decrypt_init(&ctx, YACA_ENC_AES, YACA_BCM_CBC,
153 ret = yaca_decrypt_update(ctx, enc, enc_size, NULL, &dec_size);
155 goto ex_of; // TODO: what error code?
157 ret = yaca_get_block_length(ctx);
161 dec_size += ret; // Add block size for finalize
162 dec = yaca_malloc(dec_size);
166 size_t out_size = dec_size;
167 ret = yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size);
171 size_t rem = dec_size - out_size;
172 ret = yaca_encrypt_final(ctx, dec + out_size, &rem);
176 dec_size = rem + out_size;
178 printf("Decrypted data (16 of %zu bytes): %.16s\n", dec_size, dec);
193 void encrypt_seal(void)
196 yaca_ctx_h ctx = YACA_CTX_NULL;
197 yaca_key_h key_pub = YACA_KEY_NULL;
198 yaca_key_h key_priv = YACA_KEY_NULL;
199 yaca_key_h aes_key = YACA_KEY_NULL;
200 yaca_key_h iv = YACA_KEY_NULL;
207 printf("Plain data (16 of %zu bytes): %.16s\n", (size_t)4096, lorem1024);
209 /// Generate key pair
210 ret = yaca_key_gen_pair(&key_priv, &key_pub,
211 YACA_KEY_2048BIT, YACA_KEY_TYPE_PAIR_RSA);
214 /// Encrypt a.k.a. seal
216 ret = yaca_seal_init(&ctx, key_pub,
217 YACA_ENC_AES, YACA_BCM_CBC,
222 ret = yaca_seal_update(ctx, lorem4096, 4096, NULL, &enc_size);
226 ret = yaca_get_block_length(ctx);
230 enc_size = enc_size + ret;
231 enc = yaca_malloc(enc_size);
236 size_t out_size = enc_size;
237 ret = yaca_seal_update(ctx, lorem4096, 4096, enc, &out_size);
241 size_t rem = enc_size - out_size;
242 ret = yaca_seal_final(ctx, enc + out_size, &rem);
246 enc_size = rem + out_size;
248 dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
250 yaca_ctx_free(ctx); // TODO: perhaps it should not return value
253 /// Decrypt a.k.a. open
255 ret = yaca_open_init(&ctx, key_priv,
256 YACA_ENC_AES, YACA_BCM_CBC,
263 ret = yaca_open_update(ctx, enc, enc_size, NULL, &dec_size);
267 ret = yaca_get_block_length(ctx);
271 dec_size = dec_size + ret;
272 dec = yaca_malloc(dec_size);
277 size_t out_size = enc_size;
278 ret = yaca_open_update(ctx, enc, enc_size, dec, &out_size);
282 size_t rem = dec_size - out_size;
283 ret = yaca_open_final(ctx, dec + out_size, &rem);
287 dec_size = rem + out_size;
289 printf("Decrypted data (16 of %zu bytes): %.16s\n", (size_t)dec_size, dec);
291 yaca_ctx_free(ctx); // TODO: perhaps it should not return value
299 yaca_key_free(aes_key);
302 yaca_key_free(key_pub);
303 yaca_key_free(key_priv);
308 int ret = yaca_init();
318 yaca_exit(); // TODO: what about handing of return value from exit??