blob: c13f657e4adffe81b6b7d9619fb61ac7e84cb36f [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001/*
2 Copyright (C) 2005, 2004 Erik Eliasson, Johan Bilien
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
19/*
20 * Authors: Erik Eliasson <eliasson@it.kth.se>
21 * Johan Bilien <jobi@via.ecp.fr>
22 */
23#include <gcrypt.h>
24
25#include <ccrtp/crypto/hmac.h>
26#include <stdio.h>
27
28void hmac_sha1(uint8* key, int32 keyLength,
29 const uint8* data, int32 dataLength,
30 uint8* mac, int32* macLength)
31{
32 gcry_md_hd_t hd;
33 gcry_error_t err = 0;
34
35 err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
36 gcry_md_setkey(hd, key, keyLength);
37
38 gcry_md_write (hd, data, dataLength);
39
40 uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA1);
41 memcpy(mac, p, SHA1_DIGEST_LENGTH);
42 if (macLength != NULL) {
43 *macLength = SHA1_DIGEST_LENGTH;
44 }
45 gcry_md_close (hd);
46}
47
48void hmac_sha1( uint8* key, int32 keyLength,
49 const uint8* dataChunks[],
50 uint32 dataChunkLength[],
51 uint8* mac, int32* macLength )
52{
53 gcry_md_hd_t hd;
54 gcry_error_t err = 0;
55
56 err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
57 gcry_md_setkey(hd, key, keyLength);
58
59 while (*dataChunks) {
60 gcry_md_write (hd, *dataChunks, (uint32)(*dataChunkLength));
61 dataChunks++;
62 dataChunkLength++;
63 }
64 uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA1);
65 memcpy(mac, p, SHA1_DIGEST_LENGTH);
66 if (macLength != NULL) {
67 *macLength = SHA1_DIGEST_LENGTH;
68 }
69 gcry_md_close (hd);
70}
71
72void* createSha1HmacContext(uint8_t* key, int32_t key_length)
73{
74 gcry_md_hd_t ctx;
75 gcry_error_t err = 0;
76
77 err = gcry_md_open(&ctx, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
78 gcry_md_setkey(ctx, key, key_length);
79 return ctx;
80}
81
82void hmacSha1Ctx(void* ctx, const uint8_t* data, uint32_t data_length,
83 uint8_t* mac, int32_t* mac_length)
84{
85 gcry_md_hd_t pctx = (gcry_md_hd_t)ctx;
86
87 gcry_md_reset(pctx);
88
89 gcry_md_write (pctx, data, data_length);
90
91 uint8_t* p = gcry_md_read (pctx, GCRY_MD_SHA1);
92 memcpy(mac, p, SHA1_DIGEST_LENGTH);
93 if (mac_length != NULL) {
94 *mac_length = SHA1_DIGEST_LENGTH;
95 }
96}
97
98void hmacSha1Ctx(void* ctx, const uint8_t* data[], uint32_t data_length[],
99 uint8_t* mac, int32_t* mac_length )
100{
101 gcry_md_hd_t pctx = (gcry_md_hd_t)ctx;
102
103 gcry_md_reset (pctx);
104 while (*data) {
105 gcry_md_write (pctx, *data, (uint32)(*data_length));
106 data++;
107 data_length++;
108 }
109 uint8_t* p = gcry_md_read (pctx, GCRY_MD_SHA1);
110 memcpy(mac, p, SHA1_DIGEST_LENGTH);
111 if (mac_length != NULL) {
112 *mac_length = SHA1_DIGEST_LENGTH;
113 }
114}
115
116void freeSha1HmacContext(void* ctx)
117{
118 gcry_md_hd_t pctx = (gcry_md_hd_t)ctx;
119 gcry_md_close (pctx);
120}