ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gcrypthmac.cpp
Go to the documentation of this file.
1 /*
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 <crypto/hmac.h>
26 #include <stdio.h>
27 
28 void hmac_sha1(uint8_t* key, int32_t keyLength,
29  const uint8_t* data, int32_t dataLength,
30  uint8_t* mac, int32_t* macLength)
31 {
32  gcry_md_hd_t hd;
33 
34  gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
35  gcry_md_setkey(hd, key, keyLength);
36 
37  gcry_md_write (hd, data, dataLength);
38 
39  uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA1);
40  memcpy(mac, p, SHA1_DIGEST_LENGTH);
41  if (macLength != NULL) {
42  *macLength = SHA1_DIGEST_LENGTH;
43  }
44  gcry_md_close (hd);
45 }
46 
47 void hmac_sha1( uint8_t* key, int32_t keyLength,
48  const uint8_t* dataChunks[],
49  uint32_t dataChunkLength[],
50  uint8_t* mac, int32_t* macLength )
51 {
52  gcry_md_hd_t hd;
53 
54  gcry_md_open(&hd, GCRY_MD_SHA1, 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_SHA1);
63  memcpy(mac, p, SHA1_DIGEST_LENGTH);
64  if (macLength != NULL) {
65  *macLength = SHA1_DIGEST_LENGTH;
66  }
67  gcry_md_close (hd);
68 }
69 
70 void* createSha1HmacContext(uint8_t* key, int32_t key_length)
71 {
72  gcry_md_hd_t ctx;
73 
74  gcry_md_open(&ctx, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
75  gcry_md_setkey(ctx, key, key_length);
76  return ctx;
77 }
78 
79 void hmacSha1Ctx(void* ctx, const uint8_t* data, uint32_t data_length,
80  uint8_t* mac, int32_t* mac_length)
81 {
82  gcry_md_hd_t pctx = (gcry_md_hd_t)ctx;
83 
84  gcry_md_reset(pctx);
85 
86  gcry_md_write (pctx, data, data_length);
87 
88  uint8_t* p = gcry_md_read (pctx, GCRY_MD_SHA1);
89  memcpy(mac, p, SHA1_DIGEST_LENGTH);
90  if (mac_length != NULL) {
91  *mac_length = SHA1_DIGEST_LENGTH;
92  }
93 }
94 
95 void hmacSha1Ctx(void* ctx, const uint8_t* data[], uint32_t data_length[],
96  uint8_t* mac, int32_t* mac_length )
97 {
98  gcry_md_hd_t pctx = (gcry_md_hd_t)ctx;
99 
100  gcry_md_reset (pctx);
101  while (*data) {
102  gcry_md_write (pctx, *data, (uint32_t)(*data_length));
103  data++;
104  data_length++;
105  }
106  uint8_t* p = gcry_md_read (pctx, GCRY_MD_SHA1);
107  memcpy(mac, p, SHA1_DIGEST_LENGTH);
108  if (mac_length != NULL) {
109  *mac_length = SHA1_DIGEST_LENGTH;
110  }
111 }
112 
113 void freeSha1HmacContext(void* ctx)
114 {
115  gcry_md_hd_t pctx = (gcry_md_hd_t)ctx;
116  gcry_md_close (pctx);
117 }
void hmacSha1Ctx(void *ctx, const uint8_t *data, uint32_t data_length, uint8_t *mac, int32_t *mac_length)
Compute SHA1 HMAC.
Definition: gcrypthmac.cpp:79
void * createSha1HmacContext(uint8_t *key, int32_t key_length)
Create and initialize a SHA1 HMAC context.
Definition: gcrypthmac.cpp:70
void freeSha1HmacContext(void *ctx)
Free SHA1 HMAC context.
Definition: gcrypthmac.cpp:113
Functions to compute SHA1 HAMAC.
void hmac_sha1(uint8_t *key, int32_t keyLength, const uint8_t *data, int32_t dataLength, uint8_t *mac, int32_t *macLength)
Definition: gcrypthmac.cpp:28
#define SHA1_DIGEST_LENGTH
Definition: hmac.h:54