mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-13 11:56:28 +08:00
add CBC-MAC and GM OTP, not tested
This commit is contained in:
@@ -17,13 +17,13 @@ TEST=
|
||||
APPS=
|
||||
|
||||
LIB=$(TOP)/libcrypto.a
|
||||
LIBSRC=sms4_cbc.c sms4_cfb.c sms4_ecb.c sms4_ofb.c sms4_ctr.c sms4_wrap.c sms4.c
|
||||
LIBOBJ=sms4_cbc.o sms4_cfb.o sms4_ecb.o sms4_ofb.o sms4_ctr.o sms4_wrap.o sms4.o
|
||||
LIBSRC=sms4_cbc.c sms4_cfb.c sms4_ecb.c sms4_ofb.c sms4_ctr.c sms4_wrap.c sms4_common.c sms4_setkey.c sms4_enc.c
|
||||
LIBOBJ=sms4_cbc.o sms4_cfb.o sms4_ecb.o sms4_ofb.o sms4_ctr.o sms4_wrap.o sms4_common.o sms4_setkey.o sms4_enc.o
|
||||
|
||||
SRC= $(LIBSRC)
|
||||
|
||||
EXHEADER= sms4.h
|
||||
HEADER= ../../include/openssl/modes.h $(EXHEADER)
|
||||
HEADER= ../../include/openssl/modes.h sms4_lcl.h $(EXHEADER)
|
||||
|
||||
ALL= $(GENERAL) $(SRC) $(HEADER)
|
||||
|
||||
|
||||
@@ -91,6 +91,26 @@ int sms4_wrap_key(sms4_key_t *key, const unsigned char *iv,
|
||||
int sms4_unwrap_key(sms4_key_t *key, const unsigned char *iv,
|
||||
unsigned char *out, const unsigned char *in, unsigned int inlen);
|
||||
|
||||
|
||||
|
||||
#define SMS4_EDE_KEY_LENGTH 32
|
||||
|
||||
typedef struct {
|
||||
sms4_key_t k1;
|
||||
sms4_key_t k2;
|
||||
} sms4_ede_key_t;
|
||||
|
||||
void sms4_ede_set_encrypt_key(sms4_ede_key_t *key, const unsigned char *user_key);
|
||||
void sms4_ede_set_decrypt_key(sms4_ede_key_t *key, const unsigned char *user_key);
|
||||
void sms4_ede_encrypt(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_encrypt_8blocks(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_encrypt_16blocks(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_decrypt(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_decrypt_8blocks(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_decrypt_16blocks(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
#ifndef LIBSM_SMS4_EDE_H
|
||||
#define LIBSM_SMS4_EDE_H
|
||||
|
||||
#define SMS4_EDE_KEY_LENGTH 32
|
||||
|
||||
#include "sms4.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
sms4_key_t k1;
|
||||
sms4_key_t k2;
|
||||
} sms4_ede_key_t;
|
||||
|
||||
void sms4_ede_set_encrypt_key(sms4_ede_key_t *key, const unsigned char *user_key);
|
||||
void sms4_ede_set_decrypt_key(sms4_ede_key_t *key, const unsigned char *user_key);
|
||||
void sms4_ede_encrypt(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_encrypt_8blocks(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_encrypt_16blocks(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_decrypt(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_decrypt_8blocks(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
void sms4_ede_decrypt_16blocks(sms4_ede_key_t *key, const unsigned char *in, unsigned char *out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -64,8 +64,7 @@
|
||||
x4 = S32(x4); \
|
||||
x4 = x0 ^ L32(x4)
|
||||
|
||||
|
||||
void sms4_encrypt(sms4_key_t *key, const unsigned char *in, unsigned char *out)
|
||||
void sms4_encrypt(const unsigned char *in, unsigned char *out, const sms4_key_t *key)
|
||||
{
|
||||
uint32_t *rk = key->rk;
|
||||
uint32_t x0, x1, x2, x3, x4;
|
||||
|
||||
67
crypto/sms4/sms4_setkey.c
Normal file
67
crypto/sms4/sms4_setkey.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "sms4.h"
|
||||
#include "sms4_lcl.h"
|
||||
|
||||
static uint32_t FK[4] = {
|
||||
0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc,
|
||||
};
|
||||
|
||||
static uint32_t CK[32] = {
|
||||
0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,
|
||||
0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
|
||||
0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,
|
||||
0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,
|
||||
0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229,
|
||||
0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,
|
||||
0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,
|
||||
0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279,
|
||||
};
|
||||
|
||||
#define L32_(x) \
|
||||
((x) ^ \
|
||||
ROT32((x), 13) ^ \
|
||||
ROT32((x), 23))
|
||||
|
||||
#define ENC_ROUND(x0, x1, x2, x3, x4, i) \
|
||||
x4 = x1 ^ x2 ^ x3 ^ *(CK + i); \
|
||||
x4 = S32(x4); \
|
||||
x4 = x0 ^ L32_(x4); \
|
||||
*(rk + i) = x4
|
||||
|
||||
#define DEC_ROUND(x0, x1, x2, x3, x4, i) \
|
||||
x4 = x1 ^ x2 ^ x3 ^ *(CK + i); \
|
||||
x4 = S32(x4); \
|
||||
x4 = x0 ^ L32_(x4); \
|
||||
*(rk + 31 - i) = x4
|
||||
|
||||
void sms4_set_encrypt_key(sms4_key_t *key, const unsigned char *user_key)
|
||||
{
|
||||
uint32_t *rk = key->rk;
|
||||
uint32_t x0, x1, x2, x3, x4;
|
||||
|
||||
x0 = GET32(user_key ) ^ FK[0];
|
||||
x1 = GET32(user_key + 4) ^ FK[1];
|
||||
x2 = GET32(user_key + 8) ^ FK[2];
|
||||
x3 = GET32(user_key + 12) ^ FK[3];
|
||||
|
||||
#define ROUND ENC_ROUND
|
||||
ROUNDS(x0, x1, x2, x3, x4);
|
||||
|
||||
x0 = x1 = x2 = x3 = x4 = 0;
|
||||
}
|
||||
|
||||
void sms4_set_decrypt_key(sms4_key_t *key, const unsigned char *user_key)
|
||||
{
|
||||
uint32_t *rk = key->rk;
|
||||
uint32_t x0, x1, x2, x3, x4;
|
||||
|
||||
x0 = GET32(user_key ) ^ FK[0];
|
||||
x1 = GET32(user_key + 4) ^ FK[1];
|
||||
x2 = GET32(user_key + 8) ^ FK[2];
|
||||
x3 = GET32(user_key + 12) ^ FK[3];
|
||||
|
||||
#undef ROUND
|
||||
#define ROUND DEC_ROUND
|
||||
ROUNDS(x0, x1, x2, x3, x4);
|
||||
|
||||
x0 = x1 = x2 = x3 = x4 = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user