ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Poly1305.cpp
1 /*
2  * Copyright (C) 2015 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "Poly1305.h"
24 #include "Crypto.h"
25 #include "utility/EndianUtil.h"
26 #include "utility/LimbUtil.h"
27 #include <string.h>
28 
62 // Limb array with enough space for 130 bits.
63 #define NUM_LIMBS_130BIT (NUM_LIMBS_128BIT + 1)
64 
65 // Endian helper macros for limbs and arrays of limbs.
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 #endif
76 #if defined(CRYPTO_LITTLE_ENDIAN)
77 #define littleToHost(r,size) do { ; } while (0)
78 #else
79 #define littleToHost(r,size) \
80  do { \
81  for (uint8_t i = 0; i < (size); ++i) \
82  (r)[i] = lelimbtoh((r)[i]); \
83  } while (0)
84 #endif
85 
90 {
91  state.chunkSize = 0;
92 }
93 
99 {
100  clean(state);
101 }
102 
110 void Poly1305::reset(const void *key)
111 {
112  // Copy the key into place and clear the bits we don't need.
113  uint8_t *r = (uint8_t *)state.r;
114  memcpy(r, key, 16);
115  r[3] &= 0x0F;
116  r[4] &= 0xFC;
117  r[7] &= 0x0F;
118  r[8] &= 0xFC;
119  r[11] &= 0x0F;
120  r[12] &= 0xFC;
121  r[15] &= 0x0F;
122 
123  // Convert into little-endian if necessary.
124  littleToHost(state.r, NUM_LIMBS_128BIT);
125 
126  // Reset the hashing process.
127  state.chunkSize = 0;
128  memset(state.h, 0, sizeof(state.h));
129 }
130 
142 void Poly1305::update(const void *data, size_t len)
143 {
144  // Break the input up into 128-bit chunks and process each in turn.
145  const uint8_t *d = (const uint8_t *)data;
146  while (len > 0) {
147  uint8_t size = 16 - state.chunkSize;
148  if (size > len)
149  size = len;
150  memcpy(((uint8_t *)state.c) + state.chunkSize, d, size);
151  state.chunkSize += size;
152  len -= size;
153  d += size;
154  if (state.chunkSize == 16) {
155  littleToHost(state.c, NUM_LIMBS_128BIT);
156  state.c[NUM_LIMBS_128BIT] = 1;
157  processChunk();
158  state.chunkSize = 0;
159  }
160  }
161 }
162 
179 void Poly1305::finalize(const void *nonce, void *token, size_t len)
180 {
181  dlimb_t carry;
182  uint8_t i;
183 
184  // Pad and flush the final chunk.
185  if (state.chunkSize > 0) {
186  uint8_t *c = (uint8_t *)state.c;
187  c[state.chunkSize] = 1;
188  memset(c + state.chunkSize + 1, 0, 16 - state.chunkSize - 1);
189  littleToHost(state.c, NUM_LIMBS_128BIT);
190  state.c[NUM_LIMBS_128BIT] = 0;
191  processChunk();
192  }
193 
194  // At this point, processChunk() has left h as a partially reduced
195  // result that is less than (2^130 - 5) * 6. Perform one more
196  // reduction and a trial subtraction to produce the final result.
197 
198  // Multiply the high bits of h by 5 and add them to the 130 low bits.
199  carry = (dlimb_t)((state.h[NUM_LIMBS_128BIT] >> 2) +
200  (state.h[NUM_LIMBS_128BIT] & ~((limb_t)3)));
201  state.h[NUM_LIMBS_128BIT] &= 0x0003;
202  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
203  carry += state.h[i];
204  state.h[i] = (limb_t)carry;
205  carry >>= LIMB_BITS;
206  }
207  state.h[i] += (limb_t)carry;
208 
209  // Subtract (2^130 - 5) from h by computing t = h + 5 - 2^130.
210  // The "minus 2^130" step is implicit.
211  carry = 5;
212  for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
213  carry += state.h[i];
214  state.t[i] = (limb_t)carry;
215  carry >>= LIMB_BITS;
216  }
217 
218  // Borrow occurs if bit 2^130 of the previous t result is zero.
219  // Carefully turn this into a selection mask so we can select either
220  // h or t as the final result. We don't care about the highest word
221  // of the result because we are about to drop it in the next step.
222  // We have to do it this way to avoid giving away any information
223  // about the value of h in the instruction timing.
224  limb_t mask = (~((state.t[NUM_LIMBS_128BIT] >> 2) & 1)) + 1;
225  limb_t nmask = ~mask;
226  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
227  state.h[i] = (state.h[i] & nmask) | (state.t[i] & mask);
228  }
229 
230  // Add the encrypted nonce and format the final hash.
231  memcpy(state.c, nonce, 16);
232  littleToHost(state.c, NUM_LIMBS_128BIT);
233  carry = 0;
234  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
235  carry += state.h[i];
236  carry += state.c[i];
237  state.h[i] = htolelimb((limb_t)carry);
238  carry >>= LIMB_BITS;
239  }
240  if (len > 16)
241  len = 16;
242  memcpy(token, state.h, len);
243 }
244 
251 {
252  if (state.chunkSize != 0) {
253  memset(((uint8_t *)state.c) + state.chunkSize, 0, 16 - state.chunkSize);
254  littleToHost(state.c, NUM_LIMBS_128BIT);
255  state.c[NUM_LIMBS_128BIT] = 1;
256  processChunk();
257  state.chunkSize = 0;
258  }
259 }
260 
265 {
266  clean(state);
267 }
268 
272 void Poly1305::processChunk()
273 {
274  // Compute h = ((h + c) * r) mod (2^130 - 5).
275 
276  // Start with h += c. We assume that h is less than (2^130 - 5) * 6
277  // and that c is less than 2^129, so the result will be less than 2^133.
278  dlimb_t carry = 0;
279  uint8_t i, j;
280  for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
281  carry += state.h[i];
282  carry += state.c[i];
283  state.h[i] = (limb_t)carry;
284  carry >>= LIMB_BITS;
285  }
286 
287  // Multiply h by r. We know that r is less than 2^124 because the
288  // top 4 bits were AND-ed off by reset(). That makes h * r less
289  // than 2^257. Which is less than the (2^130 - 6)^2 we want for
290  // the modulo reduction step that follows.
291  carry = 0;
292  limb_t word = state.r[0];
293  for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
294  carry += ((dlimb_t)(state.h[i])) * word;
295  state.t[i] = (limb_t)carry;
296  carry >>= LIMB_BITS;
297  }
298  state.t[NUM_LIMBS_130BIT] = (limb_t)carry;
299  for (i = 1; i < NUM_LIMBS_128BIT; ++i) {
300  word = state.r[i];
301  carry = 0;
302  for (j = 0; j < NUM_LIMBS_130BIT; ++j) {
303  carry += ((dlimb_t)(state.h[j])) * word;
304  carry += state.t[i + j];
305  state.t[i + j] = (limb_t)carry;
306  carry >>= LIMB_BITS;
307  }
308  state.t[i + NUM_LIMBS_130BIT] = (limb_t)carry;
309  }
310 
311  // Reduce h * r modulo (2^130 - 5) by multiplying the high 130 bits by 5
312  // and adding them to the low 130 bits. See the explaination in the
313  // comments for Curve25519::reduce() for a description of how this works.
314  carry = ((dlimb_t)(state.t[NUM_LIMBS_128BIT] >> 2)) +
315  (state.t[NUM_LIMBS_128BIT] & ~((limb_t)3));
316  state.t[NUM_LIMBS_128BIT] &= 0x0003;
317  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
318  // Shift the next word of t up by (LIMB_BITS - 2) bits and then
319  // multiply it by 5. Breaking it down, we can add the results
320  // of shifting up by LIMB_BITS and shifting up by (LIMB_BITS - 2).
321  // The main wrinkle here is that this can result in an intermediate
322  // carry that is (LIMB_BITS * 2 + 1) bits in size which doesn't
323  // fit within a dlimb_t variable. However, we can defer adding
324  // (word << LIMB_BITS) until after the "carry >>= LIMB_BITS" step
325  // because it won't affect the low bits of the carry.
326  word = state.t[i + NUM_LIMBS_130BIT];
327  carry += ((dlimb_t)word) << (LIMB_BITS - 2);
328  carry += state.t[i];
329  state.h[i] = (limb_t)carry;
330  carry >>= LIMB_BITS;
331  carry += word;
332  }
333  state.h[i] = (limb_t)(carry + state.t[NUM_LIMBS_128BIT]);
334 
335  // At this point, h is either the answer of reducing modulo (2^130 - 5)
336  // or it is at most 5 subtractions away from the answer we want.
337  // Leave it as-is for now with h less than (2^130 - 5) * 6. It is
338  // still within a range where the next h * r step will not overflow.
339 }
void finalize(const void *nonce, void *token, size_t len)
Finalizes the authentication process and returns the token.
Definition: Poly1305.cpp:179
void reset(const void *key)
Resets the Poly1305 message authenticator for a new session.
Definition: Poly1305.cpp:110
void update(const void *data, size_t len)
Updates the message authenticator with more data.
Definition: Poly1305.cpp:142
void pad()
Pads the input stream with zero bytes to a multiple of 16.
Definition: Poly1305.cpp:250
~Poly1305()
Destroys this Poly1305 message authenticator after clearing all sensitive information.
Definition: Poly1305.cpp:98
Poly1305()
Constructs a new Poly1305 message authenticator.
Definition: Poly1305.cpp:89
void clear()
Clears the authenticator's state, removing all sensitive data.
Definition: Poly1305.cpp:264