Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
/*
2
 *  FIPS-180-1 compliant SHA-1 implementation
3
 *
4
 *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8
 *  not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 *
19
 *  This file is part of mbed TLS (https://tls.mbed.org)
20
 *  Modified for esp32 by Lucas Saavedra Vaz on 11 Jan 2024
21
 */
22
 
23
#include <Arduino.h>
24
#if ESP_IDF_VERSION_MAJOR < 5
25
 
26
#include "BackPort_SHA1Builder.h"
27
 
28
// 32-bit integer manipulation macros (big endian)
29
 
30
#ifndef GET_UINT32_BE
31
#define GET_UINT32_BE(n, b, i) \
32
  { (n) = ((uint32_t)(b)[(i)] << 24) | ((uint32_t)(b)[(i) + 1] << 16) | ((uint32_t)(b)[(i) + 2] << 8) | ((uint32_t)(b)[(i) + 3]); }
33
#endif
34
 
35
#ifndef PUT_UINT32_BE
36
#define PUT_UINT32_BE(n, b, i)           \
37
  {                                      \
38
    (b)[(i)] = (uint8_t)((n) >> 24);     \
39
    (b)[(i) + 1] = (uint8_t)((n) >> 16); \
40
    (b)[(i) + 2] = (uint8_t)((n) >> 8);  \
41
    (b)[(i) + 3] = (uint8_t)((n));       \
42
  }
43
#endif
44
 
45
// Constants
46
 
47
static const uint8_t sha1_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48
                                         0,    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
49
 
50
// Private methods
51
 
