blob: 3a445044f0855fa33f9d12641649adfc08d54060 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
2 Copyright (C) 2006-2007 Werner Dittmann
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * Authors: Erik Eliasson <eliasson@it.kth.se>
20 * Johan Bilien <jobi@via.ecp.fr>
21 */
22
23#include <gcrypt.h>
24#include <libzrtpcpp/crypto/hmac256.h>
25
26void hmac_sha256(uint8_t* key, uint32_t keyLength,
27 uint8_t* data, int32_t dataLength,
28 uint8_t* mac, uint32_t* macLength)
29{
30 gcry_md_hd_t hd;
31 gcry_error_t err = 0;
32
33 err = gcry_md_open(&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
34 gcry_md_setkey(hd, key, keyLength);
35
36 gcry_md_write (hd, data, dataLength);
37
38 uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA256);
39 memcpy(mac, p, SHA256_DIGEST_LENGTH);
40 if (macLength != NULL) {
41 *macLength = SHA256_DIGEST_LENGTH;
42 }
43 gcry_md_close (hd);
44}
45
46void hmac_sha256( uint8_t* key, uint32_t keyLength,
47 uint8_t* dataChunks[],
48 uint32_t dataChunkLength[],
49 uint8_t* mac, uint32_t* macLength )
50{
51 gcry_md_hd_t hd;
52 gcry_error_t err = 0;
53
54 err = gcry_md_open(&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
55 gcry_md_setkey(hd, key, keyLength);
56
57 while (*dataChunks) {
58 gcry_md_write (hd, *dataChunks, (uint32_t)(*dataChunkLength));
59 dataChunks++;
60 dataChunkLength++;
61 }
62 uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA256);
63 memcpy(mac, p, SHA256_DIGEST_LENGTH);
64 if (macLength != NULL) {
65 *macLength = SHA256_DIGEST_LENGTH;
66 }
67 gcry_md_close (hd);
68}