Skip to content

Commit

Permalink
rename sm4 aesni+avx source
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 11, 2024
1 parent 51f6006 commit 5b7a9c1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ include_directories(include)

option(ENABLE_SM4_TBOX "Enable SM4 merged S-Box implementation" ON)
option(ENABLE_SM4_AARCH64 "Enable SM4 AARCH64 assembly implementation" OFF)
option(ENABLE_GMUL_AARCH64 "Enable GF(2^128) Multiplication AArch64 assembly" OFF)
option(ENABLE_SM4_CTR_AESNI_AVX "Enable SM4 CTR AESNI+AVX assembly implementation" OFF)

option(ENABLE_SM4_ECB "Enable SM4 ECB mode" OFF)
option(ENABLE_SM4_OFB "Enable SM4 OFB mode" OFF)
option(ENABLE_SM4_CFB "Enable SM4 CFB mode" OFF)
option(ENABLE_SM4_CBC_MAC "Enable SM4-CBC-MAC" OFF)
option(ENABLE_SM4_CCM "Enable SM4 CCM mode" OFF)

option(ENABLE_GMUL_AARCH64 "Enable GF(2^128) Multiplication AArch64 assembly" OFF)


set(src
Expand Down Expand Up @@ -322,12 +323,11 @@ if (ENABLE_SM4_AARCH64)
enable_language(ASM)
endif()

option(ENABLE_SM4_AESNI_AVX "Enable SM4 AESNI+AVX assembly implementation" OFF)
if (ENABLE_SM4_AESNI_AVX)
message(STATUS "ENABLE_SM4_AESNI_AVX is ON")
add_definitions(-DENABLE_SM4_AESNI_AVX)
list(APPEND src src/sm4_aesni_avx.c)
list(APPEND tests sm4_aesni_avx)
if (ENABLE_SM4_CTR_AESNI_AVX)
message(STATUS "ENABLE_SM4_CTR_AESNI_AVX is ON")
list(FIND src src/sm4_ctr.c sm4_ctr_index)
list(REMOVE_AT src ${sm4_ctr_index})
list(INSERT src ${sm4_ctr_index} src/sm4_ctr_aesni_avx.c)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
endif()

Expand Down
2 changes: 0 additions & 2 deletions src/sm4_ctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ static void ctr_incr(uint8_t a[16])
}
}

#ifndef ENABLE_SM4_AESNI_AVX
void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t inlen, uint8_t *out)
{
uint8_t block[16];
Expand All @@ -38,7 +37,6 @@ void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, siz
inlen -= len;
}
}
#endif

int sm4_ctr_encrypt_init(SM4_CTR_CTX *ctx,
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE])
Expand Down
74 changes: 67 additions & 7 deletions src/sm4_aesni_avx.c → src/sm4_ctr_aesni_avx.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,16 +32,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <x86intrin.h>
#include <gmssl/mem.h>
#include <gmssl/sm4.h>
#include <x86intrin.h>
#include <gmssl/error.h>


void sm4_aesni_avx_encrypt(const uint32_t rk[32], const uint8_t in[16 * 4], uint8_t out[16 * 4])
static void sm4_aesni_avx_encrypt(const uint32_t rk[32], const uint8_t in[16 * 4], uint8_t out[16 * 4])
{
// nibble mask
const __m128i c0f __attribute__((aligned(0x10))) = {
Expand Down Expand Up @@ -188,3 +185,66 @@ void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, siz

memset(blocks, 0, sizeof(blocks));
}

int sm4_ctr_encrypt_init(SM4_CTR_CTX *ctx,
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE])
{
sm4_set_encrypt_key(&ctx->sm4_key, key);
memcpy(ctx->ctr, ctr, SM4_BLOCK_SIZE);
memset(ctx->block, 0, SM4_BLOCK_SIZE);
ctx->block_nbytes = 0;
return 1;
}

int sm4_ctr_encrypt_update(SM4_CTR_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
{
size_t left;
size_t nblocks;
size_t len;

if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
}
*outlen = 0;
if (ctx->block_nbytes) {
left = SM4_BLOCK_SIZE - ctx->block_nbytes;
if (inlen < left) {
memcpy(ctx->block + ctx->block_nbytes, in, inlen);
ctx->block_nbytes += inlen;
return 1;
}
memcpy(ctx->block + ctx->block_nbytes, in, left);
sm4_ctr_encrypt(&ctx->sm4_key, ctx->ctr, ctx->block, SM4_BLOCK_SIZE, out);
in += left;
inlen -= left;
out += SM4_BLOCK_SIZE;
*outlen += SM4_BLOCK_SIZE;
}
if (inlen >= SM4_BLOCK_SIZE) {
nblocks = inlen / SM4_BLOCK_SIZE;
len = nblocks * SM4_BLOCK_SIZE;
sm4_ctr_encrypt(&ctx->sm4_key, ctx->ctr, in, len, out);
in += len;
inlen -= len;
out += len;
*outlen += len;
}
if (inlen) {
memcpy(ctx->block, in, inlen);
}
ctx->block_nbytes = inlen;
return 1;
}

int sm4_ctr_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
}
sm4_ctr_encrypt(&ctx->sm4_key, ctx->ctr, ctx->block, ctx->block_nbytes, out);
*outlen = ctx->block_nbytes;
return 1;
}

0 comments on commit 5b7a9c1

Please sign in to comment.