OSDN Git Service

Enhanced self-test routine + added macro to "safely" free SlunkCrypt instance.
[slunkcrypt/SlunkCrypt.git] / libslunkcrypt / include / slunkcrypt.h
1 /******************************************************************************/
2 /* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de>                                */
3 /* This work has been released under the CC0 1.0 Universal license!           */
4 /******************************************************************************/
5
6 #ifndef INC_SLUNKCRYPT_H
7 #define INC_SLUNKCRYPT_H
8
9 /*
10  * Compiler check
11  */
12 #if defined(__cplusplus) && (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || (_MSVC_LANG < 201103L))
13 #error This file requires compiler and library support for the ISO C++11 standard.
14 #elif !defined(__cplusplus) && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)) && (!defined(_MSC_VER) || (_MSC_VER < 1600))
15 #error This file requires compiler and library support for the ISO C99 standard.
16 #endif
17
18 /*
19  * Build options
20  */
21 #ifndef SLUNKCRYPT_SHARED
22 #define SLUNKCRYPT_SHARED 0
23 #endif
24 #ifndef SLUNKCRYPT_EXPORT
25 #define SLUNKCRYPT_EXPORT 0
26 #endif
27
28  /*
29   * DLL support
30   */
31 #if defined(_MSC_VER) && SLUNKCRYPT_SHARED
32 #if SLUNKCRYPT_EXPORT
33 #define SLUNKCRYPT_API __declspec(dllexport)
34 #else
35 #define SLUNKCRYPT_API __declspec(dllimport)
36 #endif
37 #else
38 #define SLUNKCRYPT_API
39 #endif
40
41 /*
42  * C++ support
43  */
44 #ifndef __cplusplus
45 #include <stdlib.h>
46 #include <stdint.h>
47 #else
48 #include <cstdlib>
49 #include <cstdint>
50 extern "C" {
51 #endif
52
53 /*
54  * Opaque handle to internal state
55  */
56 typedef uintptr_t slunkcrypt_t;
57 #define SLUNKCRYPT_NULL ((slunkcrypt_t)NULL)
58
59 /*
60  * Mode of operation
61  */
62 static const int SLUNKCRYPT_ENCRYPT = 0;
63 static const int SLUNKCRYPT_DECRYPT = 1;
64
65 /*
66  * Error codes
67  */
68 static const int SLUNKCRYPT_SUCCESS =  0;
69 static const int SLUNKCRYPT_FAILURE = -1;
70 static const int SLUNKCRYPT_ABORTED = -2;
71
72 /*
73  * Limits
74  */
75 static const size_t SLUNKCRYPT_PWDLEN_MIN =   8U;
76 static const size_t SLUNKCRYPT_PWDLEN_MAX = 256U;
77
78 /*
79  * Optional parameters
80  */
81 static const uint16_t SLUNKCRYPT_PARAM_VERSION = 1U;
82 typedef struct
83 {
84         uint16_t version;    /* Must set to SLUNKCRYPT_PARAM_VERSION */
85         size_t thread_count; /* Number of threads, set to 0 for auto-detection */
86 }
87 slunkparam_t;
88
89 /*
90  * Version info
91  */
92 SLUNKCRYPT_API extern const uint16_t SLUNKCRYPT_VERSION_MAJOR;
93 SLUNKCRYPT_API extern const uint16_t SLUNKCRYPT_VERSION_MINOR;
94 SLUNKCRYPT_API extern const uint16_t SLUNKCRYPT_VERSION_PATCH;
95
96 /*
97  * Build date and time
98  */
99 SLUNKCRYPT_API extern const char *const SLUNKCRYPT_BUILD;
100
101 /*
102  * Abort flag
103  */
104 SLUNKCRYPT_API extern volatile int g_slunkcrypt_abort_flag;
105
106 /*
107  * Nonce generator
108  */
109 SLUNKCRYPT_API int slunkcrypt_generate_nonce(uint64_t *const nonce);
110
111 /*
112  * Allocate, reset or free state
113  */
114 SLUNKCRYPT_API slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode);
115 SLUNKCRYPT_API slunkcrypt_t slunkcrypt_alloc_ext(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode, const slunkparam_t *const param);
116 SLUNKCRYPT_API int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode);
117 SLUNKCRYPT_API void slunkcrypt_free(const slunkcrypt_t context);
118
119 /*
120  * Encryption routines
121  */
122 SLUNKCRYPT_API int slunkcrypt_process(const slunkcrypt_t context, const uint8_t *const input, uint8_t *const output, size_t length);
123 SLUNKCRYPT_API int slunkcrypt_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length);
124
125 /*
126  * Auxiliary functions
127  */
128 SLUNKCRYPT_API size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length);
129 SLUNKCRYPT_API void slunkcrypt_bzero(void *const buffer, const size_t length);
130
131 /*
132  * Helper macros
133  */
134 #define SLUNKCRYPT_SAFE_FREE(X) do { if((X) != SLUNKCRYPT_NULL) { slunkcrypt_free((X)); X = SLUNKCRYPT_NULL; } } while(0)
135
136 #ifdef __cplusplus
137 }
138 #endif
139 #endif