2018-09-26 17:09:29 +02:00
|
|
|
#ifdef _MSC_VER
|
2016-01-12 23:52:30 +01:00
|
|
|
#include <platform.h>
|
2018-09-26 17:09:29 +02:00
|
|
|
#endif
|
2016-01-12 23:52:30 +01:00
|
|
|
#include "password.h"
|
|
|
|
|
2018-09-26 19:05:49 +02:00
|
|
|
#include "crypto/crypto.h"
|
2018-09-26 17:09:29 +02:00
|
|
|
|
2016-01-12 23:52:30 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2016-01-13 14:41:09 +01:00
|
|
|
|
2018-09-26 19:05:49 +02:00
|
|
|
int bcrypt_workfactor = 8;
|
|
|
|
|
2018-09-26 17:09:29 +02:00
|
|
|
bool password_is_implemented(cryptalgo_t algo) {
|
|
|
|
if (algo == PASSWORD_BCRYPT) return true;
|
2016-07-13 19:10:22 +02:00
|
|
|
return algo == PASSWORD_PLAINTEXT;
|
2016-01-12 23:52:30 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 21:06:56 +02:00
|
|
|
const char * password_hash(const char * passwd, cryptalgo_t algo) {
|
2018-11-27 20:16:27 +01:00
|
|
|
if (algo == PASSWORD_BCRYPT && bcrypt_workfactor != 0) {
|
2018-09-26 17:09:29 +02:00
|
|
|
char salt[BCRYPT_HASHSIZE];
|
|
|
|
static char hash[BCRYPT_HASHSIZE];
|
|
|
|
int ret;
|
2018-09-26 19:05:49 +02:00
|
|
|
bcrypt_gensalt(bcrypt_workfactor, salt);
|
2018-09-26 17:09:29 +02:00
|
|
|
ret = bcrypt_hashpw(passwd, salt, hash);
|
|
|
|
assert(ret == 0);
|
|
|
|
return hash;
|
|
|
|
}
|
2016-07-13 19:10:22 +02:00
|
|
|
return passwd;
|
2016-01-13 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int password_verify(const char * pwhash, const char * passwd) {
|
2018-09-26 17:09:29 +02:00
|
|
|
if (pwhash[0] == '$') {
|
|
|
|
if (pwhash[1] == '2') {
|
2018-11-27 20:16:27 +01:00
|
|
|
if (bcrypt_workfactor > 0) {
|
|
|
|
int ret = bcrypt_checkpw(passwd, pwhash);
|
|
|
|
assert(ret != -1);
|
|
|
|
return (ret == 0) ? VERIFY_OK : VERIFY_FAIL;
|
|
|
|
}
|
|
|
|
return VERIFY_OK;
|
2018-09-26 17:09:29 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 19:10:22 +02:00
|
|
|
return (strcmp(passwd, pwhash) == 0) ? VERIFY_OK : VERIFY_FAIL;
|
2016-01-12 23:52:30 +01:00
|
|
|
}
|
2019-01-12 21:26:48 +01:00
|
|
|
|
|
|
|
void password_generate(char *password, size_t length) {
|
|
|
|
char salt[BCRYPT_HASHSIZE];
|
|
|
|
|
|
|
|
assert(BCRYPT_HASHSIZE - 7 > length);
|
|
|
|
bcrypt_gensalt(4, salt);
|
|
|
|
memcpy(password, salt + 7, length);
|
|
|
|
password[length] = 0;
|
|
|
|
}
|