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
21 #include <yaca_crypto.h>
23 #include <yaca_types.h>
24 #include <yaca_error.h>
26 #include "../src/debug.h"
28 void example_password(const yaca_key_h key, yaca_key_format_e key_fmt)
33 char *password = NULL;
34 yaca_key_h lkey = YACA_KEY_NULL;
36 ret = read_stdin_line("encryption pass: ", &password);
37 if (ret != YACA_ERROR_NONE)
40 ret = yaca_key_export(key, key_fmt, YACA_KEY_FILE_FORMAT_PEM, password, &k, &kl);
41 if (ret != YACA_ERROR_NONE)
47 ret = yaca_key_import(YACA_KEY_TYPE_RSA_PRIV, NULL, k, kl, &lkey);
48 if (ret == YACA_ERROR_INVALID_PASSWORD) {
49 ret = read_stdin_line("decryption pass: ", &password);
50 if (ret != YACA_ERROR_NONE)
53 ret = yaca_key_import(YACA_KEY_TYPE_RSA_PRIV, password, k, kl, &lkey);
54 if (ret == YACA_ERROR_INVALID_PASSWORD)
55 printf("invalid password\n");
61 if (ret != YACA_ERROR_NONE)
67 ret = yaca_key_export(lkey, key_fmt, YACA_KEY_FILE_FORMAT_PEM, NULL, &k, &kl);
68 if (ret != YACA_ERROR_NONE)
71 printf("%.*s", (int)kl, k);
76 yaca_key_destroy(lkey);
79 int main(int argc, char *argv[])
82 yaca_key_h key = YACA_KEY_NULL;
84 yaca_debug_set_error_cb(debug_func);
86 ret = yaca_initialize();
87 if (ret != YACA_ERROR_NONE)
90 ret = yaca_key_generate(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT, &key);
91 if (ret != YACA_ERROR_NONE)
94 printf("Default format:\n");
95 example_password(key, YACA_KEY_FORMAT_DEFAULT);
96 printf("\nPKCS8 format:\n");
97 example_password(key, YACA_KEY_FORMAT_PKCS8);
100 yaca_key_destroy(key);