bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
a81d5c3f5a4ad5d100b258d10d4c75f4a02ab1f6Stephan Bosch
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi#include "lib.h"
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi#include "buffer.h"
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi#include "hash-method.h"
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi#include "hmac.h"
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi#include "pkcs5.h"
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi#include <stdint.h>
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi#include <arpa/inet.h>
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomistatic
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomiint pkcs5_pbkdf1(const struct hash_method *hash,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi const unsigned char *password, size_t password_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi const unsigned char *salt, size_t salt_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned int iter, uint32_t length,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi buffer_t *result)
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi{
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi if (length < 1 ||
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi length > hash->digest_size) return -1;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi if (iter < 1) return -1;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned char dk[hash->digest_size];
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned char ctx[hash->context_size];
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hash->init(ctx);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hash->loop(ctx, password, password_len);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hash->loop(ctx, salt, salt_len);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hash->result(ctx, dk);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi length--;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi for(;length>0;length--) {
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hash->init(ctx);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hash->loop(ctx, dk, hash->digest_size);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hash->result(ctx, dk);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi }
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi buffer_append(result, dk, hash->digest_size);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi return 0;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi}
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomistatic
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomiint pkcs5_pbkdf2(const struct hash_method *hash,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi const unsigned char *password, size_t password_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi const unsigned char *salt, size_t salt_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned int iter, uint32_t length,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi buffer_t *result)
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi{
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi if (length < 1 || iter < 1) return -1;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi size_t l = (length + hash->digest_size - 1)/hash->digest_size; /* same as ceil(length/hash->digest_size) */
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned char dk[l * hash->digest_size];
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned char *block;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi struct hmac_context hctx;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned int c,i,t;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned char U_c[hash->digest_size];
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi for(t = 0; t < l; t++) {
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi block = &(dk[t*hash->digest_size]);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi /* U_1 = PRF(Password, Salt|| INT_BE32(Block_Number)) */
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi c = htonl(t+1);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hmac_init(&hctx, password, password_len, hash);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hmac_update(&hctx, salt, salt_len);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hmac_update(&hctx, &c, sizeof(c));
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hmac_final(&hctx, U_c);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi /* block = U_1 ^ .. ^ U_iter */
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi memcpy(block, U_c, hash->digest_size);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi /* U_c = PRF(Password, U_c-1) */
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi for(c = 1; c < iter; c++) {
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hmac_init(&hctx, password, password_len, hash);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hmac_update(&hctx, U_c, hash->digest_size);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi hmac_final(&hctx, U_c);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi for(i = 0; i < hash->digest_size; i++)
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi block[i] ^= U_c[i];
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi }
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi }
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi buffer_append(result, dk, length);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi return 0;
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi}
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomiint pkcs5_pbkdf(enum pkcs5_pbkdf_mode mode, const struct hash_method *hash,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi const unsigned char *password, size_t password_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi const unsigned char *salt, size_t salt_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi unsigned int iterations, uint32_t dk_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi buffer_t *result)
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi{
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi if (mode == PKCS5_PBKDF1)
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi return pkcs5_pbkdf1(hash,password,password_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi salt,salt_len,iterations,dk_len,result);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi else if (mode == PKCS5_PBKDF2)
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi return pkcs5_pbkdf2(hash,password,password_len,
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi salt,salt_len,iterations,dk_len,result);
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi i_unreached();
df74b8f6ff432d9a26727669dfed9a15d77efb55Aki Tuomi}