1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 /*
17 * Description: 计算文件SHA256校验和的基本方法
18 */
19
20 #include "checksum_sha256.h"
21
22 #include <memory.h>
23 #include <stdlib.h>
24
25 static const unsigned int G_DEFAULT_KEY[64] = {
26 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
27 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
28 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
29 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
30 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
31 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
32 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
33 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
34 };
35
36 enum WordIndex {
37 A = 0, B, C, D, E, F, G, H
38 };
39
40 enum NumIndex {
41 ZERO = 0, ONE, TWO, THREE, FOUR
42 };
43
44 /**
45 * 右旋转
46 */
RotateRight(unsigned int w,int n)47 unsigned int RotateRight(unsigned int w, int n)
48 {
49 int len = 32;
50 return (w >> n) | ((w)<<(len - n));
51 }
52
53 /**
54 * 左旋转
55 */
RotateLeft(unsigned int w,int n)56 unsigned int RotateLeft(unsigned int w, int n)
57 {
58 int len = 32;
59 return (w<<n) | ((w)>>(len - n));
60 }
61
62 /**
63 * 右移位
64 */
ShiftRight(unsigned int w,int n)65 unsigned int ShiftRight(unsigned int w, int n)
66 {
67 return w >> n;
68 }
69
70 /**
71 * 计算SHA256中的参数Sigma0
72 */
CalcSigma0(unsigned int x)73 unsigned int CalcSigma0(unsigned int x)
74 {
75 unsigned int shift[] = {7, 18, 3};
76 return RotateRight(x, shift[ZERO]) ^ RotateRight(x, shift[ONE]) ^ ShiftRight(x, shift[TWO]);
77 }
78
79 /**
80 * 计算SHA256中的参数Sigma1
81 */
CalcSigma1(unsigned int x)82 unsigned int CalcSigma1(unsigned int x)
83 {
84 unsigned int shift[] = {17, 19, 10};
85 return RotateRight(x, shift[ZERO]) ^ RotateRight(x, shift[ONE]) ^ ShiftRight(x, shift[TWO]);
86 }
87
88 /**
89 * 计算SH256中的参数Ch
90 */
CalcChValue(unsigned int x,unsigned int y,unsigned int z)91 unsigned int CalcChValue(unsigned int x, unsigned int y, unsigned int z)
92 {
93 return (x & y) ^ (~(x) & z);
94 }
95
96 /**
97 * 计算SH256中的参数MAJ
98 */
CalcMajValue(unsigned int x,unsigned int y,unsigned int z)99 unsigned int CalcMajValue(unsigned int x, unsigned int y, unsigned int z)
100 {
101 return (x & y) ^ (x & z) ^ (y & z);
102 }
103
104 /**
105 * 计算SHA256中的参数Ep0
106 */
CalcEp0Value(unsigned int x)107 unsigned int CalcEp0Value(unsigned int x)
108 {
109 unsigned int shift[] = {2, 13, 22};
110 return RotateRight(x, shift[ZERO]) ^ RotateRight(x, shift[ONE]) ^ RotateRight(x, shift[TWO]);
111 }
112
113 /**
114 * 计算SHA256中的参数Ep1
115 */
CalcEp1Value(unsigned int x)116 unsigned int CalcEp1Value(unsigned int x)
117 {
118 unsigned int shift[] = {6, 11, 25};
119 return RotateRight(x, shift[ZERO]) ^ RotateRight(x, shift[ONE]) ^ RotateRight(x, shift[TWO]);
120 }
121
122 /**
123 * 初始化SHA256方法
124 */
InitSha256(MesgDigest * md)125 void InitSha256(MesgDigest *md)
126 {
127 int len = 8;
128 unsigned int hashInit[] = {
129 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
130 };
131 md->dataLen = 0;
132 md->bitLen = 0;
133
134 for (int i = 0; i < len; i++) {
135 md->hash[i] = hashInit[i];
136 }
137 }
138
139 /**
140 * 计算字符数组的SHA256校验和
141 */
CalcSha256(MesgDigest * md,unsigned char * mesg)142 void CalcSha256(MesgDigest *md, unsigned char* mesg)
143 {
144 int len = 8;
145 int wordLen = 64;
146 unsigned int t1, t2, t3, t4;
147 unsigned int word[wordLen], hash[len];
148 unsigned int hashT1, hashT2;
149 int j = 0;
150 int lenList[] = {4, 8, 16, 24};
151 int pos[] = {15, 2, 7, 16};
152
153 for (int i = 0; i < wordLen; i++) {
154 if (i < lenList[TWO]) {
155 t1 = mesg[j] << lenList[THREE];
156 t2 = mesg[j + ONE] << lenList[TWO];
157 t3 = mesg[j + TWO] << lenList[ONE];
158 t4 = mesg[j + THREE];
159 word[i] = t1 | t2 | t3 | t4;
160 j += lenList[ZERO];
161 } else {
162 unsigned int s0 = CalcSigma0(word[i - pos[ZERO]]);
163 unsigned int s1 = CalcSigma1(word[i - pos[ONE]]);
164 word[i] = s1 + word[i - pos[TWO]] + s0 + word[i - pos[THREE]];
165 }
166 }
167
168 for (int i = 0; i < len; i++) {
169 hash[i] = md->hash[i];
170 }
171
172 for (int i = 0; i < wordLen; ++i) {
173 hashT1 = hash[H] + CalcEp1Value(hash[E]) + CalcChValue(hash[E], hash[F], hash[G]) + G_DEFAULT_KEY[i] + word[i];
174 hashT2 = CalcEp0Value(hash[A]) + CalcMajValue(hash[A], hash[B], hash[C]);
175 hash[H] = hash[G];
176 hash[G] = hash[F];
177 hash[F] = hash[E];
178 hash[E] = hash[D] + hashT1;
179 hash[D] = hash[C];
180 hash[C] = hash[B];
181 hash[B] = hash[A];
182 hash[A] = hashT1 + hashT2;
183 }
184
185 for (int i = 0; i < len; i++) {
186 md->hash[i] += hash[i];
187 }
188 }