blob: b20bfe62bc179cb35eadca31c6ffc2d0c7ddf075 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05002 Copyright (C) 2006-2013 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software: you can redistribute it and/or modify
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05005 it under the terms of the GNU Lesser General Public License as published by
Alexandre Lision51140e12013-12-02 10:54:09 -05006 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 * @author Erik Eliasson <eliasson@it.kth.se>
20 * Johan Bilien <jobi@via.ecp.fr>
21 * Werner Dittmann <Werner.Dittmann@t-online.de>
22 */
23
24#include <gcrypt.h>
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050025#include <crypto/sha256.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050026
27void sha256(unsigned char* data, unsigned int dataLength,
28 unsigned char* mac)
29{
30 gcry_md_hash_buffer(GCRY_MD_SHA256, mac, data, dataLength);
31}
32
33void sha256(unsigned char* dataChunks[],
34 unsigned int dataChunkLength[],
35 unsigned char* mac)
36{
37 gcry_md_hd_t hd;
38 gcry_error_t err = 0;
39
40 err = gcry_md_open(&hd, GCRY_MD_SHA256, 0);
41 while (*dataChunks) {
42 gcry_md_write (hd, *dataChunks, (uint32_t)(*dataChunkLength));
43 dataChunks++;
44 dataChunkLength++;
45 }
46 uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA256);
47 memcpy(mac, p, SHA256_DIGEST_LENGTH);
48 gcry_md_close (hd);
49}
50
51void* createSha256Context()
52{
53 gcry_error_t err = 0;
54 gcry_md_hd_t hd;
55
56 err = gcry_md_open(&hd, GCRY_MD_SHA256, 0);
57 return (void*)hd;
58}
59
60void closeSha256Context(void* ctx, unsigned char* digest)
61{
62 gcry_md_hd_t hd = (gcry_md_hd_t)ctx;
63
64 if (digest != NULL) {
65 uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA256);
66 memcpy(digest, p, SHA256_DIGEST_LENGTH);
67 }
68 gcry_md_close (hd);
69}
70
71void sha256Ctx(void* ctx, unsigned char* data,
72 unsigned int dataLength)
73{
74 gcry_md_hd_t hd = (gcry_md_hd_t)ctx;
75
76 gcry_md_write (hd, data, dataLength);
77}
78
79void sha256Ctx(void* ctx, unsigned char* dataChunks[],
80 unsigned int dataChunkLength[])
81{
82 gcry_md_hd_t hd = (gcry_md_hd_t)ctx;
83
84 while (*dataChunks) {
85 gcry_md_write (hd, *dataChunks, (uint32_t)(*dataChunkLength));
86 dataChunks++;
87 dataChunkLength++;
88 }
89}