Migrated fips_prf plugin to INIT/METHOD macros
[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 METHOD(prf_t, get_bytes, void,
110 private_fips_prf_t *this, chunk_t seed, u_int8_t w[])
111 {
112 int i;
113 u_int8_t xval[this->b];
114 u_int8_t xseed[this->b];
115 u_int8_t sum[this->b];
116 u_int8_t *xkey = this->key;
117 u_int8_t one[this->b];
118
119 memset(one, 0, this->b);
120 one[this->b - 1] = 0x01;
121
122 /* 3.1 */
123 chunk_mod(this->b, seed, xseed);
124
125 /* 3.2 */
126 for (i = 0; i < 2; i++) /* twice */
127 {
128 /* a. XVAL = (XKEY + XSEED j) mod 2^b */
129 add_mod(this->b, xkey, xseed, xval);
130 DBG3(DBG_LIB, "XVAL %b", xval, this->b);
131 /* b. wi = G(t, XVAL ) */
132 this->g(this, chunk_create(xval, this->b), &w[i * this->b]);
133 DBG3(DBG_LIB, "w[%d] %b", i, &w[i * this->b], this->b);
134 /* c. XKEY = (1 + XKEY + wi) mod 2b */
135 add_mod(this->b, xkey, &w[i * this->b], sum);
136 add_mod(this->b, sum, one, xkey);
137 DBG3(DBG_LIB, "XKEY %b", xkey, this->b);
138 }
139
140 /* 3.3 done already, mod q not used */
141 }
142
143 METHOD(prf_t, get_block_size, size_t,
144 private_fips_prf_t *this)
145 {
146 return 2 * this->b;
147 }
148 METHOD(prf_t, allocate_bytes, void,
149 private_fips_prf_t *this, chunk_t seed, chunk_t *chunk)
150 {
151 *chunk = chunk_alloc(get_block_size(this));
152 get_bytes(this, seed, chunk->ptr);
153 }
154
155 METHOD(prf_t, get_key_size, size_t,
156 private_fips_prf_t *this)
157 {
158 return this->b;
159 }
160
161 METHOD(prf_t, set_key, void,
162 private_fips_prf_t *this, chunk_t key)
163 {
164 /* save key as "key mod 2^b" */
165 chunk_mod(this->b, key, this->key);
166 }
167
168 /**
169 * Implementation of the G() function based on SHA1
170 */
171 void g_sha1(private_fips_prf_t *this, chunk_t c, u_int8_t res[])
172 {
173 u_int8_t buf[64];
174
175 if (c.len < sizeof(buf))
176 {
177 /* pad c with zeros */
178 memset(buf, 0, sizeof(buf));
179 memcpy(buf, c.ptr, c.len);
180 c.ptr = buf;
181 c.len = sizeof(buf);
182 }
183 else
184 {
185 /* not more than 512 bits can be G()-ed */
186 c.len = sizeof(buf);
187 }
188
189 /* use the keyed hasher, but use an empty key to use SHA1 IV */
190 this->keyed_prf->set_key(this->keyed_prf, chunk_empty);
191 this->keyed_prf->get_bytes(this->keyed_prf, c, res);
192 }
193
194 METHOD(prf_t, destroy, void,
195 private_fips_prf_t *this)
196 {
197 this->keyed_prf->destroy(this->keyed_prf);
198 free(this->key);
199 free(this);
200 }
201
202 /*
203 * Described in header.
204 */
205 fips_prf_t *fips_prf_create(pseudo_random_function_t algo)
206 {
207 private_fips_prf_t *this;
208
209 INIT(this,
210 .public = {
211 .prf_interface = {
212 .get_bytes = _get_bytes,
213 .allocate_bytes = _allocate_bytes,
214 .get_block_size = _get_block_size,
215 .get_key_size = _get_key_size,
216 .set_key = _set_key,
217 .destroy = _destroy,
218 },
219 },
220 );
221
222 switch (algo)
223 {
224 case PRF_FIPS_SHA1_160:
225 {
226 this->g = g_sha1;
227 this->b = 20;
228 this->keyed_prf = lib->crypto->create_prf(lib->crypto, PRF_KEYED_SHA1);
229 if (this->keyed_prf == NULL)
230 {
231 free(this);
232 return NULL;
233 }
234 break;
235 }
236 case PRF_FIPS_DES:
237 /* not implemented yet */
238 default:
239 free(this);
240 return NULL;
241 }
242 this->key = malloc(this->b);
243
244 return &this->public;
245 }
246