52
void SHA1Builder::process(const uint8_t *data) {
53
  uint32_t temp, W[16], A, B, C, D, E;
54
 
55
  GET_UINT32_BE(W[0], data, 0);
56
  GET_UINT32_BE(W[1], data, 4);
57
  GET_UINT32_BE(W[2], data, 8);
58
  GET_UINT32_BE(W[3], data, 12);
59
  GET_UINT32_BE(W[4], data, 16);
60
  GET_UINT32_BE(W[5], data, 20);
61
  GET_UINT32_BE(W[6], data, 24);
62
  GET_UINT32_BE(W[7], data, 28);
63
  GET_UINT32_BE(W[8], data, 32);
64
  GET_UINT32_BE(W[9], data, 36);
65
  GET_UINT32_BE(W[10], data, 40);
66
  GET_UINT32_BE(W[11], data, 44);
67
  GET_UINT32_BE(W[12], data, 48);
68
  GET_UINT32_BE(W[13], data, 52);
69
  GET_UINT32_BE(W[14], data, 56);
70
  GET_UINT32_BE(W[15], data, 60);
71
 
72
#define sha1_S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
73
 
74
#define sha1_R(t) (temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ W[(t - 14) & 0x0F] ^ W[t & 0x0F], (W[t & 0x0F] = sha1_S(temp, 1)))
75
 
76
#define sha1_P(a, b, c, d, e, x)                      \
77
  {                                                   \
78
    e += sha1_S(a, 5) + sha1_F(b, c, d) + sha1_K + x; \
79
    b = sha1_S(b, 30);                                \
80
  }
81
 
82
  A = state[0];
83
  B = state[1];
84
  C = state[2];
85
  D = state[3];
86
  E = state[4];
87
 
88
#define sha1_F(x, y, z) (z ^ (x & (y ^ z)))
89
#define sha1_K          0x5A827999
90
 
91
  sha1_P(A, B, C, D, E, W[0]);
92
  sha1_P(E, A, B, C, D, W[1]);
93
  sha1_P(D, E, A, B, C, W[2]);
94
  sha1_P(C, D, E, A, B, W[3]);
95
  sha1_P(B, C, D, E, A, W[4]);
96
  sha1_P(A, B, C, D, E, W[5]);
97
  sha1_P(E, A, B, C, D, W[6]);
98
  sha1_P(D, E, A, B, C, W[7]);
99
  sha1_P(C, D, E, A, B, W[8]);
100
  sha1_P(B, C, D, E, A, W[9]);
101
  sha1_P(A, B, C, D, E, W[10]);
102
  sha1_P(E, A, B, C, D, W[11]);
103
  sha1_P(D, E, A, B, C, W[12]);
104
  sha1_P(C, D, E, A, B, W[13]);
105
  sha1_P(B, C, D, E, A, W[14]);
106
  sha1_P(A, B, C, D, E, W[15]);
107
  sha1_P(E, A, B, C, D, sha1_R(16));
108
  sha1_P(D, E, A, B, C, sha1_R(17));
109
  sha1_P(C, D, E, A, B, sha1_R(18));
110
  sha1_P(B, C, D, E, A, sha1_R(19));
111
 
112
#undef sha1_K
113
#undef sha1_F
114
 
115
#define sha1_F(x, y, z) (x ^ y ^ z)
116
#define sha1_K          0x6ED9EBA1
117
 
118
  sha1_P(A, B, C, D, E, sha1_R(20));
119
  sha1_P(E, A, B, C, D, sha1_R(21));
120
  sha1_P(D, E, A, B, C, sha1_R(22));
121
  sha1_P(C, D, E, A, B, sha1_R(23));
122
  sha1_P(B, C, D, E, A, sha1_R(24));
123
  sha1_P(A, B, C, D, E, sha1_R(25));
124
  sha1_P(E, A, B, C, D, sha1_R(26));
125
  sha1_P(D, E, A, B, C, sha1_R(27));
126
  sha1_P(C, D, E, A, B, sha1_R(28));
127
  sha1_P(B, C, D, E, A, sha1_R(29));
128
  sha1_P(A, B, C, D, E, sha1_R(30));
129
  sha1_P(E, A, B, C, D, sha1_R(31));
130
  sha1_P(D, E, A, B, C, sha1_R(32));
131
  sha1_P(C, D, E, A, B, sha1_R(33));
132
  sha1_P(B, C, D, E, A, sha1_R(34));
133
  sha1_P(A, B, C, D, E, sha1_R(35));
134
  sha1_P(E, A, B, C, D, sha1_R(36));
135
  sha1_P(D, E, A, B, C, sha1_R(37));
136
  sha1_P(C, D, E, A, B, sha1_R(38));
137
  sha1_P(B, C, D, E, A, sha1_R(39));
138
 
139
#undef sha1_K
140
#undef sha1_F
141
 
142
#define sha1_F(x, y, z) ((x & y) | (z & (x | y)))
143
#define sha1_K          0x8F1BBCDC
144
 
145
  sha1_P(A, B, C, D, E, sha1_R(40));
146
  sha1_P(E, A, B, C, D, sha1_R(41));
147
  sha1_P(D, E, A, B, C, sha1_R(42));
148
  sha1_P(C, D, E, A, B, sha1_R(43));
149
  sha1_P(B, C, D, E, A, sha1_R(44));
150
  sha1_P(A, B, C, D, E, sha1_R(45));
151
  sha1_P(E, A, B, C, D, sha1_R(46));
152
  sha1_P(D, E, A, B, C, sha1_R(47));
153
  sha1_P(C, D, E, A, B, sha1_R(48));
154
  sha1_P(B, C, D, E, A, sha1_R(49));
155
  sha1_P(A, B, C, D, E, sha1_R(50));
156
  sha1_P(E, A, B, C, D, sha1_R(51));
157
  sha1_P(D, E, A, B, C, sha1_R(52));
158
  sha1_P(C, D, E, A, B, sha1_R(53));
159
  sha1_P(B, C, D, E, A, sha1_R(54));
160
  sha1_P(A, B, C, D, E, sha1_R(55));
161
  sha1_P(E, A, B, C, D, sha1_R(56));
162
  sha1_P(D, E, A, B, C, sha1_R(57));
163
  sha1_P(C, D, E, A, B, sha1_R(58));
164
  sha1_P(B, C, D, E, A, sha1_R(59));
165
 
166
#undef sha1_K
167
#undef sha1_F
168
 
169
#define sha1_F(x, y, z) (x ^ y ^ z)
170
#define sha1_K          0xCA62C1D6
171
 
172
  sha1_P(A, B, C, D, E, sha1_R(60));
173
  sha1_P(E, A, B, C, D, sha1_R(61));
174
  sha1_P(D, E, A, B, C, sha1_R(62));
175
  sha1_P(C, D, E, A, B, sha1_R(63));
176
  sha1_P(B, C, D, E, A, sha1_R(64));
177
  sha1_P(A, B, C, D, E, sha1_R(65));
178
  sha1_P(E, A, B, C, D, sha1_R(66));
179
  sha1_P(D, E, A, B, C, sha1_R(67));
180
  sha1_P(C, D, E, A, B, sha1_R(68));
181
  sha1_P(B, C, D, E, A, sha1_R(69));
182
  sha1_P(A, B, C, D, E, sha1_R(70));
183
  sha1_P(E, A, B, C, D, sha1_R(71));
184
  sha1_P(D, E, A, B, C, sha1_R(72));
185
  sha1_P(C, D, E, A, B, sha1_R(73));
186
  sha1_P(B, C, D, E, A, sha1_R(74));
187
  sha1_P(A, B, C, D, E, sha1_R(75));
188
  sha1_P(E, A, B, C, D, sha1_R(76));
189
  sha1_P(D, E, A, B, C, sha1_R(77));
190
  sha1_P(C, D, E, A, B, sha1_R(78));
191
  sha1_P(B, C, D, E, A, sha1_R(79));
192
 
193
#undef sha1_K
194
#undef sha1_F
195
 
196
  state[0] += A;
197
  state[1] += B;
198
  state[2] += C;
199
  state[3] += D;
200
  state[4] += E;
201
}
202
 
