Arduino Cryptography Library
Crypto.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 "Crypto.h"
24 
34 void clean(void *dest, size_t size)
35 {
36  // Force the use of volatile so that we actually clear the memory.
37  // Otherwise the compiler might optimise the entire contents of this
38  // function away, which will not be secure.
39  volatile uint8_t *d = (volatile uint8_t *)dest;
40  while (size > 0) {
41  *d++ = 0;
42  --size;
43  }
44 }
45 
69 bool secure_compare(const void *data1, const void *data2, size_t len)
70 {
71  uint8_t result = 0;
72  const uint8_t *d1 = (const uint8_t *)data1;
73  const uint8_t *d2 = (const uint8_t *)data2;
74  while (len > 0) {
75  result |= (*d1++ ^ *d2++);
76  --len;
77  }
78  return (bool)((((uint16_t)0x0100) - result) >> 8);
79 }
80 
96 uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size)
97 {
98  const uint8_t *d = (const uint8_t *)data;
99  uint8_t crc = 0xFF ^ tag;
100  uint8_t bit;
101  while (size > 0) {
102  crc ^= *d++;
103  for (bit = 0; bit < 8; ++bit) {
104  // if (crc & 0x80)
105  // crc = (crc << 1) ^ 0x1D;
106  // else
107  // crc = (crc << 1);
108  uint8_t generator = (uint8_t)((((int8_t)crc) >> 7) & 0x1D);
109  crc = (crc << 1) ^ generator;
110  }
111  --size;
112  }
113  return crc;
114 }