| 1 | # Argon2 |
| 2 | |
| 3 | [](https://travis-ci.org/P-H-C/phc-winner-argon2) |
| 4 | [](https://ci.appveyor.com/project/P-H-C/phc-winner-argon2) |
| 5 | [](https://codecov.io/github/P-H-C/phc-winner-argon2?branch=master) |
| 6 | |
| 7 | This is the reference C implementation of Argon2, the password-hashing |
| 8 | function that won the [Password Hashing Competition |
| 9 | (PHC)](https://password-hashing.net). |
| 10 | |
| 11 | Argon2 is a password-hashing function that summarizes the state of the |
| 12 | art in the design of memory-hard functions and can be used to hash |
| 13 | passwords for credential storage, key derivation, or other applications. |
| 14 | |
| 15 | It has a simple design aimed at the highest memory filling rate and |
| 16 | effective use of multiple computing units, while still providing defense |
| 17 | against tradeoff attacks (by exploiting the cache and memory organization |
| 18 | of the recent processors). |
| 19 | |
| 20 | Argon2 has three variants: Argon2i, Argon2d, and Argon2id. Argon2d is faster |
| 21 | and uses data-depending memory access, which makes it highly resistant |
| 22 | against GPU cracking attacks and suitable for applications with no threats |
| 23 | from side-channel timing attacks (eg. cryptocurrencies). Argon2i instead |
| 24 | uses data-independent memory access, which is preferred for password |
| 25 | hashing and password-based key derivation, but it is slower as it makes |
| 26 | more passes over the memory to protect from tradeoff attacks. Argon2id is a |
| 27 | hybrid of Argon2i and Argon2d, using a combination of data-depending and |
| 28 | data-independent memory accesses, which gives some of Argon2i's resistance to |
| 29 | side-channel cache timing attacks and much of Argon2d's resistance to GPU |
| 30 | cracking attacks. |
| 31 | |
| 32 | Argon2i, Argon2d, and Argon2id are parametrized by: |
| 33 | |
| 34 | * A **time** cost, which defines the amount of computation realized and |
| 35 | therefore the execution time, given in number of iterations |
| 36 | * A **memory** cost, which defines the memory usage, given in kibibytes |
| 37 | * A **parallelism** degree, which defines the number of parallel threads |
| 38 | |
| 39 | The [Argon2 document](argon2-specs.pdf) gives detailed specs and design |
| 40 | rationale. |
| 41 | |
| 42 | Please report bugs as issues on this repository. |
| 43 | |
| 44 | ## Usage |
| 45 | |
| 46 | `make` builds the executable `argon2`, the static library `libargon2.a`, |
| 47 | and the shared library `libargon2.so` (or `libargon2.dylib` on OSX). |
| 48 | Make sure to run `make test` to verify that your build produces valid |
| 49 | results. `make install PREFIX=/usr` installs it to your system. |
| 50 | |
| 51 | ### Command-line utility |
| 52 | |
| 53 | `argon2` is a command-line utility to test specific Argon2 instances |
| 54 | on your system. To show usage instructions, run |
| 55 | `./argon2 -h` as |
| 56 | ``` |
| 57 | Usage: ./argon2 [-h] salt [-i|-d|-id] [-t iterations] [-m memory] [-p parallelism] [-l hash length] [-e|-r] [-v (10|13)] |
| 58 | Password is read from stdin |
| 59 | Parameters: |
| 60 | salt The salt to use, at least 8 characters |
| 61 | -i Use Argon2i (this is the default) |
| 62 | -d Use Argon2d instead of Argon2i |
| 63 | -id Use Argon2id instead of Argon2i |
| 64 | -t N Sets the number of iterations to N (default = 3) |
| 65 | -m N Sets the memory usage of 2^N KiB (default 12) |
| 66 | -p N Sets parallelism to N threads (default 1) |
| 67 | -l N Sets hash output length to N bytes (default 32) |
| 68 | -e Output only encoded hash |
| 69 | -r Output only the raw bytes of the hash |
| 70 | -v (10|13) Argon2 version (defaults to the most recent version, currently 13) |
| 71 | -h Print argon2 usage |
| 72 | ``` |
| 73 | For example, to hash "password" using "somesalt" as a salt and doing 2 |
| 74 | iterations, consuming 64 MiB, using four parallel threads and an output hash |
| 75 | of 24 bytes |
| 76 | ``` |
| 77 | $ echo -n "password" | ./argon2 somesalt -t 2 -m 16 -p 4 -l 24 |
| 78 | Type: Argon2i |
| 79 | Iterations: 2 |
| 80 | Memory: 65536 KiB |
| 81 | Parallelism: 4 |
| 82 | Hash: 45d7ac72e76f242b20b77b9bf9bf9d5915894e669a24e6c6 |
| 83 | Encoded: $argon2i$v=19$m=65536,t=2,p=4$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG |
| 84 | 0.188 seconds |
| 85 | Verification ok |
| 86 | ``` |
| 87 | |
| 88 | ### Library |
| 89 | |
| 90 | `libargon2` provides an API to both low-level and high-level functions |
| 91 | for using Argon2. |
| 92 | |
| 93 | The example program below hashes the string "password" with Argon2i |
| 94 | using the high-level API and then using the low-level API. While the |
| 95 | high-level API takes the three cost parameters (time, memory, and |
| 96 | parallelism), the password input buffer, the salt input buffer, and the |
| 97 | output buffers, the low-level API takes in these and additional parameters |
| 98 | , as defined in [`include/argon2.h`](include/argon2.h). |
| 99 | |
| 100 | There are many additional parameters, but we will highlight three of them here. |
| 101 | |
| 102 | 1. The `secret` parameter, which is used for [keyed hashing]( |
| 103 | https://en.wikipedia.org/wiki/Hash-based_message_authentication_code). |
| 104 | This allows a secret key to be input at hashing time (from some external |
| 105 | location) and be folded into the value of the hash. This means that even if |
| 106 | your salts and hashes are compromized, an attacker cannot brute-force to find |
| 107 | the password without the key. |
| 108 | |
| 109 | 2. The `ad` parameter, which is used to fold any additional data into the hash |
| 110 | value. Functionally, this behaves almost exactly like the `secret` or `salt` |
| 111 | parameters; the `ad` parameter is folding into the value of the hash. |
| 112 | However, this parameter is used for different data. The `salt` should be a |
| 113 | random string stored alongside your password. The `secret` should be a random |
| 114 | key only usable at hashing time. The `ad` is for any other data. |
| 115 | |
| 116 | 3. The `flags` parameter, which determines which memory should be securely |
| 117 | erased. This is useful if you want to securly delete the `pwd` or `secret` |
| 118 | fields right after they are used. To do this set `flags` to either |
| 119 | `ARGON2_FLAG_CLEAR_PASSWORD` or `ARGON2_FLAG_CLEAR_SECRET`. To change how |
| 120 | internal memory is cleared, change the global flag |
| 121 | `FLAG_clear_internal_memory` (defaults to clearing internal memory). |
| 122 | |
| 123 | Here the time cost `t_cost` is set to 2 iterations, the |
| 124 | memory cost `m_cost` is set to 2<sup>16</sup> kibibytes (64 mebibytes), |
| 125 | and parallelism is set to 1 (single-thread). |
| 126 | |
| 127 | Compile for example as `gcc test.c libargon2.a -Isrc -o test`, if the program |
| 128 | below is named `test.c` and placed in the project's root directory. |
| 129 | |
| 130 | ```c |
| 131 | #include "argon2.h" |
| 132 | #include <stdio.h> |
| 133 | #include <string.h> |
| 134 | #include <stdlib.h> |
| 135 | |
| 136 | #define HASHLEN 32 |
| 137 | #define SALTLEN 16 |
| 138 | #define PWD "password" |
| 139 | |
| 140 | int main(void) |
| 141 | { |
| 142 | uint8_t hash1[HASHLEN]; |
| 143 | uint8_t hash2[HASHLEN]; |
| 144 | |
| 145 | uint8_t salt[SALTLEN]; |
| 146 | memset( salt, 0x00, SALTLEN ); |
| 147 | |
| 148 | uint8_t *pwd = (uint8_t *)strdup(PWD); |
| 149 | uint32_t pwdlen = strlen((char *)pwd); |
| 150 | |
| 151 | uint32_t t_cost = 2; // 1-pass computation |
| 152 | uint32_t m_cost = (1<<16); // 64 mebibytes memory usage |
| 153 | uint32_t parallelism = 1; // number of threads and lanes |
| 154 | |
| 155 | // high-level API |
| 156 | argon2i_hash_raw(t_cost, m_cost, parallelism, pwd, pwdlen, salt, SALTLEN, hash1, HASHLEN); |
| 157 | |
| 158 | // low-level API |
| 159 | argon2_context context = { |
| 160 | hash2, /* output array, at least HASHLEN in size */ |
| 161 | HASHLEN, /* digest length */ |
| 162 | pwd, /* password array */ |
| 163 | pwdlen, /* password length */ |
| 164 | salt, /* salt array */ |
| 165 | SALTLEN, /* salt length */ |
| 166 | NULL, 0, /* optional secret data */ |
| 167 | NULL, 0, /* optional associated data */ |
| 168 | t_cost, m_cost, parallelism, parallelism, |
| 169 | ARGON2_VERSION_13, /* algorithm version */ |
| 170 | NULL, NULL, /* custom memory allocation / deallocation functions */ |
| 171 | /* by default only internal memory is cleared (pwd is not wiped) */ |
| 172 | ARGON2_DEFAULT_FLAGS |
| 173 | }; |
| 174 | |
| 175 | int rc = argon2i_ctx( &context ); |
| 176 | if(ARGON2_OK != rc) { |
| 177 | printf("Error: %s\n", argon2_error_message(rc)); |
| 178 | exit(1); |
| 179 | } |
| 180 | free(pwd); |
| 181 | |
| 182 | for( int i=0; i<HASHLEN; ++i ) printf( "%02x", hash1[i] ); printf( "\n" ); |
| 183 | if (memcmp(hash1, hash2, HASHLEN)) { |
| 184 | for( int i=0; i<HASHLEN; ++i ) { |
| 185 | printf( "%02x", hash2[i] ); |
| 186 | } |
| 187 | printf("\nfail\n"); |
| 188 | } |
| 189 | else printf("ok\n"); |
| 190 | return 0; |
| 191 | } |
| 192 | ``` |
| 193 | |
| 194 | To use Argon2d instead of Argon2i call `argon2d_hash_raw` instead of |
| 195 | `argon2i_hash_raw` using the high-level API, and `argon2d` instead of |
| 196 | `argon2i` using the low-level API. Similarly for Argon2id, call `argon2id_hash_raw` |
| 197 | and `argon2id`. |
| 198 | |
| 199 | To produce the crypt-like encoding rather than the raw hash, call |
| 200 | `argon2i_hash_encoded` for Argon2i, `argon2d_hash_encoded` for Argon2d, and |
| 201 | `argon2id_hash_encoded` for Argon2id |
| 202 | |
| 203 | See [`include/argon2.h`](include/argon2.h) for API details. |
| 204 | |
| 205 | *Note: in this example the salt is set to the all-`0x00` string for the |
| 206 | sake of simplicity, but in your application you should use a random salt.* |
| 207 | |
| 208 | |
| 209 | ### Benchmarks |
| 210 | |
| 211 | `make bench` creates the executable `bench`, which measures the execution |
| 212 | time of various Argon2 instances: |
| 213 | |
| 214 | ``` |
| 215 | $ ./bench |
| 216 | Argon2d 1 iterations 1 MiB 1 threads: 5.91 cpb 5.91 Mcycles |
| 217 | Argon2i 1 iterations 1 MiB 1 threads: 4.64 cpb 4.64 Mcycles |
| 218 | 0.0041 seconds |
| 219 | |
| 220 | Argon2d 1 iterations 1 MiB 2 threads: 2.76 cpb 2.76 Mcycles |
| 221 | Argon2i 1 iterations 1 MiB 2 threads: 2.87 cpb 2.87 Mcycles |
| 222 | 0.0038 seconds |
| 223 | |
| 224 | Argon2d 1 iterations 1 MiB 4 threads: 3.25 cpb 3.25 Mcycles |
| 225 | Argon2i 1 iterations 1 MiB 4 threads: 3.57 cpb 3.57 Mcycles |
| 226 | 0.0048 seconds |
| 227 | |
| 228 | (...) |
| 229 | |
| 230 | Argon2d 1 iterations 4096 MiB 2 threads: 2.15 cpb 8788.08 Mcycles |
| 231 | Argon2i 1 iterations 4096 MiB 2 threads: 2.15 cpb 8821.59 Mcycles |
| 232 | 13.0112 seconds |
| 233 | |
| 234 | Argon2d 1 iterations 4096 MiB 4 threads: 1.79 cpb 7343.72 Mcycles |
| 235 | Argon2i 1 iterations 4096 MiB 4 threads: 2.72 cpb 11124.86 Mcycles |
| 236 | 19.3974 seconds |
| 237 | |
| 238 | (...) |
| 239 | ``` |
| 240 | |
| 241 | ## Bindings |
| 242 | |
| 243 | Bindings are available for the following languages (make sure to read |
| 244 | their documentation): |
| 245 | |
| 246 | * [Elixir](https://github.com/riverrun/argon2_elixir) by [@riverrun](https://github.com/riverrun) |
| 247 | * [Erlang](https://github.com/ergenius/eargon2) by [@ergenius](https://github.com/ergenius) |
| 248 | * [Go](https://github.com/tvdburgt/go-argon2) by [@tvdburgt](https://github.com/tvdburgt) |
| 249 | * [Haskell](https://hackage.haskell.org/package/argon2) by [@hvr](https://github.com/hvr) |
| 250 | * [JavaScript (native)](https://github.com/ranisalt/node-argon2), by [@ranisalt](https://github.com/ranisalt) |
| 251 | * [JavaScript (native)](https://github.com/jdconley/argon2themax), by [@jdconley](https://github.com/jdconley) |
| 252 | * [JavaScript (ffi)](https://github.com/cjlarose/argon2-ffi), by [@cjlarose](https://github.com/cjlarose) |
| 253 | * [JavaScript (browser)](https://github.com/antelle/argon2-browser), by [@antelle](https://github.com/antelle) |
| 254 | * [JVM](https://github.com/phxql/argon2-jvm) by [@phXql](https://github.com/phxql) |
| 255 | * [JVM (with keyed hashing)](https://github.com/kosprov/jargon2-api) by [@kosprov](https://github.com/kosprov) |
| 256 | * [Lua (native)](https://github.com/thibaultCha/lua-argon2) by [@thibaultCha](https://github.com/thibaultCha) |
| 257 | * [Lua (ffi)](https://github.com/thibaultCha/lua-argon2-ffi) by [@thibaultCha](https://github.com/thibaultCha) |
| 258 | * [OCaml](https://github.com/Khady/ocaml-argon2) by [@Khady](https://github.com/Khady) |
| 259 | * [Python (native)](https://pypi.python.org/pypi/argon2), by [@flamewow](https://github.com/flamewow) |
| 260 | * [Python (ffi)](https://pypi.python.org/pypi/argon2_cffi), by [@hynek](https://github.com/hynek) |
| 261 | * [Python (ffi, with keyed hashing)](https://github.com/thusoy/porridge), by [@thusoy](https://github.com/thusoy) |
| 262 | * [R](https://cran.r-project.org/package=argon2) by [@wrathematics](https://github.com/wrathematics) |
| 263 | * [Ruby](https://github.com/technion/ruby-argon2) by [@technion](https://github.com/technion) |
| 264 | * [Rust](https://github.com/quininer/argon2-rs) by [@quininer](https://github.com/quininer) |
| 265 | * [Rust](https://docs.rs/argonautica/) by [@bcmyers](https://github.com/bcmyers/) |
| 266 | * [C#/.NET CoreCLR](https://github.com/kmaragon/Konscious.Security.Cryptography) by [@kmaragon](https://github.com/kmaragon) |
| 267 | * [Perl](https://github.com/Leont/crypt-argon2) by [@leont](https://github.com/Leont) |
| 268 | * [mruby](https://github.com/Asmod4n/mruby-argon2) by [@Asmod4n](https://github.com/Asmod4n) |
| 269 | * [Swift](https://github.com/ImKcat/CatCrypto) by [@ImKcat](https://github.com/ImKcat) |
| 270 | |
| 271 | |
| 272 | ## Test suite |
| 273 | |
| 274 | There are two sets of test suites. One is a low level test for the hash |
| 275 | function, the other tests the higher level API. Both of these are built and |
| 276 | executed by running: |
| 277 | |
| 278 | `make test` |
| 279 | |
| 280 | ## Intellectual property |
| 281 | |
| 282 | Except for the components listed below, the Argon2 code in this |
| 283 | repository is copyright (c) 2015 Daniel Dinu, Dmitry Khovratovich (main |
| 284 | authors), Jean-Philippe Aumasson and Samuel Neves, and dual licensed under the |
| 285 | [CC0 License](https://creativecommons.org/about/cc0) and the |
| 286 | [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0). For more info |
| 287 | see the LICENSE file. |
| 288 | |
| 289 | The string encoding routines in [`src/encoding.c`](src/encoding.c) are |
| 290 | copyright (c) 2015 Thomas Pornin, and under |
| 291 | [CC0 License](https://creativecommons.org/about/cc0). |
| 292 | |
| 293 | The BLAKE2 code in [`src/blake2/`](src/blake2) is copyright (c) Samuel |
| 294 | Neves, 2013-2015, and under |
| 295 | [CC0 License](https://creativecommons.org/about/cc0). |
| 296 | |
| 297 | All licenses are therefore GPL-compatible. |