25 #include "utility/EndianUtil.h"
26 #include "utility/LimbUtil.h"
63 #define NUM_LIMBS_130BIT (NUM_LIMBS_128BIT + 1)
66 #if BIGNUMBER_LIMB_8BIT
67 #define lelimbtoh(x) (x)
68 #define htolelimb(x) (x)
69 #elif BIGNUMBER_LIMB_16BIT
70 #define lelimbtoh(x) (le16toh((x)))
71 #define htolelimb(x) (htole16((x)))
72 #elif BIGNUMBER_LIMB_32BIT
73 #define lelimbtoh(x) (le32toh((x)))
74 #define htolelimb(x) (htole32((x)))
75 #elif BIGNUMBER_LIMB_64BIT
76 #define lelimbtoh(x) (le64toh((x)))
77 #define htolelimb(x) (htole64((x)))
79 #if defined(CRYPTO_LITTLE_ENDIAN)
80 #define littleToHost(r,size) do { ; } while (0)
82 #define littleToHost(r,size) \
84 for (uint8_t i = 0; i < (size); ++i) \
85 (r)[i] = lelimbtoh((r)[i]); \
116 uint8_t *r = (uint8_t *)state.r;
127 littleToHost(state.r, NUM_LIMBS_128BIT);
131 memset(state.h, 0,
sizeof(state.h));
148 const uint8_t *d = (
const uint8_t *)data;
150 uint8_t size = 16 - state.chunkSize;
153 memcpy(((uint8_t *)state.c) + state.chunkSize, d, size);
154 state.chunkSize += size;
157 if (state.chunkSize == 16) {
158 littleToHost(state.c, NUM_LIMBS_128BIT);
159 state.c[NUM_LIMBS_128BIT] = 1;
186 limb_t t[NUM_LIMBS_256BIT + 1];
189 if (state.chunkSize > 0) {
190 uint8_t *c = (uint8_t *)state.c;
191 c[state.chunkSize] = 1;
192 memset(c + state.chunkSize + 1, 0, 16 - state.chunkSize - 1);
193 littleToHost(state.c, NUM_LIMBS_128BIT);
194 state.c[NUM_LIMBS_128BIT] = 0;
203 carry = (dlimb_t)((state.h[NUM_LIMBS_128BIT] >> 2) +
204 (state.h[NUM_LIMBS_128BIT] & ~((limb_t)3)));
205 state.h[NUM_LIMBS_128BIT] &= 0x0003;
206 for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
208 state.h[i] = (limb_t)carry;
211 state.h[i] += (limb_t)carry;
216 for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
218 t[i] = (limb_t)carry;
228 limb_t mask = (~((t[NUM_LIMBS_128BIT] >> 2) & 1)) + 1;
229 limb_t nmask = ~mask;
230 for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
231 state.h[i] = (state.h[i] & nmask) | (t[i] & mask);
235 memcpy(state.c, nonce, 16);
236 littleToHost(state.c, NUM_LIMBS_128BIT);
238 for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
241 state.h[i] = htolelimb((limb_t)carry);
246 memcpy(token, state.h, len);
256 if (state.chunkSize != 0) {
257 memset(((uint8_t *)state.c) + state.chunkSize, 0, 16 - state.chunkSize);
258 littleToHost(state.c, NUM_LIMBS_128BIT);
259 state.c[NUM_LIMBS_128BIT] = 1;
276 void Poly1305::processChunk()
278 limb_t t[NUM_LIMBS_256BIT + 1];
286 for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
289 state.h[i] = (limb_t)carry;
298 limb_t word = state.r[0];
299 for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
300 carry += ((dlimb_t)(state.h[i])) * word;
301 t[i] = (limb_t)carry;
304 t[NUM_LIMBS_130BIT] = (limb_t)carry;
305 for (i = 1; i < NUM_LIMBS_128BIT; ++i) {
308 for (j = 0; j < NUM_LIMBS_130BIT; ++j) {
309 carry += ((dlimb_t)(state.h[j])) * word;
311 t[i + j] = (limb_t)carry;
314 t[i + NUM_LIMBS_130BIT] = (limb_t)carry;
320 carry = ((dlimb_t)(t[NUM_LIMBS_128BIT] >> 2)) +
321 (t[NUM_LIMBS_128BIT] & ~((limb_t)3));
322 t[NUM_LIMBS_128BIT] &= 0x0003;
323 for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
332 word = t[i + NUM_LIMBS_130BIT];
333 carry += ((dlimb_t)word) << (LIMB_BITS - 2);
335 state.h[i] = (limb_t)carry;
339 state.h[i] = (limb_t)(carry + t[NUM_LIMBS_128BIT]);
void reset(const void *key)
Resets the Poly1305 message authenticator for a new session.
Poly1305()
Constructs a new Poly1305 message authenticator.
void finalize(const void *nonce, void *token, size_t len)
Finalizes the authentication process and returns the token.
void pad()
Pads the input stream with zero bytes to a multiple of 16.
~Poly1305()
Destroys this Poly1305 message authenticator after clearing all sensitive information.
void clear()
Clears the authenticator's state, removing all sensitive data.
void update(const void *data, size_t len)
Updates the message authenticator with more data.