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
28 #include <yaca_types.h>
35 * @addtogroup CAPI_YACA_KEY_MODULE
40 * Note: Key Advanced API for the key and IV handling.
46 * @brief NULL value for yaca_key_h type.
50 #define YACA_KEY_NULL ((yaca_key_h) NULL)
53 * @brief Get key's type.
57 * @param[in] key Key which type we return
58 * @param[out] key_type Key type
60 * @return #YACA_ERROR_NONE on success, negative on error
61 * @retval #YACA_ERROR_NONE Successful
62 * @retval #YACA_ERROR_INVALID_PARAMETER Either of the params is NULL
64 int yaca_key_get_type(const yaca_key_h key, yaca_key_type_e *key_type);
67 * @brief Get key's length (in bits).
71 * @param[in] key Key which length we return
72 * @param[out] key_bit_len Key length in bits
74 * @return #YACA_ERROR_NONE on success, negative on error
75 * @retval #YACA_ERROR_NONE Successful
76 * @retval #YACA_ERROR_INVALID_PARAMETER Either of the params is NULL
77 * @retval #YACA_ERROR_INTERNAL Internal error
79 int yaca_key_get_bit_length(const yaca_key_h key, size_t *key_bit_len);
82 * @brief Imports a key.
86 * @remarks This function imports a key trying to match it to the key_type specified.
87 * It should autodetect both the key format and the file format.
89 * @remarks For symmetric, IV and DES keys RAW binary format and BASE64 encoded
90 * binary format are supported.
91 * For asymmetric keys PEM and DER file formats are supported.
93 * @remarks Asymmetric keys can be in PKCS#1 or SSleay key formats (for RSA and
94 * DSA respectively). Asymmetric private keys can also be in PKCS#8
95 * format. Additionally it is possible to import public RSA key from
98 * @remarks If the key is encrypted the algorithm will be autodetected and password
99 * used. If it's not known if the key is encrypted one should pass NULL as
100 * password and check for the #YACA_ERROR_INVALID_PASSWORD return code.
102 * @remarks If the imported key will be detected as a format that does not support
103 * encryption and password was passed #YACA_ERROR_INVALID_PARAMETER will
104 * be returned. For a list of keys and formats that do support encryption
105 * see yaca_key_export() documentation.
107 * @remarks The @a key should be released using yaca_key_destroy()
109 * @param[in] key_type Type of the key
110 * @param[in] password null terminated password for the key (can be NULL)
111 * @param[in] data Blob containing the key
112 * @param[in] data_len Size of the blob
113 * @param[out] key Returned key
115 * @return #YACA_ERROR_NONE on success, negative on error
116 * @retval #YACA_ERROR_NONE Successful
117 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0,
118 * invalid key_type or data_len too big)
119 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
120 * @retval #YACA_ERROR_INTERNAL Internal error
121 * @retval #YACA_ERROR_INVALID_PASSWORD Invalid password given or password was required
124 * @see #yaca_key_type_e
125 * @see yaca_key_export()
126 * @see yaca_key_destroy()
128 int yaca_key_import(yaca_key_type_e key_type,
129 const char *password,
135 * @brief Exports a key to arbitrary format. Export may fail if key is HW-based.
139 * @remarks This function exports the key to an arbitrary key format and key file format.
141 * @remarks For key formats two values are allowed:
142 * - #YACA_KEY_FORMAT_DEFAULT: this is the only option possible in case of symmetric
143 * keys (or IV), for asymmetric keys it will choose PKCS#1
144 * for RSA keys and SSLeay for DSA keys.
145 * - #YACA_KEY_FORMAT_PKCS8: this will only work for private asymmetric keys.
147 * @remarks The following file formats are supported:
148 * - #YACA_KEY_FILE_FORMAT_RAW: used only for symmetric, raw binary format
149 * - #YACA_KEY_FILE_FORMAT_BASE64: used only for symmetric, BASE64 encoded binary form
150 * - #YACA_KEY_FILE_FORMAT_PEM: used only for asymmetric, PEM file format
151 * - #YACA_KEY_FILE_FORMAT_DER: used only for asymmetric, DER file format
153 * @remarks Encryption is supported and optional for RSA/DSA private keys in the
154 * #YACA_KEY_FORMAT_DEFAULT with #YACA_KEY_FILE_FORMAT_PEM format. If no password is
155 * provided the exported key will be unencrypted. The encryption algorithm used
156 * in this case is AES-256-CBC.
158 * @remarks Encryption is obligatory for #YACA_KEY_FORMAT_PKCS8 format (for both, PEM and DER
159 * file formats). If no password is provided the #YACA_ERROR_INVALID_PARAMETER will
160 * be returned. The encryption algorithm used in this case is PBE with DES-CBC.
162 * @remakrs Encryption is not supported for the symmetric and public keys in all their
163 * supported formats. If a password is provided in such case the
164 * #YACA_ERROR_INVALID_PARAMETER will be returned.
166 * @param[in] key Key to be exported
167 * @param[in] key_fmt Format of the key
168 * @param[in] key_file_fmt Format of the key file
169 * @param[in] password Password used for the encryption (can be NULL)
170 * @param[out] data Data, allocated by the library, containing exported key
171 * (must be freed with yaca_free())
172 * @param[out] data_len Size of the output data
174 * @return #YACA_ERROR_NONE on success, negative on error
175 * @retval #YACA_ERROR_NONE Successful
176 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0,
177 * invalid key/file format or data_len too big)
178 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
179 * @retval #YACA_ERROR_INTERNAL Internal error
181 * @see #yaca_key_format_e
182 * @see #yaca_key_file_format_e
183 * @see yaca_key_import()
184 * @see yaca_key_destroy()
186 int yaca_key_export(const yaca_key_h key,
187 yaca_key_format_e key_fmt,
188 yaca_key_file_format_e key_file_fmt,
189 const char *password,
194 * @brief Generates a secure key (or an initialization vector).
198 * @remarks This function is used to generate symmetric and private asymmetric keys.
200 * @remarks Supported key lengths:
201 * - RSA: length >= 256bits
202 * - DSA: length >= 512bits, multiple of 64
204 * @remarks The @a key should be released using yaca_key_destroy()
206 * @param[in] key_type Type of the key to be generated
207 * @param[in] key_bit_len Length of the key (in bits) to be generated
208 * @param[out] key Newly generated key
210 * @return #YACA_ERROR_NONE on success, negative on error
211 * @retval #YACA_ERROR_NONE Successful
212 * @retval #YACA_ERROR_INVALID_PARAMETER key is NULL, incorrect key_type or
213 * key_bit_len is not dividable by 8
214 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
215 * @retval #YACA_ERROR_INTERNAL Internal error
217 * @see #yaca_key_type_e
218 * @see #yaca_key_bit_length_e
219 * @see yaca_key_destroy()
221 int yaca_key_generate(yaca_key_type_e key_type,
226 * @brief Extracts public key from a private one.
230 * @remarks The @a pub_key should be released using yaca_key_destroy()
232 * @param[in] prv_key Private key to extract the public one from
233 * @param[out] pub_key Extracted public key
235 * @return #YACA_ERROR_NONE on success, negative on error
236 * @retval #YACA_ERROR_NONE Successful
237 * @retval #YACA_ERROR_INVALID_PARAMETER prv_key is of invalid type or pub_key is NULL
238 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
239 * @retval #YACA_ERROR_INTERNAL Internal error
241 * @see yaca_key_generate()
242 * @see yaca_key_import()
243 * @see yaca_key_destroy()
245 int yaca_key_extract_public(const yaca_key_h prv_key, yaca_key_h *pub_key);
248 * @brief Release the key created by the library. Passing YACA_KEY_NULL is allowed.
252 * @param[in,out] key Key to be released
254 * @see yaca_key_import()
255 * @see yaca_key_export()
256 * @see yaca_key_generate()
258 void yaca_key_destroy(yaca_key_h key);
262 * Note: Key-Derivation Advanced API for the key derivation.
268 * @brief Derives a key from user password (PKCS #5 a.k.a. pbkdf2 algorithm).
272 * @remarks The @a key should be released using yaca_key_destroy()
274 * @param[in] password User password as a NULL-terminated string
275 * @param[in] salt Salt, should be a non-empty string
276 * @param[in] salt_len Length of the salt
277 * @param[in] iterations Number of iterations
278 * @param[in] algo Digest algorithm that should be used in key generation
279 * @param[in] key_bit_len Length of a key (in bits) to be generated
280 * @param[out] key Newly generated key
282 * @return #YACA_ERROR_NONE on success, negative on error
283 * @retval #YACA_ERROR_NONE Successful
284 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0,
285 * invalid algo or key_bit_len not dividable by 8)
286 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
287 * @retval #YACA_ERROR_INTERNAL Internal error
289 * @see #yaca_digest_algorithm_e
290 * @see yaca_key_destroy()
292 int yaca_key_derive_pbkdf2(const char *password,
296 yaca_digest_algorithm_e algo,
308 #endif /* YACA_KEY_H */