1 | /*
|
---|
2 | ###############################################################################
|
---|
3 | #
|
---|
4 | # Temboo Arduino library
|
---|
5 | #
|
---|
6 | # Copyright 2014, Temboo Inc.
|
---|
7 | #
|
---|
8 | # Licensed under the Apache License, Version 2.0 (the "License");
|
---|
9 | # you may not use this file except in compliance with the License.
|
---|
10 | # You may obtain a copy of the License at
|
---|
11 | #
|
---|
12 | # http://www.apache.org/licenses/LICENSE-2.0
|
---|
13 | #
|
---|
14 | # Unless required by applicable law or agreed to in writing,
|
---|
15 | # software distributed under the License is distributed on an
|
---|
16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
---|
17 | # either express or implied. See the License for the specific
|
---|
18 | # language governing permissions and limitations under the License.
|
---|
19 | #
|
---|
20 | ###############################################################################
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | #include <string.h>
|
---|
25 | #include <avr/pgmspace.h>
|
---|
26 | #include "tmbmd5.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | static const uint8_t Worder[64] PROGMEM = {
|
---|
30 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
|
---|
31 | 1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12,
|
---|
32 | 5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2,
|
---|
33 | 0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9
|
---|
34 | };
|
---|
35 |
|
---|
36 | static const uint8_t Rorder[64] PROGMEM = {
|
---|
37 | 7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
|
---|
38 | 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
|
---|
39 | 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
|
---|
40 | 6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
|
---|
41 | };
|
---|
42 |
|
---|
43 | static const uint32_t Korder[64] PROGMEM = {
|
---|
44 | 0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL, 0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL,
|
---|
45 | 0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL, 0x6b901122UL, 0xfd987193UL, 0xa679438eUL, 0x49b40821UL,
|
---|
46 | 0xf61e2562UL, 0xc040b340UL, 0x265e5a51UL, 0xe9b6c7aaUL, 0xd62f105dUL, 0x02441453UL, 0xd8a1e681UL, 0xe7d3fbc8UL,
|
---|
47 | 0x21e1cde6UL, 0xc33707d6UL, 0xf4d50d87UL, 0x455a14edUL, 0xa9e3e905UL, 0xfcefa3f8UL, 0x676f02d9UL, 0x8d2a4c8aUL,
|
---|
48 | 0xfffa3942UL, 0x8771f681UL, 0x6d9d6122UL, 0xfde5380cUL, 0xa4beea44UL, 0x4bdecfa9UL, 0xf6bb4b60UL, 0xbebfbc70UL,
|
---|
49 | 0x289b7ec6UL, 0xeaa127faUL, 0xd4ef3085UL, 0x04881d05UL, 0xd9d4d039UL, 0xe6db99e5UL, 0x1fa27cf8UL, 0xc4ac5665UL,
|
---|
50 | 0xf4292244UL, 0x432aff97UL, 0xab9423a7UL, 0xfc93a039UL, 0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL,
|
---|
51 | 0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL, 0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL
|
---|
52 | };
|
---|
53 |
|
---|
54 |
|
---|
55 | MD5::MD5() {
|
---|
56 | init();
|
---|
57 | }
|
---|
58 |
|
---|
59 | void MD5::init() {
|
---|
60 | m_state[0] = 0x67452301UL;
|
---|
61 | m_state[1] = 0xefcdab89UL;
|
---|
62 | m_state[2] = 0x98badcfeUL;
|
---|
63 | m_state[3] = 0x10325476UL;
|
---|
64 | m_bufLength = 0;
|
---|
65 | m_msgLengthBits = 0;
|
---|
66 | }
|
---|
67 |
|
---|
68 | int MD5::compress(const uint8_t* buf) {
|
---|
69 | uint32_t a;
|
---|
70 | uint32_t b;
|
---|
71 | uint32_t c;
|
---|
72 | uint32_t d;
|
---|
73 | uint32_t i;
|
---|
74 | uint32_t W[16];
|
---|
75 | uint32_t t;
|
---|
76 |
|
---|
77 | // Copy data into W[0..15] in an endian-agnostic way
|
---|
78 | for (i = 0; i < 16; i++) {
|
---|
79 | W[i] = ((uint32_t)(buf[3])) << 24
|
---|
80 | | ((uint32_t)(buf[2])) << 16
|
---|
81 | | ((uint32_t)(buf[1])) << 8
|
---|
82 | | ((uint32_t)(buf[0]));
|
---|
83 | buf += 4;
|
---|
84 | }
|
---|
85 |
|
---|
86 | // copy current state
|
---|
87 | a = m_state[0];
|
---|
88 | b = m_state[1];
|
---|
89 | c = m_state[2];
|
---|
90 | d = m_state[3];
|
---|
91 |
|
---|
92 | for (i = 0; i < 16; ++i) {
|
---|
93 | FF(&a,b,c,d,W[pgm_read_byte(&Worder[i])],pgm_read_byte(&Rorder[i]),pgm_read_dword(&Korder[i]));
|
---|
94 | t = d; d = c; c = b; b = a; a = t;
|
---|
95 | }
|
---|
96 |
|
---|
97 | for (; i < 32; ++i) {
|
---|
98 | GG(&a,b,c,d,W[pgm_read_byte(&Worder[i])],pgm_read_byte(&Rorder[i]),pgm_read_dword(&Korder[i]));
|
---|
99 | t = d; d = c; c = b; b = a; a = t;
|
---|
100 | }
|
---|
101 |
|
---|
102 | for (; i < 48; ++i) {
|
---|
103 | HH(&a,b,c,d,W[pgm_read_byte(&Worder[i])],pgm_read_byte(&Rorder[i]),pgm_read_dword(&Korder[i]));
|
---|
104 | t = d; d = c; c = b; b = a; a = t;
|
---|
105 | }
|
---|
106 |
|
---|
107 | for (; i < 64; ++i) {
|
---|
108 | II(&a,b,c,d,W[pgm_read_byte(&Worder[i])],pgm_read_byte(&Rorder[i]),pgm_read_dword(&Korder[i]));
|
---|
109 | t = d; d = c; c = b; b = a; a = t;
|
---|
110 | }
|
---|
111 |
|
---|
112 | m_state[0] = m_state[0] + a;
|
---|
113 | m_state[1] = m_state[1] + b;
|
---|
114 | m_state[2] = m_state[2] + c;
|
---|
115 | m_state[3] = m_state[3] + d;
|
---|
116 |
|
---|
117 | return MD5::MD5_OK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | int MD5::process (const uint8_t* msg, uint32_t msgLengthBytes) {
|
---|
121 | uint32_t n;
|
---|
122 | int err;
|
---|
123 |
|
---|
124 | if (m_bufLength >= sizeof(m_buf)) {
|
---|
125 | return MD5::MD5_INVALID_ARG;
|
---|
126 | }
|
---|
127 |
|
---|
128 | while (msgLengthBytes > 0) {
|
---|
129 |
|
---|
130 | // Process the input msg in 64 byte chunks
|
---|
131 | if (m_bufLength == 0 && msgLengthBytes >= 64) {
|
---|
132 | err = compress (msg);
|
---|
133 | if (err != MD5::MD5_OK) {
|
---|
134 | return err;
|
---|
135 | }
|
---|
136 | m_msgLengthBits += 64 * 8;
|
---|
137 | msg += 64;
|
---|
138 | msgLengthBytes -= 64;
|
---|
139 | } else {
|
---|
140 | n = 64 - m_bufLength;
|
---|
141 | if (msgLengthBytes < n) {
|
---|
142 | n = msgLengthBytes;
|
---|
143 | }
|
---|
144 | memcpy(m_buf + m_bufLength, msg, (size_t)n);
|
---|
145 | m_bufLength += n;
|
---|
146 | msg += n;
|
---|
147 | msgLengthBytes -= n;
|
---|
148 | if (m_bufLength == 64) {
|
---|
149 | err = compress (m_buf);
|
---|
150 | if (err != MD5::MD5_OK) {
|
---|
151 | return err;
|
---|
152 | }
|
---|
153 | m_msgLengthBits += 64 * 8;
|
---|
154 | m_bufLength = 0;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | return MD5::MD5_OK;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | int MD5::finish(uint8_t* out) {
|
---|
163 | int i;
|
---|
164 |
|
---|
165 | if (m_bufLength >= sizeof(m_buf)) {
|
---|
166 | return MD5::MD5_INVALID_ARG;
|
---|
167 | }
|
---|
168 |
|
---|
169 | m_msgLengthBits += m_bufLength * 8;
|
---|
170 |
|
---|
171 | // append a '1' bit (right-padded with zeros)
|
---|
172 | m_buf[m_bufLength++] = (uint8_t)0x80;
|
---|
173 |
|
---|
174 | // if the bufLength is > 56 bytes, pad with zeros then compress.
|
---|
175 | // Then fall back to padding with zeros and length encoding like normal.
|
---|
176 | if (m_bufLength > 56) {
|
---|
177 | while (m_bufLength < 64) {
|
---|
178 | m_buf[m_bufLength++] = (uint8_t)0;
|
---|
179 | }
|
---|
180 | compress(m_buf);
|
---|
181 | m_bufLength = 0;
|
---|
182 | }
|
---|
183 |
|
---|
184 | // pad with zeroes up to 56 bytes.
|
---|
185 | // (Why 56? because we store the 8-byte length at the end.)
|
---|
186 | // (What if bufLength == 56? Perfect! No padding prior to 8-byte length needed.)
|
---|
187 | while (m_bufLength < 56) {
|
---|
188 | m_buf[m_bufLength++] = (uint8_t)0;
|
---|
189 | }
|
---|
190 |
|
---|
191 | // add the length in an endian-agnostic way
|
---|
192 | m_buf[56] = (uint8_t)((m_msgLengthBits ) & 255);
|
---|
193 | m_buf[57] = (uint8_t)((m_msgLengthBits >> 8) & 255);
|
---|
194 | m_buf[58] = (uint8_t)((m_msgLengthBits >> 16) & 255);
|
---|
195 | m_buf[59] = (uint8_t)((m_msgLengthBits >> 24) & 255);
|
---|
196 | m_buf[60] = (uint8_t)((m_msgLengthBits >> 32) & 255);
|
---|
197 | m_buf[61] = (uint8_t)((m_msgLengthBits >> 40) & 255);
|
---|
198 | m_buf[62] = (uint8_t)((m_msgLengthBits >> 48) & 255);
|
---|
199 | m_buf[63] = (uint8_t)((m_msgLengthBits >> 56) & 255);
|
---|
200 |
|
---|
201 | compress(m_buf);
|
---|
202 |
|
---|
203 | // copy the state to the output in an endian-agnostic way
|
---|
204 | for (i = 0; i < 4; i++) {
|
---|
205 | out[0] = m_state[i] & 255;
|
---|
206 | out[1] = (m_state[i] >> 8) & 255;
|
---|
207 | out[2] = (m_state[i] >> 16) & 255;
|
---|
208 | out[3] = (m_state[i] >> 24) & 255;
|
---|
209 | out += 4;
|
---|
210 | }
|
---|
211 | return MD5::MD5_OK;
|
---|
212 | }
|
---|
213 |
|
---|