16cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26cd6a6acSopenharmony_ci//  LibSha1
36cd6a6acSopenharmony_ci//
46cd6a6acSopenharmony_ci//  Implementation of SHA1 hash function.
56cd6a6acSopenharmony_ci//  Original author:  Steve Reid <sreid@sea-to-sky.net>
66cd6a6acSopenharmony_ci//  Contributions by: James H. Brown <jbrown@burgoyne.com>, Saul Kravitz <Saul.Kravitz@celera.com>,
76cd6a6acSopenharmony_ci//  and Ralph Giles <giles@ghostscript.com>
86cd6a6acSopenharmony_ci//  Modified by WaterJuice retaining Public Domain license.
96cd6a6acSopenharmony_ci//
106cd6a6acSopenharmony_ci//  This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
116cd6a6acSopenharmony_ci//  Modified to:
126cd6a6acSopenharmony_ci//    - stop symbols being exported for libselinux shared library - October 2015
136cd6a6acSopenharmony_ci//								       Richard Haines <richard_c_haines@btinternet.com>
146cd6a6acSopenharmony_ci//    - Not cast the workspace from a byte array to a CHAR64LONG16 due to alignment issues.
156cd6a6acSopenharmony_ci//      Fixes:
166cd6a6acSopenharmony_ci//        sha1.c:73:33: error: cast from 'uint8_t *' (aka 'unsigned char *') to 'CHAR64LONG16 *' increases required alignment from 1 to 4 [-Werror,-Wcast-align]
176cd6a6acSopenharmony_ci//             CHAR64LONG16*       block = (CHAR64LONG16*) workspace;
186cd6a6acSopenharmony_ci//                                                                     William Roberts <william.c.roberts@intel.com>
196cd6a6acSopenharmony_ci//    - Silence clang's -Wextra-semi-stmt warning - July 2021, Nicolas Iooss <nicolas.iooss@m4x.org>
206cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
216cd6a6acSopenharmony_ci
226cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
236cd6a6acSopenharmony_ci//  IMPORTS
246cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
256cd6a6acSopenharmony_ci
266cd6a6acSopenharmony_ci#include "sha1.h"
276cd6a6acSopenharmony_ci#include <memory.h>
286cd6a6acSopenharmony_ci
296cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
306cd6a6acSopenharmony_ci//  TYPES
316cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
326cd6a6acSopenharmony_ci
336cd6a6acSopenharmony_citypedef union
346cd6a6acSopenharmony_ci{
356cd6a6acSopenharmony_ci    uint8_t     c [64];
366cd6a6acSopenharmony_ci    uint32_t    l [16];
376cd6a6acSopenharmony_ci} CHAR64LONG16;
386cd6a6acSopenharmony_ci
396cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
406cd6a6acSopenharmony_ci//  INTERNAL FUNCTIONS
416cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
426cd6a6acSopenharmony_ci
436cd6a6acSopenharmony_ci#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
446cd6a6acSopenharmony_ci
456cd6a6acSopenharmony_ci// blk0() and blk() perform the initial expand.
466cd6a6acSopenharmony_ci#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
476cd6a6acSopenharmony_ci    |(rol(block->l[i],8)&0x00FF00FF))
486cd6a6acSopenharmony_ci
496cd6a6acSopenharmony_ci#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
506cd6a6acSopenharmony_ci    ^block->l[(i+2)&15]^block->l[i&15],1))
516cd6a6acSopenharmony_ci
526cd6a6acSopenharmony_ci// (R0+R1), R2, R3, R4 are the different operations used in SHA1
536cd6a6acSopenharmony_ci#define R0(v,w,x,y,z,i)  do { z += ((w&(x^y))^y)     + blk0(i)+ 0x5A827999 + rol(v,5); w=rol(w,30); } while (0)
546cd6a6acSopenharmony_ci#define R1(v,w,x,y,z,i)  do { z += ((w&(x^y))^y)     + blk(i) + 0x5A827999 + rol(v,5); w=rol(w,30); } while (0)
556cd6a6acSopenharmony_ci#define R2(v,w,x,y,z,i)  do { z += (w^x^y)           + blk(i) + 0x6ED9EBA1 + rol(v,5); w=rol(w,30); } while (0)
566cd6a6acSopenharmony_ci#define R3(v,w,x,y,z,i)  do { z += (((w|x)&y)|(w&x)) + blk(i) + 0x8F1BBCDC + rol(v,5); w=rol(w,30); } while (0)
576cd6a6acSopenharmony_ci#define R4(v,w,x,y,z,i)  do { z += (w^x^y)           + blk(i) + 0xCA62C1D6 + rol(v,5); w=rol(w,30); } while (0)
586cd6a6acSopenharmony_ci
596cd6a6acSopenharmony_ci
606cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
616cd6a6acSopenharmony_ci//  TransformFunction
626cd6a6acSopenharmony_ci//
636cd6a6acSopenharmony_ci//  Hash a single 512-bit block. This is the core of the algorithm
646cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
656cd6a6acSopenharmony_cistatic
666cd6a6acSopenharmony_civoid
676cd6a6acSopenharmony_ci    TransformFunction
686cd6a6acSopenharmony_ci    (
696cd6a6acSopenharmony_ci        uint32_t            state[5],
706cd6a6acSopenharmony_ci        const uint8_t       buffer[64]
716cd6a6acSopenharmony_ci    )
726cd6a6acSopenharmony_ci{
736cd6a6acSopenharmony_ci    uint32_t            a;
746cd6a6acSopenharmony_ci    uint32_t            b;
756cd6a6acSopenharmony_ci    uint32_t            c;
766cd6a6acSopenharmony_ci    uint32_t            d;
776cd6a6acSopenharmony_ci    uint32_t            e;
786cd6a6acSopenharmony_ci    CHAR64LONG16        workspace;
796cd6a6acSopenharmony_ci    CHAR64LONG16*       block = &workspace;
806cd6a6acSopenharmony_ci
816cd6a6acSopenharmony_ci    memcpy(block, buffer, 64);
826cd6a6acSopenharmony_ci
836cd6a6acSopenharmony_ci    // Copy context->state[] to working vars
846cd6a6acSopenharmony_ci    a = state[0];
856cd6a6acSopenharmony_ci    b = state[1];
866cd6a6acSopenharmony_ci    c = state[2];
876cd6a6acSopenharmony_ci    d = state[3];
886cd6a6acSopenharmony_ci    e = state[4];
896cd6a6acSopenharmony_ci
906cd6a6acSopenharmony_ci    // 4 rounds of 20 operations each. Loop unrolled.
916cd6a6acSopenharmony_ci    R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
926cd6a6acSopenharmony_ci    R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
936cd6a6acSopenharmony_ci    R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
946cd6a6acSopenharmony_ci    R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
956cd6a6acSopenharmony_ci    R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
966cd6a6acSopenharmony_ci    R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
976cd6a6acSopenharmony_ci    R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
986cd6a6acSopenharmony_ci    R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
996cd6a6acSopenharmony_ci    R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
1006cd6a6acSopenharmony_ci    R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
1016cd6a6acSopenharmony_ci    R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
1026cd6a6acSopenharmony_ci    R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
1036cd6a6acSopenharmony_ci    R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
1046cd6a6acSopenharmony_ci    R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
1056cd6a6acSopenharmony_ci    R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
1066cd6a6acSopenharmony_ci    R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
1076cd6a6acSopenharmony_ci    R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
1086cd6a6acSopenharmony_ci    R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
1096cd6a6acSopenharmony_ci    R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
1106cd6a6acSopenharmony_ci    R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
1116cd6a6acSopenharmony_ci
1126cd6a6acSopenharmony_ci    // Add the working vars back into context.state[]
1136cd6a6acSopenharmony_ci    state[0] += a;
1146cd6a6acSopenharmony_ci    state[1] += b;
1156cd6a6acSopenharmony_ci    state[2] += c;
1166cd6a6acSopenharmony_ci    state[3] += d;
1176cd6a6acSopenharmony_ci    state[4] += e;
1186cd6a6acSopenharmony_ci}
1196cd6a6acSopenharmony_ci
1206cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1216cd6a6acSopenharmony_ci//  PUBLIC FUNCTIONS
1226cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1236cd6a6acSopenharmony_ci
1246cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1256cd6a6acSopenharmony_ci//  Sha1Initialise
1266cd6a6acSopenharmony_ci//
1276cd6a6acSopenharmony_ci//  Initialises an SHA1 Context. Use this to initialise/reset a context.
1286cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1296cd6a6acSopenharmony_civoid
1306cd6a6acSopenharmony_ci    Sha1Initialise
1316cd6a6acSopenharmony_ci    (
1326cd6a6acSopenharmony_ci        Sha1Context*                Context
1336cd6a6acSopenharmony_ci    )
1346cd6a6acSopenharmony_ci{
1356cd6a6acSopenharmony_ci    // SHA1 initialization constants
1366cd6a6acSopenharmony_ci    Context->State[0] = 0x67452301;
1376cd6a6acSopenharmony_ci    Context->State[1] = 0xEFCDAB89;
1386cd6a6acSopenharmony_ci    Context->State[2] = 0x98BADCFE;
1396cd6a6acSopenharmony_ci    Context->State[3] = 0x10325476;
1406cd6a6acSopenharmony_ci    Context->State[4] = 0xC3D2E1F0;
1416cd6a6acSopenharmony_ci    Context->Count[0] = 0;
1426cd6a6acSopenharmony_ci    Context->Count[1] = 0;
1436cd6a6acSopenharmony_ci}
1446cd6a6acSopenharmony_ci
1456cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1466cd6a6acSopenharmony_ci//  Sha1Update
1476cd6a6acSopenharmony_ci//
1486cd6a6acSopenharmony_ci//  Adds data to the SHA1 context. This will process the data and update the internal state of the context. Keep on
1496cd6a6acSopenharmony_ci//  calling this function until all the data has been added. Then call Sha1Finalise to calculate the hash.
1506cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1516cd6a6acSopenharmony_civoid
1526cd6a6acSopenharmony_ci    Sha1Update
1536cd6a6acSopenharmony_ci    (
1546cd6a6acSopenharmony_ci        Sha1Context*        Context,
1556cd6a6acSopenharmony_ci        const void*         Buffer,
1566cd6a6acSopenharmony_ci        uint32_t            BufferSize
1576cd6a6acSopenharmony_ci    )
1586cd6a6acSopenharmony_ci{
1596cd6a6acSopenharmony_ci    uint32_t    i;
1606cd6a6acSopenharmony_ci    uint32_t    j;
1616cd6a6acSopenharmony_ci
1626cd6a6acSopenharmony_ci    j = (Context->Count[0] >> 3) & 63;
1636cd6a6acSopenharmony_ci    if ((Context->Count[0] += BufferSize << 3) < (BufferSize << 3))
1646cd6a6acSopenharmony_ci    {
1656cd6a6acSopenharmony_ci        Context->Count[1]++;
1666cd6a6acSopenharmony_ci    }
1676cd6a6acSopenharmony_ci
1686cd6a6acSopenharmony_ci    Context->Count[1] += (BufferSize >> 29);
1696cd6a6acSopenharmony_ci    if ((j + BufferSize) > 63)
1706cd6a6acSopenharmony_ci    {
1716cd6a6acSopenharmony_ci        i = 64 - j;
1726cd6a6acSopenharmony_ci        memcpy(&Context->Buffer[j], Buffer, i);
1736cd6a6acSopenharmony_ci        TransformFunction(Context->State, Context->Buffer);
1746cd6a6acSopenharmony_ci        for (; i + 63 < BufferSize; i += 64)
1756cd6a6acSopenharmony_ci        {
1766cd6a6acSopenharmony_ci            TransformFunction(Context->State, (const uint8_t*)Buffer + i);
1776cd6a6acSopenharmony_ci        }
1786cd6a6acSopenharmony_ci        j = 0;
1796cd6a6acSopenharmony_ci    }
1806cd6a6acSopenharmony_ci    else
1816cd6a6acSopenharmony_ci    {
1826cd6a6acSopenharmony_ci        i = 0;
1836cd6a6acSopenharmony_ci    }
1846cd6a6acSopenharmony_ci
1856cd6a6acSopenharmony_ci    memcpy(&Context->Buffer[j], &((const uint8_t*)Buffer)[i], BufferSize - i);
1866cd6a6acSopenharmony_ci}
1876cd6a6acSopenharmony_ci
1886cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1896cd6a6acSopenharmony_ci//  Sha1Finalise
1906cd6a6acSopenharmony_ci//
1916cd6a6acSopenharmony_ci//  Performs the final calculation of the hash and returns the digest (20 byte buffer containing 160bit hash). After
1926cd6a6acSopenharmony_ci//  calling this, Sha1Initialised must be used to reuse the context.
1936cd6a6acSopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1946cd6a6acSopenharmony_civoid
1956cd6a6acSopenharmony_ci    Sha1Finalise
1966cd6a6acSopenharmony_ci    (
1976cd6a6acSopenharmony_ci        Sha1Context*                Context,
1986cd6a6acSopenharmony_ci        SHA1_HASH*                  Digest
1996cd6a6acSopenharmony_ci    )
2006cd6a6acSopenharmony_ci{
2016cd6a6acSopenharmony_ci    uint32_t    i;
2026cd6a6acSopenharmony_ci    uint8_t     finalcount[8];
2036cd6a6acSopenharmony_ci
2046cd6a6acSopenharmony_ci    for (i = 0; i < 8; i++)
2056cd6a6acSopenharmony_ci    {
2066cd6a6acSopenharmony_ci        finalcount[i] = (unsigned char)((Context->Count[(i >= 4 ? 0 : 1)]
2076cd6a6acSopenharmony_ci         >> ((3-(i & 3)) * 8) ) & 255);  // Endian independent
2086cd6a6acSopenharmony_ci    }
2096cd6a6acSopenharmony_ci    Sha1Update(Context, (const uint8_t*)"\x80", 1);
2106cd6a6acSopenharmony_ci    while ((Context->Count[0] & 504) != 448)
2116cd6a6acSopenharmony_ci    {
2126cd6a6acSopenharmony_ci        Sha1Update(Context, (const uint8_t*)"\0", 1);
2136cd6a6acSopenharmony_ci    }
2146cd6a6acSopenharmony_ci
2156cd6a6acSopenharmony_ci    Sha1Update(Context, finalcount, 8);  // Should cause a Sha1TransformFunction()
2166cd6a6acSopenharmony_ci    for (i = 0; i < SHA1_HASH_SIZE; i++)
2176cd6a6acSopenharmony_ci    {
2186cd6a6acSopenharmony_ci        Digest->bytes[i] = (uint8_t)((Context->State[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
2196cd6a6acSopenharmony_ci    }
2206cd6a6acSopenharmony_ci}
221