v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
sha-256.cc
Go to the documentation of this file.
1// Copyright 2022 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Copyright 2013 Google Inc.
6//
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may 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,
15// WITHOUT 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//
20// Optimized for minimal code size.
21//
22// This code originates from the Omaha installer for Windows but is
23// reduced in complexity. Changes made are outlined in the header file.
24
25#include "src/utils/sha-256.h"
26
27#include <stdint.h>
28#include <string.h>
29
30#define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
31#define shr(value, bits) ((value) >> (bits))
32
33namespace v8 {
34namespace internal {
35
36static const uint32_t K[64] = {
37 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
38 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
39 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
40 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
41 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
42 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
43 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
44 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
45 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
46 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
47 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
48
50 uint32_t W[64];
51 uint32_t A, B, C, D, E, F, G, H;
52 uint8_t* p = ctx->buf;
53 int t;
54
55 for (t = 0; t < 16; ++t) {
56 uint32_t tmp = (uint32_t)*p++ << 24;
57 tmp |= (uint32_t)*p++ << 16;
58 tmp |= (uint32_t)*p++ << 8;
59 tmp |= (uint32_t)*p++;
60 W[t] = tmp;
61 }
62
63 for (; t < 64; t++) {
64 uint32_t s0 = ror(W[t - 15], 7) ^ ror(W[t - 15], 18) ^ shr(W[t - 15], 3);
65 uint32_t s1 = ror(W[t - 2], 17) ^ ror(W[t - 2], 19) ^ shr(W[t - 2], 10);
66 W[t] = W[t - 16] + s0 + W[t - 7] + s1;
67 }
68
69 A = ctx->state[0];
70 B = ctx->state[1];
71 C = ctx->state[2];
72 D = ctx->state[3];
73 E = ctx->state[4];
74 F = ctx->state[5];
75 G = ctx->state[6];
76 H = ctx->state[7];
77
78 for (t = 0; t < 64; t++) {
79 uint32_t s0 = ror(A, 2) ^ ror(A, 13) ^ ror(A, 22);
80 uint32_t maj = (A & B) ^ (A & C) ^ (B & C);
81 uint32_t t2 = s0 + maj;
82 uint32_t s1 = ror(E, 6) ^ ror(E, 11) ^ ror(E, 25);
83 uint32_t ch = (E & F) ^ ((~E) & G);
84 uint32_t t1 = H + s1 + ch + K[t] + W[t];
85
86 H = G;
87 G = F;
88 F = E;
89 E = D + t1;
90 D = C;
91 C = B;
92 B = A;
93 A = t1 + t2;
94 }
95
96 ctx->state[0] += A;
97 ctx->state[1] += B;
98 ctx->state[2] += C;
99 ctx->state[3] += D;
100 ctx->state[4] += E;
101 ctx->state[5] += F;
102 ctx->state[6] += G;
103 ctx->state[7] += H;
104}
105
109
111 ctx->f = &SHA256_VTAB;
112 ctx->state[0] = 0x6a09e667;
113 ctx->state[1] = 0xbb67ae85;
114 ctx->state[2] = 0x3c6ef372;
115 ctx->state[3] = 0xa54ff53a;
116 ctx->state[4] = 0x510e527f;
117 ctx->state[5] = 0x9b05688c;
118 ctx->state[6] = 0x1f83d9ab;
119 ctx->state[7] = 0x5be0cd19;
120 ctx->count = 0;
121}
122
123void SHA256_update(LITE_SHA256_CTX* ctx, const void* data, size_t len) {
124 int i = static_cast<int>(ctx->count & 63);
125 const uint8_t* p = (const uint8_t*)data;
126
127 ctx->count += len;
128
129 while (len--) {
130 ctx->buf[i++] = *p++;
131 if (i == 64) {
132 SHA256_Transform(ctx);
133 i = 0;
134 }
135 }
136}
137
138const uint8_t* SHA256_final(LITE_SHA256_CTX* ctx) {
139 uint8_t* p = ctx->buf;
140 uint64_t cnt = LITE_LShiftU64(ctx->count, 3);
141 int i;
142
143 const uint8_t completion[] { 0x80, 0 };
144
145 SHA256_update(ctx, &completion[0], 1);
146 while ((ctx->count & 63) != 56) {
147 SHA256_update(ctx, &completion[1], 1);
148 }
149 for (i = 0; i < 8; ++i) {
150 uint8_t tmp = (uint8_t)LITE_RShiftU64(cnt, 56);
151 cnt = LITE_LShiftU64(cnt, 8);
152 SHA256_update(ctx, &tmp, 1);
153 }
154
155 for (i = 0; i < 8; i++) {
156 uint32_t tmp = ctx->state[i];
157 *p++ = (uint8_t)(tmp >> 24);
158 *p++ = (uint8_t)(tmp >> 16);
159 *p++ = (uint8_t)(tmp >> 8);
160 *p++ = (uint8_t)(tmp >> 0);
161 }
162
163 return ctx->buf;
164}
165
166/* Convenience function */
167const uint8_t* SHA256_hash(const void* data, size_t len, uint8_t* digest) {
168 LITE_SHA256_CTX ctx;
169 SHA256_init(&ctx);
170 SHA256_update(&ctx, data, len);
171 memcpy(digest, SHA256_final(&ctx), kSizeOfSha256Digest);
172 return digest;
173}
174
175} // namespace internal
176} // namespace v8
Register tmp
constexpr int W
constexpr int H
const uint8_t * SHA256_final(LITE_SHA256_CTX *ctx)
Definition sha-256.cc:138
static const HASH_VTAB SHA256_VTAB
Definition sha-256.cc:106
constexpr int B
static const uint32_t K[64]
Definition sha-256.cc:36
void SHA256_init(LITE_SHA256_CTX *ctx)
Definition sha-256.cc:110
const uint8_t * SHA256_hash(const void *data, size_t len, uint8_t *digest)
Definition sha-256.cc:167
void SHA256_update(LITE_SHA256_CTX *ctx, const void *data, size_t len)
Definition sha-256.cc:123
static void SHA256_Transform(LITE_SHA256_CTX *ctx)
Definition sha-256.cc:49
constexpr int A
#define shr(value, bits)
Definition sha-256.cc:31
#define ror(value, bits)
Definition sha-256.cc:30
#define LITE_RShiftU64(a, b)
Definition sha-256.h:41
#define LITE_LShiftU64(a, b)
Definition sha-256.h:40
const size_t kSizeOfSha256Digest
Definition sha-256.h:43
const HASH_VTAB * f
Definition sha-256.h:58
uint32_t state[8]
Definition sha-256.h:61
uint8_t buf[64]
Definition sha-256.h:60