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#5 padding for ECB and CBC chaining is not supported
29 * - GCM and CCM chaining is not supported
30 * - RC2 effective key bits property is not supported
31 * - All outputs are allocated by the library
38 #include <yaca_types.h>
45 * @addtogroup CAPI_YACA_SIMPLE_MODULE
50 * @brief Encrypts data using a symmetric cipher.
54 * @remarks yaca_simple_encrypt() doesn't support #YACA_BCM_GCM and #YACA_BCM_CCM
56 * @remarks The @a ciphertext should be freed using yaca_free()
58 * @remarks The @a plaintext can be NULL but then @a plaintext_len must be 0
60 * @param[in] algo Encryption algorithm (select #YACA_ENCRYPT_AES if unsure)
61 * @param[in] bcm Chaining mode (select #YACA_BCM_CBC if unsure)
62 * @param[in] sym_key Symmetric encryption key (see yaca_key.h for key generation functions)
63 * @param[in] iv Initialization Vector
64 * @param[in] plaintext Plaintext to be encrypted
65 * @param[in] plaintext_len Length of the plaintext
66 * @param[out] ciphertext Encrypted data, will be allocated by the library
67 * @param[out] ciphertext_len Length of the encrypted data (may be larger than decrypted)
69 * @return #YACA_ERROR_NONE on success, negative on error
70 * @retval #YACA_ERROR_NONE Successful
71 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
72 * invalid algo, bcm, sym_key or iv)
73 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
74 * @retval #YACA_ERROR_INTERNAL Internal error
76 * @see #yaca_encrypt_algorithm_e
77 * @see #yaca_block_cipher_mode_e
78 * @see yaca_simple_decrypt()
81 int yaca_simple_encrypt(yaca_encrypt_algorithm_e algo,
82 yaca_block_cipher_mode_e bcm,
83 const yaca_key_h sym_key,
85 const char *plaintext,
88 size_t *ciphertext_len);
91 * @brief Decrypts data using a symmetric cipher.
95 * @remarks yaca_simple_decrypt() doesn't support #YACA_BCM_GCM and #YACA_BCM_CCM
97 * @remarks The @a plaintext should be freed using yaca_free()
99 * @remarks The @a ciphertext can be NULL but then @a ciphertext_len must be 0
101 * @param[in] algo Decryption algorithm that was used to encrypt the data
102 * @param[in] bcm Chaining mode that was used to encrypt the data
103 * @param[in] sym_key Symmetric encryption key that was used to encrypt the data
104 * @param[in] iv Initialization Vector that was used to encrypt the data
105 * @param[in] ciphertext Ciphertext to be decrypted
106 * @param[in] ciphertext_len Length of ciphertext
107 * @param[out] plaintext Decrypted data, will be allocated by the library
108 * @param[out] plaintext_len Length of the decrypted data
110 * @return #YACA_ERROR_NONE on success, negative on error
111 * @retval #YACA_ERROR_NONE Successful
112 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
113 * invalid algo, bcm, sym_key or iv)
114 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
115 * @retval #YACA_ERROR_INTERNAL Internal error
117 * @see #yaca_encrypt_algorithm_e
118 * @see #yaca_block_cipher_mode_e
119 * @see yaca_simple_encrypt()
122 int yaca_simple_decrypt(yaca_encrypt_algorithm_e algo,
123 yaca_block_cipher_mode_e bcm,
124 const yaca_key_h sym_key,
126 const char *ciphertext,
127 size_t ciphertext_len,
129 size_t *plaintext_len);
132 * @brief Calculates a digest of a message.
136 * @remarks The @a digest should be freed using yaca_free()
138 * @remarks The @a message can be NULL but then @a message_len must be 0
140 * @param[in] algo Digest algorithm (select #YACA_DIGEST_SHA256 if unsure)
141 * @param[in] message Message from which the digest is to be calculated
142 * @param[in] message_len Length of the message
143 * @param[out] digest Message digest, will be allocated by the library
144 * @param[out] digest_len Length of message digest (depends on algorithm)
146 * @return #YACA_ERROR_NONE on success, negative on error
147 * @retval #YACA_ERROR_NONE Successful
148 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL,
150 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
151 * @retval #YACA_ERROR_INTERNAL Internal error
153 * @see #yaca_digest_algorithm_e
156 int yaca_simple_calculate_digest(yaca_digest_algorithm_e algo,
163 * @brief Creates a signature using asymmetric private key.
167 * @remarks The @a signature should be freed using yaca_free()
169 * @remarks The @a message can be NULL but then @a message_len must be 0
171 * @param[in] algo Digest algorithm that will be used
172 * @param[in] prv_key Private key that will be used, algorithm is
173 * deduced based on key type, supported key types:
174 * - #YACA_KEY_TYPE_RSA_PRIV,
175 * - #YACA_KEY_TYPE_DSA_PRIV,
176 * - #YACA_KEY_TYPE_EC_PRIV
177 * @param[in] message Message to be signed
178 * @param[in] message_len Length of the message
179 * @param[out] signature Message signature, will be allocated by the library
180 * @param[out] signature_len Length of the signature
182 * @return #YACA_ERROR_NONE on success, negative on error
183 * @retval #YACA_ERROR_NONE Successful
184 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
185 * invalid algo or key)
186 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
187 * @retval #YACA_ERROR_INTERNAL Internal error
189 * @see #yaca_key_type_e
190 * @see #yaca_digest_algorithm_e
191 * @see yaca_simple_verify_signature()
194 int yaca_simple_calculate_signature(yaca_digest_algorithm_e algo,
195 const yaca_key_h prv_key,
199 size_t *signature_len);
202 * @brief Verifies a signature using asymmetric public key.
206 * @remarks The @a message can be NULL but then @a message_len must be 0
208 * @param[in] algo Digest algorithm that will be used
209 * @param[in] pub_key Public key that will be used, algorithm is
210 * deduced based on key type, supported key types:
211 * - #YACA_KEY_TYPE_RSA_PUB,
212 * - #YACA_KEY_TYPE_DSA_PUB,
213 * - #YACA_KEY_TYPE_EC_PUB
214 * @param[in] message Message
215 * @param[in] message_len Length of the message
216 * @param[in] signature Message signature to be verified
217 * @param[in] signature_len Length of the signature
219 * @return #YACA_ERROR_NONE on success, negative on error
220 * @retval #YACA_ERROR_NONE Successful
221 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
222 * invalid algo or key)
223 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
224 * @retval #YACA_ERROR_INTERNAL Internal error
225 * @retval #YACA_ERROR_DATA_MISMATCH The verification failed
227 * @see #yaca_key_type_e
228 * @see #yaca_digest_algorithm_e
229 * @see yaca_simple_calculate_signature()
231 int yaca_simple_verify_signature(yaca_digest_algorithm_e algo,
232 const yaca_key_h pub_key,
235 const char *signature,
236 size_t signature_len);
239 * @brief Calculates a HMAC of given message using symmetric key.
243 * @remarks For verification, calculate message HMAC and compare with received MAC using
246 * @remarks The @a mac should be freed using yaca_free()
248 * @remarks The @a message can be NULL but then @a message_len must be 0
250 * @param[in] algo Digest algorithm that will be used
251 * @param[in] sym_key Key that will be used, supported key types:
252 * - #YACA_KEY_TYPE_SYMMETRIC,
253 * - #YACA_KEY_TYPE_DES
254 * @param[in] message Message to calculate HMAC from
255 * @param[in] message_len Length of the message
256 * @param[out] mac MAC, will be allocated by the library
257 * @param[out] mac_len Length of the MAC
259 * @return #YACA_ERROR_NONE on success, negative on error
260 * @retval #YACA_ERROR_NONE Successful
261 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
262 * invalid algo or key)
263 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
264 * @retval #YACA_ERROR_INTERNAL Internal error
266 * @see #yaca_key_type_e
267 * @see #yaca_digest_algorithm_e
271 int yaca_simple_calculate_hmac(yaca_digest_algorithm_e algo,
272 const yaca_key_h sym_key,
279 * @brief Calculates a CMAC of given message using symmetric key.
283 * @remarks For verification, calculate message CMAC and compare with received MAC using
286 * @remarks The @a mac should be freed using yaca_free()
288 * @remarks The @a message can be NULL but then @a message_len must be 0
290 * @param[in] algo Encryption algorithm that will be used
291 * @param[in] sym_key Key that will be used, supported key types:
292 * - #YACA_KEY_TYPE_SYMMETRIC,
293 * - #YACA_KEY_TYPE_DES
294 * @param[in] message Message to calculate CMAC from
295 * @param[in] message_len Length of the message
296 * @param[out] mac MAC, will be allocated by the library
297 * @param[out] mac_len Length of the MAC
299 * @return #YACA_ERROR_NONE on success, negative on error
300 * @retval #YACA_ERROR_NONE Successful
301 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0
302 * invalid algo or key)
303 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
304 * @retval #YACA_ERROR_INTERNAL Internal error
306 * @see #yaca_key_type_e
307 * @see #yaca_encrypt_algorithm_e
311 int yaca_simple_calculate_cmac(yaca_encrypt_algorithm_e algo,
312 const yaca_key_h sym_key,
326 #endif /* YACA_SIMPLE_H */