version 2.5.3

new sms4 api, go api and ciphersuites
This commit is contained in:
Zhi Guan
2019-08-13 15:07:53 +08:00
parent 94f91c0f8a
commit b42251945e
39 changed files with 23201 additions and 5685 deletions

View File

@@ -1,4 +1,9 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=\
sms4_common.c sms4_setkey.c sms4_enc.c sms4_enc_nblks.c \
sms4_common.c sms4_setkey.c sms4_enc.c sms4_enc_avx2.c sms4_ede.c \
sms4_ecb.c sms4_cbc.c sms4_cfb.c sms4_ctr.c sms4_ofb.c sms4_wrap.c
INCLUDE[sms4_setkey.o]=../modes
INCLUDE[sms4_enc.o]=../modes
INCLUDE[sms4_enc_avx2.o]=../modes

File diff suppressed because it is too large Load Diff

129
crypto/sms4/sms4_ede.c Normal file
View File

@@ -0,0 +1,129 @@
/* ====================================================================
* Copyright (c) 2014 - 2019 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <openssl/sms4.h>
#include <openssl/modes.h>
void sms4_ede_set_encrypt_key(sms4_ede_key_t *key,
const unsigned char user_key[48])
{
sms4_set_encrypt_key(&key->k1, user_key);
sms4_set_decrypt_key(&key->k2, user_key + 16);
sms4_set_encrypt_key(&key->k3, user_key + 32);
}
void sms4_ede_set_decrypt_key(sms4_ede_key_t *key,
const unsigned char user_key[48])
{
sms4_set_decrypt_key(&key->k1, user_key + 32);
sms4_set_encrypt_key(&key->k2, user_key + 16);
sms4_set_decrypt_key(&key->k3, user_key);
}
void sms4_ede_encrypt(const unsigned char in[16], unsigned char out[16],
const sms4_ede_key_t *key)
{
sms4_encrypt(in, out, &key->k1);
sms4_encrypt(out, out, &key->k2);
sms4_encrypt(out, out, &key->k3);
}
void sms4_ede_ecb_encrypt(const unsigned char *in, unsigned char *out,
const sms4_ede_key_t *key, int enc)
{
sms4_ede_encrypt(in, out, key);
}
void sms4_ede_cbc_encrypt(const unsigned char *in, unsigned char *out,
size_t len, const sms4_ede_key_t *key, unsigned char *iv, int enc)
{
if (enc)
CRYPTO_cbc128_encrypt(in, out, len, key, iv,
(block128_f)sms4_ede_encrypt);
else CRYPTO_cbc128_decrypt(in, out, len, key, iv,
(block128_f)sms4_ede_encrypt);
}
void sms4_ede_cfb128_encrypt(const unsigned char *in, unsigned char *out,
size_t len, const sms4_ede_key_t *key, unsigned char *iv, int *num,
int enc)
{
CRYPTO_cfb128_encrypt(in, out, len, key, iv, num, enc,
(block128_f)sms4_ede_encrypt);
}
void sms4_ede_ofb128_encrypt(const unsigned char *in, unsigned char *out,
size_t len, const sms4_ede_key_t *key, unsigned char *iv, int *num)
{
CRYPTO_ofb128_encrypt(in, out, len, key, iv, num,
(block128_f)sms4_ede_encrypt);
}
void sms4_ede_ctr128_encrypt(const unsigned char *in, unsigned char *out,
size_t len, const sms4_ede_key_t *key, unsigned char *iv,
unsigned char ecount_buf[SMS4_BLOCK_SIZE], unsigned int *num)
{
CRYPTO_ctr128_encrypt(in, out, len, key, iv, ecount_buf, num,
(block128_f)sms4_ede_encrypt);
}
int sms4_ede_wrap_key(sms4_ede_key_t *key, const unsigned char *iv,
unsigned char *out, const unsigned char *in, unsigned int inlen)
{
return CRYPTO_128_wrap(key, iv, out, in, inlen,
(block128_f)sms4_ede_encrypt);
}
int sms4_ede_unwrap_key(sms4_ede_key_t *key, const unsigned char *iv,
unsigned char *out, const unsigned char *in, unsigned int inlen)
{
return CRYPTO_128_unwrap(key, iv, out, in, inlen,
(block128_f)sms4_ede_encrypt);
}

View File

@@ -47,39 +47,87 @@
* ====================================================================
*/
#include <stdio.h>
#include <openssl/sms4.h>
#include "internal/rotate.h"
#include "modes_lcl.h"
#include "sms4_lcl.h"
#define L32(x) \
((x) ^ \
ROT32((x), 2) ^ \
ROT32((x), 10) ^ \
ROT32((x), 18) ^ \
ROT32((x), 24))
#define L32(x) \
((x) ^ \
ROL32((x), 2) ^ \
ROL32((x), 10) ^ \
ROL32((x), 18) ^ \
ROL32((x), 24))
#define ROUND(x0, x1, x2, x3, x4, i) \
x4 = x1 ^ x2 ^ x3 ^ *(rk + i); \
x4 = S32(x4); \
#define ROUND_SBOX(x0, x1, x2, x3, x4, i) \
x4 = x1 ^ x2 ^ x3 ^ *(rk + i); \
x4 = S32(x4); \
x4 = x0 ^ L32(x4)
void sms4_encrypt(const unsigned char *in, unsigned char *out, const sms4_key_t *key)
#define ROUND_TBOX(x0, x1, x2, x3, x4, i) \
x4 = x1 ^ x2 ^ x3 ^ *(rk + i); \
t0 = ROL32(SMS4_T[(uint8_t)x4], 8); \
x4 >>= 8; \
x0 ^= t0; \
t0 = ROL32(SMS4_T[(uint8_t)x4], 16); \
x4 >>= 8; \
x0 ^= t0; \
t0 = ROL32(SMS4_T[(uint8_t)x4], 24); \
x4 >>= 8; \
x0 ^= t0; \
t1 = SMS4_T[x4]; \
x4 = x0 ^ t1
#define ROUND_DBOX(x0, x1, x2, x3, x4, i) \
x4 = x1 ^ x2 ^ x3 ^ *(rk + i); \
x4 = x0 ^ SMS4_D[(uint16_t)(x4 >> 16)] ^ \
ROL32(SMS4_D[(uint16_t)x4], 16)
#define ROUND ROUND_TBOX
void sms4_encrypt(const unsigned char in[16], unsigned char out[16], const sms4_key_t *key)
{
const uint32_t *rk = key->rk;
uint32_t x0, x1, x2, x3, x4;
uint32_t t0, t1;
x0 = GET32(in );
x1 = GET32(in + 4);
x2 = GET32(in + 8);
x3 = GET32(in + 12);
x0 = GETU32(in );
x1 = GETU32(in + 4);
x2 = GETU32(in + 8);
x3 = GETU32(in + 12);
ROUNDS(x0, x1, x2, x3, x4);
PUT32(x0, out );
PUT32(x4, out + 4);
PUT32(x3, out + 8);
PUT32(x2, out + 12);
x0 = x1 = x2 = x3 = x4 = 0;
PUTU32(out , x0);
PUTU32(out + 4, x4);
PUTU32(out + 8, x3);
PUTU32(out + 12, x2);
}
/* caller make sure counter not overflow */
void sms4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
size_t blocks, const sms4_key_t *key, const unsigned char iv[16])
{
const uint32_t *rk = key->rk;
unsigned int c0 = GETU32(iv );
unsigned int c1 = GETU32(iv + 4);
unsigned int c2 = GETU32(iv + 8);
unsigned int c3 = GETU32(iv + 12);
uint32_t x0, x1, x2, x3, x4;
uint32_t t0, t1;
while (blocks--) {
x0 = c0;
x1 = c1;
x2 = c2;
x3 = c3;
ROUNDS(x0, x1, x2, x3, x4);
PUTU32(out , GETU32(in ) ^ x0);
PUTU32(out + 4, GETU32(in + 4) ^ x4);
PUTU32(out + 8, GETU32(in + 8) ^ x3);
PUTU32(out + 12, GETU32(in + 12) ^ x2);
in += 16;
out += 16;
c3++;
}
}

