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/simple.h>
28 #include <yaca/encrypt.h>
30 #include <yaca/error.h>
34 void encrypt_simple(const yaca_enc_algo_e algo,
35 const yaca_block_cipher_mode_e bcm,
36 const size_t key_bits)
38 yaca_key_h key = YACA_KEY_NULL;
39 yaca_key_h iv = YACA_KEY_NULL;
47 printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
50 if (yaca_key_derive_pbkdf2("foo bar", "123456789", 10, 1000,
51 YACA_DIGEST_SHA256, key_bits, &key) != 0)
54 if (yaca_get_iv_bits(algo, bcm, key_bits, &iv_bits) != 0)
57 if (iv_bits > 0 && yaca_key_gen(&iv, YACA_KEY_TYPE_IV, iv_bits) != 0)
60 if (yaca_encrypt(algo, bcm, key, iv, lorem4096, LOREM4096_SIZE, &enc, &enc_size) != 0)
63 dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
65 if (yaca_decrypt(algo, bcm, key, iv, enc, enc_size, &dec, &dec_size) != 0)
68 printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
78 void encrypt_advanced(const yaca_enc_algo_e algo,
79 const yaca_block_cipher_mode_e bcm,
80 const yaca_key_type_e key_type,
81 const size_t key_bits)
83 yaca_ctx_h ctx = YACA_CTX_NULL;
84 yaca_key_h key = YACA_KEY_NULL;
85 yaca_key_h iv = YACA_KEY_NULL;
98 printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
101 if (yaca_key_gen(&key, key_type, key_bits) != 0)
104 if (yaca_get_iv_bits(algo, bcm, key_bits, &iv_bits) != 0)
107 if (iv_bits > 0 && yaca_key_gen(&iv, YACA_KEY_TYPE_IV, iv_bits) != 0)
112 if (yaca_encrypt_init(&ctx, algo, bcm, key, iv) != 0)
115 if (yaca_get_block_length(ctx, &block_len) != 0)
118 if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != 0)
121 /* Calculate max output: size of update + final chunks */
122 enc_size = output_len + block_len;
123 if ((enc = yaca_malloc(enc_size)) == NULL)
127 if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != 0)
130 rem = enc_size - out_size;
131 if (yaca_encrypt_final(ctx, enc + out_size, &rem) != 0)
134 enc_size = rem + out_size;
136 dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
144 if (yaca_decrypt_init(&ctx, algo, bcm, key, iv) != 0)
147 if (yaca_get_block_length(ctx, &block_len) != 0)
150 if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != 0)
153 /* Calculate max output: size of update + final chunks */
154 dec_size = output_len + block_len;
155 if ((dec = yaca_malloc(dec_size)) == NULL)
159 if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != 0)
162 rem = dec_size - out_size;
163 if (yaca_decrypt_final(ctx, dec + out_size, &rem) != 0)
166 dec_size = rem + out_size;
168 printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
185 yaca_error_set_debug_func(debug_func);
187 int ret = yaca_init();
191 yaca_enc_algo_e algo = YACA_ENC_AES;
192 yaca_block_cipher_mode_e bcm = YACA_BCM_ECB;
193 yaca_key_type_e key_type = YACA_KEY_TYPE_SYMMETRIC;
194 size_t key_bits = YACA_KEY_256BIT;
196 encrypt_simple(algo, bcm, key_bits);
197 encrypt_advanced(algo, bcm, key_type,key_bits);
199 algo = YACA_ENC_3DES_3TDEA;
201 key_type = YACA_KEY_TYPE_DES;
202 key_bits = YACA_KEY_192BIT;
204 encrypt_advanced(algo, bcm, key_type,key_bits);
206 algo = YACA_ENC_CAST5;
208 key_type = YACA_KEY_TYPE_SYMMETRIC;
209 key_bits = YACA_KEY_UNSAFE_40BIT;
211 encrypt_simple(algo, bcm, key_bits);
212 encrypt_advanced(algo, bcm, key_type,key_bits);
214 algo = YACA_ENC_UNSAFE_RC2;
216 key_type = YACA_KEY_TYPE_SYMMETRIC;
217 key_bits = YACA_KEY_UNSAFE_8BIT;
219 encrypt_simple(algo, bcm, key_bits);
220 encrypt_advanced(algo, bcm, key_type,key_bits);
222 algo = YACA_ENC_UNSAFE_RC4;
224 key_type = YACA_KEY_TYPE_SYMMETRIC;
225 key_bits = YACA_KEY_2048BIT;
227 encrypt_simple(algo, bcm, key_bits);
228 encrypt_advanced(algo, bcm, key_type,key_bits);