bf4980bdba6fc2c97fb53ce8d4a0aa7c9e03ec8d
[bertos.git] / bertos / sec / kdf / pbkdf1.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief PBKDF1 implementation
34  * \author Giovanni Bajo <rasky@develer.com>
35  * 
36  */
37
38 #include "pbkdf1.h"
39 #include <sec/hash.h>
40 #include <sec/util.h>
41
42
43 static void PBKDF1_begin(Kdf *ctx_, const char *pwd, size_t pwd_len,
44                                              const uint8_t *salt, size_t salt_len)
45 {
46         PBKDF1_Context *ctx = (PBKDF1_Context *)ctx_;
47
48         hash_begin(ctx->hash);
49         hash_update(ctx->hash, pwd, pwd_len);
50         hash_update(ctx->hash, salt, salt_len);
51         
52         ctx->kdf.to_read = 0;
53         ctx->kdf.block = NULL;
54 }
55
56 static void PBKDF1_next(Kdf *ctx_)
57 {
58         PBKDF1_Context *ctx = (PBKDF1_Context *)ctx_;
59         
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);
65
66         int hlen = hash_digest_len(ctx->hash);
67         uint8_t temp[hlen];
68         uint8_t *final = hash_final(ctx->hash);
69         
70         for (uint32_t i=0; i<ctx->iterations-1; i++)
71         {
72                 memcpy(temp, final, hlen);
73                 hash_begin(ctx->hash);
74                 hash_update(ctx->hash, temp, hlen);
75                 final = hash_final(ctx->hash);
76         }
77
78         PURGE(temp);
79
80         ctx->kdf.to_read = ctx->kdf.block_len;
81         ctx->kdf.block = final;
82 }
83
84 /**********************************************************************/
85
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
91
92
93 void PBKDF1_init(PBKDF1_Context *ctx, Hash *h)
94 {
95         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);
100 }
101
102 void PBKDF1_set_iterations(Kdf *ctx_, uint32_t iterations)
103 {
104         PBKDF1_Context *ctx = (PBKDF1_Context *)ctx_;
105         ctx->iterations = iterations;
106 }