forked from rweather/arduinolibs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
GHASH.cpp
153 lines (143 loc) · 4.38 KB
/
GHASH.cpp
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
151
152
153
/*
* Copyright (C) 2015 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "GHASH.h"
#include "GF128.h"
#include "Crypto.h"
#include <string.h>
/**
* \class GHASH GHASH.h <GHASH.h>
* \brief Implementation of the GHASH message authenticator.
*
* GHASH is the message authentication part of Galois Counter Mode (GCM).
*
* \note GHASH is not the same as GMAC. GHASH implements the low level
* hashing primitive that is used by both GCM and GMAC. GMAC can be
* simulated using GCM and an empty plaintext/ciphertext.
*
* References: <a href="http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf">NIST SP 800-38D</a>,
* http://en.wikipedia.org/wiki/Galois/Counter_Mode
*
* \sa GCM
*/
/**
* \brief Constructs a new GHASH message authenticator.
*/
GHASH::GHASH()
{
state.posn = 0;
}
/**
* \brief Destroys this GHASH message authenticator.
*/
GHASH::~GHASH()
{
clean(state);
}
/**
* \brief Resets the GHASH message authenticator for a new session.
*
* \param key Points to the 16 byte authentication key.
*
* \sa update(), finalize()
*/
void GHASH::reset(const void *key)
{
GF128::mulInit(state.H, key);
memset(state.Y, 0, sizeof(state.Y));
state.posn = 0;
}
/**
* \brief Updates the message authenticator with more data.
*
* \param data Data to be hashed.
* \param len Number of bytes of data to be hashed.
*
* If finalize() has already been called, then the behavior of update() will
* be undefined. Call reset() first to start a new authentication process.
*
* \sa pad(), reset(), finalize()
*/
void GHASH::update(const void *data, size_t len)
{
// XOR the input with state.Y in 128-bit chunks and process them.
const uint8_t *d = (const uint8_t *)data;
while (len > 0) {
uint8_t size = 16 - state.posn;
if (size > len)
size = len;
uint8_t *y = ((uint8_t *)state.Y) + state.posn;
for (uint8_t i = 0; i < size; ++i)
y[i] ^= d[i];
state.posn += size;
len -= size;
d += size;
if (state.posn == 16) {
GF128::mul(state.Y, state.H);
state.posn = 0;
}
}
}
/**
* \brief Finalizes the authentication process and returns the token.
*
* \param token The buffer to return the token value in.
* \param len The length of the \a token buffer between 0 and 16.
*
* If \a len is less than 16, then the token value will be truncated to
* the first \a len bytes. If \a len is greater than 16, then the remaining
* bytes will left unchanged.
*
* If finalize() is called again, then the returned \a token value is
* undefined. Call reset() first to start a new authentication process.
*
* \sa reset(), update()
*/
void GHASH::finalize(void *token, size_t len)
{
// Pad with zeroes to a multiple of 16 bytes.
pad();
// The token is the current value of Y.
if (len > 16)
len = 16;
memcpy(token, state.Y, len);
}
/**
* \brief Pads the input stream with zero bytes to a multiple of 16.
*
* \sa update()
*/
void GHASH::pad()
{
if (state.posn != 0) {
// Padding involves XOR'ing the rest of state.Y with zeroes,
// which does nothing. Immediately process the next chunk.
GF128::mul(state.Y, state.H);
state.posn = 0;
}
}
/**
* \brief Clears the authenticator's state, removing all sensitive data.
*/
void GHASH::clear()
{
clean(state);
}