| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifndef ARGON2_H |
| 19 |
#define ARGON2_H |
| 20 |
|
| 21 |
#include <stdint.h> |
| 22 |
#include <stddef.h> |
| 23 |
#include <limits.h> |
| 24 |
|
| 25 |
#if defined(__cplusplus) |
| 26 |
extern "C" { |
| 27 |
#endif |
| 28 |
|
| 29 |
|
| 30 |
#ifdef A2_VISCTL |
| 31 |
#define ARGON2_PUBLIC __attribute__((visibility("default"))) |
| 32 |
#define ARGON2_LOCAL __attribute__ ((visibility ("hidden"))) |
| 33 |
#elif _MSC_VER |
| 34 |
#define ARGON2_PUBLIC __declspec(dllexport) |
| 35 |
#define ARGON2_LOCAL |
| 36 |
#else |
| 37 |
#define ARGON2_PUBLIC |
| 38 |
#define ARGON2_LOCAL |
| 39 |
#endif |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#define ARGON2_MIN_LANES UINT32_C(1) |
| 47 |
#define ARGON2_MAX_LANES UINT32_C(0xFFFFFF) |
| 48 |
|
| 49 |
|
| 50 |
#define ARGON2_MIN_THREADS UINT32_C(1) |
| 51 |
#define ARGON2_MAX_THREADS UINT32_C(0xFFFFFF) |
| 52 |
|
| 53 |
|
| 54 |
#define ARGON2_SYNC_POINTS UINT32_C(4) |
| 55 |
|
| 56 |
|
| 57 |
#define ARGON2_MIN_OUTLEN UINT32_C(4) |
| 58 |
#define ARGON2_MAX_OUTLEN UINT32_C(0xFFFFFFFF) |
| 59 |
|
| 60 |
|
| 61 |
#define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS) |
| 62 |
|
| 63 |
#define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 64 |
|
| 65 |
#define ARGON2_MAX_MEMORY_BITS \ |
| 66 |
ARGON2_MIN(UINT32_C(32), (sizeof(void *) * CHAR_BIT - 10 - 1)) |
| 67 |
#define ARGON2_MAX_MEMORY \ |
| 68 |
ARGON2_MIN(UINT32_C(0xFFFFFFFF), UINT64_C(1) << ARGON2_MAX_MEMORY_BITS) |
| 69 |
|
| 70 |
|
| 71 |
#define ARGON2_MIN_TIME UINT32_C(1) |
| 72 |
#define ARGON2_MAX_TIME UINT32_C(0xFFFFFFFF) |
| 73 |
|
| 74 |
|
| 75 |
#define ARGON2_MIN_PWD_LENGTH UINT32_C(0) |
| 76 |
#define ARGON2_MAX_PWD_LENGTH UINT32_C(0xFFFFFFFF) |
| 77 |
|
| 78 |
|
| 79 |
#define ARGON2_MIN_AD_LENGTH UINT32_C(0) |
| 80 |
#define ARGON2_MAX_AD_LENGTH UINT32_C(0xFFFFFFFF) |
| 81 |
|
| 82 |
|
| 83 |
#define ARGON2_MIN_SALT_LENGTH UINT32_C(8) |
| 84 |
#define ARGON2_MAX_SALT_LENGTH UINT32_C(0xFFFFFFFF) |
| 85 |
|
| 86 |
|
| 87 |
#define ARGON2_MIN_SECRET UINT32_C(0) |
| 88 |
#define ARGON2_MAX_SECRET UINT32_C(0xFFFFFFFF) |
| 89 |
|
| 90 |
|
| 91 |
#define ARGON2_DEFAULT_FLAGS UINT32_C(0) |
| 92 |
#define ARGON2_FLAG_CLEAR_PASSWORD (UINT32_C(1) << 0) |
| 93 |
#define ARGON2_FLAG_CLEAR_SECRET (UINT32_C(1) << 1) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
extern int FLAG_clear_internal_memory; |
| 98 |
|
| 99 |
|
| 100 |
typedef enum Argon2_ErrorCodes { |
| 101 |
ARGON2_OK = 0, |
| 102 |
|
| 103 |
ARGON2_OUTPUT_PTR_NULL = -1, |
| 104 |
|
| 105 |
ARGON2_OUTPUT_TOO_SHORT = -2, |
| 106 |
ARGON2_OUTPUT_TOO_LONG = -3, |
| 107 |
|
| 108 |
ARGON2_PWD_TOO_SHORT = -4, |
| 109 |
ARGON2_PWD_TOO_LONG = -5, |
| 110 |
|
| 111 |
ARGON2_SALT_TOO_SHORT = -6, |
| 112 |
ARGON2_SALT_TOO_LONG = -7, |
| 113 |
|
| 114 |
ARGON2_AD_TOO_SHORT = -8, |
| 115 |
ARGON2_AD_TOO_LONG = -9, |
| 116 |
|
| 117 |
ARGON2_SECRET_TOO_SHORT = -10, |
| 118 |
ARGON2_SECRET_TOO_LONG = -11, |
| 119 |
|
| 120 |
ARGON2_TIME_TOO_SMALL = -12, |
| 121 |
ARGON2_TIME_TOO_LARGE = -13, |
| 122 |
|
| 123 |
ARGON2_MEMORY_TOO_LITTLE = -14, |
| 124 |
ARGON2_MEMORY_TOO_MUCH = -15, |
| 125 |
|
| 126 |
ARGON2_LANES_TOO_FEW = -16, |
| 127 |
ARGON2_LANES_TOO_MANY = -17, |
| 128 |
|
| 129 |
ARGON2_PWD_PTR_MISMATCH = -18, |
| 130 |
ARGON2_SALT_PTR_MISMATCH = -19, |
| 131 |
ARGON2_SECRET_PTR_MISMATCH = -20, |
| 132 |
ARGON2_AD_PTR_MISMATCH = -21, |
| 133 |
|
| 134 |
ARGON2_MEMORY_ALLOCATION_ERROR = -22, |
| 135 |
|
| 136 |
ARGON2_FREE_MEMORY_CBK_NULL = -23, |
| 137 |
ARGON2_ALLOCATE_MEMORY_CBK_NULL = -24, |
| 138 |
|
| 139 |
ARGON2_INCORRECT_PARAMETER = -25, |
| 140 |
ARGON2_INCORRECT_TYPE = -26, |
| 141 |
|
| 142 |
ARGON2_OUT_PTR_MISMATCH = -27, |
| 143 |
|
| 144 |
ARGON2_THREADS_TOO_FEW = -28, |
| 145 |
ARGON2_THREADS_TOO_MANY = -29, |
| 146 |
|
| 147 |
ARGON2_MISSING_ARGS = -30, |
| 148 |
|
| 149 |
ARGON2_ENCODING_FAIL = -31, |
| 150 |
|
| 151 |
ARGON2_DECODING_FAIL = -32, |
| 152 |
|
| 153 |
ARGON2_THREAD_FAIL = -33, |
| 154 |
|
| 155 |
ARGON2_DECODING_LENGTH_FAIL = -34, |
| 156 |
|
| 157 |
ARGON2_VERIFY_MISMATCH = -35 |
| 158 |
} argon2_error_codes; |
| 159 |
|
| 160 |
|
| 161 |
typedef int (*allocate_fptr)(uint8_t **memory, size_t bytes_to_allocate); |
| 162 |
typedef void (*deallocate_fptr)(uint8_t *memory, size_t bytes_to_allocate); |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
typedef struct Argon2_Context { |
| 192 |
uint8_t *out; |
| 193 |
uint32_t outlen; |
| 194 |
|
| 195 |
uint8_t *pwd; |
| 196 |
uint32_t pwdlen; |
| 197 |
|
| 198 |
uint8_t *salt; |
| 199 |
uint32_t saltlen; |
| 200 |
|
| 201 |
uint8_t *secret; |
| 202 |
uint32_t secretlen; |
| 203 |
|
| 204 |
uint8_t *ad; |
| 205 |
uint32_t adlen; |
| 206 |
|
| 207 |
uint32_t t_cost; |
| 208 |
uint32_t m_cost; |
| 209 |
uint32_t lanes; |
| 210 |
uint32_t threads; |
| 211 |
|
| 212 |
uint32_t version; |
| 213 |
|
| 214 |
allocate_fptr allocate_cbk; |
| 215 |
deallocate_fptr free_cbk; |
| 216 |
|
| 217 |
uint32_t flags; |
| 218 |
} argon2_context; |
| 219 |
|
| 220 |
|
| 221 |
typedef enum Argon2_type { |
| 222 |
Argon2_d = 0, |
| 223 |
Argon2_i = 1, |
| 224 |
Argon2_id = 2 |
| 225 |
} argon2_type; |
| 226 |
|
| 227 |
|
| 228 |
typedef enum Argon2_version { |
| 229 |
ARGON2_VERSION_10 = 0x10, |
| 230 |
ARGON2_VERSION_13 = 0x13, |
| 231 |
ARGON2_VERSION_NUMBER = ARGON2_VERSION_13 |
| 232 |
} argon2_version; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
ARGON2_PUBLIC const char *argon2_type2string(argon2_type type, int uppercase); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
ARGON2_PUBLIC int argon2_ctx(argon2_context *context, argon2_type type); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
ARGON2_PUBLIC int argon2i_hash_encoded(const uint32_t t_cost, |
| 265 |
const uint32_t m_cost, |
| 266 |
const uint32_t parallelism, |
| 267 |
const void *pwd, const size_t pwdlen, |
| 268 |
const void *salt, const size_t saltlen, |
| 269 |
const size_t hashlen, char *encoded, |
| 270 |
const size_t encodedlen); |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
ARGON2_PUBLIC int argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost, |
| 287 |
const uint32_t parallelism, const void *pwd, |
| 288 |
const size_t pwdlen, const void *salt, |
| 289 |
const size_t saltlen, void *hash, |
| 290 |
const size_t hashlen); |
| 291 |
|
| 292 |
ARGON2_PUBLIC int argon2d_hash_encoded(const uint32_t t_cost, |
| 293 |
const uint32_t m_cost, |
| 294 |
const uint32_t parallelism, |
| 295 |
const void *pwd, const size_t pwdlen, |
| 296 |
const void *salt, const size_t saltlen, |
| 297 |
const size_t hashlen, char *encoded, |
| 298 |
const size_t encodedlen); |
| 299 |
|
| 300 |
ARGON2_PUBLIC int argon2d_hash_raw(const uint32_t t_cost, const uint32_t m_cost, |
| 301 |
const uint32_t parallelism, const void *pwd, |
| 302 |
const size_t pwdlen, const void *salt, |
| 303 |
const size_t saltlen, void *hash, |
| 304 |
const size_t hashlen); |
| 305 |
|
| 306 |
ARGON2_PUBLIC int argon2id_hash_encoded(const uint32_t t_cost, |
| 307 |
const uint32_t m_cost, |
| 308 |
const uint32_t parallelism, |
| 309 |
const void *pwd, const size_t pwdlen, |
| 310 |
const void *salt, const size_t saltlen, |
| 311 |
const size_t hashlen, char *encoded, |
| 312 |
const size_t encodedlen); |
| 313 |
|
| 314 |
ARGON2_PUBLIC int argon2id_hash_raw(const uint32_t t_cost, |
| 315 |
const uint32_t m_cost, |
| 316 |
const uint32_t parallelism, const void *pwd, |
| 317 |
const size_t pwdlen, const void *salt, |
| 318 |
const size_t saltlen, void *hash, |
| 319 |
const size_t hashlen); |
| 320 |
|
| 321 |
|
| 322 |
ARGON2_PUBLIC int argon2_hash(const uint32_t t_cost, const uint32_t m_cost, |
| 323 |
const uint32_t parallelism, const void *pwd, |
| 324 |
const size_t pwdlen, const void *salt, |
| 325 |
const size_t saltlen, void *hash, |
| 326 |
const size_t hashlen, char *encoded, |
| 327 |
const size_t encodedlen, argon2_type type, |
| 328 |
const uint32_t version); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
ARGON2_PUBLIC int argon2i_verify(const char *encoded, const void *pwd, |
| 338 |
const size_t pwdlen); |
| 339 |
|
| 340 |
ARGON2_PUBLIC int argon2d_verify(const char *encoded, const void *pwd, |
| 341 |
const size_t pwdlen); |
| 342 |
|
| 343 |
ARGON2_PUBLIC int argon2id_verify(const char *encoded, const void *pwd, |
| 344 |
const size_t pwdlen); |
| 345 |
|
| 346 |
|
| 347 |
ARGON2_PUBLIC int argon2_verify(const char *encoded, const void *pwd, |
| 348 |
const size_t pwdlen, argon2_type type); |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
ARGON2_PUBLIC int argon2d_ctx(argon2_context *context); |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
ARGON2_PUBLIC int argon2i_ctx(argon2_context *context); |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
ARGON2_PUBLIC int argon2id_ctx(argon2_context *context); |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
ARGON2_PUBLIC int argon2d_verify_ctx(argon2_context *context, const char *hash); |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
ARGON2_PUBLIC int argon2i_verify_ctx(argon2_context *context, const char *hash); |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
ARGON2_PUBLIC int argon2id_verify_ctx(argon2_context *context, |
| 407 |
const char *hash); |
| 408 |
|
| 409 |
|
| 410 |
ARGON2_PUBLIC int argon2_verify_ctx(argon2_context *context, const char *hash, |
| 411 |
argon2_type type); |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
ARGON2_PUBLIC const char *argon2_error_message(int error_code); |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
ARGON2_PUBLIC size_t argon2_encodedlen(uint32_t t_cost, uint32_t m_cost, |
| 430 |
uint32_t parallelism, uint32_t saltlen, |
| 431 |
uint32_t hashlen, argon2_type type); |
| 432 |
|
| 433 |
#if defined(__cplusplus) |
| 434 |
} |
| 435 |
#endif |
| 436 |
|
| 437 |
#endif |