e3d1f5f823aa7596d2120eefd74324845066d124
[bertos.git] / bertos / sec / kdf / pbkdf2.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 PBKDF2 implementation
34  * \author Giovanni Bajo <rasky@develer.com>
35  * 
36  */
37
38 #include "pbkdf2.h"
39 #include <cpu/byteorder.h>
40 #include <sec/util.h>
41 #include <cfg/debug.h>
42
43 static void PBKDF2_begin(Kdf *ctx_, const char *pwd, size_t pwd_len,
44                                              const uint8_t *salt, size_t salt_len)
45 {
46         PBKDF2_Context *ctx = (PBKDF2_Context*)ctx_;
47         
48         ASSERT(sizeof(ctx->salt) >= salt_len);
49         
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);
53         ctx->c = 0;
54         ctx->kdf.to_read = 0;
55         ctx->kdf.block = NULL;
56 }
57
58 static void PBKDF2_next(Kdf *ctx_)
59 {
60         PBKDF2_Context *ctx = (PBKDF2_Context*)ctx_;
61         int dlen = mac_digest_len(ctx->mac);
62         uint8_t last[dlen];
63         
64         ++ctx->c;
65         uint32_t bec = cpu_to_be32(ctx->c);
66         
67         mac_begin(ctx->mac);
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);
72         
73         for (uint32_t i=0; i<ctx->iterations-1; ++i)
74         {
75                 mac_begin(ctx->mac);
76                 mac_update(ctx->mac, last, dlen);
77                 memcpy(last, mac_final(ctx->mac), dlen);
78                 xor_block(ctx->block, ctx->block, last, dlen);
79         }
80
81         ctx->kdf.to_read = dlen;
82         ctx->kdf.block = ctx->block;
83         
84         PURGE(last);
85 }
86
87
88 /**********************************************************************/
89
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
95
96
97 void PBKDF2_init(PBKDF2_Context *ctx, Mac *mac)
98 {
99         ctx->salt_len = 0;
100         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));
106 }
107
108 void PBKDF2_set_iterations(Kdf *ctx_, uint32_t iterations)
109 {
110         PBKDF2_Context *ctx = (PBKDF2_Context*)ctx_;
111         ctx->iterations = iterations;
112 }
113