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 PBKDF1 implementation
34 * \author Giovanni Bajo <rasky@develer.com>
43 static void PBKDF1_begin(Kdf *ctx_, const char *pwd, size_t pwd_len,
44 const uint8_t *salt, size_t salt_len)
46 PBKDF1_Context *ctx = (PBKDF1_Context *)ctx_;
48 hash_begin(ctx->hash);
49 hash_update(ctx->hash, pwd, pwd_len);
50 hash_update(ctx->hash, salt, salt_len);
53 ctx->kdf.block = NULL;
56 static void PBKDF1_next(Kdf *ctx_)
58 PBKDF1_Context *ctx = (PBKDF1_Context *)ctx_;
60 // PBKDF1 will generate only one block of data (whose len depends
61 // on the underlying hash function). After that, the generation stops
62 // with an ASSERT. If you use PKBDF1, you are supposed to be aware
63 // of this limit while designing your algorithm.
64 ASSERT(ctx->kdf.block == NULL);
66 int hlen = hash_digest_len(ctx->hash);
68 uint8_t *final = hash_final(ctx->hash);
70 for (uint32_t i=0; i<ctx->iterations-1; i++)
72 memcpy(temp, final, hlen);
73 hash_begin(ctx->hash);
74 hash_update(ctx->hash, temp, hlen);
75 final = hash_final(ctx->hash);
80 ctx->kdf.to_read = ctx->kdf.block_len;
81 ctx->kdf.block = final;
84 /**********************************************************************/
86 // Default iteration count. The RFC does not specify a "good" default
87 // value; it just says that this should be a high value to slow down
88 // computations. Since slowing down is not much of a concern for an
89 // embedded system, we settle for a value which is not too big.
90 #define PBKDF1_DEFAULT_ITERATIONS 100
93 void PBKDF1_init(PBKDF1_Context *ctx, Hash *h)
96 ctx->iterations = PBKDF1_DEFAULT_ITERATIONS;
97 ctx->kdf.begin = PBKDF1_begin;
98 ctx->kdf.next = PBKDF1_next;
99 ctx->kdf.block_len = hash_digest_len(h);
102 void PBKDF1_set_iterations(Kdf *ctx_, uint32_t iterations)
104 PBKDF1_Context *ctx = (PBKDF1_Context *)ctx_;
105 ctx->iterations = iterations;