203
// Public methods
204
 
205
void SHA1Builder::begin() {
206
  total[0] = 0;
207
  total[1] = 0;
208
 
209
  state[0] = 0x67452301;
210
  state[1] = 0xEFCDAB89;
211
  state[2] = 0x98BADCFE;
212
  state[3] = 0x10325476;
213
  state[4] = 0xC3D2E1F0;
214
 
215
  memset(buffer, 0x00, sizeof(buffer));
216
  memset(hash, 0x00, sizeof(hash));
217
}
218
 
219
void SHA1Builder::add(const uint8_t *data, size_t len) {
220
  size_t fill;
221
  uint32_t left;
222
 
223
  if (len == 0) {
224
    return;
225
  }
226
 
227
  left = total[0] & 0x3F;
228
  fill = 64 - left;
229
 
230
  total[0] += (uint32_t)len;
231
  total[0] &= 0xFFFFFFFF;
232
 
233
  if (total[0] < (uint32_t)len) {
234
    total[1]++;
235
  }
236
 
237
  if (left && len >= fill) {
238
    memcpy((void *)(buffer + left), data, fill);
239
    process(buffer);
240
    data += fill;
241
    len -= fill;
242
    left = 0;
243
  }
244
 
245
  while (len >= 64) {
246
    process(data);
247
    data += 64;
248
    len -= 64;
249
  }
250
 
251
  if (len > 0) {
252
    memcpy((void *)(buffer + left), data, len);
253
  }
254
}
255
 
256
void SHA1Builder::calculate(void) {
257
  uint32_t last, padn;
258
  uint32_t high, low;
259
  uint8_t msglen[8];
260
 
261
  high = (total[0] >> 29) | (total[1] << 3);
262
  low = (total[0] << 3);
263
 
264
  PUT_UINT32_BE(high, msglen, 0);
265
  PUT_UINT32_BE(low, msglen, 4);
266
 
267
  last = total[0] & 0x3F;
268
  padn = (last < 56) ? (56 - last) : (120 - last);
269
 
270
  add((uint8_t *)sha1_padding, padn);
271
  add(msglen, 8);
272
 
273
  PUT_UINT32_BE(state[0], hash, 0);
274
  PUT_UINT32_BE(state[1], hash, 4);
275
  PUT_UINT32_BE(state[2], hash, 8);
276
  PUT_UINT32_BE(state[3], hash, 12);
277
  PUT_UINT32_BE(state[4], hash, 16);
278
}
279
 
280
void SHA1Builder::getBytes(uint8_t *output) {
281
  memcpy(output, hash, SHA1_HASH_SIZE);
282
}
283
 
284
#endif  // ESP_IDF_VERSION_MAJOR < 5