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 * @defgroup Non-Crypto Yet Another Crypto API - non crypto related functions.
42 * @brief NULL value for the crypto context.
46 #define YACA_CONTEXT_NULL ((yaca_context_h) NULL)
49 * @brief Initializes the library. Must be called before any other crypto
50 * function. Should be called once in each thread that uses yaca.
54 * @return #YACA_ERROR_NONE on success, negative on error
55 * @retval #YACA_ERROR_NONE Successful
56 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
57 * @retval #YACA_ERROR_INTERNAL Internal error
61 int yaca_initialize(void);
64 * @brief Cleans up the library. Must be called before exiting the thread that
65 * called yaca_initialize().
69 * @return #YACA_ERROR_NONE on success
70 * @retval #YACA_ERROR_NONE Successful
72 * @see yaca_initialize()
74 int yaca_cleanup(void);
77 * @brief Allocates the memory.
81 * @remarks The @a memory should be freed using yaca_free()
83 * @param[in] size Size of the allocation (bytes)
84 * @param[out] memory Allocated memory
86 * @return #YACA_ERROR_NONE on success, negative on error
87 * @retval #YACA_ERROR_NONE Successful
88 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0)
89 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
95 int yaca_malloc(size_t size, void **memory);
98 * @brief Allocates the zeroed memory.
102 * @remarks The @a memory should be freed using yaca_free()
104 * @param[in] size Size of the allocation (bytes)
105 * @param[out] memory Allocated memory
107 * @return #YACA_ERROR_NONE on success, negative on error
108 * @retval #YACA_ERROR_NONE Successful
109 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0)
110 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
113 * @see yaca_realloc()
116 int yaca_zalloc(size_t size, void **memory);
119 * @brief Re-allocates the memory.
123 * @remarks In case of failure the function doesn't free the memory pointed by @a memory.
125 * @remarks If @a *memory is NULL then the call is equivalent to yaca_malloc().
127 * @remarks If the function fails the contents of @a memory will be left unchanged.
129 * @remarks The @a memory should be freed using yaca_free()
131 * @param[in] size Size of the new allocation (bytes)
132 * @param[in,out] memory Memory to be reallocated
134 * @return #YACA_ERROR_NONE on success, negative on error
135 * @retval #YACA_ERROR_NONE Successful
136 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0)
137 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
143 int yaca_realloc(size_t size, void **memory);
146 * @brief Frees the memory allocated by yaca_malloc(), yaca_zalloc(),
147 * yaca_realloc() or one of the cryptographic operations.
151 * @param[in] memory Pointer to the memory to be freed
155 * @see yaca_realloc()
157 void yaca_free(void *memory);
160 * @brief Safely compares first @a len bytes of two buffers.
164 * @param[in] first Pointer to the first buffer
165 * @param[in] second Pointer to the second buffer
166 * @param[in] len Length to compare
168 * @return #YACA_ERROR_NONE when buffers are equal otherwise #YACA_ERROR_DATA_MISMATCH
169 * @retval #YACA_ERROR_NONE Successful
170 * @retval #YACA_ERROR_DATA_MISMATCH Buffers are different
172 int yaca_memcmp(const void *first, const void *second, size_t len);
175 * @brief Generates random data.
179 * @param[in,out] data Pointer to the memory to be randomized
180 * @param[in] data_len Length of the memory to be randomized
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 * @retval #YACA_ERROR_INTERNAL Internal error
187 int yaca_randomize_bytes(char *data, size_t data_len);
190 * @brief Sets the non-standard context properties. Can only be called on an
191 * initialized context.
193 * @remarks The @a value has to be of type appropriate for given property. See #yaca_property_e
194 * for details on corresponding types.
198 * @param[in,out] ctx Previously initialized crypto context
199 * @param[in] property Property to be set
200 * @param[in] value Property value
201 * @param[in] value_len Length of the property value
203 * @return #YACA_ERROR_NONE on success, negative on error
204 * @retval #YACA_ERROR_NONE Successful
205 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0,
206 * invalid ctx or property)
207 * @retval #YACA_ERROR_INTERNAL Internal error
209 * @see #yaca_property_e
210 * @see yaca_context_get_property()
212 int yaca_context_set_property(yaca_context_h ctx,
213 yaca_property_e property,
218 * @brief Returns the non-standard context properties. Can only be called on an
219 * initialized context.
223 * @remarks The @a value should be freed using yaca_free()
225 * @remarks The @a value has to be of type appropriate for given property. See #yaca_property_e
226 * for details on corresponding types.
228 * @remarks @a value_len can be NULL if returned @a value is a single object (i.e. not an array/buffer)
230 * @param[in] ctx Previously initialized crypto context
231 * @param[in] property Property to be read
232 * @param[out] value Copy of the property value
233 * @param[out] value_len Length of the property value will be returned here
235 * @return #YACA_ERROR_NONE on success, negative on error
236 * @retval #YACA_ERROR_NONE Successful
237 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL,
238 * invalid ctx or property)
239 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
240 * @retval #YACA_ERROR_INTERNAL Internal error
242 * @see #yaca_property_e
243 * @see yaca_context_set_property()
246 int yaca_context_get_property(const yaca_context_h ctx,
247 yaca_property_e property,
252 * @brief Destroys the crypto context. Must be called on all contexts that are
253 * no longer used. Passing #YACA_CONTEXT_NULL is allowed.
257 * @param[in,out] ctx Crypto context
259 * @see #yaca_context_h
262 void yaca_context_destroy(yaca_context_h ctx);
265 * @brief Returns the output length for a given algorithm. Can only be called
266 * on an initialized context.
270 * @remarks This function can be used to learn the required size of the output buffer
271 * for a single operation (eg. *_update or *_finalize). In case the operation
272 * has no input (eg. *_finalize), the value of @a input_len has to be set to 0.
274 * @param[in] ctx Previously initialized crypto context
275 * @param[in] input_len Length of the input data to be processed
276 * @param[out] output_len Required length of the output
278 * @return #YACA_ERROR_NONE on success, negative on error
279 * @retval #YACA_ERROR_NONE Successful
280 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL,
281 * invalid context or too big input_len)
282 * @retval #YACA_ERROR_INTERNAL Internal error
284 int yaca_context_get_output_length(const yaca_context_h ctx,
294 #endif /* YACA_CRYPTO_H */