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
23 * @details This is simple API.
25 * - All operations are single-shot (no streaming possible)
26 * - Context is not used
27 * - Only digest, signatures and symmetric ciphers are supported
28 * - Disabling PKCS#7 padding for ECB and CBC chaining is not supported
29 * - Changing the default PKCS#1 padding for sign/verify is not supported
30 * - GCM and CCM chaining is not supported
31 * - RC2 effective key bits property is not supported
32 * - All outputs are allocated by the library
39 #include <yaca_types.h>
46 * @addtogroup CAPI_YACA_SIMPLE_MODULE
51 * @brief Encrypts data using a symmetric cipher.
55 * @remarks yaca_simple_encrypt() doesn't support #YACA_BCM_GCM and #YACA_BCM_CCM
57 * @remarks The @a ciphertext should be freed using yaca_free()
59 * @remarks The @a plaintext can be NULL but then @a plaintext_len must be 0
61 * @param[in] algo Encryption algorithm (select #YACA_ENCRYPT_AES if unsure)
62 * @param[in] bcm Chaining mode (select #YACA_BCM_CBC if unsure)
63 * @param[in] sym_key Symmetric encryption key (see yaca_key.h for key generation functions)
64 * @param[in] iv Initialization Vector
65 * @param[in] plaintext Plaintext to be encrypted
66 * @param[in] plaintext_len Length of the plaintext
67 * @param[out] ciphertext Encrypted data, will be allocated by the library
68 * @param[out] ciphertext_len Length of the encrypted data (may be larger than decrypted)
70 * @return #YACA_ERROR_NONE on success, negative on error
71 * @retval #YACA_ERROR_NONE Successful
72 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
73 * invalid algo, bcm, sym_key or iv)
74 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
75 * @retval #YACA_ERROR_INTERNAL Internal error
77 * @see #yaca_encrypt_algorithm_e
78 * @see #yaca_block_cipher_mode_e
79 * @see yaca_simple_decrypt()
82 int yaca_simple_encrypt(yaca_encrypt_algorithm_e algo,
83 yaca_block_cipher_mode_e bcm,
84 const yaca_key_h sym_key,
86 const char *plaintext,
89 size_t *ciphertext_len);
92 * @brief Decrypts data using a symmetric cipher.
96 * @remarks yaca_simple_decrypt() doesn't support #YACA_BCM_GCM and #YACA_BCM_CCM
98 * @remarks The @a plaintext should be freed using yaca_free()
100 * @remarks The @a ciphertext can be NULL but then @a ciphertext_len must be 0
102 * @param[in] algo Decryption algorithm that was used to encrypt the data
103 * @param[in] bcm Chaining mode that was used to encrypt the data
104 * @param[in] sym_key Symmetric encryption key that was used to encrypt the data
105 * @param[in] iv Initialization Vector that was used to encrypt the data
106 * @param[in] ciphertext Ciphertext to be decrypted
107 * @param[in] ciphertext_len Length of ciphertext
108 * @param[out] plaintext Decrypted data, will be allocated by the library
109 * @param[out] plaintext_len Length of the decrypted data
111 * @return #YACA_ERROR_NONE on success, negative on error
112 * @retval #YACA_ERROR_NONE Successful
113 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
114 * invalid algo, bcm, sym_key or iv)
115 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
116 * @retval #YACA_ERROR_INTERNAL Internal error
118 * @see #yaca_encrypt_algorithm_e
119 * @see #yaca_block_cipher_mode_e
120 * @see yaca_simple_encrypt()
123 int yaca_simple_decrypt(yaca_encrypt_algorithm_e algo,
124 yaca_block_cipher_mode_e bcm,
125 const yaca_key_h sym_key,
127 const char *ciphertext,
128 size_t ciphertext_len,
130 size_t *plaintext_len);
133 * @brief Calculates a digest of a message.
137 * @remarks The @a digest should be freed using yaca_free()
139 * @remarks The @a message can be NULL but then @a message_len must be 0
141 * @param[in] algo Digest algorithm (select #YACA_DIGEST_SHA256 if unsure)
142 * @param[in] message Message from which the digest is to be calculated
143 * @param[in] message_len Length of the message
144 * @param[out] digest Message digest, will be allocated by the library
145 * @param[out] digest_len Length of message digest (depends on algorithm)
147 * @return #YACA_ERROR_NONE on success, negative on error
148 * @retval #YACA_ERROR_NONE Successful
149 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL,
151 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
152 * @retval #YACA_ERROR_INTERNAL Internal error
154 * @see #yaca_digest_algorithm_e
157 int yaca_simple_calculate_digest(yaca_digest_algorithm_e algo,
164 * @brief Creates a signature using asymmetric private key.
168 * @remarks The @a signature should be freed using yaca_free()
170 * @remarks The @a message can be NULL but then @a message_len must be 0
172 * @param[in] algo Digest algorithm that will be used
173 * @param[in] prv_key Private key that will be used, algorithm is
174 * deduced based on key type, supported key types:
175 * - #YACA_KEY_TYPE_RSA_PRIV,
176 * - #YACA_KEY_TYPE_DSA_PRIV,
177 * - #YACA_KEY_TYPE_EC_PRIV
178 * @param[in] message Message to be signed
179 * @param[in] message_len Length of the message
180 * @param[out] signature Message signature, will be allocated by the library
181 * @param[out] signature_len Length of the signature
183 * @return #YACA_ERROR_NONE on success, negative on error
184 * @retval #YACA_ERROR_NONE Successful
185 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
186 * invalid algo or key)
187 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
188 * @retval #YACA_ERROR_INTERNAL Internal error
190 * @see #yaca_key_type_e
191 * @see #yaca_digest_algorithm_e
192 * @see yaca_simple_verify_signature()
195 int yaca_simple_calculate_signature(yaca_digest_algorithm_e algo,
196 const yaca_key_h prv_key,
200 size_t *signature_len);
203 * @brief Verifies a signature using asymmetric public key.
207 * @remarks The @a message can be NULL but then @a message_len must be 0
209 * @param[in] algo Digest algorithm that will be used
210 * @param[in] pub_key Public key that will be used, algorithm is
211 * deduced based on key type, supported key types:
212 * - #YACA_KEY_TYPE_RSA_PUB,
213 * - #YACA_KEY_TYPE_DSA_PUB,
214 * - #YACA_KEY_TYPE_EC_PUB
215 * @param[in] message Message
216 * @param[in] message_len Length of the message
217 * @param[in] signature Message signature to be verified
218 * @param[in] signature_len Length of the signature
220 * @return #YACA_ERROR_NONE on success, negative on error
221 * @retval #YACA_ERROR_NONE Successful
222 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
223 * invalid algo or key)
224 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
225 * @retval #YACA_ERROR_INTERNAL Internal error
226 * @retval #YACA_ERROR_DATA_MISMATCH The verification failed
228 * @see #yaca_key_type_e
229 * @see #yaca_digest_algorithm_e
230 * @see yaca_simple_calculate_signature()
232 int yaca_simple_verify_signature(yaca_digest_algorithm_e algo,
233 const yaca_key_h pub_key,
236 const char *signature,
237 size_t signature_len);
240 * @brief Calculates a HMAC of given message using symmetric key.
244 * @remarks For verification, calculate message HMAC and compare with received MAC using
247 * @remarks The @a mac should be freed using yaca_free()
249 * @remarks The @a message can be NULL but then @a message_len must be 0
251 * @param[in] algo Digest algorithm that will be used
252 * @param[in] sym_key Key that will be used, supported key types:
253 * - #YACA_KEY_TYPE_SYMMETRIC,
254 * - #YACA_KEY_TYPE_DES
255 * @param[in] message Message to calculate HMAC from
256 * @param[in] message_len Length of the message
257 * @param[out] mac MAC, will be allocated by the library
258 * @param[out] mac_len Length of the MAC
260 * @return #YACA_ERROR_NONE on success, negative on error
261 * @retval #YACA_ERROR_NONE Successful
262 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
263 * invalid algo or key)
264 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
265 * @retval #YACA_ERROR_INTERNAL Internal error
267 * @see #yaca_key_type_e
268 * @see #yaca_digest_algorithm_e
272 int yaca_simple_calculate_hmac(yaca_digest_algorithm_e algo,
273 const yaca_key_h sym_key,
280 * @brief Calculates a CMAC of given message using symmetric key.
284 * @remarks For verification, calculate message CMAC and compare with received MAC using
287 * @remarks The @a mac should be freed using yaca_free()
289 * @remarks The @a message can be NULL but then @a message_len must be 0
291 * @param[in] algo Encryption algorithm that will be used
292 * @param[in] sym_key Key that will be used, supported key types:
293 * - #YACA_KEY_TYPE_SYMMETRIC,
294 * - #YACA_KEY_TYPE_DES
295 * @param[in] message Message to calculate CMAC from
296 * @param[in] message_len Length of the message
297 * @param[out] mac MAC, will be allocated by the library
298 * @param[out] mac_len Length of the MAC
300 * @return #YACA_ERROR_NONE on success, negative on error
301 * @retval #YACA_ERROR_NONE Successful
302 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
303 * invalid algo or key)
304 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
305 * @retval #YACA_ERROR_INTERNAL Internal error
307 * @see #yaca_key_type_e
308 * @see #yaca_encrypt_algorithm_e
312 int yaca_simple_calculate_cmac(yaca_encrypt_algorithm_e algo,
313 const yaca_key_h sym_key,
327 #endif /* YACA_SIMPLE_H */