70978a692b2d1ed5d392f63bc64ec8a6489612bd
[platform/core/security/yaca.git] / examples / digest.c
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18
19 /**
20  * @file digest.c
21  * @brief
22  */
23
24 #include <stdio.h>
25 #include <yaca/crypto.h>
26 #include <yaca/digest.h>
27 #include <yaca/simple.h>
28 #include "lorem.h"
29 #include "misc.h"
30
31 void digest_simple(void)
32 {
33         int ret = 0;
34         char *digest;
35         size_t digest_len;
36
37         ret = yaca_digest_calc(YACA_DIGEST_SHA256,
38                                lorem1024,
39                                1024, &digest, &digest_len);
40         if (ret < 0)
41                 return;
42
43         dump_hex(digest, digest_len, "Message digest: ");
44
45         yaca_free(digest);
46 }
47
48 void digest_advanced(void)
49 {
50         int ret = 0;
51         yaca_ctx_h ctx;
52
53         ret = yaca_digest_init(&ctx, YACA_DIGEST_SHA256);
54         if (ret < 0)
55                 return;
56
57         ret = yaca_digest_update(ctx, lorem1024, 1024);
58         if (ret < 0)
59                 goto exit_ctx;
60
61         // TODO: rename to yaca_digest_get_length??
62         size_t digest_len;
63         digest_len = yaca_get_digest_length(ctx);
64         if (digest_len <= 0)
65                 goto exit_ctx;
66
67         {
68                 char digest[digest_len];
69
70                 ret = yaca_digest_final(ctx, digest, &digest_len);
71                 if (ret < 0)
72                         goto exit_ctx;
73
74                 dump_hex(digest, digest_len, "Message digest: ");
75         }
76
77 exit_ctx:
78         yaca_ctx_free(ctx);
79 }
80
81 int main()
82 {
83         int ret = yaca_init();
84         if (ret < 0)
85                 return ret;
86
87         digest_simple();
88
89         digest_advanced();
90
91         yaca_exit(); // TODO: what about handing of return value from exit??
92         return ret;
93 }