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 <string.h>
27 
61 // Useful sizes for limb array and word manipulation.
62 #define NUM_LIMBS_128BIT (16 / sizeof(limb_t))
63 #define NUM_LIMBS_130BIT ((16 / sizeof(limb_t)) + 1)
64 #define LIMB_BITS (sizeof(limb_t) * 8)
65 
66 // Endian helper macros for limbs and arrays of limbs.
67 #if BIGNUMBER_LIMB_8BIT
68 #define lelimbtoh(x) (x)
69 #define htolelimb(x) (x)
70 #elif BIGNUMBER_LIMB_16BIT
71 #define lelimbtoh(x) (le16toh((x)))
72 #define htolelimb(x) (htole16((x)))
73 #elif BIGNUMBER_LIMB_32BIT
74 #define lelimbtoh(x) (le32toh((x)))
75 #define htolelimb(x) (htole32((x)))
76 #endif
77 #if defined(CRYPTO_LITTLE_ENDIAN)
78 #define littleToHost(r,size) do { ; } while (0)
79 #else
80 #define littleToHost(r,size) \
81  do { \
82  for (uint8_t i = 0; i < (size); ++i) \
83  (r)[i] = lelimbtoh((r)[i]); \
84  } while (0)
85 #endif
86 
91 {
92  state.chunkSize = 0;
93 }
94 
100 {
101  clean(state);
102 }
103 
111 void Poly1305::reset(const void *key)
112 {
113  // Copy the key into place and clear the bits we don't need.
114  uint8_t *r = (uint8_t *)state.r;
115  memcpy(r, key, 16);
116  r[3] &= 0x0F;
117  r[4] &= 0xFC;
118  r[7] &= 0x0F;
119  r[8] &= 0xFC;
120  r[11] &= 0x0F;
121  r[12] &= 0xFC;
122  r[15] &= 0x0F;
123 
124  // Convert into little-endian if necessary.
125  littleToHost(state.r, NUM_LIMBS_128BIT);
126 
127  // Reset the hashing process.
128  state.chunkSize = 0;
129  memset(state.h, 0, sizeof(state.h));
130 }
131 
143 void Poly1305::update(const void *data, size_t len)
144 {
145  // Break the input up into 128-bit chunks and process each in turn.
146  const uint8_t *d = (const uint8_t *)data;
147  while (len > 0) {
148  uint8_t size = 16 - state.chunkSize;
149  if (size > len)
150  size = len;
151  memcpy(((uint8_t *)state.c) + state.chunkSize, d, size);
152  state.chunkSize += size;
153  len -= size;
154  d += size;
155  if (state.chunkSize == 16) {
156  littleToHost(state.c, NUM_LIMBS_128BIT);
157  state.c[NUM_LIMBS_128BIT] = 1;
158  processChunk();
159  state.chunkSize = 0;
160  }
161  }
162 }
163 
180 void Poly1305::finalize(const void *nonce, void *token, size_t len)
181 {
182  dlimb_t carry;
183  uint8_t i;
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  state.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 = (~((state.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) | (state.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  // Compute h = ((h + c) * r) mod (2^130 - 5).
276 
277  // Start with h += c. We assume that h is less than (2^130 - 5) * 6
278  // and that c is less than 2^129, so the result will be less than 2^133.
279  dlimb_t carry = 0;
280  uint8_t i, j;
281  for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
282  carry += state.h[i];
283  carry += state.c[i];
284  state.h[i] = (limb_t)carry;
285  carry >>= LIMB_BITS;
286  }
287 
288  // Multiply h by r. We know that r is less than 2^124 because the
289  // top 4 bits were AND-ed off by reset(). That makes h * r less
290  // than 2^257. Which is less than the (2^130 - 6)^2 we want for
291  // the modulo reduction step that follows.
292  carry = 0;
293  limb_t word = state.r[0];
294  for (i = 0; i < NUM_LIMBS_130BIT; ++i) {
295  carry += ((dlimb_t)(state.h[i])) * word;
296  state.t[i] = (limb_t)carry;
297  carry >>= LIMB_BITS;
298  }
299  state.t[NUM_LIMBS_130BIT] = (limb_t)carry;
300  for (i = 1; i < NUM_LIMBS_128BIT; ++i) {
301  word = state.r[i];
302  carry = 0;
303  for (j = 0; j < NUM_LIMBS_130BIT; ++j) {
304  carry += ((dlimb_t)(state.h[j])) * word;
305  carry += state.t[i + j];
306  state.t[i + j] = (limb_t)carry;
307  carry >>= LIMB_BITS;
308  }
309  state.t[i + NUM_LIMBS_130BIT] = (limb_t)carry;
310  }
311 
312  // Reduce h * r modulo (2^130 - 5) by multiplying the high 130 bits by 5
313  // and adding them to the low 130 bits. See the explaination in the
314  // comments for Curve25519::reduce() for a description of how this works.
315  carry = ((dlimb_t)(state.t[NUM_LIMBS_128BIT] >> 2)) +
316  (state.t[NUM_LIMBS_128BIT] & ~((limb_t)3));
317  state.t[NUM_LIMBS_128BIT] &= 0x0003;
318  for (i = 0; i < NUM_LIMBS_128BIT; ++i) {
319  // Shift the next word of t up by (LIMB_BITS - 2) bits and then
320  // multiply it by 5. Breaking it down, we can add the results
321  // of shifting up by LIMB_BITS and shifting up by (LIMB_BITS - 2).
322  // The main wrinkle here is that this can result in an intermediate
323  // carry that is (LIMB_BITS * 2 + 1) bits in size which doesn't
324  // fit within a dlimb_t variable. However, we can defer adding
325  // (word << LIMB_BITS) until after the "carry >>= LIMB_BITS" step
326  // because it won't affect the low bits of the carry.
327  word = state.t[i + NUM_LIMBS_130BIT];
328  carry += ((dlimb_t)word) << (LIMB_BITS - 2);
329  carry += state.t[i];
330  state.h[i] = (limb_t)carry;
331  carry >>= LIMB_BITS;
332  carry += word;
333  }
334  state.h[i] = (limb_t)(carry + state.t[NUM_LIMBS_128BIT]);
335 
336  // At this point, h is either the answer of reducing modulo (2^130 - 5)
337  // or it is at most 5 subtractions away from the answer we want.
338  // Leave it as-is for now with h less than (2^130 - 5) * 6. It is
339  // still within a range where the next h * r step will not overflow.
340 }
void finalize(const void *nonce, void *token, size_t len)
Finalizes the authentication process and returns the token.
Definition: Poly1305.cpp:180
void reset(const void *key)
Resets the Poly1305 message authenticator for a new session.
Definition: Poly1305.cpp:111
void update(const void *data, size_t len)
Updates the message authenticator with more data.
Definition: Poly1305.cpp:143
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:99
Poly1305()
Constructs a new Poly1305 message authenticator.
Definition: Poly1305.cpp:90
void clear()
Clears the authenticator's state, removing all sensitive data.
Definition: Poly1305.cpp:265