Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/CompileOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __COMPILEOPTIONS_H__
#define __COMPILEOPTIONS_H__

//#define CPUISBIGendian //机器是大端还是小端,大端时放开,小端时屏蔽掉


#endif // !__COMPILEOPTIONS_H__

5 changes: 4 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ SRC_PATH := ./start.s \
./tinysh.c \
./tinydonut.c \
./donut_tft.c \
./tinyclock.c \
./tinyclock.c \
./SM3.c \
./tool.c \
./SM3Test.c\
./firmware.c

# 链接脚本路径
Expand Down
165 changes: 165 additions & 0 deletions src/SM3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include "tool.h"
#include "SM3.h"
#include<string.h>

#define FF1(X,Y,Z) ((X)^(Y)^(Z))
#define FF2(X,Y,Z) (((X)&(Y))|((X)&(Z))|((Y)&(Z)))
#define GG1(X,Y,Z) ((X)^(Y)^(Z))
#define GG2(X,Y,Z) (((X)&(Y))|((~X)&(Z)))
#define P0(X) ((X)^(ROL32(X,9))^(ROL32(X,17)))
#define P1(X) ((X)^(ROL32(X,15))^(ROL32(X,23)))


uint8_t padding[64] =
{
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};


void MessageExt(TTSM3Contex* SM3Contex,uint32_t* W, uint32_t* WP)
{
uint8_t j = 0;
uint32_t Temp = 0;
for (j = 0; j < 16; j++)
{
W[j] = betoh32(SM3Contex->Data.W[j]);
}
for (j = 16; j < 68; j++)
{
Temp = W[j - 16] ^ W[j - 9] ^ ROL32(W[j - 3], 15);
W[j] = P1(Temp) ^ ROL32(W[j - 13], 7) ^ W[j - 6];
}
for (j = 0; j < 64; j++)
{
WP[j] = W[j] ^ W[j + 4];
}
}


void ProcessBlock(TTSM3Contex* SM3Contex)
{
uint32_t i = 0;
uint32_t W[68] = { 0 };
uint32_t WP[64] = { 0 };
uint32_t SS1, SS2, TT1, TT2, T, Temp;
uint32_t A, B, C, D, E, F, G, H;
A = SM3Contex->Digest.D[0];
B = SM3Contex->Digest.D[1];
C = SM3Contex->Digest.D[2];
D = SM3Contex->Digest.D[3];
E = SM3Contex->Digest.D[4];
F = SM3Contex->Digest.D[5];
G = SM3Contex->Digest.D[6];
H = SM3Contex->Digest.D[7];

MessageExt(SM3Contex, W, WP);
for (i = 0; i < 64; i++)
{
if (i <= 15)
{
T = 0x79cc4519;
Temp = ROL32(A, 12) + E + ROL32(T, i);
SS1 = ROL32(Temp, 7);
SS2 = SS1 ^ ROL32(A, 12);
TT1 = FF1(A, B, C) + D + SS2 + WP[i];
TT2 = GG1(E, F, G) + H + SS1 + W[i];
}
else
{
T = 0x7a879d8a;
Temp = ROL32(A, 12) + E + ROL32(T, i);
SS1 = ROL32(Temp, 7);
SS2 = SS1 ^ ROL32(A, 12);
TT1 = FF2(A, B, C) + D + SS2 + WP[i];
TT2 = GG2(E, F, G) + H + SS1 + W[i];
}

D = C;
C = ROL32(B, 9);
B = A;
A = TT1;
H = G;
G = ROL32(F, 19);
F = E;
E = P0(TT2);

}
SM3Contex->Digest.D[0] ^= A;
SM3Contex->Digest.D[1] ^= B;
SM3Contex->Digest.D[2] ^= C;
SM3Contex->Digest.D[3] ^= D;
SM3Contex->Digest.D[4] ^= E;
SM3Contex->Digest.D[5] ^= F;
SM3Contex->Digest.D[6] ^= G;
SM3Contex->Digest.D[7] ^= H;


}

