4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief PBKDF2 implementation
34 * \author Giovanni Bajo <rasky@develer.com>
39 #include <cpu/byteorder.h>
41 #include <cfg/debug.h>
43 static void PBKDF2_begin(Kdf *ctx_, const char *pwd, size_t pwd_len,
44 const uint8_t *salt, size_t salt_len)
46 PBKDF2_Context *ctx = (PBKDF2_Context*)ctx_;
48 ASSERT(sizeof(ctx->salt) >= salt_len);
50 mac_set_key(ctx->mac, (const uint8_t*)pwd, pwd_len);
51 ctx->salt_len = salt_len;
52 memcpy(ctx->salt, salt, salt_len);
55 ctx->kdf.block = NULL;
58 static void PBKDF2_next(Kdf *ctx_)
60 PBKDF2_Context *ctx = (PBKDF2_Context*)ctx_;
61 int dlen = mac_digest_len(ctx->mac);
65 uint32_t bec = cpu_to_be32(ctx->c);
68 mac_update(ctx->mac, ctx->salt, ctx->salt_len);
69 mac_update(ctx->mac, &bec, 4);
70 memcpy(last, mac_final(ctx->mac), dlen);
71 memcpy(ctx->block, last, dlen);
73 for (uint32_t i=0; i<ctx->iterations-1; ++i)
76 mac_update(ctx->mac, last, dlen);
77 memcpy(last, mac_final(ctx->mac), dlen);
78 xor_block(ctx->block, ctx->block, last, dlen);
81 ctx->kdf.to_read = dlen;
82 ctx->kdf.block = ctx->block;
88 /**********************************************************************/
90 // Default iteration count. The RFC does not specify a "good" default
91 // value; it just says that this should be a high value to slow down
92 // computations. Since slowing down is not much of a concern for an
93 // embedded system, we settle for a value which is not too big.
94 #define PBKDF2_DEFAULT_ITERATIONS 100
97 void PBKDF2_init(PBKDF2_Context *ctx, Mac *mac)
101 ctx->iterations = PBKDF2_DEFAULT_ITERATIONS;
102 ctx->kdf.begin = PBKDF2_begin;
103 ctx->kdf.next = PBKDF2_next;
104 ctx->kdf.block_len = mac_digest_len(mac);
105 ASSERT(ctx->kdf.block_len <= sizeof(ctx->block));
108 void PBKDF2_set_iterations(Kdf *ctx_, uint32_t iterations)
110 PBKDF2_Context *ctx = (PBKDF2_Context*)ctx_;
111 ctx->iterations = iterations;