44cb46b4d961f2ff61b57f8ebef28cbbda942499
[strongswan.git] / src / libstrongswan / plugins / hmac / hmac.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "hmac.h"
19
20 #include <crypto/mac.h>
21 #include <crypto/prfs/mac_prf.h>
22 #include <crypto/signers/mac_signer.h>
23
24 typedef struct private_mac_t private_mac_t;
25
26 /**
27 * Private data of a mac_t object.
28 *
29 * The variable names are the same as in the RFC.
30 */
31 struct private_mac_t {
32
33 /**
34 * Implements mac_t interface
35 */
36 mac_t public;
37
38 /**
39 * Block size, as in RFC.
40 */
41 u_int8_t b;
42
43 /**
44 * Hash function.
45 */
46 hasher_t *h;
47
48 /**
49 * Previously xor'ed key using opad.
50 */
51 chunk_t opaded_key;
52
53 /**
54 * Previously xor'ed key using ipad.
55 */
56 chunk_t ipaded_key;
57 };
58
59 METHOD(mac_t, get_mac, bool,
60 private_mac_t *this, chunk_t data, u_int8_t *out)
61 {
62 /* H(K XOR opad, H(K XOR ipad, text))
63 *
64 * if out is NULL, we append text to the inner hash.
65 * else, we complete the inner and do the outer.
66 *
67 */
68
69 u_int8_t buffer[this->h->get_hash_size(this->h)];
70 chunk_t inner;
71
72 if (out == NULL)
73 {
74 /* append data to inner */
75 return this->h->get_hash(this->h, data, NULL);
76 }
77
78 /* append and do outer hash */
79 inner.ptr = buffer;
80 inner.len = this->h->get_hash_size(this->h);
81
82 /* complete inner, do outer and reinit for next call */
83 return this->h->get_hash(this->h, data, buffer) &&
84 this->h->get_hash(this->h, this->opaded_key, NULL) &&
85 this->h->get_hash(this->h, inner, out) &&
86 this->h->get_hash(this->h, this->ipaded_key, NULL);
87 }
88
89 METHOD(mac_t, get_mac_size, size_t,
90 private_mac_t *this)
91 {
92 return this->h->get_hash_size(this->h);
93 }
94
95 METHOD(mac_t, set_key, bool,
96 private_mac_t *this, chunk_t key)
97 {
98 int i;
99 u_int8_t buffer[this->b];
100
101 memset(buffer, 0, this->b);
102
103 if (key.len > this->b)
104 {
105 /* if key is too long, it will be hashed */
106 if (!this->h->get_hash(this->h, key, buffer))
107 {
108 return FALSE;
109 }
110 }
111 else
112 {
113 /* if not, just copy it in our pre-padded k */
114 memcpy(buffer, key.ptr, key.len);
115 }
116
117 /* apply ipad and opad to key */
118 for (i = 0; i < this->b; i++)
119 {
120 this->ipaded_key.ptr[i] = buffer[i] ^ 0x36;
121 this->opaded_key.ptr[i] = buffer[i] ^ 0x5C;
122 }
123
124 /* begin hashing of inner pad */
125 return this->h->reset(this->h) &&
126 this->h->get_hash(this->h, this->ipaded_key, NULL);
127 }
128
129 METHOD(mac_t, destroy, void,
130 private_mac_t *this)
131 {
132 this->h->destroy(this->h);
133 chunk_clear(&this->opaded_key);
134 chunk_clear(&this->ipaded_key);
135 free(this);
136 }
137
138 /*
139 * Creates an mac_t object
140 */
141 static mac_t *hmac_create(hash_algorithm_t hash_algorithm)
142 {
143 private_mac_t *this;
144
145 INIT(this,
146 .public = {
147 .get_mac = _get_mac,
148 .get_mac_size = _get_mac_size,
149 .set_key = _set_key,
150 .destroy = _destroy,
151 },
152 );
153
154 /* set b, according to hasher */
155 switch (hash_algorithm)
156 {
157 case HASH_SHA1:
158 case HASH_MD5:
159 case HASH_SHA256:
160 this->b = 64;
161 break;
162 case HASH_SHA384:
163 case HASH_SHA512:
164 this->b = 128;
165 break;
166 default:
167 free(this);
168 return NULL;
169 }
170
171 this->h = lib->crypto->create_hasher(lib->crypto, hash_algorithm);
172 if (this->h == NULL)
173 {
174 free(this);
175 return NULL;
176 }
177
178 /* build ipad and opad */
179 this->opaded_key.ptr = malloc(this->b);
180 this->opaded_key.len = this->b;
181
182 this->ipaded_key.ptr = malloc(this->b);
183 this->ipaded_key.len = this->b;
184
185 return &this->public;
186 }
187
188 /*
189 * Described in header
190 */
191 prf_t *hmac_prf_create(pseudo_random_function_t algo)
192 {
193 mac_t *hmac;
194
195 hmac = hmac_create(hasher_algorithm_from_prf(algo));
196 if (hmac)
197 {
198 return mac_prf_create(hmac);
199 }
200 return NULL;
201 }
202
203 /*
204 * Described in header
205 */
206 signer_t *hmac_signer_create(integrity_algorithm_t algo)
207 {
208 mac_t *hmac;
209 size_t trunc;
210
211 hmac = hmac_create(hasher_algorithm_from_integrity(algo, &trunc));
212 if (hmac)
213 {
214 return mac_signer_create(hmac, trunc);
215 }
216 return NULL;
217 }