1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci/*
17425bb815Sopenharmony_ci *  FIPS-180-1 compliant SHA-1 implementation
18425bb815Sopenharmony_ci *
19425bb815Sopenharmony_ci *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
20425bb815Sopenharmony_ci *  SPDX-License-Identifier: Apache-2.0
21425bb815Sopenharmony_ci *
22425bb815Sopenharmony_ci *  Licensed under the Apache License, Version 2.0 (the "License"); you may
23425bb815Sopenharmony_ci *  not use this file except in compliance with the License.
24425bb815Sopenharmony_ci *  You may obtain a copy of the License at
25425bb815Sopenharmony_ci *
26425bb815Sopenharmony_ci *  http://www.apache.org/licenses/LICENSE-2.0
27425bb815Sopenharmony_ci *
28425bb815Sopenharmony_ci *  Unless required by applicable law or agreed to in writing, software
29425bb815Sopenharmony_ci *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30425bb815Sopenharmony_ci *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31425bb815Sopenharmony_ci *  See the License for the specific language governing permissions and
32425bb815Sopenharmony_ci *  limitations under the License.
33425bb815Sopenharmony_ci *
34425bb815Sopenharmony_ci *  This file is part of mbed TLS (https://tls.mbed.org)
35425bb815Sopenharmony_ci */
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci/*
38425bb815Sopenharmony_ci *  The SHA-1 standard was published by NIST in 1993.
39425bb815Sopenharmony_ci *
40425bb815Sopenharmony_ci *  http://www.itl.nist.gov/fipspubs/fip180-1.htm
41425bb815Sopenharmony_ci */
42425bb815Sopenharmony_ci
43425bb815Sopenharmony_ci#include "debugger-sha1.h"
44425bb815Sopenharmony_ci#include "jext-common.h"
45425bb815Sopenharmony_ci
46425bb815Sopenharmony_ci#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
47425bb815Sopenharmony_ci
48425bb815Sopenharmony_ci/**
49425bb815Sopenharmony_ci * SHA-1 context structure.
50425bb815Sopenharmony_ci */
51425bb815Sopenharmony_citypedef struct
52425bb815Sopenharmony_ci{
53425bb815Sopenharmony_ci  uint32_t total[2]; /**< number of bytes processed */
54425bb815Sopenharmony_ci  uint32_t state[5]; /**< intermediate digest state */
55425bb815Sopenharmony_ci  uint8_t buffer[64]; /**< data block being processed */
56425bb815Sopenharmony_ci} jerryx_sha1_context;
57425bb815Sopenharmony_ci
58425bb815Sopenharmony_ci/* 32-bit integer manipulation macros (big endian). */
59425bb815Sopenharmony_ci
60425bb815Sopenharmony_ci#define JERRYX_SHA1_GET_UINT32_BE(n, b, i) \
61425bb815Sopenharmony_ci{ \
62425bb815Sopenharmony_ci  (n) = (((uint32_t) (b)[(i) + 0]) << 24) \
63425bb815Sopenharmony_ci        | (((uint32_t) (b)[(i) + 1]) << 16) \
64425bb815Sopenharmony_ci        | (((uint32_t) (b)[(i) + 2]) << 8) \
65425bb815Sopenharmony_ci        | ((uint32_t) (b)[(i) + 3]); \
66425bb815Sopenharmony_ci}
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_ci#define JERRYX_SHA1_PUT_UINT32_BE(n, b, i) \
69425bb815Sopenharmony_ci{ \
70425bb815Sopenharmony_ci  (b)[(i) + 0] = (uint8_t) ((n) >> 24); \
71425bb815Sopenharmony_ci  (b)[(i) + 1] = (uint8_t) ((n) >> 16); \
72425bb815Sopenharmony_ci  (b)[(i) + 2] = (uint8_t) ((n) >> 8); \
73425bb815Sopenharmony_ci  (b)[(i) + 3] = (uint8_t) ((n)); \
74425bb815Sopenharmony_ci}
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci/**
77425bb815Sopenharmony_ci * Initialize SHA-1 context.
78425bb815Sopenharmony_ci */
79425bb815Sopenharmony_cistatic void
80425bb815Sopenharmony_cijerryx_sha1_init (jerryx_sha1_context *sha1_context_p) /**< SHA-1 context */
81425bb815Sopenharmony_ci{
82425bb815Sopenharmony_ci  memset (sha1_context_p, 0, sizeof (jerryx_sha1_context));
83425bb815Sopenharmony_ci
84425bb815Sopenharmony_ci  sha1_context_p->total[0] = 0;
85425bb815Sopenharmony_ci  sha1_context_p->total[1] = 0;
86425bb815Sopenharmony_ci
87425bb815Sopenharmony_ci  sha1_context_p->state[0] = 0x67452301;
88425bb815Sopenharmony_ci  sha1_context_p->state[1] = 0xEFCDAB89;
89425bb815Sopenharmony_ci  sha1_context_p->state[2] = 0x98BADCFE;
90425bb815Sopenharmony_ci  sha1_context_p->state[3] = 0x10325476;
91425bb815Sopenharmony_ci  sha1_context_p->state[4] = 0xC3D2E1F0;
92425bb815Sopenharmony_ci} /* jerryx_sha1_init */
93425bb815Sopenharmony_ci
94425bb815Sopenharmony_ci#define JERRYX_SHA1_P(a, b, c, d, e, x) \
95425bb815Sopenharmony_cido { \
96425bb815Sopenharmony_ci  e += JERRYX_SHA1_SHIFT (a, 5) + JERRYX_SHA1_F (b, c, d) + K + x; \
97425bb815Sopenharmony_ci  b = JERRYX_SHA1_SHIFT (b, 30); \
98425bb815Sopenharmony_ci} while (0)
99425bb815Sopenharmony_ci
100425bb815Sopenharmony_ci/**
101425bb815Sopenharmony_ci * Update SHA-1 internal buffer status.
102425bb815Sopenharmony_ci */
103425bb815Sopenharmony_cistatic void
104425bb815Sopenharmony_cijerryx_sha1_process (jerryx_sha1_context *sha1_context_p, /**< SHA-1 context */
105425bb815Sopenharmony_ci                     const uint8_t data[64]) /**< data buffer */
106425bb815Sopenharmony_ci{
107425bb815Sopenharmony_ci  uint32_t temp, W[16], A, B, C, D, E;
108425bb815Sopenharmony_ci
109425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[0], data, 0);
110425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[1], data, 4);
111425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[2], data, 8);
112425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[3], data, 12);
113425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[4], data, 16);
114425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[5], data, 20);
115425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[6], data, 24);
116425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[7], data, 28);
117425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[8], data, 32);
118425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[9], data, 36);
119425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[10], data, 40);
120425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[11], data, 44);
121425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[12], data, 48);
122425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[13], data, 52);
123425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[14], data, 56);
124425bb815Sopenharmony_ci  JERRYX_SHA1_GET_UINT32_BE (W[15], data, 60);
125425bb815Sopenharmony_ci
126425bb815Sopenharmony_ci#define JERRYX_SHA1_SHIFT(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
127425bb815Sopenharmony_ci
128425bb815Sopenharmony_ci#define JERRYX_SHA1_R(t) \
129425bb815Sopenharmony_ci( \
130425bb815Sopenharmony_ci  temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ W[(t - 14) & 0x0F] ^ W[t & 0x0F], \
131425bb815Sopenharmony_ci  W[t & 0x0F] = JERRYX_SHA1_SHIFT (temp, 1) \
132425bb815Sopenharmony_ci)
133425bb815Sopenharmony_ci
134425bb815Sopenharmony_ci  A = sha1_context_p->state[0];
135425bb815Sopenharmony_ci  B = sha1_context_p->state[1];
136425bb815Sopenharmony_ci  C = sha1_context_p->state[2];
137425bb815Sopenharmony_ci  D = sha1_context_p->state[3];
138425bb815Sopenharmony_ci  E = sha1_context_p->state[4];
139425bb815Sopenharmony_ci
140425bb815Sopenharmony_ci  uint32_t K = 0x5A827999;
141425bb815Sopenharmony_ci
142425bb815Sopenharmony_ci#define JERRYX_SHA1_F(x, y, z) (z ^ (x & (y ^ z)))
143425bb815Sopenharmony_ci
144425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, W[0]);
145425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, W[1]);
146425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, W[2]);
147425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, W[3]);
148425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, W[4]);
149425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, W[5]);
150425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, W[6]);
151425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, W[7]);
152425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, W[8]);
153425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, W[9]);
154425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, W[10]);
155425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, W[11]);
156425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, W[12]);
157425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, W[13]);
158425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, W[14]);
159425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, W[15]);
160425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (16));
161425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (17));
162425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (18));
163425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (19));
164425bb815Sopenharmony_ci
165425bb815Sopenharmony_ci#undef JERRYX_SHA1_F
166425bb815Sopenharmony_ci
167425bb815Sopenharmony_ci  K = 0x6ED9EBA1;
168425bb815Sopenharmony_ci
169425bb815Sopenharmony_ci#define JERRYX_SHA1_F(x, y, z) (x ^ y ^ z)
170425bb815Sopenharmony_ci
171425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (20));
172425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (21));
173425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (22));
174425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (23));
175425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (24));
176425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (25));
177425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (26));
178425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (27));
179425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (28));
180425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (29));
181425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (30));
182425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (31));
183425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (32));
184425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (33));
185425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (34));
186425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (35));
187425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (36));
188425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (37));
189425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (38));
190425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (39));
191425bb815Sopenharmony_ci
192425bb815Sopenharmony_ci#undef JERRYX_SHA1_F
193425bb815Sopenharmony_ci
194425bb815Sopenharmony_ci  K = 0x8F1BBCDC;
195425bb815Sopenharmony_ci
196425bb815Sopenharmony_ci#define JERRYX_SHA1_F(x, y, z) ((x & y) | (z & (x | y)))
197425bb815Sopenharmony_ci
198425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (40));
199425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (41));
200425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (42));
201425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (43));
202425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (44));
203425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (45));
204425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (46));
205425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (47));
206425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (48));
207425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (49));
208425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (50));
209425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (51));
210425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (52));
211425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (53));
212425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (54));
213425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (55));
214425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (56));
215425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (57));
216425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (58));
217425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (59));
218425bb815Sopenharmony_ci
219425bb815Sopenharmony_ci#undef JERRYX_SHA1_F
220425bb815Sopenharmony_ci
221425bb815Sopenharmony_ci  K = 0xCA62C1D6;
222425bb815Sopenharmony_ci
223425bb815Sopenharmony_ci#define JERRYX_SHA1_F(x, y, z) (x ^ y ^ z)
224425bb815Sopenharmony_ci
225425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (60));
226425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (61));
227425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (62));
228425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (63));
229425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (64));
230425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (65));
231425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (66));
232425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (67));
233425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (68));
234425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (69));
235425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (70));
236425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (71));
237425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (72));
238425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (73));
239425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (74));
240425bb815Sopenharmony_ci  JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (75));
241425bb815Sopenharmony_ci  JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (76));
242425bb815Sopenharmony_ci  JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (77));
243425bb815Sopenharmony_ci  JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (78));
244425bb815Sopenharmony_ci  JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (79));
245425bb815Sopenharmony_ci
246425bb815Sopenharmony_ci#undef JERRYX_SHA1_F
247425bb815Sopenharmony_ci
248425bb815Sopenharmony_ci  sha1_context_p->state[0] += A;
249425bb815Sopenharmony_ci  sha1_context_p->state[1] += B;
250425bb815Sopenharmony_ci  sha1_context_p->state[2] += C;
251425bb815Sopenharmony_ci  sha1_context_p->state[3] += D;
252425bb815Sopenharmony_ci  sha1_context_p->state[4] += E;
253425bb815Sopenharmony_ci
254425bb815Sopenharmony_ci#undef JERRYX_SHA1_SHIFT
255425bb815Sopenharmony_ci#undef JERRYX_SHA1_R
256425bb815Sopenharmony_ci} /* jerryx_sha1_process */
257425bb815Sopenharmony_ci
258425bb815Sopenharmony_ci#undef JERRYX_SHA1_P
259425bb815Sopenharmony_ci
260425bb815Sopenharmony_ci/**
261425bb815Sopenharmony_ci * SHA-1 update buffer.
262425bb815Sopenharmony_ci */
263425bb815Sopenharmony_cistatic void
264425bb815Sopenharmony_cijerryx_sha1_update (jerryx_sha1_context *sha1_context_p, /**< SHA-1 context */
265425bb815Sopenharmony_ci                    const uint8_t *source_p, /**< source buffer */
266425bb815Sopenharmony_ci                    size_t source_length) /**< length of source buffer */
267425bb815Sopenharmony_ci{
268425bb815Sopenharmony_ci  size_t fill;
269425bb815Sopenharmony_ci  uint32_t left;
270425bb815Sopenharmony_ci
271425bb815Sopenharmony_ci  if (source_length == 0)
272425bb815Sopenharmony_ci  {
273425bb815Sopenharmony_ci    return;
274425bb815Sopenharmony_ci  }
275425bb815Sopenharmony_ci
276425bb815Sopenharmony_ci  left = sha1_context_p->total[0] & 0x3F;
277425bb815Sopenharmony_ci  fill = 64 - left;
278425bb815Sopenharmony_ci
279425bb815Sopenharmony_ci  sha1_context_p->total[0] += (uint32_t) source_length;
280425bb815Sopenharmony_ci
281425bb815Sopenharmony_ci  /* Check overflow. */
282425bb815Sopenharmony_ci  if (sha1_context_p->total[0] < (uint32_t) source_length)
283425bb815Sopenharmony_ci  {
284425bb815Sopenharmony_ci    sha1_context_p->total[1]++;
285425bb815Sopenharmony_ci  }
286425bb815Sopenharmony_ci
287425bb815Sopenharmony_ci  if (left && source_length >= fill)
288425bb815Sopenharmony_ci  {
289425bb815Sopenharmony_ci    memcpy ((void *) (sha1_context_p->buffer + left), source_p, fill);
290425bb815Sopenharmony_ci    jerryx_sha1_process (sha1_context_p, sha1_context_p->buffer);
291425bb815Sopenharmony_ci    source_p += fill;
292425bb815Sopenharmony_ci    source_length -= fill;
293425bb815Sopenharmony_ci    left = 0;
294425bb815Sopenharmony_ci  }
295425bb815Sopenharmony_ci
296425bb815Sopenharmony_ci  while (source_length >= 64)
297425bb815Sopenharmony_ci  {
298425bb815Sopenharmony_ci    jerryx_sha1_process (sha1_context_p, source_p);
299425bb815Sopenharmony_ci    source_p += 64;
300425bb815Sopenharmony_ci    source_length -= 64;
301425bb815Sopenharmony_ci  }
302425bb815Sopenharmony_ci
303425bb815Sopenharmony_ci  if (source_length > 0)
304425bb815Sopenharmony_ci  {
305425bb815Sopenharmony_ci    memcpy ((void *) (sha1_context_p->buffer + left), source_p, source_length);
306425bb815Sopenharmony_ci  }
307425bb815Sopenharmony_ci} /* jerryx_sha1_update */
308425bb815Sopenharmony_ci
309425bb815Sopenharmony_ci/**
310425bb815Sopenharmony_ci * SHA-1 final digest.
311425bb815Sopenharmony_ci */
312425bb815Sopenharmony_cistatic void
313425bb815Sopenharmony_cijerryx_sha1_finish (jerryx_sha1_context *sha1_context_p, /**< SHA-1 context */
314425bb815Sopenharmony_ci                    uint8_t destination_p[20]) /**< result */
315425bb815Sopenharmony_ci{
316425bb815Sopenharmony_ci  uint8_t buffer[16];
317425bb815Sopenharmony_ci
318425bb815Sopenharmony_ci  uint32_t high = (sha1_context_p->total[0] >> 29) | (sha1_context_p->total[1] << 3);
319425bb815Sopenharmony_ci  uint32_t low = (sha1_context_p->total[0] << 3);
320425bb815Sopenharmony_ci
321425bb815Sopenharmony_ci  uint32_t last = sha1_context_p->total[0] & 0x3F;
322425bb815Sopenharmony_ci  uint32_t padn = (last < 56) ? (56 - last) : (120 - last);
323425bb815Sopenharmony_ci
324425bb815Sopenharmony_ci  memset (buffer, 0, sizeof (buffer));
325425bb815Sopenharmony_ci  buffer[0] = 0x80;
326425bb815Sopenharmony_ci
327425bb815Sopenharmony_ci  while (padn > sizeof (buffer))
328425bb815Sopenharmony_ci  {
329425bb815Sopenharmony_ci    jerryx_sha1_update (sha1_context_p, buffer, sizeof (buffer));
330425bb815Sopenharmony_ci    buffer[0] = 0;
331425bb815Sopenharmony_ci    padn -= (uint32_t) sizeof (buffer);
332425bb815Sopenharmony_ci  }
333425bb815Sopenharmony_ci
334425bb815Sopenharmony_ci  jerryx_sha1_update (sha1_context_p, buffer, padn);
335425bb815Sopenharmony_ci
336425bb815Sopenharmony_ci  JERRYX_SHA1_PUT_UINT32_BE (high, buffer, 0);
337425bb815Sopenharmony_ci  JERRYX_SHA1_PUT_UINT32_BE (low, buffer, 4);
338425bb815Sopenharmony_ci
339425bb815Sopenharmony_ci  jerryx_sha1_update (sha1_context_p, buffer, 8);
340425bb815Sopenharmony_ci
341425bb815Sopenharmony_ci  JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[0], destination_p, 0);
342425bb815Sopenharmony_ci  JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[1], destination_p, 4);
343425bb815Sopenharmony_ci  JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[2], destination_p, 8);
344425bb815Sopenharmony_ci  JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[3], destination_p, 12);
345425bb815Sopenharmony_ci  JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[4], destination_p, 16);
346425bb815Sopenharmony_ci} /* jerryx_sha1_finish */
347425bb815Sopenharmony_ci
348425bb815Sopenharmony_ci#undef JERRYX_SHA1_GET_UINT32_BE
349425bb815Sopenharmony_ci#undef JERRYX_SHA1_PUT_UINT32_BE
350425bb815Sopenharmony_ci
351425bb815Sopenharmony_ci/**
352425bb815Sopenharmony_ci * Computes the SHA-1 value of the combination of the two input buffers.
353425bb815Sopenharmony_ci */
354425bb815Sopenharmony_civoid
355425bb815Sopenharmony_cijerryx_debugger_compute_sha1 (const uint8_t *source1_p, /**< first part of the input */
356425bb815Sopenharmony_ci                              size_t source1_length, /**< length of the first part */
357425bb815Sopenharmony_ci                              const uint8_t *source2_p, /**< second part of the input */
358425bb815Sopenharmony_ci                              size_t source2_length, /**< length of the second part */
359425bb815Sopenharmony_ci                              uint8_t destination_p[20]) /**< result */
360425bb815Sopenharmony_ci{
361425bb815Sopenharmony_ci  jerryx_sha1_context sha1_context;
362425bb815Sopenharmony_ci
363425bb815Sopenharmony_ci  jerryx_sha1_init (&sha1_context);
364425bb815Sopenharmony_ci  jerryx_sha1_update (&sha1_context, source1_p, source1_length);
365425bb815Sopenharmony_ci  jerryx_sha1_update (&sha1_context, source2_p, source2_length);
366425bb815Sopenharmony_ci  jerryx_sha1_finish (&sha1_context, destination_p);
367425bb815Sopenharmony_ci} /* jerryx_debugger_compute_sha1 */
368425bb815Sopenharmony_ci
369425bb815Sopenharmony_ci#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
370