blob: 593b5e578e800a95075bb81bed9d40b95424762e [file] [log] [blame]
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05001/*
2 Copyright (C) 2012 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 * @author: Werner Dittmann
34 */
35
36#include <zrtp/crypto/sha2.h>
37#include <zrtp/crypto/sha256.h>
38
39void sha256(unsigned char *data, unsigned int dataLength, unsigned char *digest )
40{
41 sha256_ctx ctx;
42
43 sha256_begin(&ctx);
44 sha256_hash(data, dataLength, &ctx);
45 sha256_end(digest, &ctx);
46}
47
48void sha256(unsigned char *dataChunks[], unsigned int dataChunckLength[], unsigned char *digest)
49{
50 sha256_ctx ctx;
51
52 sha256_begin(&ctx);
53 while(*dataChunks) {
54 sha256_hash(*dataChunks, *dataChunckLength, &ctx);
55 dataChunks++;
56 dataChunckLength++;
57 }
58 sha256_end(digest, &ctx);
59}
60
61void* createSha256Context()
62{
63 sha256_ctx *ctx = reinterpret_cast<sha256_ctx*>(malloc(sizeof(sha256_ctx)));
64 sha256_begin(ctx);
65 return (void*)ctx;
66}
67
68void closeSha256Context(void* ctx, unsigned char* digest)
69{
70 sha256_ctx* hd = reinterpret_cast<sha256_ctx*>(ctx);
71
72 if (digest != NULL) {
73 sha256_end(digest, hd);
74 }
75 free(hd);
76}
77
78void sha256Ctx(void* ctx, unsigned char* data, unsigned int dataLength)
79{
80 sha256_ctx* hd = reinterpret_cast<sha256_ctx*>(ctx);
81
82 sha256_hash(data, dataLength, hd);
83}
84
85void sha256Ctx(void* ctx, unsigned char* dataChunks[], unsigned int dataChunkLength[])
86{
87 sha256_ctx* hd = reinterpret_cast<sha256_ctx*>(ctx);
88
89 while (*dataChunks) {
90 sha256_hash(*dataChunks, *dataChunkLength, hd);
91 dataChunks++;
92 dataChunkLength++;
93 }
94}