ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends 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  limb_t t[NUM_LIMBS_256BIT + 1];
184 
185  // Pad and flush the final chunk.
186  if (state.chunkSize > 0) {
187  uint8_t *c = (uint8_t *)state.c;
188  c[state.chunkSize] = 1;
189  memset(c + state.chunkSize + 1, 0, 16 - state.chunkSize - 1);
190  littleToHost(state.c, NUM_LIMBS_128BIT);
191  state.c[NUM_LIMBS_128BIT] = 0;
192  processChunk();
193  }
194 
195  // At this point, processChunk() has left h as a partially reduced
196  // result that is less than (2^130 - 5) * 6. Perform one more
197  // reduction and a trial subtraction to produce the final result.
198 
199  // Multiply the high bits of h by 5 and add them to the 130 low bits.
200  carry = (dlimb_t)((state.h[NUM_LIMBS_128BIT] >> 2) +
201  (state.h[NUM_LIMBS_128BIT] & ~((limb_t)3)));
202  state.h[NUM_LIMBS_128BIT] &= 0x0003;
203  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
204  carry += state.h[i];
205  state.h[i] = (limb_t)carry;
206  carry >>= LIMB_BITS;
207  }
208  state.h[i] += (limb_t)carry;
209 
210  // Subtract (2^130 - 5) from h by computing t = h + 5 - 2^130.
211  // The "minus 2^130" step is implicit.
212  carry = 5;
213  for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
214  carry += state.h[i];
215  t[i] = (limb_t)carry;
216  carry >>= LIMB_BITS;
217  }
218 
219  // Borrow occurs if bit 2^130 of the previous t result is zero.
220  // Carefully turn this into a selection mask so we can select either
221  // h or t as the final result. We don't care about the highest word
222  // of the result because we are about to drop it in the next step.
223  // We have to do it this way to avoid giving away any information
224  // about the value of h in the instruction timing.
225  limb_t mask = (~((t[NUM_LIMBS_128BIT] >> 2) & 1)) + 1;
226  limb_t nmask = ~mask;
227  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
228  state.h[i] = (state.h[i] & nmask) | (t[i] & mask);
229  }
230 
231  // Add the encrypted nonce and format the final hash.
232  memcpy(state.c, nonce, 16);
233  littleToHost(state.c, NUM_LIMBS_128BIT);
234  carry = 0;
235  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
236  carry += state.h[i];
237  carry += state.c[i];
238  state.h[i] = htolelimb((limb_t)carry);
239  carry >>= LIMB_BITS;
240  }
241  if (len > 16)
242  len = 16;
243  memcpy(token, state.h, len);
244 }
245 
252 {
253  if (state.chunkSize != 0) {
254  memset(((uint8_t *)state.c) + state.chunkSize, 0, 16 - state.chunkSize);
255  littleToHost(state.c, NUM_LIMBS_128BIT);
256  state.c[NUM_LIMBS_128BIT] = 1;
257  processChunk();
258  state.chunkSize = 0;
259  }
260 }
261 
266 {
267  clean(state);
268 }
269 
273 void Poly1305::processChunk()
274 {
275  limb_t t[NUM_LIMBS_256BIT + 1];
276 
277  // Compute h = ((h + c) * r) mod (2^130 - 5).
278 
279  // Start with h += c. We assume that h is less than (2^130 - 5) * 6
280  // and that c is less than 2^129, so the result will be less than 2^133.
281  dlimb_t carry = 0;
282  uint8_t i, j;
283  for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
284  carry += state.h[i];
285  carry += state.c[i];
286  state.h[i] = (limb_t)carry;
287  carry >>= LIMB_BITS;
288  }
289 
290  // Multiply h by r. We know that r is less than 2^124 because the
291  // top 4 bits were AND-ed off by reset(). That makes h * r less
292  // than 2^257. Which is less than the (2^130 - 6)^2 we want for
293  // the modulo reduction step that follows.
294  carry = 0;
295  limb_t word = state.r[0];
296  for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
297  carry += ((dlimb_t)(state.h[i])) * word;
298  t[i] = (limb_t)carry;
299  carry >>= LIMB_BITS;
300  }
301  t[NUM_LIMBS_130BIT] = (limb_t)carry;
302  for (i = 1; i < NUM_LIMBS_128BIT; ++i) {
303  word = state.r[i];
304  carry = 0;
305  for (j = 0; j < NUM_LIMBS_130BIT; ++j) {
306  carry += ((dlimb_t)(state.h[j])) * word;
307  carry += t[i + j];
308  t[i + j] = (limb_t)carry;
309  carry >>= LIMB_BITS;
310  }
311  t[i + NUM_LIMBS_130BIT] = (limb_t)carry;
312  }
313 
314  // Reduce h * r modulo (2^130 - 5) by multiplying the high 130 bits by 5
315  // and adding them to the low 130 bits. See the explaination in the
316  // comments for Curve25519::reduce() for a description of how this works.
317  carry = ((dlimb_t)(t[NUM_LIMBS_128BIT] >> 2)) +
318  (t[NUM_LIMBS_128BIT] & ~((limb_t)3));
319  t[NUM_LIMBS_128BIT] &= 0x0003;
320  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
321  // Shift the next word of t up by (LIMB_BITS - 2) bits and then
322  // multiply it by 5. Breaking it down, we can add the results
323  // of shifting up by LIMB_BITS and shifting up by (LIMB_BITS - 2).
324  // The main wrinkle here is that this can result in an intermediate
325  // carry that is (LIMB_BITS * 2 + 1) bits in size which doesn't
326  // fit within a dlimb_t variable. However, we can defer adding
327  // (word << LIMB_BITS) until after the "carry >>= LIMB_BITS" step
328  // because it won't affect the low bits of the carry.
329  word = t[i + NUM_LIMBS_130BIT];
330  carry += ((dlimb_t)word) << (LIMB_BITS - 2);
331  carry += t[i];
332  state.h[i] = (limb_t)carry;
333  carry >>= LIMB_BITS;
334  carry += word;
335  }
336  state.h[i] = (limb_t)(carry + t[NUM_LIMBS_128BIT]);
337 
338  // At this point, h is either the answer of reducing modulo (2^130 - 5)
339  // or it is at most 5 subtractions away from the answer we want.
340  // Leave it as-is for now with h less than (2^130 - 5) * 6. It is
341  // still within a range where the next h * r step will not overflow.
342 }
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:251
~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:265