blob: 88d33a1cf6ddad6a63db0745867add617f02b2ea [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001/*
2 Copyright (C) 2005, 2004, 2010, Erik Eliasson, Johan Bilien, Werner Dittmann
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 * In addition, as a special exception, the copyright holders give
19 * permission to link the code of portions of this program with the
20 * OpenSSL library under certain conditions as described in each
21 * individual source file, and distribute linked combinations
22 * including the two.
23 * You must obey the GNU General Public License in all respects
24 * for all of the code used other than OpenSSL. If you modify
25 * file(s) with this exception, you may extend this exception to your
26 * version of the file(s), but you are not obligated to do so. If you
27 * do not wish to do so, delete this exception statement from your
28 * version. If you delete this exception statement from all source
29 * files in the program, then also delete it here.
30 */
31
32/*
33 * Authors: Erik Eliasson <eliasson@it.kth.se>
34 * Johan Bilien <jobi@via.ecp.fr>
35 * Werner Dittmann
36 */
37
38#include <stdint.h>
39#include <openssl/hmac.h>
Alexandre Lisionddd731e2014-01-31 11:50:08 -050040#include <crypto/hmac.h>
Emeric Vigier2f625822012-08-06 11:09:52 -040041
42void hmac_sha1(uint8_t * key, int32_t key_length,
43 const uint8_t* data, uint32_t data_length,
44 uint8_t* mac, int32_t* mac_length )
45{
46 HMAC(EVP_sha1(), key, key_length,
47 data, data_length, mac,
48 reinterpret_cast<uint32_t*>(mac_length) );
49}
50
51void hmac_sha1( uint8_t* key, int32_t key_length,
52 const uint8_t* data_chunks[],
53 uint32_t data_chunck_length[],
54 uint8_t* mac, int32_t* mac_length ) {
55 HMAC_CTX ctx;
Alexandre Lisionddd731e2014-01-31 11:50:08 -050056 HMAC_CTX_init(&ctx);
57 HMAC_Init_ex(&ctx, key, key_length, EVP_sha1(), NULL);
58 while (*data_chunks) {
59 HMAC_Update(&ctx, *data_chunks, *data_chunck_length);
Emeric Vigier2f625822012-08-06 11:09:52 -040060 data_chunks ++;
61 data_chunck_length ++;
62 }
Alexandre Lisionddd731e2014-01-31 11:50:08 -050063 HMAC_Final(&ctx, mac, reinterpret_cast<uint32_t*>(mac_length));
64 HMAC_CTX_cleanup(&ctx);
Emeric Vigier2f625822012-08-06 11:09:52 -040065}
66
67void* createSha1HmacContext(uint8_t* key, int32_t key_length)
68{
69 HMAC_CTX* ctx = (HMAC_CTX*)malloc(sizeof(HMAC_CTX));
70
71 HMAC_CTX_init(ctx);
72 HMAC_Init_ex(ctx, key, key_length, EVP_sha1(), NULL);
73 return ctx;
74}
75
76void hmacSha1Ctx(void* ctx, const uint8_t* data, uint32_t data_length,
77 uint8_t* mac, int32_t* mac_length)
78{
79 HMAC_CTX* pctx = (HMAC_CTX*)ctx;
Alexandre Lisionddd731e2014-01-31 11:50:08 -050080
Emeric Vigier2f625822012-08-06 11:09:52 -040081 HMAC_Init_ex(pctx, NULL, 0, NULL, NULL );
82 HMAC_Update(pctx, data, data_length );
83 HMAC_Final(pctx, mac, reinterpret_cast<uint32_t*>(mac_length) );
84}
85
86void hmacSha1Ctx(void* ctx, const uint8_t* data[], uint32_t data_length[],
87 uint8_t* mac, int32_t* mac_length )
88{
89 HMAC_CTX* pctx = (HMAC_CTX*)ctx;
Alexandre Lisionddd731e2014-01-31 11:50:08 -050090
Emeric Vigier2f625822012-08-06 11:09:52 -040091 HMAC_Init_ex(pctx, NULL, 0, NULL, NULL );
92 while (*data) {
93 HMAC_Update(pctx, *data, *data_length);
94 data++;
95 data_length++;
96 }
97 HMAC_Final(pctx, mac, reinterpret_cast<uint32_t*>(mac_length) );
98}
99
100void freeSha1HmacContext(void* ctx)
101{
102 if (ctx) {
103 HMAC_CTX_cleanup((HMAC_CTX*)ctx);
104 free(ctx);
105 }
106}