131c75014Sopenharmony_ci/*
231c75014Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
331c75014Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
431c75014Sopenharmony_ci * you may not use this file except in compliance with the License.
531c75014Sopenharmony_ci * You may obtain a copy of the License at
631c75014Sopenharmony_ci *
731c75014Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
831c75014Sopenharmony_ci *
931c75014Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1031c75014Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1131c75014Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1231c75014Sopenharmony_ci * See the License for the specific language governing permissions and
1331c75014Sopenharmony_ci * limitations under the License.
1431c75014Sopenharmony_ci */
1531c75014Sopenharmony_ci
1631c75014Sopenharmony_ci/*
1731c75014Sopenharmony_ci * Description: 计算文件SHA256校验和的基本方法
1831c75014Sopenharmony_ci */
1931c75014Sopenharmony_ci
2031c75014Sopenharmony_ci#include "checksum_sha256.h"
2131c75014Sopenharmony_ci
2231c75014Sopenharmony_ci#include <memory.h>
2331c75014Sopenharmony_ci#include <stdlib.h>
2431c75014Sopenharmony_ci
2531c75014Sopenharmony_cistatic const unsigned int G_DEFAULT_KEY[64] = {
2631c75014Sopenharmony_ci    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
2731c75014Sopenharmony_ci    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
2831c75014Sopenharmony_ci    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
2931c75014Sopenharmony_ci    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
3031c75014Sopenharmony_ci    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
3131c75014Sopenharmony_ci    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
3231c75014Sopenharmony_ci    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
3331c75014Sopenharmony_ci    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
3431c75014Sopenharmony_ci};
3531c75014Sopenharmony_ci
3631c75014Sopenharmony_cienum WordIndex {
3731c75014Sopenharmony_ci    A = 0, B, C, D, E, F, G, H
3831c75014Sopenharmony_ci};
3931c75014Sopenharmony_ci
4031c75014Sopenharmony_cienum NumIndex {
4131c75014Sopenharmony_ci    ZERO = 0, ONE, TWO, THREE, FOUR
4231c75014Sopenharmony_ci};
4331c75014Sopenharmony_ci
4431c75014Sopenharmony_ci/**
4531c75014Sopenharmony_ci * 右旋转
4631c75014Sopenharmony_ci */
4731c75014Sopenharmony_ciunsigned int RotateRight(unsigned int w, int n)
4831c75014Sopenharmony_ci{
4931c75014Sopenharmony_ci    int len = 32;
5031c75014Sopenharmony_ci    return (w >> n) | ((w)<<(len - n));
5131c75014Sopenharmony_ci}
5231c75014Sopenharmony_ci
5331c75014Sopenharmony_ci/**
5431c75014Sopenharmony_ci * 左旋转
5531c75014Sopenharmony_ci */
5631c75014Sopenharmony_ciunsigned int RotateLeft(unsigned int w, int n)
5731c75014Sopenharmony_ci{
5831c75014Sopenharmony_ci    int len = 32;
5931c75014Sopenharmony_ci    return (w<<n) | ((w)>>(len - n));
6031c75014Sopenharmony_ci}
6131c75014Sopenharmony_ci
6231c75014Sopenharmony_ci/**
6331c75014Sopenharmony_ci * 右移位
6431c75014Sopenharmony_ci */
6531c75014Sopenharmony_ciunsigned int ShiftRight(unsigned int w, int n)
6631c75014Sopenharmony_ci{
6731c75014Sopenharmony_ci    return w >> n;
6831c75014Sopenharmony_ci}
6931c75014Sopenharmony_ci
7031c75014Sopenharmony_ci/**
7131c75014Sopenharmony_ci * 计算SHA256中的参数Sigma0
7231c75014Sopenharmony_ci */
7331c75014Sopenharmony_ciunsigned int CalcSigma0(unsigned int x)
7431c75014Sopenharmony_ci{
7531c75014Sopenharmony_ci    unsigned int shift[] = {7, 18, 3};
7631c75014Sopenharmony_ci    return RotateRight(x, shift[ZERO]) ^ RotateRight(x, shift[ONE]) ^ ShiftRight(x, shift[TWO]);
7731c75014Sopenharmony_ci}
7831c75014Sopenharmony_ci
7931c75014Sopenharmony_ci/**
8031c75014Sopenharmony_ci * 计算SHA256中的参数Sigma1
8131c75014Sopenharmony_ci */
8231c75014Sopenharmony_ciunsigned int CalcSigma1(unsigned int x)
8331c75014Sopenharmony_ci{
8431c75014Sopenharmony_ci    unsigned int shift[] = {17, 19, 10};
8531c75014Sopenharmony_ci    return RotateRight(x, shift[ZERO]) ^ RotateRight(x, shift[ONE]) ^ ShiftRight(x, shift[TWO]);
8631c75014Sopenharmony_ci}
8731c75014Sopenharmony_ci
8831c75014Sopenharmony_ci/**
8931c75014Sopenharmony_ci * 计算SH256中的参数Ch
9031c75014Sopenharmony_ci */
9131c75014Sopenharmony_ciunsigned int CalcChValue(unsigned int x, unsigned int y, unsigned int z)
9231c75014Sopenharmony_ci{
9331c75014Sopenharmony_ci    return (x & y) ^ (~(x) & z);
9431c75014Sopenharmony_ci}
9531c75014Sopenharmony_ci
9631c75014Sopenharmony_ci/**
9731c75014Sopenharmony_ci * 计算SH256中的参数MAJ
9831c75014Sopenharmony_ci */
9931c75014Sopenharmony_ciunsigned int CalcMajValue(unsigned int x, unsigned int y, unsigned int z)
10031c75014Sopenharmony_ci{
10131c75014Sopenharmony_ci    return (x & y) ^ (x & z) ^ (y & z);
10231c75014Sopenharmony_ci}
10331c75014Sopenharmony_ci
10431c75014Sopenharmony_ci/**
10531c75014Sopenharmony_ci * 计算SHA256中的参数Ep0
10631c75014Sopenharmony_ci */
10731c75014Sopenharmony_ciunsigned int CalcEp0Value(unsigned int x)
10831c75014Sopenharmony_ci{
10931c75014Sopenharmony_ci    unsigned int shift[] = {2, 13, 22};
11031c75014Sopenharmony_ci    return RotateRight(x, shift[ZERO]) ^ RotateRight(x, shift[ONE]) ^ RotateRight(x, shift[TWO]);
11131c75014Sopenharmony_ci}
11231c75014Sopenharmony_ci
11331c75014Sopenharmony_ci/**
11431c75014Sopenharmony_ci * 计算SHA256中的参数Ep1
11531c75014Sopenharmony_ci */
11631c75014Sopenharmony_ciunsigned int CalcEp1Value(unsigned int x)
11731c75014Sopenharmony_ci{
11831c75014Sopenharmony_ci    unsigned int shift[] = {6, 11, 25};
11931c75014Sopenharmony_ci    return RotateRight(x, shift[ZERO]) ^ RotateRight(x, shift[ONE]) ^ RotateRight(x, shift[TWO]);
12031c75014Sopenharmony_ci}
12131c75014Sopenharmony_ci
12231c75014Sopenharmony_ci/**
12331c75014Sopenharmony_ci * 初始化SHA256方法
12431c75014Sopenharmony_ci */
12531c75014Sopenharmony_civoid InitSha256(MesgDigest *md)
12631c75014Sopenharmony_ci{
12731c75014Sopenharmony_ci    int len = 8;
12831c75014Sopenharmony_ci    unsigned int hashInit[] = {
12931c75014Sopenharmony_ci        0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
13031c75014Sopenharmony_ci    };
13131c75014Sopenharmony_ci    md->dataLen = 0;
13231c75014Sopenharmony_ci    md->bitLen = 0;
13331c75014Sopenharmony_ci
13431c75014Sopenharmony_ci    for (int i = 0; i < len; i++) {
13531c75014Sopenharmony_ci        md->hash[i] = hashInit[i];
13631c75014Sopenharmony_ci    }
13731c75014Sopenharmony_ci}
13831c75014Sopenharmony_ci
13931c75014Sopenharmony_ci/**
14031c75014Sopenharmony_ci * 计算字符数组的SHA256校验和
14131c75014Sopenharmony_ci */
14231c75014Sopenharmony_civoid CalcSha256(MesgDigest *md, unsigned char* mesg)
14331c75014Sopenharmony_ci{
14431c75014Sopenharmony_ci    int len = 8;
14531c75014Sopenharmony_ci    int wordLen = 64;
14631c75014Sopenharmony_ci    unsigned int t1, t2, t3, t4;
14731c75014Sopenharmony_ci    unsigned int word[wordLen], hash[len];
14831c75014Sopenharmony_ci    unsigned int hashT1, hashT2;
14931c75014Sopenharmony_ci    int j = 0;
15031c75014Sopenharmony_ci    int lenList[] = {4, 8, 16, 24};
15131c75014Sopenharmony_ci    int pos[] = {15, 2, 7, 16};
15231c75014Sopenharmony_ci
15331c75014Sopenharmony_ci    for (int i = 0; i < wordLen; i++) {
15431c75014Sopenharmony_ci        if (i < lenList[TWO]) {
15531c75014Sopenharmony_ci            t1 = mesg[j] << lenList[THREE];
15631c75014Sopenharmony_ci            t2 = mesg[j + ONE] << lenList[TWO];
15731c75014Sopenharmony_ci            t3 = mesg[j + TWO] << lenList[ONE];
15831c75014Sopenharmony_ci            t4 = mesg[j + THREE];
15931c75014Sopenharmony_ci            word[i] = t1 | t2 | t3 | t4;
16031c75014Sopenharmony_ci            j += lenList[ZERO];
16131c75014Sopenharmony_ci        } else {
16231c75014Sopenharmony_ci            unsigned int s0 = CalcSigma0(word[i - pos[ZERO]]);
16331c75014Sopenharmony_ci            unsigned int s1 = CalcSigma1(word[i - pos[ONE]]);
16431c75014Sopenharmony_ci            word[i] = s1 + word[i - pos[TWO]] + s0 + word[i - pos[THREE]];
16531c75014Sopenharmony_ci        }
16631c75014Sopenharmony_ci    }
16731c75014Sopenharmony_ci
16831c75014Sopenharmony_ci    for (int i = 0; i < len; i++) {
16931c75014Sopenharmony_ci        hash[i] = md->hash[i];
17031c75014Sopenharmony_ci    }
17131c75014Sopenharmony_ci
17231c75014Sopenharmony_ci    for (int i = 0; i < wordLen; ++i) {
17331c75014Sopenharmony_ci        hashT1 = hash[H] + CalcEp1Value(hash[E]) + CalcChValue(hash[E], hash[F], hash[G]) + G_DEFAULT_KEY[i] + word[i];
17431c75014Sopenharmony_ci        hashT2 = CalcEp0Value(hash[A]) + CalcMajValue(hash[A], hash[B], hash[C]);
17531c75014Sopenharmony_ci        hash[H] = hash[G];
17631c75014Sopenharmony_ci        hash[G] = hash[F];
17731c75014Sopenharmony_ci        hash[F] = hash[E];
17831c75014Sopenharmony_ci        hash[E] = hash[D] + hashT1;
17931c75014Sopenharmony_ci        hash[D] = hash[C];
18031c75014Sopenharmony_ci        hash[C] = hash[B];
18131c75014Sopenharmony_ci        hash[B] = hash[A];
18231c75014Sopenharmony_ci        hash[A] = hashT1 + hashT2;
18331c75014Sopenharmony_ci    }
18431c75014Sopenharmony_ci
18531c75014Sopenharmony_ci    for (int i = 0; i < len; i++) {
18631c75014Sopenharmony_ci        md->hash[i] += hash[i];
18731c75014Sopenharmony_ci    }
18831c75014Sopenharmony_ci}