summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/sha3/sha3_hasher.c
blob: 0fbcbb8dcb0e52d60b11b8477d1996abf6797dad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2015-2016 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <string.h>

#include "sha3_hasher.h"
#include "sha3_keccak.h"

typedef struct private_sha3_hasher_t private_sha3_hasher_t;

/**
 * Private data structure with hashing context for SHA-3
 */
struct private_sha3_hasher_t {

	/**
	 * Public interface for this hasher.
	 */
	sha3_hasher_t public;

	/**
	 * SHA-3 algorithm to be used
	 */
	hash_algorithm_t algorithm;

	/**
	 * SHA-3 Keccak state
	 */
	sha3_keccak_t *keccak;

};

METHOD(hasher_t, reset, bool,
	private_sha3_hasher_t *this)
{
	this->keccak->reset(this->keccak);
	return TRUE;
}

METHOD(hasher_t, get_hash_size, size_t,
	private_sha3_hasher_t *this)
{
	switch (this->algorithm)
	{
		case HASH_SHA3_224:
			return HASH_SIZE_SHA224;
		case HASH_SHA3_256:
			return HASH_SIZE_SHA256;
		case HASH_SHA3_384:
			return HASH_SIZE_SHA384;
		case HASH_SHA3_512:
			return HASH_SIZE_SHA512;
		default:
			return 0;
	}
}


METHOD(hasher_t, get_hash, bool,
	private_sha3_hasher_t *this, chunk_t chunk, uint8_t *buffer)
{
	this->keccak->absorb(this->keccak, chunk);

	if (buffer != NULL)
	{
		this->keccak->finalize(this->keccak);
		this->keccak->squeeze(this->keccak, get_hash_size(this), buffer);
		this->keccak->reset(this->keccak);
	}
	return TRUE;
}

METHOD(hasher_t, allocate_hash, bool,
	private_sha3_hasher_t *this, chunk_t chunk, chunk_t *hash)
{
	chunk_t allocated_hash;

	this->keccak->absorb(this->keccak, chunk);

	if (hash != NULL)
	{
		this->keccak->finalize(this->keccak);
		allocated_hash = chunk_alloc(get_hash_size(this));
		this->keccak->squeeze(this->keccak, allocated_hash.len,
											allocated_hash.ptr);
		this->keccak->reset(this->keccak);
		*hash = allocated_hash;
	}
	return TRUE;
}

METHOD(hasher_t, destroy, void,
	private_sha3_hasher_t *this)
{
	this->keccak->destroy(this->keccak);
	free(this);
}

/*
 * Described in header.
 */
sha3_hasher_t *sha3_hasher_create(hash_algorithm_t algorithm)
{
	private_sha3_hasher_t *this;

	switch (algorithm)
	{
		case HASH_SHA3_224:
		case HASH_SHA3_256:
		case HASH_SHA3_384:
		case HASH_SHA3_512:
			break;
		default:
			return NULL;
	}

	INIT(this,
		.public = {
			.hasher_interface = {
				.reset = _reset,
				.get_hash_size = _get_hash_size,
				.get_hash = _get_hash,
				.allocate_hash = _allocate_hash,
				.destroy = _destroy,
			},
		},
		.algorithm = algorithm,
	);

	this->keccak = sha3_keccak_create(2*get_hash_size(this), 0x06);
	if (!this->keccak)
	{
		free(this);
		return NULL;
	}

	return &this->public;
}