password-scheme-rpa.c revision b193066823000095cae0781445fe9234c7c01366
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include "lib.h"
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen#include "buffer.h"
76a58bc863893374000881e69d6dd4e3dccadddeTimo Sirainen#include "md5.h"
f1ed4fa248aaf6841ba638b9d66b2738d9f7aa18Timo Sirainen#include "hex-binary.h"
76a58bc863893374000881e69d6dd4e3dccadddeTimo Sirainen#include "safe-memset.h"
f1ed4fa248aaf6841ba638b9d66b2738d9f7aa18Timo Sirainen#include "password-scheme.h"
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainenvoid *ucs2be_str(pool_t pool, const char *str, size_t *size);
f1ed4fa248aaf6841ba638b9d66b2738d9f7aa18Timo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen/*
76d4ff1c1b31a1a09f6cbfe613a8d0efe62cbfd2Timo Sirainen * Convert string to big-endian ucs2.
f1ed4fa248aaf6841ba638b9d66b2738d9f7aa18Timo Sirainen */
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainenvoid *ucs2be_str(pool_t pool, const char *str, size_t *size)
f1ed4fa248aaf6841ba638b9d66b2738d9f7aa18Timo Sirainen{
f1ed4fa248aaf6841ba638b9d66b2738d9f7aa18Timo Sirainen buffer_t *buf = buffer_create_dynamic(pool, 32);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen while (*str) {
f1ed4fa248aaf6841ba638b9d66b2738d9f7aa18Timo Sirainen buffer_append_c(buf, '\0');
f1ed4fa248aaf6841ba638b9d66b2738d9f7aa18Timo Sirainen buffer_append_c(buf, *str++);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen }
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen *size = buffer_get_used_size(buf);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen return buffer_free_without_data(buf);
be6ad6e46ecb8c7126b421819046e7f4857a2babTimo Sirainen}
const char *password_generate_rpa(const char *pw)
{
unsigned char hash[MD5_RESULTLEN];
unsigned char *ucs2be_pw;
size_t size;
ucs2be_pw = ucs2be_str(unsafe_data_stack_pool, pw, &size);
md5_get_digest(ucs2be_pw, size, hash);
safe_memset(ucs2be_pw, 0, size);
return binary_to_hex(hash, sizeof(hash));
}