Last active
October 21, 2022 14:44
-
-
Save z3bra/acee29fb2815e170c5674177beb041bc to your computer and use it in GitHub Desktop.
Libsodium Argon2 parallelism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* cc argon2id.c -lsodium -o argon2id | |
* | |
* printf "password" | argon2id -s salt -m mem -t time -p thread > file.key | |
*/ | |
#include <string.h> | |
#include <unistd.h> | |
#include <sodium.h> | |
/* | |
* Copy me if you can. | |
* by 20h | |
*/ | |
#define ARGBEGIN for (argv0 = *argv, argv++, argc--; argv[0] && argv[0][0] == '-' && argv[0][1];argc--,argv++) {\ | |
char argc_, **argv_; int brk_;\ | |
if (argv[0][1] == '-' && argv[0][2] == '\0') {\ | |
argv++; argc--; break;\ | |
}\ | |
for (int i_=1, brk_=0, argv_ = argv; argv[0][i_] && !brk_; i_++) {\ | |
if (argv_ != argv) break;\ | |
argc_ = argv[0][i_];\ | |
switch (argc_) | |
#define ARGEND }} | |
#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ | |
((x), abort(), (char *)0) :\ | |
(brk_ = 1, (argv[0][i_+1] != '\0')?\ | |
(&argv[0][i_+1]) :\ | |
(argc--, argv++, argv[0]))) | |
void | |
usage(char *pgm) | |
{ | |
fprintf(stderr, "usage: %s -s salt -m mem -t time -p parallelism > file.key\n", pgm); | |
exit(1); | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
int fd, memory, time, parallelism; | |
uint8_t key[32]; | |
char *argv0, *salt, pw[256]; | |
ssize_t n; | |
/* default key derivation values */ | |
memory = 65536; | |
time = 3; | |
parallelism = 1; | |
ARGBEGIN { | |
case 'm': | |
memory = atoi(EARGF(usage(argv0))); | |
break; | |
case 't': | |
time = atoi(EARGF(usage(argv0))); | |
break; | |
case 'p': | |
parallelism = atoi(EARGF(usage(argv0))); | |
break; | |
case 's': | |
salt = EARGF(usage(argv0)); | |
break; | |
default: | |
usage(argv0); | |
} ARGEND | |
if (sodium_init() < 0) | |
return -1; | |
n = fread(pw, 1, sizeof(pw), stdin); | |
/* | |
* note: parallelism not used as it's hardcoded in libsodium. | |
* See https://github.com/jedisct1/libsodium/discussions/1092 | |
*/ | |
if (crypto_pwhash_argon2id(key, 32, pw, n, salt, time, memory*1024, crypto_pwhash_ALG_ARGON2ID13)) | |
return -1; | |
write(1, key, sizeof(key)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* usage | |
*/ | |
package main | |
import ( | |
"flag" | |
"os" | |
"bufio" | |
"golang.org/x/crypto/argon2" | |
) | |
const ( | |
// libsodium defaults values | |
argon2id_memory_cost = 65536 // 64 Kib | |
argon2id_time_cost = 3 | |
argon2id_threads = 1 | |
) | |
func main() { | |
var salt string | |
var time, memory, threads uint64 | |
flag.StringVar(&salt, "s", "0123456789abcdef", "Salt") | |
flag.Uint64Var(&memory, "m", argon2id_memory_cost, "Memory cost") | |
flag.Uint64Var(&time, "t", argon2id_time_cost, "Time cost") | |
flag.Uint64Var(&threads, "p", argon2id_threads, "Parallel threads") | |
flag.Parse() | |
pw, _ := bufio.NewReader(os.Stdin).ReadString('\n') | |
key := argon2.IDKey([]byte(pw), []byte(salt[:16]), uint32(time), uint32(memory), uint8(threads), 32) | |
os.Stdout.Write(key) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
pass="libsodium" | |
salt="0123456789abcdef" | |
time=3 | |
mem=65536 | |
thread=1 | |
printf 'm=%d,t=%d,p=%d\n' $mem $time $thread | |
[ -x argon2 ] && printf 'phc :' && printf "%s" $pass | ./argon2 $salt -id -k $mem -t $time -p $thread -r | |
[ -x argon2id_go ] && printf 'go :' && printf "%s" $pass | ./argon2id_go -s $salt -m $mem -t $time -p $thread | xxd -c 32 -p | |
[ -x argon2id_sodium ] && printf 'sodium :' && printf "%s" $pass | ./argon2id_sodium -s $salt -m $mem -t $time -p $thread | xxd -c 32 -p | |
echo | |
thread=4 | |
printf 'm=%d,t=%d,p=%d\n' $mem $time $thread | |
[ -x argon2 ] && printf 'phc :' && printf "%s" $pass | ./argon2 $salt -id -k $mem -t $time -p $thread -r | |
[ -x argon2id_go ] && printf 'go :' && printf "%s" $pass | ./argon2id_go -s $salt -m $mem -t $time -p $thread | xxd -c 32 -p | |
[ -x argon2id_sodium ] && printf 'sodium :' && printf "%s" $pass | ./argon2id_sodium -s $salt -m $mem -t $time -p $thread | xxd -c 32 -p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m=65536,t=3,p=1 | |
phc :bac8c2da3ea370cd916560750fa83b3b50b2d5b42fe0ba1017c6e517066ee715 | |
go :bac8c2da3ea370cd916560750fa83b3b50b2d5b42fe0ba1017c6e517066ee715 | |
sodium :bac8c2da3ea370cd916560750fa83b3b50b2d5b42fe0ba1017c6e517066ee715 | |
m=65536,t=3,p=4 | |
phc :57bf8befb740f474ef6c7cff20b3c84b7e7e8075a4c15499f90f50893bb5dd02 | |
go :57bf8befb740f474ef6c7cff20b3c84b7e7e8075a4c15499f90f50893bb5dd02 | |
sodium :bac8c2da3ea370cd916560750fa83b3b50b2d5b42fe0ba1017c6e517066ee715 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment