Refactored OpenSSL based HMAC implementation
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_hmac.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
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 /*
17 * Copyright (C) 2012 Aleksandr Grinberg
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this software and associated documentation files (the "Software"), to deal
21 * in the Software without restriction, including without limitation the rights
22 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23 * copies of the Software, and to permit persons to whom the Software is
24 * furnished to do so, subject to the following conditions:
25 *
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 * THE SOFTWARE.
36 */
37
38 #include <openssl/evp.h>
39 #include <openssl/hmac.h>
40
41 #include "openssl_hmac.h"
42
43 typedef struct private_openssl_hmac_t private_openssl_hmac_t;
44
45 /**
46 * Private data of a openssl_hmac_t object.
47 */
48 struct private_openssl_hmac_t {
49
50 /**
51 * Public interface
52 */
53 openssl_hmac_t public;
54
55 /**
56 * Hasher to use
57 */
58 const EVP_MD *hasher;
59
60 /**
61 * Current HMAC context
62 */
63 HMAC_CTX hmac;
64
65 /**
66 * Key
67 */
68 chunk_t key;
69 };
70
71 /**
72 * Resets HMAC context
73 */
74 static void reset(private_openssl_hmac_t *this)
75 {
76 HMAC_Init_ex(&this->hmac, this->key.ptr, this->key.len, this->hasher, NULL);
77 }
78
79 METHOD(openssl_hmac_t, get_mac, void,
80 private_openssl_hmac_t *this, chunk_t data, u_int8_t *out)
81 {
82 if (out == NULL)
83 {
84 HMAC_Update(&this->hmac, data.ptr, data.len);
85 }
86 else
87 {
88 HMAC_Update(&this->hmac, data.ptr, data.len);
89 HMAC_Final(&this->hmac, out, NULL);
90 reset(this);
91 }
92 }
93
94 METHOD(openssl_hmac_t, allocate_mac, void,
95 private_openssl_hmac_t *this, chunk_t data, chunk_t *out)
96 {
97 if (out == NULL)
98 {
99 get_mac(this, data, NULL);
100 }
101 else
102 {
103 *out = chunk_alloc(EVP_MD_size(this->hasher));
104 get_mac(this, data, out->ptr);
105 }
106 }
107
108 METHOD(openssl_hmac_t, get_block_size, size_t,
109 private_openssl_hmac_t *this)
110 {
111 return EVP_MD_size(this->hasher);
112 }
113
114 METHOD(openssl_hmac_t, set_key, void,
115 private_openssl_hmac_t *this, chunk_t key)
116 {
117 chunk_clear(&this->key);
118 this->key = chunk_clone(key);
119 reset(this);
120 }
121
122 METHOD(openssl_hmac_t, destroy, void,
123 private_openssl_hmac_t *this)
124 {
125 HMAC_CTX_cleanup(&this->hmac);
126 chunk_clear(&this->key);
127 free(this);
128 }
129
130 /*
131 * Described in header
132 */
133 openssl_hmac_t *openssl_hmac_create(hash_algorithm_t algo)
134 {
135 private_openssl_hmac_t *this;
136
137 INIT(this,
138 .public = {
139 .get_mac = _get_mac,
140 .allocate_mac = _allocate_mac,
141 .get_block_size = _get_block_size,
142 .set_key = _set_key,
143 .destroy = _destroy,
144 },
145 );
146
147 switch (algo)
148 {
149 case HASH_MD5:
150 this->hasher = EVP_get_digestbyname("md5");
151 break;
152 case HASH_SHA1:
153 this->hasher = EVP_get_digestbyname("sha1");
154 break;
155 case HASH_SHA256:
156 this->hasher = EVP_get_digestbyname("sha256");
157 break;
158 case HASH_SHA384:
159 this->hasher = EVP_get_digestbyname("sha384");
160 break;
161 case HASH_SHA512:
162 this->hasher = EVP_get_digestbyname("sha512");
163 break;
164 default:
165 break;
166 }
167
168 if (!this->hasher)
169 {
170 free(this);
171 return NULL;
172 }
173
174 HMAC_CTX_init(&this->hmac);
175
176 return &this->public;
177 }