資源簡介
SM4分組密碼算法是一個迭代分組密碼算法,由加解密算法和密鑰擴展算法組成。SM4分組密碼算法采用非平衡Feistel結構,分組長度為128b密鑰長度為128b。加密算法與密鑰擴展算法均采用非線性迭代結構。加密運算和解密運算的算法結構相同,解密運算的輪密鑰的使用順序與加密運算相反。

代碼片段和文件信息
#include?
#include?
#ifndef?XYSSL_SM4_H
#define?XYSSL_SM4_H
#define?SM4_ENCRYPT?????1
#define?SM4_DECRYPT?????0
/**
*?\brief??????????SM4?context?structure
*/
typedef?struct{
????????int?mode;???????????????????/*!?encrypt/decrypt???*/
????????unsigned?long?sk[32];???????/*!?SM4?subkeys???????*/
}sm4_context;
#ifdef?__cplusplus
extern?“C“?{
#endif
/**
?*?\brief??????????SM4?key?schedule?(128-bit?encryption)
?*
?*?\param?ctx??????SM4?context?to?be?initialized
?*?\param?key??????16-byte?secret?key
?*/
????void?sm4_setkey_enc(?sm4_context?*ctx?unsigned?char?key[16]?);
/**
?*?\brief??????????SM4?key?schedule?(128-bit?decryption)
?*
?*?\param?ctx??????SM4?context?to?be?initialized
?*?\param?key??????16-byte?secret?key
?*/
void?sm4_setkey_dec(?sm4_context?*ctx?unsigned?char?key[16]?);
?/**
?*?\brief??????????SM4-ECB?block?encryption/decryption
?*?\param?ctx??????SM4?context
?*?\param?mode?????SM4_ENCRYPT?or?SM4_DECRYPT
?*?\param?length???length?of?the?input?data
?*?\param?input????input?block
?*?\param?output???output?block
?*/
void?sm4_crypt_ecb(?sm4_context?*ctx
int?mode????int?length????unsigned?char?*input????unsigned?char?*output);
#ifdef?__cplusplus
}
#endif
#endif
/*
?*?32-bit?integer?manipulation?macros?(big?endian)
?*/
#ifndef?GET_ULONG_BE
#define?GET_ULONG_BE(nbi)?????????????????????????????\
{???????????????????????????????????????????????????????\
????(n)?=?(?(unsigned?long)?(b)[(i)????]?<24?)????????\
?????????|?(?(unsigned?long)?(b)[(i)?+?1]?<16?)????????\
?????????|?(?(unsigned?long)?(b)[(i)?+?2]?<?8?)????????\
?????????|?(?(unsigned?long)?(b)[(i)?+?3]???????);???????\
}
#endif
#ifndef?PUT_ULONG_BE
#define?PUT_ULONG_BE(nbi)?????????????????????????????\
{???????????????????????????????????????????????????????\
????(b)[(i)????]?=?(unsigned?char)?(?(n)?>>?24?);???????\
????(b)[(i)?+?1]?=?(unsigned?char)?(?(n)?>>?16?);???????\
????(b)[(i)?+?2]?=?(unsigned?char)?(?(n)?>>??8?);???????\
????(b)[(i)?+?3]?=?(unsigned?char)?(?(n)???????);???????\
}
#endif
/*
?*rotate?shift?left?marco?definition
?*
*/
#define??SHL(xn)?(((x)?&?0xFFFFFFFF)?<#define?ROTL(xn)?(SHL((x)n)?|?((x)?>>?(32?-?n)))
#define?SWAP(ab)?{?unsigned?long?t?=?a;?a?=?b;?b?=?t;?t?=?0;?}
/*
?*?Expanded?SM4?S-boxes
/*?Sbox?table:?8bits?input?convert?to?8?bits?output*/
static?const?unsigned?char?SboxTable[16][16]?=
{
{0xd60x900xe90xfe0xcc0xe10x3d0xb70x160xb60x140xc20x280xfb0x2c0x05}
{0x2b0x670x9a0x760x2a0xbe0x040xc30xaa0x440x130x260x490x860x060x99}
{0x9c0x420x500xf40x910xef0x980x7a0x330x540x0b0x430xed0xcf0xac0x62}
{0xe40xb30x1c0xa90xc90x080xe80x950x800xdf0x940xfa0x750x8f0x3f0xa6}
{0x470x070xa70xfc0xf30x730x170xba0x830x590x3c0x190xe60x850x4f0xa8}
{0x680x6b0x810xb20x710x640xda0x8b0xf80xeb0x0f0x4b0x700x560x9d0x35}
{0x1e0x240x0e0x5e0x63
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????9467??2018-06-07?13:29??SM4.c
- 上一篇:SHA-1算法實現源代碼
- 下一篇:基于哈夫曼編碼的文件壓縮解壓程序的C語言實現
評論
共有 條評論