| 1 |
/* |
| 2 |
* Argon2 reference source code package - reference C implementations |
| 3 |
* |
| 4 |
* Copyright 2015 |
| 5 |
* Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves |
| 6 |
* |
| 7 |
* You may use this work under the terms of a Creative Commons CC0 1.0 |
| 8 |
* License/Waiver or the Apache Public License 2.0, at your option. The terms of |
| 9 |
* these licenses can be found at: |
| 10 |
* |
| 11 |
* - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 |
| 12 |
* - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* You should have received a copy of both of these licenses along with this |
| 15 |
* software. If not, they may be obtained at the above URLs. |
| 16 |
*/ |
| 17 |
|
| 18 |
/* |
| 19 |
* modified from original include/argon2.h |
| 20 |
* - remove __declspec(dllexport) |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef ARGON2_H |
| 24 |
#define ARGON2_H |
| 25 |
|
| 26 |
#include <stdint.h> |
| 27 |
#include <stddef.h> |
| 28 |
#include <limits.h> |
| 29 |
|
| 30 |
#if defined(__cplusplus) |
| 31 |
extern "C" { |
| 32 |
#endif |
| 33 |
|
| 34 |
/* Symbols visibility control */ |
| 35 |
#ifdef A2_VISCTL |
| 36 |
#define ARGON2_PUBLIC __attribute__((visibility("default"))) |
| 37 |
#define ARGON2_LOCAL __attribute__ ((visibility ("hidden"))) |
| 38 |
#elif _MSC_VER |
| 39 |
// #define ARGON2_PUBLIC __declspec(dllexport) |
| 40 |
#define ARGON2_PUBLIC |
| 41 |
#define ARGON2_LOCAL |
| 42 |
#else |
| 43 |
#define ARGON2_PUBLIC |
| 44 |
#define ARGON2_LOCAL |
| 45 |
#endif |
| 46 |
|
| 47 |
/* |
| 48 |
* Argon2 input parameter restrictions |
| 49 |
*/ |
| 50 |
|
| 51 |
/* Minimum and maximum number of lanes (degree of parallelism) */ |
| 52 |
#define ARGON2_MIN_LANES UINT32_C(1) |
| 53 |
#define ARGON2_MAX_LANES UINT32_C(0xFFFFFF) |
| 54 |
|
| 55 |
/* Minimum and maximum number of threads */ |
| 56 |
#define ARGON2_MIN_THREADS UINT32_C(1) |
| 57 |
#define ARGON2_MAX_THREADS UINT32_C(0xFFFFFF) |
| 58 |
|
| 59 |
/* Number of synchronization points between lanes per pass */ |
| 60 |
#define ARGON2_SYNC_POINTS UINT32_C(4) |
| 61 |
|
| 62 |
/* Minimum and maximum digest size in bytes */ |
| 63 |
#define ARGON2_MIN_OUTLEN UINT32_C(4) |
| 64 |
#define ARGON2_MAX_OUTLEN UINT32_C(0xFFFFFFFF) |
| 65 |
|
| 66 |
/* Minimum and maximum number of memory blocks (each of BLOCK_SIZE bytes) */ |
| 67 |
#define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS) /* 2 blocks per slice */ |
| 68 |
|
| 69 |
#define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 70 |
/* Max memory size is addressing-space/2, topping at 2^32 blocks (4 TB) */ |
| 71 |
#define ARGON2_MAX_MEMORY_BITS \ |
| 72 |
ARGON2_MIN(UINT32_C(32), (sizeof(void *) * CHAR_BIT - 10 - 1)) |
| 73 |
#define ARGON2_MAX_MEMORY \ |
| 74 |
ARGON2_MIN(UINT32_C(0xFFFFFFFF), UINT64_C(1) << ARGON2_MAX_MEMORY_BITS) |
| 75 |
|
| 76 |
/* Minimum and maximum number of passes */ |
| 77 |
#define ARGON2_MIN_TIME UINT32_C(1) |
| 78 |
#define ARGON2_MAX_TIME UINT32_C(0xFFFFFFFF) |
| 79 |
|
| 80 |
/* Minimum and maximum password length in bytes */ |
| 81 |
#define ARGON2_MIN_PWD_LENGTH UINT32_C(0) |
| 82 |
#define ARGON2_MAX_PWD_LENGTH UINT32_C(0xFFFFFFFF) |
| 83 |
|
| 84 |
/* Minimum and maximum associated data length in bytes */ |
| 85 |
#define ARGON2_MIN_AD_LENGTH UINT32_C(0) |
| 86 |
#define ARGON2_MAX_AD_LENGTH UINT32_C(0xFFFFFFFF) |
| 87 |
|
| 88 |
/* Minimum and maximum salt length in bytes */ |
| 89 |
#define ARGON2_MIN_SALT_LENGTH UINT32_C(8) |
| 90 |
#define ARGON2_MAX_SALT_LENGTH UINT32_C(0xFFFFFFFF) |
| 91 |
|
| 92 |
/* Minimum and maximum key length in bytes */ |
| 93 |
#define ARGON2_MIN_SECRET UINT32_C(0) |
| 94 |
#define ARGON2_MAX_SECRET UINT32_C(0xFFFFFFFF) |
| 95 |
|
| 96 |
/* Flags to determine which fields are securely wiped (default = no wipe). */ |
| 97 |
#define ARGON2_DEFAULT_FLAGS UINT32_C(0) |
| 98 |
#define ARGON2_FLAG_CLEAR_PASSWORD (UINT32_C(1) << 0) |
| 99 |
#define ARGON2_FLAG_CLEAR_SECRET (UINT32_C(1) << 1) |
| 100 |
|
| 101 |
/* Global flag to determine if we are wiping internal memory buffers. This flag |
| 102 |
* is defined in core.c and defaults to 1 (wipe internal memory). */ |
| 103 |
extern int FLAG_clear_internal_memory; |
| 104 |
|
| 105 |
/* Error codes */ |
| 106 |
typedef enum Argon2_ErrorCodes { |
| 107 |
ARGON2_OK = 0, |
| 108 |
|
| 109 |
ARGON2_OUTPUT_PTR_NULL = -1, |
| 110 |
|
| 111 |
ARGON2_OUTPUT_TOO_SHORT = -2, |
| 112 |
ARGON2_OUTPUT_TOO_LONG = -3, |
| 113 |
|
| 114 |
ARGON2_PWD_TOO_SHORT = -4, |
| 115 |
ARGON2_PWD_TOO_LONG = -5, |
| 116 |
|
| 117 |
ARGON2_SALT_TOO_SHORT = -6, |
| 118 |
ARGON2_SALT_TOO_LONG = -7, |
| 119 |
|
| 120 |
ARGON2_AD_TOO_SHORT = -8, |
| 121 |
ARGON2_AD_TOO_LONG = -9, |
| 122 |
|
| 123 |
ARGON2_SECRET_TOO_SHORT = -10, |
| 124 |
ARGON2_SECRET_TOO_LONG = -11, |
| 125 |
|
| 126 |
ARGON2_TIME_TOO_SMALL = -12, |
| 127 |
ARGON2_TIME_TOO_LARGE = -13, |
| 128 |
|
| 129 |
ARGON2_MEMORY_TOO_LITTLE = -14, |
| 130 |
ARGON2_MEMORY_TOO_MUCH = -15, |
| 131 |
|
| 132 |
ARGON2_LANES_TOO_FEW = -16, |
| 133 |
ARGON2_LANES_TOO_MANY = -17, |
| 134 |
|
| 135 |
ARGON2_PWD_PTR_MISMATCH = -18, /* NULL ptr with non-zero length */ |
| 136 |
ARGON2_SALT_PTR_MISMATCH = -19, /* NULL ptr with non-zero length */ |
| 137 |
ARGON2_SECRET_PTR_MISMATCH = -20, /* NULL ptr with non-zero length */ |
| 138 |
ARGON2_AD_PTR_MISMATCH = -21, /* NULL ptr with non-zero length */ |
| 139 |
|
| 140 |
ARGON2_MEMORY_ALLOCATION_ERROR = -22, |
| 141 |
|
| 142 |
ARGON2_FREE_MEMORY_CBK_NULL = -23, |
| 143 |
ARGON2_ALLOCATE_MEMORY_CBK_NULL = -24, |
| 144 |
|
| 145 |
ARGON2_INCORRECT_PARAMETER = -25, |
| 146 |
ARGON2_INCORRECT_TYPE = -26, |
| 147 |
|
| 148 |
ARGON2_OUT_PTR_MISMATCH = -27, |
| 149 |
|
| 150 |
ARGON2_THREADS_TOO_FEW = -28, |
| 151 |
ARGON2_THREADS_TOO_MANY = -29, |
| 152 |
|
| 153 |
ARGON2_MISSING_ARGS = -30, |
| 154 |
|
| 155 |
ARGON2_ENCODING_FAIL = -31, |
| 156 |
|
| 157 |
ARGON2_DECODING_FAIL = -32, |
| 158 |
|
| 159 |
ARGON2_THREAD_FAIL = -33, |
| 160 |
|
| 161 |
ARGON2_DECODING_LENGTH_FAIL = -34, |
| 162 |
|
| 163 |
ARGON2_VERIFY_MISMATCH = -35 |
| 164 |
} argon2_error_codes; |
| 165 |
|
| 166 |
/* Memory allocator types --- for external allocation */ |
| 167 |
typedef int (*allocate_fptr)(uint8_t **memory, size_t bytes_to_allocate); |
| 168 |
typedef void (*deallocate_fptr)(uint8_t *memory, size_t bytes_to_allocate); |
| 169 |
|
| 170 |
/* Argon2 external data structures */ |
| 171 |
|
| 172 |
/* |
| 173 |
***** |
| 174 |
* Context: structure to hold Argon2 inputs: |
| 175 |
* output array and its length, |
| 176 |
* password and its length, |
| 177 |
* salt and its length, |
| 178 |
* secret and its length, |
| 179 |
* associated data and its length, |
| 180 |
* number of passes, amount of used memory (in KBytes, can be rounded up a bit) |
| 181 |
* number of parallel threads that will be run. |
| 182 |
* All the parameters above affect the output hash value. |
| 183 |
* Additionally, two function pointers can be provided to allocate and |
| 184 |
* deallocate the memory (if NULL, memory will be allocated internally). |
| 185 |
* Also, three flags indicate whether to erase password, secret as soon as they |
| 186 |
* are pre-hashed (and thus not needed anymore), and the entire memory |
| 187 |
***** |
| 188 |
* Simplest situation: you have output array out[8], password is stored in |
| 189 |
* pwd[32], salt is stored in salt[16], you do not have keys nor associated |
| 190 |
* data. You need to spend 1 GB of RAM and you run 5 passes of Argon2d with |
| 191 |
* 4 parallel lanes. |
| 192 |
* You want to erase the password, but you're OK with last pass not being |
| 193 |
* erased. You want to use the default memory allocator. |
| 194 |
* Then you initialize: |
| 195 |
Argon2_Context(out,8,pwd,32,salt,16,NULL,0,NULL,0,5,1<<20,4,4,NULL,NULL,true,false,false,false) |
| 196 |
*/ |
| 197 |
typedef struct Argon2_Context { |
| 198 |
uint8_t *out; /* output array */ |
| 199 |
uint32_t outlen; /* digest length */ |
| 200 |
|
| 201 |
uint8_t *pwd; /* password array */ |
| 202 |
uint32_t pwdlen; /* password length */ |
| 203 |
|
| 204 |
uint8_t *salt; /* salt array */ |
| 205 |
uint32_t saltlen; /* salt length */ |
| 206 |
|
| 207 |
uint8_t *secret; /* key array */ |
| 208 |
uint32_t secretlen; /* key length */ |
| 209 |
|
| 210 |
uint8_t *ad; /* associated data array */ |
| 211 |
uint32_t adlen; /* associated data length */ |
| 212 |
|
| 213 |
uint32_t t_cost; /* number of passes */ |
| 214 |
uint32_t m_cost; /* amount of memory requested (KB) */ |
| 215 |
uint32_t lanes; /* number of lanes */ |
| 216 |
uint32_t threads; /* maximum number of threads */ |
| 217 |
|
| 218 |
uint32_t version; /* version number */ |
| 219 |
|
| 220 |
allocate_fptr allocate_cbk; /* pointer to memory allocator */ |
| 221 |
deallocate_fptr free_cbk; /* pointer to memory deallocator */ |
| 222 |
|
| 223 |
uint32_t flags; /* array of bool options */ |
| 224 |
} argon2_context; |
| 225 |
|
| 226 |
/* Argon2 primitive type */ |
| 227 |
typedef enum Argon2_type { |
| 228 |
Argon2_d = 0, |
| 229 |
Argon2_i = 1, |
| 230 |
Argon2_id = 2 |
| 231 |
} argon2_type; |
| 232 |
|
| 233 |
/* Version of the algorithm */ |
| 234 |
typedef enum Argon2_version { |
| 235 |
ARGON2_VERSION_10 = 0x10, |
| 236 |
ARGON2_VERSION_13 = 0x13, |
| 237 |
ARGON2_VERSION_NUMBER = ARGON2_VERSION_13 |
| 238 |
} argon2_version; |
| 239 |
|
| 240 |
/* |
| 241 |
* Function that gives the string representation of an argon2_type. |
| 242 |
* @param type The argon2_type that we want the string for |
| 243 |
* @param uppercase Whether the string should have the first letter uppercase |
| 244 |
* @return NULL if invalid type, otherwise the string representation. |
| 245 |
*/ |
| 246 |
ARGON2_PUBLIC const char *argon2_type2string(argon2_type type, int uppercase); |
| 247 |
|
| 248 |
/* |
| 249 |
* Function that performs memory-hard hashing with certain degree of parallelism |
| 250 |
* @param context Pointer to the Argon2 internal structure |
| 251 |
* @return Error code if smth is wrong, ARGON2_OK otherwise |
| 252 |
*/ |
| 253 |
ARGON2_PUBLIC int argon2_ctx(argon2_context *context, argon2_type type); |
| 254 |
|
| 255 |
/** |
| 256 |
* Hashes a password with Argon2i, producing an encoded hash |
| 257 |
* @param t_cost Number of iterations |
| 258 |
* @param m_cost Sets memory usage to m_cost kibibytes |
| 259 |
* @param parallelism Number of threads and compute lanes |
| 260 |
* @param pwd Pointer to password |
| 261 |
* @param pwdlen Password size in bytes |
| 262 |
* @param salt Pointer to salt |
| 263 |
* @param saltlen Salt size in bytes |
| 264 |
* @param hashlen Desired length of the hash in bytes |
| 265 |
* @param encoded Buffer where to write the encoded hash |
| 266 |
* @param encodedlen Size of the buffer (thus max size of the encoded hash) |
| 267 |
* @pre Different parallelism levels will give different results |
| 268 |
* @pre Returns ARGON2_OK if successful |
| 269 |
*/ |
| 270 |
ARGON2_PUBLIC int argon2i_hash_encoded(const uint32_t t_cost, |
| 271 |
const uint32_t m_cost, |
| 272 |
const uint32_t parallelism, |
| 273 |
const void *pwd, const size_t pwdlen, |
| 274 |
const void *salt, const size_t saltlen, |
| 275 |
const size_t hashlen, char *encoded, |
| 276 |
const size_t encodedlen); |
| 277 |
|
| 278 |
/** |
| 279 |
* Hashes a password with Argon2i, producing a raw hash at @hash |
| 280 |
* @param t_cost Number of iterations |
| 281 |
* @param m_cost Sets memory usage to m_cost kibibytes |
| 282 |
* @param parallelism Number of threads and compute lanes |
| 283 |
* @param pwd Pointer to password |
| 284 |
* @param pwdlen Password size in bytes |
| 285 |
* @param salt Pointer to salt |
| 286 |
* @param saltlen Salt size in bytes |
| 287 |
* @param hash Buffer where to write the raw hash - updated by the function |
| 288 |
* @param hashlen Desired length of the hash in bytes |
| 289 |
* @pre Different parallelism levels will give different results |
| 290 |
* @pre Returns ARGON2_OK if successful |
| 291 |
*/ |
| 292 |
ARGON2_PUBLIC int argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost, |
| 293 |
const uint32_t parallelism, const void *pwd, |
| 294 |
const size_t pwdlen, const void *salt, |
| 295 |
const size_t saltlen, void *hash, |
| 296 |
const size_t hashlen); |
| 297 |
|
| 298 |
ARGON2_PUBLIC int argon2d_hash_encoded(const uint32_t t_cost, |
| 299 |
const uint32_t m_cost, |
| 300 |
const uint32_t parallelism, |
| 301 |
const void *pwd, const size_t pwdlen, |
| 302 |
const void *salt, const size_t saltlen, |
| 303 |
const size_t hashlen, char *encoded, |
| 304 |
const size_t encodedlen); |
| 305 |
|
| 306 |
ARGON2_PUBLIC int argon2d_hash_raw(const uint32_t t_cost, const uint32_t m_cost, |
| 307 |
const uint32_t parallelism, const void *pwd, |
| 308 |
const size_t pwdlen, const void *salt, |
| 309 |
const size_t saltlen, void *hash, |
| 310 |
const size_t hashlen); |
| 311 |
|
| 312 |
ARGON2_PUBLIC int argon2id_hash_encoded(const uint32_t t_cost, |
| 313 |
const uint32_t m_cost, |
| 314 |
const uint32_t parallelism, |
| 315 |
const void *pwd, const size_t pwdlen, |
| 316 |
const void *salt, const size_t saltlen, |
| 317 |
const size_t hashlen, char *encoded, |
| 318 |
const size_t encodedlen); |
| 319 |
|
| 320 |
ARGON2_PUBLIC int argon2id_hash_raw(const uint32_t t_cost, |
| 321 |
const uint32_t m_cost, |
| 322 |
const uint32_t parallelism, const void *pwd, |
| 323 |
const size_t pwdlen, const void *salt, |
| 324 |
const size_t saltlen, void *hash, |
| 325 |
const size_t hashlen); |
| 326 |
|
| 327 |
/* generic function underlying the above ones */ |
| 328 |
ARGON2_PUBLIC int argon2_hash(const uint32_t t_cost, const uint32_t m_cost, |
| 329 |
const uint32_t parallelism, const void *pwd, |
| 330 |
const size_t pwdlen, const void *salt, |
| 331 |
const size_t saltlen, void *hash, |
| 332 |
const size_t hashlen, char *encoded, |
| 333 |
const size_t encodedlen, argon2_type type, |
| 334 |
const uint32_t version); |
| 335 |
|
| 336 |
/** |
| 337 |
* Verifies a password against an encoded string |
| 338 |
* Encoded string is restricted as in validate_inputs() |
| 339 |
* @param encoded String encoding parameters, salt, hash |
| 340 |
* @param pwd Pointer to password |
| 341 |
* @pre Returns ARGON2_OK if successful |
| 342 |
*/ |
| 343 |
ARGON2_PUBLIC int argon2i_verify(const char *encoded, const void *pwd, |
| 344 |
const size_t pwdlen); |
| 345 |
|
| 346 |
ARGON2_PUBLIC int argon2d_verify(const char *encoded, const void *pwd, |
| 347 |
const size_t pwdlen); |
| 348 |
|
| 349 |
ARGON2_PUBLIC int argon2id_verify(const char *encoded, const void *pwd, |
| 350 |
const size_t pwdlen); |
| 351 |
|
| 352 |
/* generic function underlying the above ones */ |
| 353 |
ARGON2_PUBLIC int argon2_verify(const char *encoded, const void *pwd, |
| 354 |
const size_t pwdlen, argon2_type type); |
| 355 |
|
| 356 |
/** |
| 357 |
* Argon2d: Version of Argon2 that picks memory blocks depending |
| 358 |
* on the password and salt. Only for side-channel-free |
| 359 |
* environment!! |
| 360 |
***** |
| 361 |
* @param context Pointer to current Argon2 context |
| 362 |
* @return Zero if successful, a non zero error code otherwise |
| 363 |
*/ |
| 364 |
ARGON2_PUBLIC int argon2d_ctx(argon2_context *context); |
| 365 |
|
| 366 |
/** |
| 367 |
* Argon2i: Version of Argon2 that picks memory blocks |
| 368 |
* independent on the password and salt. Good for side-channels, |
| 369 |
* but worse w.r.t. tradeoff attacks if only one pass is used. |
| 370 |
***** |
| 371 |
* @param context Pointer to current Argon2 context |
| 372 |
* @return Zero if successful, a non zero error code otherwise |
| 373 |
*/ |
| 374 |
ARGON2_PUBLIC int argon2i_ctx(argon2_context *context); |
| 375 |
|
| 376 |
/** |
| 377 |
* Argon2id: Version of Argon2 where the first half-pass over memory is |
| 378 |
* password-independent, the rest are password-dependent (on the password and |
| 379 |
* salt). OK against side channels (they reduce to 1/2-pass Argon2i), and |
| 380 |
* better with w.r.t. tradeoff attacks (similar to Argon2d). |
| 381 |
***** |
| 382 |
* @param context Pointer to current Argon2 context |
| 383 |
* @return Zero if successful, a non zero error code otherwise |
| 384 |
*/ |
| 385 |
ARGON2_PUBLIC int argon2id_ctx(argon2_context *context); |
| 386 |
|
| 387 |
/** |
| 388 |
* Verify if a given password is correct for Argon2d hashing |
| 389 |
* @param context Pointer to current Argon2 context |
| 390 |
* @param hash The password hash to verify. The length of the hash is |
| 391 |
* specified by the context outlen member |
| 392 |
* @return Zero if successful, a non zero error code otherwise |
| 393 |
*/ |
| 394 |
ARGON2_PUBLIC int argon2d_verify_ctx(argon2_context *context, const char *hash); |
| 395 |
|
| 396 |
/** |
| 397 |
* Verify if a given password is correct for Argon2i hashing |
| 398 |
* @param context Pointer to current Argon2 context |
| 399 |
* @param hash The password hash to verify. The length of the hash is |
| 400 |
* specified by the context outlen member |
| 401 |
* @return Zero if successful, a non zero error code otherwise |
| 402 |
*/ |
| 403 |
ARGON2_PUBLIC int argon2i_verify_ctx(argon2_context *context, const char *hash); |
| 404 |
|
| 405 |
/** |
| 406 |
* Verify if a given password is correct for Argon2id hashing |
| 407 |
* @param context Pointer to current Argon2 context |
| 408 |
* @param hash The password hash to verify. The length of the hash is |
| 409 |
* specified by the context outlen member |
| 410 |
* @return Zero if successful, a non zero error code otherwise |
| 411 |
*/ |
| 412 |
ARGON2_PUBLIC int argon2id_verify_ctx(argon2_context *context, |
| 413 |
const char *hash); |
| 414 |
|
| 415 |
/* generic function underlying the above ones */ |
| 416 |
ARGON2_PUBLIC int argon2_verify_ctx(argon2_context *context, const char *hash, |
| 417 |
argon2_type type); |
| 418 |
|
| 419 |
/** |
| 420 |
* Get the associated error message for given error code |
| 421 |
* @return The error message associated with the given error code |
| 422 |
*/ |
| 423 |
ARGON2_PUBLIC const char *argon2_error_message(int error_code); |
| 424 |
|
| 425 |
/** |
| 426 |
* Returns the encoded hash length for the given input parameters |
| 427 |
* @param t_cost Number of iterations |
| 428 |
* @param m_cost Memory usage in kibibytes |
| 429 |
* @param parallelism Number of threads; used to compute lanes |
| 430 |
* @param saltlen Salt size in bytes |
| 431 |
* @param hashlen Hash size in bytes |
| 432 |
* @param type The argon2_type that we want the encoded length for |
| 433 |
* @return The encoded hash length in bytes |
| 434 |
*/ |
| 435 |
ARGON2_PUBLIC size_t argon2_encodedlen(uint32_t t_cost, uint32_t m_cost, |
| 436 |
uint32_t parallelism, uint32_t saltlen, |
| 437 |
uint32_t hashlen, argon2_type type); |
| 438 |
|
| 439 |
#if defined(__cplusplus) |
| 440 |
} |
| 441 |
#endif |
| 442 |
|
| 443 |
#endif |