sm4.c File Reference

SM4 encryption algorithm. More...

#include "core/crypto.h"
#include "cipher/sm4.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 
#define TAU(a)
 
#define L(b)   ((b) ^ ROL32(b, 2) ^ ROL32(b, 10) ^ ROL32(b, 18) ^ ROL32(b, 24))
 
#define LP(b)   ((b) ^ ROL32(b, 13) ^ ROL32(b, 23))
 
#define F(x0, x1, x2, x3, rk)
 

Functions

__weak_func error_t sm4Init (Sm4Context *context, const uint8_t *key, size_t keyLen)
 Key expansion. More...
 
__weak_func void sm4EncryptBlock (Sm4Context *context, const uint8_t *input, uint8_t *output)
 Encrypt a 16-byte block using SM4 algorithm. More...
 
__weak_func void sm4DecryptBlock (Sm4Context *context, const uint8_t *input, uint8_t *output)
 Decrypt a 16-byte block using SM4 algorithm. More...
 
__weak_func void sm4Deinit (Sm4Context *context)
 Release SM4 context. More...
 

Variables

const uint8_t SM4_ECB_OID [8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x01}
 
const uint8_t SM4_CBC_OID [8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x02}
 
const uint8_t SM4_OFB_OID [8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x03}
 
const uint8_t SM4_CFB_OID [8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x04}
 
const uint8_t SM4_CTR_OID [8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x07}
 
const uint8_t SM4_GCM_OID [8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x08}
 
const uint8_t SM4_CCM_OID [8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x09}
 
const uint8_t SM4_XTS_OID [8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x0A}
 
const CipherAlgo sm4CipherAlgo
 

Detailed Description

SM4 encryption algorithm.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneCRYPTO Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.4

Definition in file sm4.c.

Macro Definition Documentation

◆ F

#define F (   x0,
  x1,
  x2,
  x3,
  rk 
)
Value:
{ \
uint32_t temp; \
temp = (x1) ^ (x2) ^ (x3) ^ (rk); \
temp = TAU(temp); \
x0 ^= L(temp); \
}

Definition at line 52 of file sm4.c.

◆ L

#define L (   b)    ((b) ^ ROL32(b, 2) ^ ROL32(b, 10) ^ ROL32(b, 18) ^ ROL32(b, 24))

Definition at line 48 of file sm4.c.

◆ LP

#define LP (   b)    ((b) ^ ROL32(b, 13) ^ ROL32(b, 23))

Definition at line 49 of file sm4.c.

◆ TAU

#define TAU (   a)
Value:
((uint32_t) s[(a) & 0xFF] | \
((uint32_t) s[((a) >> 8) & 0xFF] << 8) | \
((uint32_t) s[((a) >> 16) & 0xFF] << 16) | \
((uint32_t) s[((a) >> 24) & 0xFF] << 24))

Definition at line 42 of file sm4.c.

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 32 of file sm4.c.

Function Documentation

◆ sm4DecryptBlock()

__weak_func void sm4DecryptBlock ( Sm4Context context,
const uint8_t *  input,
uint8_t *  output 
)

Decrypt a 16-byte block using SM4 algorithm.

Parameters
[in]contextPointer to the SM4 context
[in]inputCiphertext block to decrypt
[out]outputPlaintext block resulting from decryption

Definition at line 218 of file sm4.c.

◆ sm4Deinit()

__weak_func void sm4Deinit ( Sm4Context context)

Release SM4 context.

Parameters
[in]contextPointer to the SM4 context

Definition at line 256 of file sm4.c.

◆ sm4EncryptBlock()

__weak_func void sm4EncryptBlock ( Sm4Context context,
const uint8_t *  input,
uint8_t *  output 
)

Encrypt a 16-byte block using SM4 algorithm.

Parameters
[in]contextPointer to the SM4 context
[in]inputPlaintext block to encrypt
[out]outputCiphertext block resulting from encryption

Definition at line 179 of file sm4.c.

◆ sm4Init()

__weak_func error_t sm4Init ( Sm4Context context,
const uint8_t *  key,
size_t  keyLen 
)

Key expansion.

Parameters
[in]contextPointer to the SM4 context to initialize
[in]keyPointer to the key
[in]keyLenLength of the key
Returns
Error code

Definition at line 137 of file sm4.c.

Variable Documentation

◆ SM4_CBC_OID

const uint8_t SM4_CBC_OID = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x02}

Definition at line 99 of file sm4.c.

◆ SM4_CCM_OID

const uint8_t SM4_CCM_OID[8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x09}

Definition at line 109 of file sm4.c.

◆ SM4_CFB_OID

const uint8_t SM4_CFB_OID[8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x04}

Definition at line 103 of file sm4.c.

◆ SM4_CTR_OID

const uint8_t SM4_CTR_OID[8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x07}

Definition at line 105 of file sm4.c.

◆ SM4_ECB_OID

const uint8_t SM4_ECB_OID[8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x01}

Definition at line 97 of file sm4.c.

◆ SM4_GCM_OID

const uint8_t SM4_GCM_OID[8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x08}

Definition at line 107 of file sm4.c.

◆ SM4_OFB_OID

const uint8_t SM4_OFB_OID[8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x03}

Definition at line 101 of file sm4.c.

◆ SM4_XTS_OID

const uint8_t SM4_XTS_OID[8] = {0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x68, 0x0A}

Definition at line 111 of file sm4.c.

◆ sm4CipherAlgo

const CipherAlgo sm4CipherAlgo
uint8_t a
Definition: ndp.h:411
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1009
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:953
SM4 algorithm context.
Definition: sm4.h:58
#define L(b)
Definition: sm4.c:48
__weak_func void sm4Deinit(Sm4Context *context)
Release SM4 context.
Definition: sm4.c:256
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1012
#define SM4_BLOCK_SIZE
Definition: sm4.h:43
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1000
__weak_func error_t sm4Init(Sm4Context *context, const uint8_t *key, size_t keyLen)
Key expansion.
Definition: sm4.c:137
__weak_func void sm4DecryptBlock(Sm4Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using SM4 algorithm.
Definition: sm4.c:218
#define TAU(a)
Definition: sm4.c:42
__weak_func void sm4EncryptBlock(Sm4Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using SM4 algorithm.
Definition: sm4.c:179
void(* CipherAlgoDeinit)(void *context)
Definition: crypto.h:1015