pkcs5_pbkdf2.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 14 Jun 2017 00:27:40 -0400
changeset 51 30d2d7598591
parent 10 85496558f3a4
child 54 3d70f17a68b8
permissions -rw-r--r--
Merge gtkui branch to default.
icculus@0
     1
/*	$OpenBSD: pkcs5_pbkdf2.c,v 1.4 2013/06/03 21:07:02 tedu Exp $	*/
icculus@0
     2
icculus@0
     3
/*-
icculus@0
     4
 * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
icculus@0
     5
 *
icculus@0
     6
 * Permission to use, copy, modify, and distribute this software for any
icculus@0
     7
 * purpose with or without fee is hereby granted, provided that the above
icculus@0
     8
 * copyright notice and this permission notice appear in all copies.
icculus@0
     9
 *
icculus@0
    10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
icculus@0
    11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
icculus@0
    12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
icculus@0
    13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
icculus@0
    14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
icculus@0
    15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
icculus@0
    16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
icculus@0
    17
 */
icculus@0
    18
icculus@0
    19
#include <sys/types.h>
icculus@0
    20
#include <sys/param.h>
icculus@0
    21
icculus@0
    22
#include <string.h>
icculus@0
    23
#include <limits.h>
icculus@0
    24
#include <stdlib.h>
icculus@10
    25
//#include <util.h>
icculus@0
    26
icculus@0
    27
#include "sha1.h"
icculus@0
    28
icculus@0
    29
/*
icculus@0
    30
 * HMAC-SHA-1 (from RFC 2202).
icculus@0
    31
 */
icculus@0
    32
static void
icculus@0
    33
hmac_sha1(const u_int8_t *text, size_t text_len, const u_int8_t *key,
icculus@0
    34
    size_t key_len, u_int8_t digest[SHA1_DIGEST_LENGTH])
icculus@0
    35
{
icculus@0
    36
	SHA1_CTX ctx;
icculus@0
    37
	u_int8_t k_pad[SHA1_BLOCK_LENGTH];
icculus@0
    38
	u_int8_t tk[SHA1_DIGEST_LENGTH];
icculus@0
    39
	int i;
icculus@0
    40
icculus@0
    41
	if (key_len > SHA1_BLOCK_LENGTH) {
icculus@0
    42
		SHA1Init(&ctx);
icculus@0
    43
		SHA1Update(&ctx, key, key_len);
icculus@0
    44
		SHA1Final(tk, &ctx);
icculus@0
    45
icculus@0
    46
		key = tk;
icculus@0
    47
		key_len = SHA1_DIGEST_LENGTH;
icculus@0
    48
	}
icculus@0
    49
icculus@0
    50
	bzero(k_pad, sizeof k_pad);
icculus@0
    51
	bcopy(key, k_pad, key_len);
icculus@0
    52
	for (i = 0; i < SHA1_BLOCK_LENGTH; i++)
icculus@0
    53
		k_pad[i] ^= 0x36;
icculus@0
    54
icculus@0
    55
	SHA1Init(&ctx);
icculus@0
    56
	SHA1Update(&ctx, k_pad, SHA1_BLOCK_LENGTH);
icculus@0
    57
	SHA1Update(&ctx, text, text_len);
icculus@0
    58
	SHA1Final(digest, &ctx);
icculus@0
    59
icculus@0
    60
	bzero(k_pad, sizeof k_pad);
icculus@0
    61
	bcopy(key, k_pad, key_len);
icculus@0
    62
	for (i = 0; i < SHA1_BLOCK_LENGTH; i++)
icculus@0
    63
		k_pad[i] ^= 0x5c;
icculus@0
    64
icculus@0
    65
	SHA1Init(&ctx);
icculus@0
    66
	SHA1Update(&ctx, k_pad, SHA1_BLOCK_LENGTH);
icculus@0
    67
	SHA1Update(&ctx, digest, SHA1_DIGEST_LENGTH);
icculus@0
    68
	SHA1Final(digest, &ctx);
icculus@0
    69
}
icculus@0
    70
icculus@0
    71
/*
icculus@0
    72
 * Password-Based Key Derivation Function 2 (PKCS #5 v2.0).
icculus@0
    73
 * Code based on IEEE Std 802.11-2007, Annex H.4.2.
icculus@0
    74
 */
icculus@0
    75
int
icculus@0
    76
pkcs5_pbkdf2(const char *pass, size_t pass_len, const uint8_t *salt, size_t salt_len,
icculus@0
    77
    uint8_t *key, size_t key_len, unsigned int rounds)
icculus@0
    78
{
icculus@0
    79
	uint8_t *asalt, obuf[SHA1_DIGEST_LENGTH];
icculus@0
    80
	uint8_t d1[SHA1_DIGEST_LENGTH], d2[SHA1_DIGEST_LENGTH];
icculus@0
    81
	unsigned int i, j;
icculus@0
    82
	unsigned int count;
icculus@0
    83
	size_t r;
icculus@0
    84
icculus@0
    85
	if (rounds < 1 || key_len == 0)
icculus@0
    86
		return -1;
icculus@0
    87
	if (salt_len == 0 || salt_len > SIZE_MAX - 4)
icculus@0
    88
		return -1;
icculus@0
    89
	if ((asalt = malloc(salt_len + 4)) == NULL)
icculus@0
    90
		return -1;
icculus@0
    91
icculus@0
    92
	memcpy(asalt, salt, salt_len);
icculus@0
    93
icculus@0
    94
	for (count = 1; key_len > 0; count++) {
icculus@0
    95
		asalt[salt_len + 0] = (count >> 24) & 0xff;
icculus@0
    96
		asalt[salt_len + 1] = (count >> 16) & 0xff;
icculus@0
    97
		asalt[salt_len + 2] = (count >> 8) & 0xff;
icculus@0
    98
		asalt[salt_len + 3] = count & 0xff;
icculus@0
    99
		hmac_sha1(asalt, salt_len + 4, (const uint8_t *) pass, pass_len, d1);
icculus@0
   100
		memcpy(obuf, d1, sizeof(obuf));
icculus@0
   101
icculus@0
   102
		for (i = 1; i < rounds; i++) {
icculus@0
   103
			hmac_sha1(d1, sizeof(d1), (const uint8_t *) pass, pass_len, d2);
icculus@0
   104
			memcpy(d1, d2, sizeof(d1));
icculus@0
   105
			for (j = 0; j < sizeof(obuf); j++)
icculus@0
   106
				obuf[j] ^= d1[j];
icculus@0
   107
		}
icculus@0
   108
icculus@0
   109
		r = MIN(key_len, SHA1_DIGEST_LENGTH);
icculus@0
   110
		memcpy(key, obuf, r);
icculus@0
   111
		key += r;
icculus@0
   112
		key_len -= r;
icculus@0
   113
	};
icculus@0
   114
	bzero(asalt, salt_len + 4);
icculus@0
   115
	free(asalt);
icculus@0
   116
	bzero(d1, sizeof(d1));
icculus@0
   117
	bzero(d2, sizeof(d2));
icculus@0
   118
	bzero(obuf, sizeof(obuf));
icculus@0
   119
icculus@0
   120
	return 0;
icculus@0
   121
}
icculus@0
   122