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)
97 char *lciphertext = NULL;
99 size_t lciphertext_len = 0;
102 if ((plaintext == NULL && plaintext_len > 0) || (plaintext != NULL && plaintext_len == 0) ||
103 ciphertext == NULL || ciphertext_len == NULL ||
104 sym_key == YACA_KEY_NULL ||
105 bcm == YACA_BCM_CCM || bcm == YACA_BCM_GCM)
106 return YACA_ERROR_INVALID_PARAMETER;
108 ret = yaca_encrypt_initialize(&ctx, algo, bcm, sym_key, iv);
109 if (ret != YACA_ERROR_NONE)
112 if (plaintext_len > 0) {
113 ret = yaca_context_get_output_length(ctx, plaintext_len, &out_len);
114 if (ret != YACA_ERROR_NONE)
118 ret = yaca_context_get_output_length(ctx, 0, &lciphertext_len);
119 if (ret != YACA_ERROR_NONE)
122 if (out_len > SIZE_MAX - lciphertext_len) {
123 ret = YACA_ERROR_INVALID_PARAMETER;
127 lciphertext_len += out_len;
128 assert(lciphertext_len > 0);
130 ret = yaca_malloc(lciphertext_len, (void**)&lciphertext);
131 if (ret != YACA_ERROR_NONE)
134 if (plaintext_len > 0) {
135 ret = yaca_encrypt_update(ctx, plaintext, plaintext_len, lciphertext, &out_len);
136 if (ret != YACA_ERROR_NONE)
140 assert(out_len <= lciphertext_len);
143 ret = yaca_encrypt_finalize(ctx, lciphertext + written, &out_len);
144 if (ret != YACA_ERROR_NONE)
148 assert(written <= lciphertext_len);
150 if (((bcm == YACA_BCM_CBC || bcm == YACA_BCM_ECB) && written == 0) ||
151 (bcm != YACA_BCM_CBC && bcm != YACA_BCM_ECB && plaintext_len == 0 && written > 0)) {
152 ret = YACA_ERROR_INTERNAL;
157 ret = yaca_realloc(written, (void**)&lciphertext);
158 if (ret != YACA_ERROR_NONE)
161 yaca_free(lciphertext);
165 *ciphertext = lciphertext;
166 *ciphertext_len = written;
168 ret = YACA_ERROR_NONE;
171 yaca_free(lciphertext);
172 yaca_context_destroy(ctx);
177 API int yaca_simple_decrypt(yaca_encrypt_algorithm_e algo,
178 yaca_block_cipher_mode_e bcm,
179 const yaca_key_h sym_key,
181 const char *ciphertext,
182 size_t ciphertext_len,
184 size_t *plaintext_len)
188 char *lplaintext = NULL;
190 size_t lplaintext_len = 0;
193 if ((ciphertext == NULL && ciphertext_len > 0) || (ciphertext != NULL && ciphertext_len == 0) ||
194 ((bcm == YACA_BCM_ECB || bcm == YACA_BCM_CBC) && ciphertext == NULL && ciphertext_len == 0) ||
195 plaintext == NULL || plaintext_len == NULL ||
196 sym_key == YACA_KEY_NULL ||
197 bcm == YACA_BCM_CCM || bcm == YACA_BCM_GCM)
198 return YACA_ERROR_INVALID_PARAMETER;
200 ret = yaca_decrypt_initialize(&ctx, algo, bcm, sym_key, iv);
201 if (ret != YACA_ERROR_NONE)
204 if (ciphertext_len > 0) {
205 ret = yaca_context_get_output_length(ctx, ciphertext_len, &out_len);
206 if (ret != YACA_ERROR_NONE)
210 ret = yaca_context_get_output_length(ctx, 0, &lplaintext_len);
211 if (ret != YACA_ERROR_NONE)
214 if (out_len > SIZE_MAX - lplaintext_len) {
215 ret = YACA_ERROR_INVALID_PARAMETER;
219 lplaintext_len += out_len;
220 assert(lplaintext_len > 0);
222 ret = yaca_malloc(lplaintext_len, (void**)&lplaintext);
223 if (ret != YACA_ERROR_NONE)
226 if (ciphertext_len > 0) {
227 ret = yaca_decrypt_update(ctx, ciphertext, ciphertext_len, lplaintext, &out_len);
228 if (ret != YACA_ERROR_NONE)
232 assert(out_len <= lplaintext_len);
235 ret = yaca_decrypt_finalize(ctx, lplaintext + written, &out_len);
236 if (ret != YACA_ERROR_NONE)
240 assert(written <= lplaintext_len);
242 if (bcm != YACA_BCM_CBC && bcm != YACA_BCM_ECB && ciphertext_len == 0 && written > 0) {
243 ret = YACA_ERROR_INTERNAL;
248 ret = yaca_realloc(written, (void**)&lplaintext);
249 if (ret != YACA_ERROR_NONE)
252 yaca_free(lplaintext);
256 *plaintext = lplaintext;
257 *plaintext_len = written;
259 ret = YACA_ERROR_NONE;
262 yaca_free(lplaintext);
263 yaca_context_destroy(ctx);
268 static int sign(const yaca_context_h ctx, const char *data, size_t data_len,
269 char **signature, size_t *signature_len)
273 assert(signature != NULL);
274 assert(signature_len != NULL);
277 ret = yaca_sign_update(ctx, data, data_len);
278 if (ret != YACA_ERROR_NONE)
282 ret = yaca_context_get_output_length(ctx, 0, signature_len);
283 if (ret != YACA_ERROR_NONE)
286 assert(*signature_len > 0);
288 ret = yaca_malloc(*signature_len, (void**)signature);
289 if (ret != YACA_ERROR_NONE)
292 ret = yaca_sign_finalize(ctx, *signature, signature_len);
293 if (ret != YACA_ERROR_NONE) {
294 yaca_free(*signature);
301 API int yaca_simple_calculate_signature(yaca_digest_algorithm_e algo,
302 const yaca_key_h prv_key,
306 size_t *signature_len)
309 yaca_context_h ctx = YACA_CONTEXT_NULL;
311 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
312 signature == NULL || signature_len == NULL)
313 return YACA_ERROR_INVALID_PARAMETER;
315 ret = yaca_sign_initialize(&ctx, algo, prv_key);
316 if (ret != YACA_ERROR_NONE)
319 ret = sign(ctx, data, data_len, signature, signature_len);
321 yaca_context_destroy(ctx);
326 API int yaca_simple_verify_signature(yaca_digest_algorithm_e algo,
327 const yaca_key_h pub_key,
330 const char *signature,
331 size_t signature_len)
334 yaca_context_h ctx = YACA_CONTEXT_NULL;
336 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
337 signature == NULL || signature_len == 0)
338 return YACA_ERROR_INVALID_PARAMETER;
340 ret = yaca_verify_initialize(&ctx, algo, pub_key);
341 if (ret != YACA_ERROR_NONE)
345 ret = yaca_verify_update(ctx, data, data_len);
346 if (ret != YACA_ERROR_NONE)
350 ret = yaca_verify_finalize(ctx, signature, signature_len);
353 yaca_context_destroy(ctx);
358 API int yaca_simple_calculate_hmac(yaca_digest_algorithm_e algo,
359 const yaca_key_h sym_key,
366 yaca_context_h ctx = YACA_CONTEXT_NULL;
368 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
369 mac == NULL || mac_len == NULL)
370 return YACA_ERROR_INVALID_PARAMETER;
372 ret = yaca_sign_initialize_hmac(&ctx, algo, sym_key);
373 if (ret != YACA_ERROR_NONE)
376 ret = sign(ctx, data, data_len, mac, mac_len);
378 yaca_context_destroy(ctx);
383 API int yaca_simple_calculate_cmac(yaca_encrypt_algorithm_e algo,
384 const yaca_key_h sym_key,
391 yaca_context_h ctx = YACA_CONTEXT_NULL;
393 if ((data == NULL && data_len > 0) || (data != NULL && data_len == 0) ||
394 mac == NULL || mac_len == NULL)
395 return YACA_ERROR_INVALID_PARAMETER;
397 ret = yaca_sign_initialize_cmac(&ctx, algo, sym_key);
398 if (ret != YACA_ERROR_NONE)
401 ret = sign(ctx, data, data_len, mac, mac_len);
403 yaca_context_destroy(ctx);