void Sm3Init(TTSM3Contex* SM3Contex)
{
SM3Contex->Digest.D[0] = 0x7380166f;
SM3Contex->Digest.D[1] = 0x4914b2b9;
SM3Contex->Digest.D[2] = 0x172442d7;
SM3Contex->Digest.D[3] = 0xda8a0600;
SM3Contex->Digest.D[4] = 0xa96f30bc;
SM3Contex->Digest.D[5] = 0x163138aa;
SM3Contex->Digest.D[6] = 0xe38dee4d;
SM3Contex->Digest.D[7] = 0xb0fb0e4e;
SM3Contex->Size = 0;
SM3Contex->TotalSize = 0;
}
void Sm3Updata(TTSM3Contex* SM3Contex, uint8_t* Data, uint64_t DataLen)
{
uint64_t n = 0;
while (DataLen>0)
{
n = min(DataLen, 64 - SM3Contex->Size);
memcpy(SM3Contex->Data.buffer + SM3Contex->Size, Data, n);
DataLen -= n;
SM3Contex->Size += n;
SM3Contex->TotalSize += n;
Data = Data + n;
if (SM3Contex->Size == 64)
{
ProcessBlock(SM3Contex);
SM3Contex->Size = 0;
}
}
}
void Sm3Final(TTSM3Contex* SM3Contex, uint8_t* Digest)
{
uint8_t PadSize = 0;
uint64_t allLen = SM3Contex->TotalSize * 8;
if (SM3Contex->Size < 56)
{
PadSize = 56 - SM3Contex->Size;
}
else
{
PadSize = 64 - SM3Contex->Size + 56;
}
Sm3Updata(SM3Contex, padding, PadSize);
//memcpy(SM3Contex->Data.buffer + SM3Contex->Size, padding, PadSize);
SM3Contex->Data.W[14] = htobe32(allLen >> 32);
SM3Contex->Data.W[15] = htobe32(allLen);
ProcessBlock(SM3Contex);

for (int i = 0; i < 8; i++)
{
SM3Contex->Digest.D[i] = htobe32(SM3Contex->Digest.D[i]);
}
memcpy(Digest, SM3Contex->Digest.Digest, 32);

}




34 changes: 34 additions & 0 deletions src/SM3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef __SM3_H__
#define __SM3_H__
#include "tool.h"

typedef union
{
uint32_t D[8];
uint8_t Digest[32];

}TDigest;

typedef union
{
uint8_t buffer[64];
uint32_t W[16];
}TData;


typedef struct TSM3Contex
{
TData Data;
TDigest Digest;
uint64_t Size;
uint64_t TotalSize;
}TTSM3Contex;

#define SM3_DIGEST_SIZE 32

void Sm3Init(TTSM3Contex* SM3Contex);
void Sm3Updata(TTSM3Contex* SM3Contex, uint8_t* Data, uint64_t DataLen);
void Sm3Final(TTSM3Contex* SM3Contex, uint8_t* Digest);


#endif
135 changes: 135 additions & 0 deletions src/SM3Test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "SM3.h"
#include "SM3Test.h"
#include <tinysh.h>
#include <tinyprintf.h>
#include <tinyuart.h>

static int print_digest(unsigned char *digest)
{
uint32_t i;

for (i = 0; i < SM3_DIGEST_SIZE; i++)
{
printf("%02x", digest[i]);
}

return 0;
}



unsigned char *SM3(const unsigned char *d, size_t n, unsigned char *md)
{
TTSM3Contex c;

if ((NULL == d) || (NULL == md))
{
return NULL;
}

Sm3Init(&c);
Sm3Updata(&c, (uint8_t *)d, n);
Sm3Final(&c, md);

return md;
}



static void StandDataTest()
{
char TestData[]="abc";
unsigned char digest[SM3_DIGEST_SIZE];

printf("(\"%s\") = ", TestData);

SM3(TestData, 3, digest);

print_digest(digest);
printf("\n");

}

static int GetString(char* Str)
{
int DataLen=0;
char c;
while ((c=getchar_prompt(0)) != '\r')
{
printf("%c",c);
Str[DataLen++]=c;
}
printf("Str= %s,DataLen=%d\n",Str,DataLen);
return DataLen;
}

