Target code is written goldboar teacher SM4 algorithm, VS2015 can run, a total of three files,
Is the new DLL, and then find the function interface, write DLL. J h, then generate DLL can call, there is a question that is generated after the DLL call the method also can't understand,
Hope the teacher can help you solve, or recommend some books or materials, thank you,
The following attached SM4 algorithm source code
Sm4. H
/* *H * \ file sm4.
*/
# # ifndef XYSSL_SM4_H
# define XYSSL_SM4_H
# define SM4_ENCRYPT 1
# define SM4_DECRYPT 0
/* *
* \ brief SM4 context structure
*/
Typedef struct
{
Int mode;/*!Unsigned long sk [32];/*! }
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 (CTX sm4_context * and 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 (CTX sm4_context * and unsigned char key [16]);
/* *
* \ brief SM4 - the 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);
/* *
* \ brief SM4 - CBC buffer encryption/decryption
* \ param CTX SM4 context
* \ param mode SM4_ENCRYPT or SM4_DECRYPT
* \ param length length of the input data
* \ param iv initialization vector (updated after use)
* \ param input buffer holding the input data
* \ param output buffer holding the output data
*/
Void sm4_crypt_cbc (sm4_context * CTX,
Int mode,
Int length,
Unsigned char iv [16],
Unsigned char * input,
Unsigned char * output);
# ifdef __cplusplus
}
# endif
# endif/* sm4 in h */
CodePudding user response:
Sm4. C
# include "sm4. H"
#include
#include
/*
* 32 - bit integer manipulation macros (big endian)
*/
# # ifndef GET_ULONG_BE
# define GET_ULONG_BE (n, b, I) \
{\
A (n)=((unsigned long) (b)/(I) & lt; <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 (n, b, I) \
{\
(b)/(I)=(unsigned char) ((n) & gt;> 24); \
(b) ((I) + 1)=(unsigned char) ((n) & gt;> 16); \
(b) ((I) + 2)=(unsigned char) ((n) & gt;> 8); \
(b)/(I) + 3=(unsigned char) ((n)); \
}
# endif
/*
* the rotate shift left Marco definition
*
*/
# define SHL (x, n) (((x) & amp; 0 XFFFFFFFF) & lt;# define ROTL (x, n) (SHL ((x), n) | ((x) & gt;> (32 - n)))
# define SWAP (a, b) {unsigned long t=a; A=b; B=t; t=0; }
/*
* Expanded SM4 S - boxes
/* Sbox table: 8 bits input the convert to 8 bits output */
The static const unsigned char SboxTable [16] [16]=
{
Xe9 x90 xd6 {0, 0, 0, 0 xfe, 0 XCC, 0 xe1, 0 x3d, 0 xb7, 0 x16, 0 xb6, 0 x14, 0 xc2, 0 x28, 0 XFB, x2c, 0 0 x05},
X9a x67 x2b {0, 0, 0, 0 x76, 0 x2a, 0 xbe, 0 x04, xc3 0, 0 xaa, 0 x44, 0 x13, 0 x26, 0 x49, 0 x86, 0 x06, 0 x99},
X50 x42 x9c {0, 0, 0, 0 xf4, 0 x91, 0 xef, 0 x98, 0 x7a, 0 x33, 0 x54, 0 x0b, 0 x43, 0 xed, XCF 0, 0 xac, 0 x62},
X1c xb2 xe4 {0, 0, 0, 0 xa9, 0 xc9, 0 x08, 0 xe8, 0 x95, 0 x80, 0 XDF, 0 x94, 0 xfa, 0 x75, 0 x8f, 0 x3f, 0 xa6},
Xa7 x07 x47 {0, 0, 0, 0 XFC, 0 xf3, 0 x73, 0 x17, xba 0, 0 x83, 0 x59, 0 x3c, 0 x19, 0 xe6, 0 x85, 0 x4f, 0 xa8},
X81 x6b x68 {0, 0, 0, 0 sets, 0 x71, x64, 0 0 xda, 0 x8b, 0 xf8, 0 xeb, 0 x0f, 0 x4b, 0 x70, 0 x56, 0 x9d, 0 x35},
X0e x24 x1e {0, 0, 0, 0 x5e, 0 x63, 0 x58, 0 xd1, 0 xa2, 0 x25, 0 x22, 0 x7c, 0 x3b, 0 x01, 0 x21, 0 x78, 0 x87},
X46 xd4 {0, 0 x00, 0, 0 x57, 0 x9f, 0 xd3, 0 x27, 0 x52, 0 x4c, 0 x36, 0 x02, 0 xe7, 0 xa0, 0 xc4, 0 xc8, 0 x9e},
XBF {0 xea, 0, 0 x8a, 0 xd2, 0 x40, 0 xc7, 0 x38, 0 xb5, 0 xa3, 0 xf7, 0 xf2, 0 xce, 0 xf9, 0 x61, 0 x15, 0 xa1},
Xae {0 xe0-0xfc, 0, 0 x5d, 0 xa4, 0 x9b, 0 x34, 0 x1a, 0 x55, 0 xad, 0 x93, 0 x32, 0 x30, 0 xf5, 0 x8c, xb1 0, 0 xe3},
Xe2 xf6 x1d {0, 0, 0, 0 x2e, 0 x82, 0 x66, 0 xca, 0 x60, 0 xc0, 0 x29, 0 x23, 0 xab, 0 x0d, 0 x53, 0 x4e, 0 x6f},
X37 xd5 {0, 0 XDB, 0, 0 x45, 0 xde, 0 XFD, 0 x8e, x2f, 0 0 x03, 0 XFF, 0 x6a, 0 x72, 0 x6d, 0 x6c, 0 x5b, 0 x51},
Xaf x1b x8d {0, 0, 0, 0 x92, 0 XBB, 0 XDD, 0 XBC, 0 x7f, x11, 0 0 xd9, 0 x5c, 0 x41, 0 x1f, 0 x10, 0 x5a, 0 xd8},
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull