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
153 * @return #YACA_ERROR_NONE on success
154 * @retval #YACA_ERROR_NONE Successful
158 * @see yaca_realloc()
160 int yaca_free(void *memory);
163 * @brief Safely compares first @a len bytes of two buffers.
167 * @param[in] first Pointer to the first buffer
168 * @param[in] second Pointer to the second buffer
169 * @param[in] len Length to compare
171 * @return #YACA_ERROR_NONE when buffers are equal otherwise #YACA_ERROR_DATA_MISMATCH
172 * @retval #YACA_ERROR_NONE Successful
173 * @retval #YACA_ERROR_DATA_MISMATCH Buffers are different
175 int yaca_memcmp(const void *first, const void *second, size_t len);
178 * @brief Generates random data.
182 * @param[in,out] data Pointer to the memory to be randomized
183 * @param[in] data_len Length of the memory to be randomized
185 * @return #YACA_ERROR_NONE on success, negative on error
186 * @retval #YACA_ERROR_NONE Successful
187 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL, 0)
188 * @retval #YACA_ERROR_INTERNAL Internal error
190 int yaca_randomize_bytes(char *data, size_t data_len);
193 * @brief Sets the non-standard context properties. Can only be called on an
194 * initialized context.
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 * @param[in] ctx Previously initialized crypto context
226 * @param[in] property Property to be read
227 * @param[out] value Copy of the property value
228 * @param[out] value_len Length of the property value will be returned here
230 * @return #YACA_ERROR_NONE on success, negative on error
231 * @retval #YACA_ERROR_NONE Successful
232 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL,
233 * invalid ctx or property)
234 * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
235 * @retval #YACA_ERROR_INTERNAL Internal error
237 * @see #yaca_property_e
238 * @see yaca_context_set_property()
241 int yaca_context_get_property(const yaca_context_h ctx,
242 yaca_property_e property,
247 * @brief Destroys the crypto context. Must be called on all contexts that are
248 * no longer used. Passing #YACA_CONTEXT_NULL is allowed.
252 * @param[in,out] ctx Crypto context
254 * @return #YACA_ERROR_NONE on success
255 * @retval #YACA_ERROR_NONE Successful
257 * @see #yaca_context_h
260 int yaca_context_destroy(yaca_context_h ctx);
263 * @brief Returns the output length for a given algorithm. Can only be called
264 * on an initialized context.
268 * @remarks This function can be used to learn the required size of the output buffer
269 * for a single operation (eg. *_update or *_finalize). In case the operation
270 * has no input (eg. *_finalize), the value of @a input_len has to be set to 0.
272 * @param[in] ctx Previously initialized crypto context
273 * @param[in] input_len Length of the input data to be processed
274 * @param[out] output_len Required length of the output
276 * @return #YACA_ERROR_NONE on success, negative on error
277 * @retval #YACA_ERROR_NONE Successful
278 * @retval #YACA_ERROR_INVALID_PARAMETER Required parameters have incorrect values (NULL,
279 * invalid context or too big input_len)
280 * @retval #YACA_ERROR_INTERNAL Internal error
282 int yaca_context_get_output_length(const yaca_context_h ctx,
292 #endif /* YACA_CRYPTO_H */