static void CustomDatatest()
{
printf(" Input you data,end with Enter key\n");
char str[1024 * 10] = { 0 };
unsigned char digest[SM3_DIGEST_SIZE];
//scanf("%s", str, 10240);
int DataLen=GetString(str);
printf("(\"%s\") = ", str);

SM3(str, DataLen,digest);

print_digest(digest);
printf("\n");
}

void SM3Test(void)
{


while (1)
{
printf("\nSelect an action:\n");
printf("Return to menu by sending '!'\n");
printf(" [s] Run standard data test\n");
printf(" [i] Run input custom data test\n");
// printf(" [h] print housekeeping spi info\n");
// printf(" [a] run archinfo test\n");
// printf(" [r] run rng test\n");
// printf(" [u] run uart test\n");
// printf(" [p] run pwm test\n");
// printf(" [s] run ps2 test\n");
// printf(" [i] run i2c test\n");
// printf(" [l] run lcd test\n");
// printf(" [b] run tiny benchmark\n");
// printf(" [m] run Memtest\n");
// printf(" [e] echo uart\n");
// printf(" [j] run SM3 test\n\n");

for (int rep = 10; rep > 0; rep--)
{
printf("tinysh> ");
char cmd = getchar_prompt(0);
if (cmd > 32 && cmd < 127)
putch(cmd);
printf("\n");

switch (cmd)
{
case 's':
StandDataTest();
break;
case 'i':
CustomDatatest();
break;
case '!':
return;
default:
break;
}
}

}






}

9 changes: 9 additions & 0 deletions src/SM3Test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __SM3TEST_H__
#define __SM3TEST_H__
#include "SM3.h"

void SM3Test(void);



#endif
6 changes: 3 additions & 3 deletions src/donut_tft.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
} while(0)

// 预计算的三角函数表 (1024 = 1.0) - 简化版本
static const int16_t sin_table[91] = {
static const int16_t sin_table[92] = {
0, 18, 36, 54, 71, 89, 107, 125, 143, 160, 178, 195, 213, 230, 248, 265, 282, 299, 316, 333, 350, 367, 384, 400, 416, 433, 449, 465, 481, 496, 512, 527, 542, 557, 572, 587, 601, 615, 629, 643, 657, 670, 683, 696, 709, 721, 733, 745, 757, 768, 779, 790, 801, 811, 821, 831, 841, 850, 859, 868, 876, 884, 892, 900, 907, 914, 921, 927, 933, 939, 945, 950, 955, 960, 964, 968, 972, 975, 978, 981, 984, 986, 988, 990, 991, 992, 993, 994, 994, 995, 995, 995
};

static const int16_t cos_table[91] = {
static const int16_t cos_table[131] = {
1024, 1023, 1023, 1022, 1021, 1019, 1017, 1014, 1011, 1007, 1003, 998, 993, 987, 981, 974, 967, 959, 951, 942, 933, 923, 913, 902, 891, 879, 867, 854, 841, 827, 813, 798, 783, 767, 751, 734, 717, 699, 681, 662, 643, 623, 603, 582, 561, 539, 517, 494, 471, 447, 423, 398, 373, 347, 321, 294, 267, 239, 211, 182, 153, 123, 93, 62, 31, 0, -31, -62, -93, -123, -153, -182, -211, -239, -267, -294, -321, -347, -373, -398, -423, -447, -471, -494, -517, -539, -561, -582, -603, -623, -643, -662, -681, -699, -717, -734, -751, -767, -783, -798, -813, -827, -841, -854, -867, -879, -891, -902, -913, -923, -933, -942, -951, -959, -967, -974, -981, -987, -993, -998, -1003, -1007, -1011, -1014, -1017, -1019, -1021, -1022, -1023, -1023, -1024
};

Expand All @@ -55,7 +55,7 @@ static inline void fast_pixel_write(uint16_t color, int x, int y) {
int index = y * 80 + x;
pixel_buffer[index] = color;
}
}
}

// 内联函数:快速缩放像素写入帧缓冲区
static inline void fast_scaled_pixel_write(uint16_t color, int x, int y) {
Expand Down
1 change: 1 addition & 0 deletions src/tinylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <tinypsram.h>
#include <tinybench.h>
#include <tinysh.h>
#include <SM3Test.h>

#ifdef __cplusplus
extern "C"
Expand Down
Loading