View File

@@ -1,5 +1,5 @@
/* ====================================================================
* Copyright (c) 2014 - 2016 The GmSSL Project. All rights reserved.
* Copyright (c) 2014 - 2019 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -47,32 +47,15 @@
* ====================================================================
*/
#include <immintrin.h>
#include <openssl/sms4.h>
#include "internal/rotate.h"
#include "modes_lcl.h"
#include "sms4_lcl.h"
static __m256i mask_ffff;
static __m256i vindex_0s;
static __m256i vindex_4i;
static __m256i vindex_swap;
static __m256i vindex_read;
#ifdef SMS4_AVX2
# include <immintrin.h>
void sms4_avx2_encrypt_init(sms4_key_t *key)
{
mask_ffff = _mm256_set1_epi32(0xffff);
vindex_0s = _mm256_set1_epi32(0);
vindex_4i = _mm256_setr_epi32(0,4,8,12,16,20,24,28);
vindex_read = _mm256_setr_epi32(0,8,16,24,1,9,17,25);
vindex_swap = _mm256_setr_epi8(
3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12,
3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12
);
sms4_init_sbox32();
}
#define GET_BLKS(x0, x1, x2, x3, in) \
# define GET_BLKS(x0, x1, x2, x3, in) \
t0 = _mm256_i32gather_epi32((int *)(in+4*0), vindex_4i, 4); \
t1 = _mm256_i32gather_epi32((int *)(in+4*1), vindex_4i, 4); \
t2 = _mm256_i32gather_epi32((int *)(in+4*2), vindex_4i, 4); \
@@ -82,7 +65,7 @@ void sms4_avx2_encrypt_init(sms4_key_t *key)
x2 = _mm256_shuffle_epi8(t2, vindex_swap); \
x3 = _mm256_shuffle_epi8(t3, vindex_swap)
#define PUT_BLKS(out, x0, x1, x2, x3) \
# define PUT_BLKS(out, x0, x1, x2, x3) \
t0 = _mm256_shuffle_epi8(x0, vindex_swap); \
t1 = _mm256_shuffle_epi8(x1, vindex_swap); \
t2 = _mm256_shuffle_epi8(x2, vindex_swap); \
@@ -91,59 +74,135 @@ void sms4_avx2_encrypt_init(sms4_key_t *key)
_mm256_storeu_si256((__m256i *)(out+32*1), t1); \
_mm256_storeu_si256((__m256i *)(out+32*2), t2); \
_mm256_storeu_si256((__m256i *)(out+32*3), t3); \
x0 = _mm256_i32gather_epi32((int *)(in+32*0), vindex_read, 4); \
x1 = _mm256_i32gather_epi32((int *)(in+32*1), vindex_read, 4); \
x2 = _mm256_i32gather_epi32((int *)(in+32*2), vindex_read, 4); \
x3 = _mm256_i32gather_epi32((int *)(in+32*3), vindex_read, 4); \
_mm256_storeu_si256((__m256i *)(out+2*0), x0); \
_mm256_storeu_si256((__m256i *)(out+2*1), x1); \
_mm256_storeu_si256((__m256i *)(out+2*2), x2); \
_mm256_storeu_si256((__m256i *)(out+2*3), x3)
x0 = _mm256_i32gather_epi32((int *)(out+8*0), vindex_read, 4); \
x1 = _mm256_i32gather_epi32((int *)(out+8*1), vindex_read, 4); \
x2 = _mm256_i32gather_epi32((int *)(out+8*2), vindex_read, 4); \
x3 = _mm256_i32gather_epi32((int *)(out+8*3), vindex_read, 4); \
_mm256_storeu_si256((__m256i *)(out+32*0), x0); \
_mm256_storeu_si256((__m256i *)(out+32*1), x1); \
_mm256_storeu_si256((__m256i *)(out+32*2), x2); \
_mm256_storeu_si256((__m256i *)(out+32*3), x3)
#define S(x0, t0, t1, t2) \
t0 = _mm256_and_si256(x0, mask_ffff); \
t1 = _mm256_i32gather_epi32(SBOX32L, t0, 4); \
t0 = _mm256_srli_epi32(x0, 16); \
t2 = _mm256_i32gather_epi32(SBOX32H, t0, 4); \
x0 = _mm256_xor_si256(t1, t2)
# define _mm256_rotl_epi32(a, i) _mm256_xor_si256( \
_mm256_slli_epi32(a, i), _mm256_srli_epi32(a, 32 - i))
#define ROT(r0, x0, i, t0, t1) \
t0 = _mm256_slli_epi32(x0, i); \
t1 = _mm256_srli_epi32(x0,32-i); \
r0 = _mm256_xor_si256(t0, t1)
# define INDEX_MASK_TBOX 0xff
#define L(x0, t0, t1, t2, t3, t4) \
ROT(t0, x0, 2, t2, t3); \
ROT(t1, x0, 10, t2, t3); \
t4 = _mm256_xor_si256(t0, t1); \
ROT(t0, x0, 18, t2, t3); \
ROT(t1, x0, 24, t2, t3); \
t3 = _mm256_xor_si256(t0, t1); \
t2 = _mm256_xor_si256(x0, t3); \
x0 = _mm256_xor_si256(t2, t4)
# define ROUND_TBOX(x0, x1, x2, x3, x4, i) \
t0 = _mm256_set1_epi32(*(rk + i)); \
t1 = _mm256_xor_si256(x1, x2); \
t2 = _mm256_xor_si256(x3, t0); \
x4 = _mm256_xor_si256(t1, t2); \
t0 = _mm256_and_si256(x4, vindex_mask); \
t0 = _mm256_i32gather_epi32((int *)SMS4_T, t0, 4); \
t0 = _mm256_rotl_epi32(t0, 8); \
x4 = _mm256_srli_epi32(x4, 8); \
x0 = _mm256_xor_si256(x0, t0); \
t0 = _mm256_and_si256(x4, vindex_mask); \
t0 = _mm256_i32gather_epi32((int *)SMS4_T, t0, 4); \
t0 = _mm256_rotl_epi32(t0, 16); \
x4 = _mm256_srli_epi32(x4, 8); \
x0 = _mm256_xor_si256(x0, t0); \
t0 = _mm256_and_si256(x4, vindex_mask); \
t0 = _mm256_i32gather_epi32((int *)SMS4_T, t0, 4); \
t0 = _mm256_rotl_epi32(t0, 24); \
x4 = _mm256_srli_epi32(x4, 8); \
x0 = _mm256_xor_si256(x0, t0); \
t1 = _mm256_i32gather_epi32((int *)SMS4_T, x4, 4); \
x4 = _mm256_xor_si256(x0, t1)
#define ROUND(x0, x1, x2, x3, x4, i) \
t0 = _mm256_i32gather_epi32(rk+i, vindex_0s, 4); \
t1 = _mm256_xor_si256(x1, x2); \
t2 = _mm256_xor_si256(x3, t0); \
t0 = _mm256_xor_si256(t1, t2); \
S(t0, x4, t1, t2); \
L(t0, x4, t1, t2, t3, t4); \
x4 = _mm256_xor_si256(x0, t0);
# define INDEX_MASK_DBOX 0xffff
# define ROUND_DBOX(x0, x1, x2, x3, x4, i) \
t0 = _mm256_set1_epi32(*(rk + i)); \
t1 = _mm256_xor_si256(x1, x2); \
t2 = _mm256_xor_si256(x3, t0); \
x4 = _mm256_xor_si256(t1, t2); \
t0 = _mm256_srli_epi32(x4, 16); \
t1 = _mm256_i32gather_epi32((int *)SMS4_D, t0, 4); \
t2 = _mm256_and_si256(x4, vindex_mask); \
t3 = _mm256_i32gather_epi32((int *)SMS4_D, t2, 4); \
t0 = _mm256_rotl_epi32(t3, 16); \
x4 = _mm256_xor_si256(x0, t1); \
x4 = _mm256_xor_si256(x4, t0)
# define ROUND ROUND_TBOX
# define INDEX_MASK INDEX_MASK_TBOX
void sms4_avx2_encrypt_8blocks(const unsigned char *in, unsigned char *out, const sms4_key_t *key)
void sms4_avx2_ecb_encrypt_blocks(const unsigned char *in, unsigned char *out,
size_t blocks, const sms4_key_t *key)
{
const int *rk = (int *)key->rk;
__m256i x0, x1, x2, x3, x4;
__m256i t0, t1, t2, t3, t4;
GET_BLKS(x0, x1, x2, x3, in);
ROUNDS(x0, x1, x2, x3, x4);
PUT_BLKS(out, x0, x4, x3, x2);
__m256i t0, t1, t2, t3;
__m256i vindex_4i = _mm256_setr_epi32(0,4,8,12,16,20,24,28);
__m256i vindex_mask = _mm256_set1_epi32(INDEX_MASK);
__m256i vindex_read = _mm256_setr_epi32(0,8,16,24,1,9,17,25);
__m256i vindex_swap = _mm256_setr_epi8(
3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12,
3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12
);
while (blocks >= 8) {
GET_BLKS(x0, x1, x2, x3, in);
ROUNDS(x0, x1, x2, x3, x4);
PUT_BLKS(out, x0, x4, x3, x2);
in += 128;
out += 128;
blocks -= 8;
}
while (blocks--) {
sms4_encrypt(in, out, key);
in += 16;
out += 16;
}
}
void sms4_avx2_encrypt_16blocks(const unsigned char *in, unsigned char *out, const sms4_key_t *key)
void sms4_avx2_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
size_t blocks, const sms4_key_t *key, const unsigned char iv[16])
{
sms4_encrypt_8blocks(key, in, out);
sms4_encrypt_8blocks(key, in + 16*8, out + 16*8);
const int *rk = (int *)key->rk;
__m256i x0, x1, x2, x3, x4;
__m256i t0, t1, t2, t3;
__m256i vindex_4i = _mm256_setr_epi32(0,4,8,12,16,20,24,28);
__m256i vindex_mask = _mm256_set1_epi32(INDEX_MASK);
__m256i vindex_read = _mm256_setr_epi32(0,8,16,24,1,9,17,25);
__m256i vindex_swap = _mm256_setr_epi8(
3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12,
3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12
);
__m256i incr = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
int c0 = (int)GETU32(iv );
int c1 = (int)GETU32(iv + 4);
int c2 = (int)GETU32(iv + 8);
int c3 = (int)GETU32(iv + 12);
while (blocks >= 8) {
x0 = _mm256_set1_epi32(c0);
x1 = _mm256_set1_epi32(c1);
x2 = _mm256_set1_epi32(c2);
x3 = _mm256_set1_epi32(c3);
x3 = _mm256_add_epi32(x3, incr);
ROUNDS(x0, x1, x2, x3, x4);
GET_BLKS(t0, t1, t2, t3, in);
x0 = _mm256_xor_si256(x0, t0);
x4 = _mm256_xor_si256(x4, t1);
x3 = _mm256_xor_si256(x3, t2);
x2 = _mm256_xor_si256(x2, t3);
PUT_BLKS(out, x0, x4, x3, x2);
c3 += 8;
in += 128;
out += 128;
blocks -= 8;
}
if (blocks) {
unsigned char ctr[16];
memcpy(ctr, iv, 12);
PUTU32(ctr + 12, c3);
sms4_ctr32_encrypt_blocks(in, out, blocks, key, ctr);
}
}
#endif

