ad03fa585f201d6fa5d85e8b0a7d4be17e1e6b57
[strongswan.git] / src / libstrongswan / plugins / fips_prf / fips_prf.c
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "fips_prf.h"
17
18 #include <arpa/inet.h>
19
20 #include <debug.h>
21
22 typedef struct private_fips_prf_t private_fips_prf_t;
23
24 /**
25 * Private data of a fips_prf_t object.
26 */
27 struct private_fips_prf_t {
28 /**
29 * Public fips_prf_t interface.
30 */
31 fips_prf_t public;
32
33 /**
34 * key of prf function, "b" long
35 */
36 u_int8_t *key;
37
38 /**
39 * size of "b" in bytes
40 */
41 size_t b;
42
43 /**
44 * Keyed SHA1 prf: It does not use SHA1Final operation
45 */
46 prf_t *keyed_prf;
47
48 /**
49 * G function, either SHA1 or DES
50 */
51 void (*g)(private_fips_prf_t *this, chunk_t c, u_int8_t res[]);
52 };
53
54 /**
55 * sum = (a + b) mod 2 ^ (length * 8)
56 */
57 static void add_mod(size_t length, u_int8_t a[], u_int8_t b[], u_int8_t sum[])
58 {
59 int i, c = 0;
60
61 for(i = length - 1; i >= 0; i--)
62 {
63 u_int32_t tmp;
64
65 tmp = a[i] + b[i] + c;
66 sum[i] = 0xff & tmp;
67 c = tmp >> 8;
68 }
69 }
70
71 /**
72 * calculate "chunk mod 2^(length*8)" and save it into buffer
73 */
74 static void chunk_mod(size_t length, chunk_t chunk, u_int8_t buffer[])
75 {
76 if (chunk.len < length)
77 {
78 /* apply seed as least significant bits, others are zero */
79 memset(buffer, 0, length - chunk.len);
80 memcpy(buffer + length - chunk.len, chunk.ptr, chunk.len);
81 }
82 else
83 {
84 /* use least significant bytes from seed, as we use mod 2^b */
85 memcpy(buffer, chunk.ptr + chunk.len - length, length);
86 }
87 }
88
89 /**
90 * Implementation of prf_t.get_bytes.
91 *
92 * Test vector:
93 *
94 * key:
95 * 0xbd, 0x02, 0x9b, 0xbe, 0x7f, 0x51, 0x96, 0x0b,
96 * 0xcf, 0x9e, 0xdb, 0x2b, 0x61, 0xf0, 0x6f, 0x0f,
97 * 0xeb, 0x5a, 0x38, 0xb6
98 *
99 * seed:
100 * 0x00
101 *
102 * result:
103 * 0x20, 0x70, 0xb3, 0x22, 0x3d, 0xba, 0x37, 0x2f,
104 * 0xde, 0x1c, 0x0f, 0xfc, 0x7b, 0x2e, 0x3b, 0x49,
105 * 0x8b, 0x26, 0x06, 0x14, 0x3c, 0x6c, 0x18, 0xba,
106 * 0xcb, 0x0f, 0x6c, 0x55, 0xba, 0xbb, 0x13, 0x78,
107 * 0x8e, 0x20, 0xd7, 0x37, 0xa3, 0x27, 0x51, 0x16
108 */
109 static void get_bytes(private_fips_prf_t *this, chunk_t seed, u_int8_t w[])
110 {
111 int i;
112 u_int8_t xval[this->b];
113 u_int8_t xseed[this->b];
114 u_int8_t sum[this->b];
115 u_int8_t *xkey = this->key;
116 u_int8_t one[this->b];
117
118 memset(one, 0, this->b);
119 one[this->b - 1] = 0x01;
120
121 /* 3.1 */
122 chunk_mod(this->b, seed, xseed);
123
124 /* 3.2 */
125 for (i = 0; i < 2; i++) /* twice */
126 {
127 /* a. XVAL = (XKEY + XSEED j) mod 2^b */
128 add_mod(this->b, xkey, xseed, xval);
129 DBG3(DBG_LIB, "XVAL %b", xval, this->b);
130 /* b. wi = G(t, XVAL ) */
131 this->g(this, chunk_create(xval, this->b), &w[i * this->b]);
132 DBG3(DBG_LIB, "w[%d] %b", i, &w[i * this->b], this->b);
133 /* c. XKEY = (1 + XKEY + wi) mod 2b */
134 add_mod(this->b, xkey, &w[i * this->b], sum);
135 add_mod(this->b, sum, one, xkey);
136 DBG3(DBG_LIB, "XKEY %b", xkey, this->b);
137 }
138
139 /* 3.3 done already, mod q not used */
140 }
141
142 /**
143 * Implementation of prf_t.get_block_size.
144 */
145 static size_t get_block_size(private_fips_prf_t *this)
146 {
147 return 2 * this->b;
148 }
149 /**
150 * Implementation of prf_t.allocate_bytes.
151 */
152 static void allocate_bytes(private_fips_prf_t *this, chunk_t seed, chunk_t *chunk)
153 {
154 *chunk = chunk_alloc(get_block_size(this));
155 get_bytes(this, seed, chunk->ptr);
156 }
157
158 /**
159 * Implementation of prf_t.get_key_size.
160 */
161 static size_t get_key_size(private_fips_prf_t *this)
162 {
163 return this->b;
164 }
165
166 /**
167 * Implementation of prf_t.set_key.
168 */
169 static void set_key(private_fips_prf_t *this, chunk_t key)
170 {
171 /* save key as "key mod 2^b" */
172 chunk_mod(this->b, key, this->key);
173 }
174
175 /**
176 * Implementation of the G() function based on SHA1
177 */
178 void g_sha1(private_fips_prf_t *this, chunk_t c, u_int8_t res[])
179 {
180 u_int8_t buf[64];
181
182 if (c.len < sizeof(buf))
183 {
184 /* pad c with zeros */
185 memset(buf, 0, sizeof(buf));
186 memcpy(buf, c.ptr, c.len);
187 c.ptr = buf;
188 c.len = sizeof(buf);
189 }
190 else
191 {
192 /* not more than 512 bits can be G()-ed */
193 c.len = sizeof(buf);
194 }
195
196 /* use the keyed hasher, but use an empty key to use SHA1 IV */
197 this->keyed_prf->set_key(this->keyed_prf, chunk_empty);
198 this->keyed_prf->get_bytes(this->keyed_prf, c, res);
199 }
200
201 /**
202 * Implementation of prf_t.destroy.
203 */
204 static void destroy(private_fips_prf_t *this)
205 {
206 this->keyed_prf->destroy(this->keyed_prf);
207 free(this->key);
208 free(this);
209 }
210
211 /*
212 * Described in header.
213 */
214 fips_prf_t *fips_prf_create(pseudo_random_function_t algo)
215 {
216 private_fips_prf_t *this = malloc_thing(private_fips_prf_t);
217
218 this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
219 this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
220 this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
221 this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size;
222 this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
223 this->public.prf_interface.destroy = (void (*) (prf_t *))destroy;
224
225 switch (algo)
226 {
227 case PRF_FIPS_SHA1_160:
228 {
229 this->g = g_sha1;
230 this->b = 20;
231 this->keyed_prf = lib->crypto->create_prf(lib->crypto, PRF_KEYED_SHA1);
232 if (this->keyed_prf == NULL)
233 {
234 free(this);
235 return NULL;
236 }
237 break;
238 }
239 case PRF_FIPS_DES:
240 /* not implemented yet */
241 default:
242 free(this);
243 return NULL;
244 }
245 this->key = malloc(this->b);
246
247 return &this->public;
248 }
249