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
27 #include <yaca_crypto.h>
28 #include <yaca_error.h>
29 #include <yaca_encrypt.h>
30 #include <yaca_digest.h>
32 #include <yaca_sign.h>
36 API int yaca_simple_calculate_digest(yaca_digest_algorithm_e algo,
47 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
48 digest == NULL || digest_len == NULL)
49 return YACA_ERROR_INVALID_PARAMETER;
51 ret = yaca_digest_initialize(&ctx, algo);
52 if (ret != YACA_ERROR_NONE)
56 ret = yaca_digest_update(ctx, data, data_len);
57 if (ret != YACA_ERROR_NONE)
61 ret = yaca_context_get_output_length(ctx, 0, &ldigest_len);
62 if (ret != YACA_ERROR_NONE)
65 assert(ldigest_len > 0);
67 ret = yaca_malloc(ldigest_len, (void**)&ldigest);
68 if (ret != YACA_ERROR_NONE)
71 ret = yaca_digest_finalize(ctx, ldigest, &ldigest_len);
72 if (ret != YACA_ERROR_NONE)
75 *digest_len = ldigest_len;
81 yaca_context_destroy(ctx);
86 API int yaca_simple_encrypt(yaca_encrypt_algorithm_e algo,
87 yaca_block_cipher_mode_e bcm,
88 const yaca_key_h sym_key,
90 const char *plaintext,
93 size_t *ciphertext_len)
98 size_t out_len, lcipher_len, written;
100 if (plaintext == NULL || plaintext_len == 0 ||
101 ciphertext == NULL || ciphertext_len == NULL ||
102 sym_key == YACA_KEY_NULL)
103 return YACA_ERROR_INVALID_PARAMETER;
105 ret = yaca_encrypt_initialize(&ctx, algo, bcm, sym_key, iv);
106 if (ret != YACA_ERROR_NONE)
109 ret = yaca_context_get_output_length(ctx, plaintext_len, &out_len);
110 if (ret != YACA_ERROR_NONE)
113 ret = yaca_context_get_output_length(ctx, 0, &lcipher_len);
114 if (ret != YACA_ERROR_NONE)
117 if (out_len > SIZE_MAX - lcipher_len) {
118 ret = YACA_ERROR_INVALID_PARAMETER;
122 lcipher_len += out_len;
124 assert(lcipher_len > 0);
126 ret = yaca_malloc(lcipher_len, (void**)&lcipher);
127 if (ret != YACA_ERROR_NONE)
130 out_len = lcipher_len;
131 ret = yaca_encrypt_update(ctx, plaintext, plaintext_len, lcipher, &out_len);
132 if (ret != YACA_ERROR_NONE)
135 assert(out_len <= lcipher_len);
138 out_len = lcipher_len - written;
139 ret = yaca_encrypt_finalize(ctx, lcipher + written, &out_len);
140 if (ret != YACA_ERROR_NONE)
144 assert(written <= lcipher_len && written > 0);
146 ret = yaca_realloc(written, (void**)&lcipher);
147 if (ret != YACA_ERROR_NONE)
150 *ciphertext = lcipher;
151 *ciphertext_len = written;
153 ret = YACA_ERROR_NONE;
157 yaca_context_destroy(ctx);
162 API int yaca_simple_decrypt(yaca_encrypt_algorithm_e algo,
163 yaca_block_cipher_mode_e bcm,
164 const yaca_key_h sym_key,
166 const char *ciphertext,
167 size_t ciphertext_len,
169 size_t *plaintext_len)
174 size_t out_len, lplain_len, written;
176 if (ciphertext == NULL || ciphertext_len == 0 ||
177 plaintext == NULL || plaintext_len == NULL ||
178 sym_key == YACA_KEY_NULL)
179 return YACA_ERROR_INVALID_PARAMETER;
181 ret = yaca_decrypt_initialize(&ctx, algo, bcm, sym_key, iv);
182 if (ret != YACA_ERROR_NONE)
185 ret = yaca_context_get_output_length(ctx, ciphertext_len, &out_len);
186 if (ret != YACA_ERROR_NONE)
189 ret = yaca_context_get_output_length(ctx, 0, &lplain_len);
190 if (ret != YACA_ERROR_NONE)
193 if (out_len > SIZE_MAX - lplain_len) {
194 ret = YACA_ERROR_INVALID_PARAMETER;
198 lplain_len += out_len;
199 assert(lplain_len > 0);
201 ret = yaca_malloc(lplain_len, (void**)&lplain);
202 if (ret != YACA_ERROR_NONE)
205 out_len = lplain_len;
206 ret = yaca_decrypt_update(ctx, ciphertext, ciphertext_len, lplain, &out_len);
207 if (ret != YACA_ERROR_NONE)
210 assert(out_len <= lplain_len);
213 out_len = lplain_len - written;
214 ret = yaca_decrypt_finalize(ctx, lplain + written, &out_len);
215 if (ret != YACA_ERROR_NONE)
219 assert(written <= lplain_len && written > 0);
221 ret = yaca_realloc(written, (void**)&lplain);
222 if (ret != YACA_ERROR_NONE)
226 *plaintext_len = written;
228 ret = YACA_ERROR_NONE;
232 yaca_context_destroy(ctx);
237 static int sign(const yaca_context_h ctx, const char *data, size_t data_len,
238 char **signature, size_t *signature_len)
242 assert(signature != NULL);
243 assert(signature_len != NULL);
246 ret = yaca_sign_update(ctx, data, data_len);
247 if (ret != YACA_ERROR_NONE)
251 ret = yaca_context_get_output_length(ctx, 0, signature_len);
252 if (ret != YACA_ERROR_NONE)
255 assert(*signature_len > 0);
257 ret = yaca_malloc(*signature_len, (void**)signature);
258 if (ret != YACA_ERROR_NONE)
261 ret = yaca_sign_finalize(ctx, *signature, signature_len);
262 if (ret != YACA_ERROR_NONE) {
263 yaca_free(*signature);
270 API int yaca_simple_calculate_signature(yaca_digest_algorithm_e algo,
271 const yaca_key_h key,
275 size_t *signature_len)
278 yaca_context_h ctx = YACA_CONTEXT_NULL;
280 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
281 signature == NULL || signature_len == NULL)
282 return YACA_ERROR_INVALID_PARAMETER;
284 ret = yaca_sign_initialize(&ctx, algo, key);
285 if (ret != YACA_ERROR_NONE)
288 ret = sign(ctx, data, data_len, signature, signature_len);
290 yaca_context_destroy(ctx);
295 API int yaca_simple_verify_signature(yaca_digest_algorithm_e algo,
296 const yaca_key_h key,
299 const char *signature,
300 size_t signature_len)
303 yaca_context_h ctx = YACA_CONTEXT_NULL;
305 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
306 signature == NULL || signature_len == 0)
307 return YACA_ERROR_INVALID_PARAMETER;
309 ret = yaca_verify_initialize(&ctx, algo, key);
310 if (ret != YACA_ERROR_NONE)
314 ret = yaca_verify_update(ctx, data, data_len);
315 if (ret != YACA_ERROR_NONE)
319 ret = yaca_verify_finalize(ctx, signature, signature_len);
322 yaca_context_destroy(ctx);
327 API int yaca_simple_calculate_hmac(yaca_digest_algorithm_e algo,
328 const yaca_key_h key,
335 yaca_context_h ctx = YACA_CONTEXT_NULL;
337 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
338 mac == NULL || mac_len == NULL)
339 return YACA_ERROR_INVALID_PARAMETER;
341 ret = yaca_sign_initialize_hmac(&ctx, algo, key);
342 if (ret != YACA_ERROR_NONE)
345 ret = sign(ctx, data, data_len, mac, mac_len);
347 yaca_context_destroy(ctx);
352 API int yaca_simple_calculate_cmac(yaca_encrypt_algorithm_e algo,
353 const yaca_key_h key,
360 yaca_context_h ctx = YACA_CONTEXT_NULL;
362 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
363 mac == NULL || mac_len == NULL)
364 return YACA_ERROR_INVALID_PARAMETER;
366 ret = yaca_sign_initialize_cmac(&ctx, algo, key);
367 if (ret != YACA_ERROR_NONE)
370 ret = sign(ctx, data, data_len, mac, mac_len);
372 yaca_context_destroy(ctx);