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/seal.h>
32 void encrypt_seal(void)
35 yaca_ctx_h ctx = YACA_CTX_NULL;
36 yaca_key_h key_pub = YACA_KEY_NULL;
37 yaca_key_h key_priv = YACA_KEY_NULL;
38 yaca_key_h aes_key = YACA_KEY_NULL;
39 yaca_key_h iv = YACA_KEY_NULL;
46 printf("Plain data (16 of %zu bytes): %.16s\n", (size_t)4096, lorem1024);
49 ret = yaca_key_gen_pair(&key_priv, &key_pub,
50 YACA_KEY_TYPE_PAIR_RSA,
54 /// Encrypt a.k.a. seal
59 ret = yaca_seal_init(&ctx, key_pub,
60 YACA_ENC_AES, YACA_BCM_CBC,
65 ret = yaca_seal_update(ctx, lorem4096, 4096, NULL, &enc_size);
69 ret = yaca_get_block_length(ctx);
73 enc_size = enc_size + ret;
74 enc = yaca_malloc(enc_size);
80 ret = yaca_seal_update(ctx, lorem4096, 4096, enc, &out_size);
84 rem = enc_size - out_size;
85 ret = yaca_seal_final(ctx, enc + out_size, &rem);
89 enc_size = rem + out_size;
91 dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
93 yaca_ctx_free(ctx); // TODO: perhaps it should not return value
96 /// Decrypt a.k.a. open
101 ret = yaca_open_init(&ctx, key_priv,
102 YACA_ENC_AES, YACA_BCM_CBC,
109 ret = yaca_open_update(ctx, enc, enc_size, NULL, &dec_size);
113 ret = yaca_get_block_length(ctx);
117 dec_size = dec_size + ret;
118 dec = yaca_malloc(dec_size);
124 ret = yaca_open_update(ctx, enc, enc_size, dec, &out_size);
128 rem = dec_size - out_size;
129 ret = yaca_open_final(ctx, dec + out_size, &rem);
133 dec_size = rem + out_size;
135 printf("Decrypted data (16 of %zu bytes): %.16s\n", (size_t)dec_size, dec);
137 yaca_ctx_free(ctx); // TODO: perhaps it should not return value
145 yaca_key_free(aes_key);
148 yaca_key_free(key_pub);
149 yaca_key_free(key_priv);
154 int ret = yaca_init();
160 yaca_exit(); // TODO: what about handing of return value from exit??