sm4.h
Go to the documentation of this file.
1 /**
2  * @file sm4.h
3  * @brief SM4 encryption algorithm
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _SM4_H
32 #define _SM4_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 
37 //Application specific context
38 #ifndef SM4_PRIVATE_CONTEXT
39  #define SM4_PRIVATE_CONTEXT
40 #endif
41 
42 //SM4 block size
43 #define SM4_BLOCK_SIZE 16
44 //Common interface for encryption algorithms
45 #define SM4_CIPHER_ALGO (&sm4CipherAlgo)
46 
47 //C++ guard
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 
53 /**
54  * @brief SM4 algorithm context
55  **/
56 
57 typedef struct
58 {
60  uint32_t rk[32];
62 } Sm4Context;
63 
64 
65 //SM4 related constants
66 extern const uint8_t SM4_CBC_OID[8];
67 extern const uint8_t SM4_ECB_OID[8];
68 extern const uint8_t SM4_CBC_OID[8];
69 extern const uint8_t SM4_OFB_OID[8];
70 extern const uint8_t SM4_CFB_OID[8];
71 extern const uint8_t SM4_CTR_OID[8];
72 extern const uint8_t SM4_GCM_OID[8];
73 extern const uint8_t SM4_CCM_OID[8];
74 extern const uint8_t SM4_XTS_OID[8];
75 extern const CipherAlgo sm4CipherAlgo;
76 
77 //SM4 related functions
78 error_t sm4Init(Sm4Context *context, const uint8_t *key, size_t keyLen);
79 
80 void sm4EncryptBlock(Sm4Context *context, const uint8_t *input,
81  uint8_t *output);
82 
83 void sm4DecryptBlock(Sm4Context *context, const uint8_t *input,
84  uint8_t *output);
85 
86 void sm4Deinit(Sm4Context *context);
87 
88 //C++ guard
89 #ifdef __cplusplus
90 }
91 #endif
92 
93 #endif
unsigned int uint_t
Definition: compiler_port.h:50
General definitions for cryptographic algorithms.
error_t
Error codes.
Definition: error.h:43
const uint8_t SM4_CBC_OID[8]
Definition: sm4.h:68
const uint8_t SM4_ECB_OID[8]
Definition: sm4.c:97
error_t sm4Init(Sm4Context *context, const uint8_t *key, size_t keyLen)
Key expansion.
Definition: sm4.c:137
const uint8_t SM4_CFB_OID[8]
Definition: sm4.c:103
#define SM4_PRIVATE_CONTEXT
Definition: sm4.h:39
const uint8_t SM4_XTS_OID[8]
Definition: sm4.c:111
const uint8_t SM4_OFB_OID[8]
Definition: sm4.c:101
const CipherAlgo sm4CipherAlgo
Definition: sm4.c:114
void sm4EncryptBlock(Sm4Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using SM4 algorithm.
Definition: sm4.c:179
const uint8_t SM4_GCM_OID[8]
Definition: sm4.c:107
void sm4Deinit(Sm4Context *context)
Release SM4 context.
Definition: sm4.c:256
const uint8_t SM4_CCM_OID[8]
Definition: sm4.c:109
void sm4DecryptBlock(Sm4Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using SM4 algorithm.
Definition: sm4.c:218
const uint8_t SM4_CTR_OID[8]
Definition: sm4.c:105
Common interface for encryption algorithms.
Definition: crypto.h:1036
SM4 algorithm context.
Definition: sm4.h:58
uint_t nr
Definition: sm4.h:59