xref: /isa-l_crypto/include/isal_crypto_api.h (revision 3aa2266bba7e1bce04a3fdb96386b1daf42fb065)
1 /**********************************************************************
2   Copyright(c) 2024 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #ifndef _ISAL_CRYPTO_API_H
31 #define _ISAL_CRYPTO_API_H
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /* Utility macros */
38 #define CONCAT_VERSION_(a, b, c) a##.##b##.##c
39 #define CONCAT_VERSION(a, b, c)  CONCAT_VERSION_(a, b, c)
40 #define TO_STRING_(a)            #a
41 #define TO_STRING(a)             TO_STRING_(a)
42 
43 /* Library version numbers */
44 #define ISAL_CRYPTO_MAJOR_VERSION 2
45 #define ISAL_CRYPTO_MINOR_VERSION 25
46 #define ISAL_CRYPTO_PATCH_VERSION 0
47 
48 #define ISAL_CRYPTO_MAKE_VERSION(maj, min, patch) ((maj) * 0x10000 + (min) * 0x100 + (patch))
49 #define ISAL_CRYPTO_VERSION                                                                        \
50         ISAL_CRYPTO_MAKE_VERSION(ISAL_CRYPTO_MAJOR_VERSION, ISAL_CRYPTO_MINOR_VERSION,             \
51                                  ISAL_CRYPTO_PATCH_VERSION)
52 #define ISAL_CRYPTO_VERSION_STR                                                                    \
53         TO_STRING(CONCAT_VERSION(ISAL_CRYPTO_MAJOR_VERSION, ISAL_CRYPTO_MINOR_VERSION,             \
54                                  ISAL_CRYPTO_PATCH_VERSION))
55 
56 /**
57  * @brief Library error types
58  */
59 typedef enum {
60         ISAL_CRYPTO_ERR_NONE = 0,           //!< No error
61         ISAL_CRYPTO_ERR_NULL_SRC = 2000,    //!< Null source pointer
62         ISAL_CRYPTO_ERR_NULL_DST,           //!< Null destination pointer
63         ISAL_CRYPTO_ERR_NULL_CTX,           //!< Null context pointer
64         ISAL_CRYPTO_ERR_NULL_MGR,           //!< Null manager pointer
65         ISAL_CRYPTO_ERR_NULL_KEY,           //!< Null key pointer
66         ISAL_CRYPTO_ERR_NULL_EXP_KEY,       //!< Null expanded key pointer
67         ISAL_CRYPTO_ERR_NULL_IV,            //!< Null IV pointer
68         ISAL_CRYPTO_ERR_NULL_AUTH,          //!< Null authentication tag pointer
69         ISAL_CRYPTO_ERR_NULL_AAD,           //!< Null AAD pointer
70         ISAL_CRYPTO_ERR_CIPH_LEN,           //!< Invalid cipher length
71         ISAL_CRYPTO_ERR_AUTH_TAG_LEN,       //!< Invalid authentication tag length
72         ISAL_CRYPTO_ERR_INVALID_FLAGS,      //!< Invalid context flags
73         ISAL_CRYPTO_ERR_ALREADY_PROCESSING, //!< Job already processing
74         ISAL_CRYPTO_ERR_ALREADY_COMPLETED,  //!< Job already completed
75         ISAL_CRYPTO_ERR_XTS_NULL_TWEAK,     //!< Null AES-XTS tweak pointer
76         ISAL_CRYPTO_ERR_XTS_SAME_KEYS,      //!< Equal AES-XTS k1 and k2 keys
77         ISAL_CRYPTO_ERR_SELF_TEST,          //!< Self tests
78         ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO,  //!< Non NIST-approved algorithm
79         ISAL_CRYPTO_ERR_WINDOW_SIZE,        //!< Invalid Rolling hash window size
80         ISAL_CRYPTO_ERR_NULL_OFFSET,        //!< Null Rolling hash offset pointer
81         ISAL_CRYPTO_ERR_NULL_MATCH,         //!< Null Rolling hash match pointer
82         ISAL_CRYPTO_ERR_NULL_MASK,          //!< Null Rolling hash mask pointer
83         ISAL_CRYPTO_ERR_NULL_INIT_VAL,      //!< Null Rolling hash initial value pointer
84         ISAL_CRYPTO_ERR_FIPS_DISABLED,      //!< FIPS Mode is not enabled
85         /* add new error types above this comment */
86         ISAL_CRYPTO_ERR_MAX /* don't move this one */
87 } ISAL_CRYPTO_ERROR;
88 
89 /**
90  * @brief Run all crypto self tests
91  *
92  * When FIPS Mode is enabled, all isal_XXX API which performs any crypto processing
93  * on a NIST-approved algorithm (such as isal_aes_cbc_enc_128) will require this function
94  * to be run.
95  *
96  * This API can be run from the application or it will be run internally in the library,
97  * after calling any of the isal_XXX API.
98  *
99  * Either way, once the self tests have passed, all API calls will be able to start
100  * performing the crypto operation. If the self tests fail, no crypto processing will be done.
101  *
102  * This function is thread safe, so only one thread will run the tests and the rest of the threads
103  * will wait until the tests are finished.
104  *
105  * @return  Self test result
106  * @retval  0 on success, ISAL_CRYPTO_ERR_SELF_TEST on failure
107  */
108 int
109 isal_self_tests(void);
110 
111 /**
112  * @brief Get library version in string format
113  *
114  * @return library version string
115  */
116 const char *
117 isal_crypto_get_version_str(void);
118 
119 /**
120  * @brief Get library version in numerical format
121  *
122  * Use ISAL_CRYPTO_MAKE_VERSION macro to compare this
123  * numerical version against known library version.
124  *
125  * @return library version number
126  */
127 unsigned
128 isal_crypto_get_version(void);
129 
130 #ifdef __cplusplus
131 }
132 #endif //__cplusplus
133 #endif // ifndef _ISAL_CRYPTO_API_H
134