View File

@@ -1,73 +0,0 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <openssl/sms4.h>
void sms4_encrypt_init(sms4_key_t *key)
{
}
void sms4_encrypt_8blocks(const unsigned char *in, unsigned char *out, const sms4_key_t *key)
{
sms4_encrypt(in, out, key);
sms4_encrypt(in + 16, out + 16, key);
sms4_encrypt(in + 16 * 2, out + 16 * 2, key);
sms4_encrypt(in + 16 * 3, out + 16 * 3, key);
sms4_encrypt(in + 16 * 4, out + 16 * 4, key);
sms4_encrypt(in + 16 * 5, out + 16 * 5, key);
sms4_encrypt(in + 16 * 6, out + 16 * 6, key);
sms4_encrypt(in + 16 * 7, out + 16 * 7, key);
}
void sms4_encrypt_16blocks(const unsigned char *in, unsigned char *out, const sms4_key_t *key)
{
sms4_encrypt_8blocks(in, out, key);
sms4_encrypt_8blocks(in + 16 * 8, out + 16 * 8, key);
}

View File

@@ -1,5 +1,5 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
* Copyright (c) 2014 - 2019 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -50,37 +50,18 @@
#ifndef HEADER_SMS4_LCL_H
#define HEADER_SMS4_LCL_H
#include <openssl/sms4.h>
#include <openssl/e_os2.h>
#ifdef __cplusplus
extern "C" {
#endif
extern const uint8_t SMS4_S[256];
extern const uint32_t SMS4_T[256];
extern const uint32_t SMS4_D[65536];
extern uint8_t SBOX[256];
extern uint32_t SBOX32L[256 * 256];
extern uint32_t SBOX32H[256 * 256];
#define GET32(pc) ( \
((uint32_t)(pc)[0] << 24) ^ \
((uint32_t)(pc)[1] << 16) ^ \
((uint32_t)(pc)[2] << 8) ^ \
((uint32_t)(pc)[3]))
#define PUT32(st, ct) \
(ct)[0] = (uint8_t)((st) >> 24); \
(ct)[1] = (uint8_t)((st) >> 16); \
(ct)[2] = (uint8_t)((st) >> 8); \
(ct)[3] = (uint8_t)(st)
#define ROT32(x,i) \
(((x) << i) | ((x) >> (32-i)))
#define S32(A) \
((SBOX[((A) >> 24) ] << 24) ^ \
(SBOX[((A) >> 16) & 0xff] << 16) ^ \
(SBOX[((A) >> 8) & 0xff] << 8) ^ \
(SBOX[((A)) & 0xff]))
#define S32(A) \
((SMS4_S[((A) >> 24) ] << 24) ^ \
(SMS4_S[((A) >> 16) & 0xff] << 16) ^ \
(SMS4_S[((A) >> 8) & 0xff] << 8) ^ \
(SMS4_S[((A)) & 0xff]))
#define ROUNDS(x0, x1, x2, x3, x4) \
ROUND(x0, x1, x2, x3, x4, 0); \
@@ -116,9 +97,14 @@ extern uint32_t SBOX32H[256 * 256];
ROUND(x0, x1, x2, x3, x4, 30); \
ROUND(x1, x2, x3, x4, x0, 31)
void sms4_init_sbox32(void);
void sms4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
size_t blocks, const sms4_key_t *key, const unsigned char iv[16]);
# ifdef SMS4_AVX2
void sms4_avx2_ecb_encrypt_blocks(const unsigned char *in, unsigned char *out,
size_t blocks, const sms4_key_t *key);
void sms4_avx2_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
size_t blocks, const sms4_key_t *key, const unsigned char iv[16]);
# endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,5 +1,5 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
* Copyright (c) 2014 - 2019 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -48,6 +48,8 @@
*/
#include <openssl/sms4.h>
#include "internal/rotate.h"
#include "modes_lcl.h"
#include "sms4_lcl.h"
static uint32_t FK[4] = {
@@ -67,8 +69,8 @@ static uint32_t CK[32] = {
#define L32_(x) \
((x) ^ \
ROT32((x), 13) ^ \
ROT32((x), 23))
ROL32((x), 13) ^ \
ROL32((x), 23))
#define ENC_ROUND(x0, x1, x2, x3, x4, i) \
x4 = x1 ^ x2 ^ x3 ^ *(CK + i); \
@@ -82,35 +84,36 @@ static uint32_t CK[32] = {
x4 = x0 ^ L32_(x4); \
*(rk + 31 - i) = x4
void sms4_set_encrypt_key(sms4_key_t *key, const unsigned char *user_key)
void sms4_set_encrypt_key(sms4_key_t *key, const unsigned char user_key[16])
{
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];
x0 = GETU32(user_key ) ^ FK[0];
x1 = GETU32(user_key + 4) ^ FK[1];
x2 = GETU32(user_key + 8) ^ FK[2];
x3 = GETU32(user_key + 12) ^ FK[3];
#define ROUND ENC_ROUND
ROUNDS(x0, x1, x2, x3, x4);
#undef ROUND
x0 = x1 = x2 = x3 = x4 = 0;
}
void sms4_set_decrypt_key(sms4_key_t *key, const unsigned char *user_key)
void sms4_set_decrypt_key(sms4_key_t *key, const unsigned char user_key[16])
{
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];
x0 = GETU32(user_key ) ^ FK[0];
x1 = GETU32(user_key + 4) ^ FK[1];
x2 = GETU32(user_key + 8) ^ FK[2];
x3 = GETU32(user_key + 12) ^ FK[3];
#undef ROUND
#define ROUND DEC_ROUND
ROUNDS(x0, x1, x2, x3, x4);
#undef ROUND
x0 = x1 = x2 = x3 = x4 = 0;
}