bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen#include "lib.h"
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen#include "buffer.h"
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen#include "md5.h"
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen#include "hex-binary.h"
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen#include "safe-memset.h"
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen#include "password-scheme.h"
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainenvoid *ucs2be_str(pool_t pool, const char *str, size_t *size);
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen/*
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen * Convert string to big-endian ucs2.
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen */
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainenvoid *ucs2be_str(pool_t pool, const char *str, size_t *size)
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen{
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen buffer_t *buf = buffer_create_dynamic(pool, 32);
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen while (*str != '\0') {
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen buffer_append_c(buf, '\0');
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen buffer_append_c(buf, *str++);
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen }
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen
2d5d7890bd9b282eb1092fc788a2432a6c79a7adTimo Sirainen *size = buf->used;
0f66f12eb4cdbf47670975044c88d8f388bf92dfTimo Sirainen return buffer_free_without_data(&buf);
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen}
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainenvoid password_generate_rpa(const char *pw, unsigned char result[])
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen{
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen unsigned char *ucs2be_pw;
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen size_t size;
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen ucs2be_pw = ucs2be_str(unsafe_data_stack_pool, pw, &size);
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen md5_get_digest(ucs2be_pw, size, result);
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen safe_memset(ucs2be_pw, 0, size);
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen}