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)
36 const yaca_enc_algo_e algo = YACA_ENC_AES;
37 const yaca_block_cipher_mode_e bcm = YACA_BCM_CBC;
38 const size_t key_bits = YACA_KEY_256BIT;
40 yaca_key_h key = YACA_KEY_NULL;
41 yaca_key_h iv = YACA_KEY_NULL;
42 char *enc_data = NULL;
43 char *dec_data = NULL;
48 printf("Simple Encrypt\nPlain data (16 of %zu bytes): %.16s\n",
49 LOREM1024_SIZE, lorem1024);
51 ret = yaca_key_derive_pbkdf2("foo bar", "123456789", 10, 1000,
52 YACA_DIGEST_SHA256, key_bits, &key);
56 iv_bits = yaca_get_iv_bits(algo, bcm, key_bits);
61 ret = yaca_key_gen(&iv, YACA_KEY_TYPE_IV, iv_bits);
66 ret = yaca_encrypt(algo, bcm, key, iv, lorem1024, LOREM1024_SIZE,
71 dump_hex(enc_data, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
73 ret = yaca_decrypt(algo, bcm, key, iv, enc_data, enc_len, &dec_data,
78 printf("Decrypted data (16 of %zu bytes): %.16s\n", dec_len, dec_data);
84 if (iv != YACA_KEY_NULL)
89 // Symmetric encryption using advanced API
90 void encrypt_advanced(void)
92 const yaca_enc_algo_e algo = YACA_ENC_AES;
93 const yaca_block_cipher_mode_e bcm = YACA_BCM_CBC;
94 const size_t key_bits = YACA_KEY_256BIT;
97 yaca_key_h key = YACA_KEY_NULL;
98 yaca_key_h iv = YACA_KEY_NULL;
105 printf("Advanced Encrypt\nPlain data (16 of %zu bytes): %.16s\n",
106 LOREM4096_SIZE, lorem4096);
110 ret = yaca_key_derive_pbkdf2("foo bar", "123456789", 10, 1000,
111 YACA_DIGEST_SHA256, key_bits, &key);
115 iv_bits = yaca_get_iv_bits(algo, bcm, key_bits);
120 ret = yaca_key_gen(&iv, YACA_KEY_TYPE_IV, iv_bits);
132 ret = yaca_encrypt_init(&ctx, algo, bcm, key, iv);
136 ret = yaca_get_block_length(ctx);
142 ret = yaca_get_output_length(ctx, LOREM4096_SIZE);
148 /* Calculate max output: size of update + final chunks */
149 enc_size = output_len + block_len;
150 enc = yaca_malloc(enc_size);
155 ret = yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc,
160 rem = enc_size - out_size;
161 ret = yaca_encrypt_final(ctx, enc + out_size, &rem);
165 enc_size = rem + out_size;
167 dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ",
170 yaca_ctx_free(ctx); // TODO: perhaps it should not return value
180 ret = yaca_decrypt_init(&ctx, algo, bcm, key, iv);
186 ret = yaca_get_block_length(ctx);
192 ret = yaca_get_output_length(ctx, LOREM4096_SIZE);
198 /* Calculate max output: size of update + final chunks */
199 dec_size = output_len + block_len;
200 dec = yaca_malloc(dec_size);
205 ret = yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size);
209 rem = dec_size - out_size;
210 ret = yaca_decrypt_final(ctx, dec + out_size, &rem);
214 dec_size = rem + out_size;
216 printf("Decrypted data (16 of %zu bytes): %.16s\n", dec_size, dec);
233 int ret = yaca_init();
241 yaca_exit(); // TODO: what about handing of return value from exit??