Skip to content

Instantly share code, notes, and snippets.

@nyg
Created April 21, 2020 09:43
Show Gist options
  • Save nyg/e366c27a70a77bf06581a0e6a8211cc9 to your computer and use it in GitHub Desktop.
Save nyg/e366c27a70a77bf06581a0e6a8211cc9 to your computer and use it in GitHub Desktop.
Create a password-derived key using libsodium.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/
#include <pwd.h>
#include <sodium.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
// time command
// for m in sensitive moderate interactive; do /usr/bin/time -l ./pdk $m; done
// verifier
// https://argon2.online
void hex_s(char *const out, const unsigned char *const array, size_t len);
int main(int argc, const char * argv[]) {
if (sodium_init() < 0) {
fprintf(stderr, "Failure to initialize libsodium.\n");
exit(EXIT_FAILURE);
}
/* Set computation and memory limits. */
unsigned long long opslimit = crypto_pwhash_OPSLIMIT_SENSITIVE;
size_t memlimit = crypto_pwhash_MEMLIMIT_SENSITIVE;
if (argc > 1) {
if (!strcmp("interactive", argv[1])) {
opslimit = crypto_pwhash_OPSLIMIT_INTERACTIVE;
memlimit = crypto_pwhash_MEMLIMIT_INTERACTIVE;
}
else if (!strcmp("moderate", argv[1])) {
opslimit = crypto_pwhash_OPSLIMIT_MODERATE;
memlimit = crypto_pwhash_MEMLIMIT_MODERATE;
}
}
/* Ask user for the password. */
char *pwd = getpass("Enter password (will not echo): ");
size_t pwd_len = strlen(pwd);
if (pwd_len < crypto_pwhash_PASSWD_MIN || pwd_len > crypto_pwhash_PASSWD_MAX) {
fprintf(stderr, "Password too long (or too short).\n");
goto fail;
}
/* Initialize key variable. */
unsigned char pdk[crypto_box_SEEDBYTES];
/* Initialize random salt. */
unsigned char salt[crypto_pwhash_SALTBYTES];
randombytes_buf(salt, sizeof(salt));
/* Compute the password-derived key. */
if (-1 == crypto_pwhash(pdk, sizeof(pdk),
pwd, pwd_len,
salt,
opslimit,
memlimit,
crypto_pwhash_ALG_ARGON2ID13)) {
fprintf(stderr, "Key not computed.\n");
goto fail;
}
/* Password is not needed anymore. */
sodium_mlock(pwd, pwd_len);
/* Print result. */
printf("ops : %llu\n", opslimit);
printf("mem : %zu\n", memlimit);
char salt_s[sizeof(salt) * 2 + 1];
hex_s(salt_s, salt, sizeof(salt));
printf("salt: %s\n", salt_s);
char pdk_s[sizeof(pdk) * 2 + 1];
hex_s(pdk_s, pdk, sizeof(pdk));
printf("pdk : %s\n", pdk_s);
return EXIT_SUCCESS;
fail:
/* Erase and lock password memory region. */
sodium_mlock(pwd, pwd_len);
return EXIT_FAILURE;
}
void hex_s(char *const out, const unsigned char *const array, size_t len)
{
int i;
for (i = 0; i < len; i++) {
sprintf(out + 2 * i, "%02x", array[i]);
}
out[2 * i] = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment