├── 国密算法SMS4.pdf ├── .gitignore ├── main └── jni │ ├── Application.mk │ ├── sm4test.c │ └── Android.mk ├── libsm4 └── jni │ ├── Application.mk │ ├── Android.mk │ ├── sm4.h │ ├── e_os2.h │ └── sm4.c ├── clean.sh ├── run.sh └── README.md /国密算法SMS4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiang/SM4/HEAD/国密算法SMS4.pdf -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | libsm4/libs/ 2 | libsm4/obj/ 3 | main/libs/ 4 | main/obj/ 5 | 6 | -------------------------------------------------------------------------------- /main/jni/Application.mk: -------------------------------------------------------------------------------- 1 | APP_ABI := armeabi-v7a 2 | APP_PLATFORM := android-14 3 | -------------------------------------------------------------------------------- /libsm4/jni/Application.mk: -------------------------------------------------------------------------------- 1 | APP_ABI := armeabi-v7a 2 | APP_PLATFORM := android-14 3 | -------------------------------------------------------------------------------- /clean.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ndk-build -C ./main/jni clean 4 | ndk-build -C ./libsm4/jni clean -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | adb push ./main/libs/armeabi-v7a/libsm4.so ./main/libs/armeabi-v7a/main /data/local/tmp 4 | adb shell "chmod +x /data/local/tmp/main" 5 | adb shell "export LD_LIBRARY_PATH=/data/local/tmp; /data/local/tmp/main" -------------------------------------------------------------------------------- /main/jni/sm4test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SM4/SMS4 algorithm test programme 3 | * 2012-4-21 4 | */ 5 | 6 | #include 7 | #include 8 | #include "sm4.h" 9 | 10 | int main() 11 | { 12 | unsigned char key[16] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; 13 | unsigned char input[16] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; 14 | unsigned char output[16]; 15 | 16 | SM4_KEY sm_key; 17 | 18 | 19 | //加密 20 | SM4_set_key(key,&sm_key); 21 | SM4_encrypt(input, output, &sm_key); 22 | 23 | int i; 24 | for(i=0;i<16;i++) 25 | printf("%02x ", output[i]); 26 | printf("\n"); 27 | 28 | //解密 29 | SM4_decrypt(output, output, &sm_key); 30 | for(i=0;i<16;i++) 31 | printf("%02x ", output[i]); 32 | printf("\n"); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /libsm4/jni/Android.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2009 The Android Open Source Project 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # 17 | 18 | LOCAL_PATH := $(call my-dir) 19 | 20 | include $(CLEAR_VARS) 21 | LOCAL_MODULE := sm4 22 | LOCAL_SRC_FILES := sm4.c 23 | LOCAL_CFLAGS := -Wall -Wextra -Werror #-O0 24 | LOCAL_CONLYFLAGS := -std=c11 25 | include $(BUILD_SHARED_LIBRARY) 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /libsm4/jni/sm4.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. 3 | * Copyright 2017 Ribose Inc. All Rights Reserved. 4 | * 5 | * Licensed under the OpenSSL license (the "License"). You may not use 6 | * this file except in compliance with the License. You can obtain a copy 7 | * in the file LICENSE in the source distribution or at 8 | * https://www.openssl.org/source/license.html 9 | */ 10 | 11 | #ifndef HEADER_SM4_H 12 | # define HEADER_SM4_H 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | #include "e_os2.h" 19 | 20 | # ifdef OPENSSL_NO_SM4 21 | # error SM4 is disabled. 22 | # endif 23 | 24 | # define SM4_ENCRYPT 1 25 | # define SM4_DECRYPT 0 26 | 27 | # define SM4_BLOCK_SIZE 16 28 | # define SM4_KEY_SCHEDULE 32 29 | 30 | typedef struct SM4_KEY_st { 31 | uint32_t rk[SM4_KEY_SCHEDULE]; 32 | } SM4_KEY; 33 | 34 | int SM4_set_key(const uint8_t *key, SM4_KEY *ks); 35 | 36 | void SM4_encrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks); 37 | 38 | void SM4_decrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks); 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /main/jni/Android.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2009 The Android Open Source Project 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # 17 | LOCAL_PATH := $(call my-dir) 18 | 19 | include $(CLEAR_VARS) 20 | LOCAL_MODULE := sm4 21 | LOCAL_SRC_FILES := $(LOCAL_PATH)/../../libsm4/libs/$(TARGET_ARCH_ABI)/libsm4.so 22 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../libsm4/jni 23 | include $(PREBUILT_SHARED_LIBRARY) 24 | 25 | 26 | 27 | ## 编译test文件 28 | 29 | include $(CLEAR_VARS) 30 | LOCAL_MODULE := main 31 | LOCAL_SRC_FILES := sm4test.c 32 | LOCAL_SHARED_LIBRARIES := sm4 33 | 34 | LOCAL_CONLYFLAGS := -std=c11 35 | LOCAL_CFLAGS := -Wall -Wextra -Werror -fPIE 36 | LOCAL_LDLIBS += -fPIE -pie 37 | 38 | include $(BUILD_EXECUTABLE) 39 | 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 概述 2 | 这是国产加密算法 SM4 的实现,这个加密算法是在开源项目[OpenSSL](https://github.com/openssl/openssl)中获取到的,从安全性和可信程度上有一定的保障 3 | 4 | ## 目录结构解释 5 | 6 | ``` 7 | ├── README.md 8 | ├── build_libs.sh (可以用文本编辑器打开看下,里面不过ndk是编译命令的集合而已) 9 | ├── clean.sh (用于清除编译后产生的文件,比如sm4.so 等) 10 | ├── libsm4 (sm4加密核心工程) 11 | │   └── jni 12 | │   ├── Android.mk(编译生成动态库,libsm4.so) 13 | │   ├── Application.mk 14 | │   ├── e_os2.h(由于代码来自OpenSSL,这个东西是里面的一些基本宏定义,可以和下面的sm4.h 进行合并) 15 | │   ├── sm4.c (主要实现sm4的加解密算法) 16 | │   └── sm4.h (相对于OpenSSL的源码进行了些修改,使其能够导出方法) 17 | ├── main (测试工程) 18 | │   └── jni 19 | │   ├── Android.mk(编译生成可执行文件,并且链接上面生成的动态库) 20 | │   ├── Application.mk(指定生成不同CPU架构的库) 21 | │   └── sm4test.c (用来调用上面生成的动态库,然后加解密一个字节数组) 22 | └── run.sh (用于把编译生成的测试文件(sm4的动态库和可执行文件),推送到手机,并且在手机环境中运行) 23 | ``` 24 | 25 | # 环境配置 26 | 需要下载安装[NDK](https://developer.android.com/ndk/downloads/index.html) 来编译这个工程 ~~这不废话吗~~ 27 | 28 | 29 | - 本工程在开发和调试中使用的 NDK 版本是 **r17**。ndk的环境变量的配置可以参考[这篇文章](https://www.jianshu.com/p/5cbddbdac211) 30 | 31 | - 配置adb环境变量 32 | 33 | - 测试机:使用的是华为p9,Android 7.0 34 | 35 | - 开发环境:Mac os ,jdk 8 ,ndk 17 36 | 37 | >如果测试机的CPU架构不一样,需要修改`Application.mk`文件里面的APP_ABI的值,同时还要修改辅助脚本里面和测试工程的`Android.mk`文件里面的动态库的路径。 38 | 39 | # 编译 40 | 41 | 为了减少操作命令行的次数,增加了以下三个shell脚本来辅助编译和测试。 42 | 43 | * 编译动态库 (libsm4.so 和其他的用于测试的可执行文件) 44 | 45 | ``` 46 | ./build_libs.sh 47 | ``` 48 | 49 | * 把编译生成的动态库和可执行文件,通过adb push到手机的环境中运行 50 | 51 | ``` 52 | ./run.sh 53 | ``` 54 | 55 | * 清除动态库 56 | 57 | ``` 58 | ./clean_libs.sh 59 | ``` 60 | 61 | # 结束语 62 | 其实上面这么编译纯粹是为了演示先编译出动态库,再调用动态库的这个流程。除此之外还可以: 63 | 64 | 1. 直接把sm4.c,和sm4test.c 一起编译生成一个可执行文件,把这个可执行文件复制到手机,就能直接运行了,不用复制动态库到手机了 65 | 2. 把上面的生成动态库的过程,生成静态库,然后在main这个项目中引用静态库,这样编译出来的东西也只有一个文件,直接复制到手机,就能运行 66 | 67 | 编译的方式很多也很灵活,就看你想怎么在Android中使用编译好库。 -------------------------------------------------------------------------------- /libsm4/jni/e_os2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. 3 | * 4 | * Licensed under the OpenSSL license (the "License"). You may not use 5 | * this file except in compliance with the License. You can obtain a copy 6 | * in the file LICENSE in the source distribution or at 7 | * https://www.openssl.org/source/license.html 8 | */ 9 | 10 | #ifndef HEADER_E_OS2_H 11 | # define HEADER_E_OS2_H 12 | 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | /****************************************************************************** 19 | * Detect operating systems. This probably needs completing. 20 | * The result is that at least one OPENSSL_SYS_os macro should be defined. 21 | * However, if none is defined, Unix is assumed. 22 | **/ 23 | 24 | # define OPENSSL_SYS_UNIX 25 | 26 | /* --------------------- Microsoft operating systems ---------------------- */ 27 | 28 | /* 29 | * Note that MSDOS actually denotes 32-bit environments running on top of 30 | * MS-DOS, such as DJGPP one. 31 | */ 32 | # if defined(OPENSSL_SYS_MSDOS) 33 | # undef OPENSSL_SYS_UNIX 34 | # endif 35 | 36 | /* 37 | * For 32 bit environment, there seems to be the CygWin environment and then 38 | * all the others that try to do the same thing Microsoft does... 39 | */ 40 | /* 41 | * UEFI lives here because it might be built with a Microsoft toolchain and 42 | * we need to avoid the false positive match on Windows. 43 | */ 44 | # if defined(OPENSSL_SYS_UEFI) 45 | # undef OPENSSL_SYS_UNIX 46 | # elif defined(OPENSSL_SYS_UWIN) 47 | # undef OPENSSL_SYS_UNIX 48 | # define OPENSSL_SYS_WIN32_UWIN 49 | # else 50 | # if defined(__CYGWIN__) || defined(OPENSSL_SYS_CYGWIN) 51 | # define OPENSSL_SYS_WIN32_CYGWIN 52 | # else 53 | # if defined(_WIN32) || defined(OPENSSL_SYS_WIN32) 54 | # undef OPENSSL_SYS_UNIX 55 | # if !defined(OPENSSL_SYS_WIN32) 56 | # define OPENSSL_SYS_WIN32 57 | # endif 58 | # endif 59 | # if defined(_WIN64) || defined(OPENSSL_SYS_WIN64) 60 | # undef OPENSSL_SYS_UNIX 61 | # if !defined(OPENSSL_SYS_WIN64) 62 | # define OPENSSL_SYS_WIN64 63 | # endif 64 | # endif 65 | # if defined(OPENSSL_SYS_WINNT) 66 | # undef OPENSSL_SYS_UNIX 67 | # endif 68 | # if defined(OPENSSL_SYS_WINCE) 69 | # undef OPENSSL_SYS_UNIX 70 | # endif 71 | # endif 72 | # endif 73 | 74 | /* Anything that tries to look like Microsoft is "Windows" */ 75 | # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN64) || defined(OPENSSL_SYS_WINNT) || defined(OPENSSL_SYS_WINCE) 76 | # undef OPENSSL_SYS_UNIX 77 | # define OPENSSL_SYS_WINDOWS 78 | # ifndef OPENSSL_SYS_MSDOS 79 | # define OPENSSL_SYS_MSDOS 80 | # endif 81 | # endif 82 | 83 | /* 84 | * DLL settings. This part is a bit tough, because it's up to the 85 | * application implementor how he or she will link the application, so it 86 | * requires some macro to be used. 87 | */ 88 | # ifdef OPENSSL_SYS_WINDOWS 89 | # ifndef OPENSSL_OPT_WINDLL 90 | # if defined(_WINDLL) /* This is used when building OpenSSL to 91 | * indicate that DLL linkage should be used */ 92 | # define OPENSSL_OPT_WINDLL 93 | # endif 94 | # endif 95 | # endif 96 | 97 | /* ------------------------------- OpenVMS -------------------------------- */ 98 | # if defined(__VMS) || defined(VMS) || defined(OPENSSL_SYS_VMS) 99 | # if !defined(OPENSSL_SYS_VMS) 100 | # undef OPENSSL_SYS_UNIX 101 | # endif 102 | # define OPENSSL_SYS_VMS 103 | # if defined(__DECC) 104 | # define OPENSSL_SYS_VMS_DECC 105 | # elif defined(__DECCXX) 106 | # define OPENSSL_SYS_VMS_DECC 107 | # define OPENSSL_SYS_VMS_DECCXX 108 | # else 109 | # define OPENSSL_SYS_VMS_NODECC 110 | # endif 111 | # endif 112 | 113 | /* -------------------------------- Unix ---------------------------------- */ 114 | # ifdef OPENSSL_SYS_UNIX 115 | # if defined(linux) || defined(__linux__) && !defined(OPENSSL_SYS_LINUX) 116 | # define OPENSSL_SYS_LINUX 117 | # endif 118 | # if defined(_AIX) && !defined(OPENSSL_SYS_AIX) 119 | # define OPENSSL_SYS_AIX 120 | # endif 121 | # endif 122 | 123 | /* -------------------------------- VOS ----------------------------------- */ 124 | # if defined(__VOS__) && !defined(OPENSSL_SYS_VOS) 125 | # define OPENSSL_SYS_VOS 126 | # ifdef __HPPA__ 127 | # define OPENSSL_SYS_VOS_HPPA 128 | # endif 129 | # ifdef __IA32__ 130 | # define OPENSSL_SYS_VOS_IA32 131 | # endif 132 | # endif 133 | 134 | /** 135 | * That's it for OS-specific stuff 136 | *****************************************************************************/ 137 | 138 | /* Specials for I/O an exit */ 139 | # ifdef OPENSSL_SYS_MSDOS 140 | # define OPENSSL_UNISTD_IO 141 | # define OPENSSL_DECLARE_EXIT extern void exit(int); 142 | # else 143 | # define OPENSSL_UNISTD_IO OPENSSL_UNISTD 144 | # define OPENSSL_DECLARE_EXIT /* declared in unistd.h */ 145 | # endif 146 | 147 | /*- 148 | * OPENSSL_EXTERN is normally used to declare a symbol with possible extra 149 | * attributes to handle its presence in a shared library. 150 | * OPENSSL_EXPORT is used to define a symbol with extra possible attributes 151 | * to make it visible in a shared library. 152 | * Care needs to be taken when a header file is used both to declare and 153 | * define symbols. Basically, for any library that exports some global 154 | * variables, the following code must be present in the header file that 155 | * declares them, before OPENSSL_EXTERN is used: 156 | * 157 | * #ifdef SOME_BUILD_FLAG_MACRO 158 | * # undef OPENSSL_EXTERN 159 | * # define OPENSSL_EXTERN OPENSSL_EXPORT 160 | * #endif 161 | * 162 | * The default is to have OPENSSL_EXPORT and OPENSSL_EXTERN 163 | * have some generally sensible values. 164 | */ 165 | 166 | # if defined(OPENSSL_SYS_WINDOWS) && defined(OPENSSL_OPT_WINDLL) 167 | # define OPENSSL_EXPORT extern __declspec(dllexport) 168 | # define OPENSSL_EXTERN extern __declspec(dllimport) 169 | # else 170 | # define OPENSSL_EXPORT extern 171 | # define OPENSSL_EXTERN extern 172 | # endif 173 | 174 | /*- 175 | * Macros to allow global variables to be reached through function calls when 176 | * required (if a shared library version requires it, for example. 177 | * The way it's done allows definitions like this: 178 | * 179 | * // in foobar.c 180 | * OPENSSL_IMPLEMENT_GLOBAL(int,foobar,0) 181 | * // in foobar.h 182 | * OPENSSL_DECLARE_GLOBAL(int,foobar); 183 | * #define foobar OPENSSL_GLOBAL_REF(foobar) 184 | */ 185 | # ifdef OPENSSL_EXPORT_VAR_AS_FUNCTION 186 | # define OPENSSL_IMPLEMENT_GLOBAL(type,name,value) \ 187 | type *_shadow_##name(void) \ 188 | { static type _hide_##name=value; return &_hide_##name; } 189 | # define OPENSSL_DECLARE_GLOBAL(type,name) type *_shadow_##name(void) 190 | # define OPENSSL_GLOBAL_REF(name) (*(_shadow_##name())) 191 | # else 192 | # define OPENSSL_IMPLEMENT_GLOBAL(type,name,value) type _shadow_##name=value; 193 | # define OPENSSL_DECLARE_GLOBAL(type,name) OPENSSL_EXPORT type _shadow_##name 194 | # define OPENSSL_GLOBAL_REF(name) _shadow_##name 195 | # endif 196 | 197 | # ifdef _WIN32 198 | # ifdef _WIN64 199 | # define ossl_ssize_t __int64 200 | # define OSSL_SSIZE_MAX _I64_MAX 201 | # else 202 | # define ossl_ssize_t int 203 | # define OSSL_SSIZE_MAX INT_MAX 204 | # endif 205 | # endif 206 | 207 | # if defined(OPENSSL_SYS_UEFI) && !defined(ossl_ssize_t) 208 | # define ossl_ssize_t INTN 209 | # define OSSL_SSIZE_MAX MAX_INTN 210 | # endif 211 | 212 | # ifndef ossl_ssize_t 213 | # define ossl_ssize_t ssize_t 214 | # if defined(SSIZE_MAX) 215 | # define OSSL_SSIZE_MAX SSIZE_MAX 216 | # elif defined(_POSIX_SSIZE_MAX) 217 | # define OSSL_SSIZE_MAX _POSIX_SSIZE_MAX 218 | # endif 219 | # endif 220 | 221 | # ifdef DEBUG_UNUSED 222 | # define __owur __attribute__((__warn_unused_result__)) 223 | # else 224 | # define __owur 225 | # endif 226 | 227 | /* Standard integer types */ 228 | # if defined(OPENSSL_SYS_UEFI) 229 | typedef INT8 int8_t; 230 | typedef UINT8 uint8_t; 231 | typedef INT16 int16_t; 232 | typedef UINT16 uint16_t; 233 | typedef INT32 int32_t; 234 | typedef UINT32 uint32_t; 235 | typedef INT64 int64_t; 236 | typedef UINT64 uint64_t; 237 | # elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \ 238 | defined(__osf__) || defined(__sgi) || defined(__hpux) || \ 239 | defined(OPENSSL_SYS_VMS) || defined (__OpenBSD__) 240 | # include 241 | # elif defined(_MSC_VER) && _MSC_VER<=1500 242 | /* 243 | * minimally required typdefs for systems not supporting inttypes.h or 244 | * stdint.h: currently just older VC++ 245 | */ 246 | typedef signed char int8_t; 247 | typedef unsigned char uint8_t; 248 | typedef short int16_t; 249 | typedef unsigned short uint16_t; 250 | typedef int int32_t; 251 | typedef unsigned int uint32_t; 252 | typedef __int64 int64_t; 253 | typedef unsigned __int64 uint64_t; 254 | # else 255 | # include 256 | # endif 257 | 258 | /* ossl_inline: portable inline definition usable in public headers */ 259 | # if !defined(inline) && !defined(__cplusplus) 260 | # if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L 261 | /* just use inline */ 262 | # define ossl_inline inline 263 | # elif defined(__GNUC__) && __GNUC__>=2 264 | # define ossl_inline __inline__ 265 | # elif defined(_MSC_VER) 266 | /* 267 | * Visual Studio: inline is available in C++ only, however 268 | * __inline is available for C, see 269 | * http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx 270 | */ 271 | # define ossl_inline __inline 272 | # else 273 | # define ossl_inline 274 | # endif 275 | # else 276 | # define ossl_inline inline 277 | # endif 278 | 279 | # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L 280 | # define ossl_noreturn _Noreturn 281 | # elif defined(__GNUC__) && __GNUC__ >= 2 282 | # define ossl_noreturn __attribute__((noreturn)) 283 | # else 284 | # define ossl_noreturn 285 | # endif 286 | 287 | #ifdef __cplusplus 288 | } 289 | #endif 290 | #endif -------------------------------------------------------------------------------- /libsm4/jni/sm4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. 3 | * Copyright 2017 Ribose Inc. All Rights Reserved. 4 | * Ported from Ribose contributions from Botan. 5 | * 6 | * Licensed under the OpenSSL license (the "License"). You may not use 7 | * this file except in compliance with the License. You can obtain a copy 8 | * in the file LICENSE in the source distribution or at 9 | * https://www.openssl.org/source/license.html 10 | */ 11 | 12 | #include "sm4.h" 13 | 14 | static const uint8_t SM4_S[256] = { 15 | 0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 16 | 0x28, 0xFB, 0x2C, 0x05, 0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 17 | 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, 0x9C, 0x42, 0x50, 0xF4, 18 | 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62, 19 | 0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA, 20 | 0x75, 0x8F, 0x3F, 0xA6, 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 21 | 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8, 0x68, 0x6B, 0x81, 0xB2, 22 | 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35, 23 | 0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 24 | 0x01, 0x21, 0x78, 0x87, 0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 25 | 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, 0xEA, 0xBF, 0x8A, 0xD2, 26 | 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1, 27 | 0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 28 | 0xF5, 0x8C, 0xB1, 0xE3, 0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 29 | 0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F, 0xD5, 0xDB, 0x37, 0x45, 30 | 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51, 31 | 0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 32 | 0x1F, 0x10, 0x5A, 0xD8, 0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 33 | 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0, 0x89, 0x69, 0x97, 0x4A, 34 | 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84, 35 | 0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 36 | 0xD7, 0xCB, 0x39, 0x48 37 | }; 38 | 39 | /* 40 | * SM4_SBOX_T[j] == L(SM4_SBOX[j]). 41 | */ 42 | static const uint32_t SM4_SBOX_T[256] = { 43 | 0x8ED55B5B, 0xD0924242, 0x4DEAA7A7, 0x06FDFBFB, 0xFCCF3333, 0x65E28787, 44 | 0xC93DF4F4, 0x6BB5DEDE, 0x4E165858, 0x6EB4DADA, 0x44145050, 0xCAC10B0B, 45 | 0x8828A0A0, 0x17F8EFEF, 0x9C2CB0B0, 0x11051414, 0x872BACAC, 0xFB669D9D, 46 | 0xF2986A6A, 0xAE77D9D9, 0x822AA8A8, 0x46BCFAFA, 0x14041010, 0xCFC00F0F, 47 | 0x02A8AAAA, 0x54451111, 0x5F134C4C, 0xBE269898, 0x6D482525, 0x9E841A1A, 48 | 0x1E061818, 0xFD9B6666, 0xEC9E7272, 0x4A430909, 0x10514141, 0x24F7D3D3, 49 | 0xD5934646, 0x53ECBFBF, 0xF89A6262, 0x927BE9E9, 0xFF33CCCC, 0x04555151, 50 | 0x270B2C2C, 0x4F420D0D, 0x59EEB7B7, 0xF3CC3F3F, 0x1CAEB2B2, 0xEA638989, 51 | 0x74E79393, 0x7FB1CECE, 0x6C1C7070, 0x0DABA6A6, 0xEDCA2727, 0x28082020, 52 | 0x48EBA3A3, 0xC1975656, 0x80820202, 0xA3DC7F7F, 0xC4965252, 0x12F9EBEB, 53 | 0xA174D5D5, 0xB38D3E3E, 0xC33FFCFC, 0x3EA49A9A, 0x5B461D1D, 0x1B071C1C, 54 | 0x3BA59E9E, 0x0CFFF3F3, 0x3FF0CFCF, 0xBF72CDCD, 0x4B175C5C, 0x52B8EAEA, 55 | 0x8F810E0E, 0x3D586565, 0xCC3CF0F0, 0x7D196464, 0x7EE59B9B, 0x91871616, 56 | 0x734E3D3D, 0x08AAA2A2, 0xC869A1A1, 0xC76AADAD, 0x85830606, 0x7AB0CACA, 57 | 0xB570C5C5, 0xF4659191, 0xB2D96B6B, 0xA7892E2E, 0x18FBE3E3, 0x47E8AFAF, 58 | 0x330F3C3C, 0x674A2D2D, 0xB071C1C1, 0x0E575959, 0xE99F7676, 0xE135D4D4, 59 | 0x661E7878, 0xB4249090, 0x360E3838, 0x265F7979, 0xEF628D8D, 0x38596161, 60 | 0x95D24747, 0x2AA08A8A, 0xB1259494, 0xAA228888, 0x8C7DF1F1, 0xD73BECEC, 61 | 0x05010404, 0xA5218484, 0x9879E1E1, 0x9B851E1E, 0x84D75353, 0x00000000, 62 | 0x5E471919, 0x0B565D5D, 0xE39D7E7E, 0x9FD04F4F, 0xBB279C9C, 0x1A534949, 63 | 0x7C4D3131, 0xEE36D8D8, 0x0A020808, 0x7BE49F9F, 0x20A28282, 0xD4C71313, 64 | 0xE8CB2323, 0xE69C7A7A, 0x42E9ABAB, 0x43BDFEFE, 0xA2882A2A, 0x9AD14B4B, 65 | 0x40410101, 0xDBC41F1F, 0xD838E0E0, 0x61B7D6D6, 0x2FA18E8E, 0x2BF4DFDF, 66 | 0x3AF1CBCB, 0xF6CD3B3B, 0x1DFAE7E7, 0xE5608585, 0x41155454, 0x25A38686, 67 | 0x60E38383, 0x16ACBABA, 0x295C7575, 0x34A69292, 0xF7996E6E, 0xE434D0D0, 68 | 0x721A6868, 0x01545555, 0x19AFB6B6, 0xDF914E4E, 0xFA32C8C8, 0xF030C0C0, 69 | 0x21F6D7D7, 0xBC8E3232, 0x75B3C6C6, 0x6FE08F8F, 0x691D7474, 0x2EF5DBDB, 70 | 0x6AE18B8B, 0x962EB8B8, 0x8A800A0A, 0xFE679999, 0xE2C92B2B, 0xE0618181, 71 | 0xC0C30303, 0x8D29A4A4, 0xAF238C8C, 0x07A9AEAE, 0x390D3434, 0x1F524D4D, 72 | 0x764F3939, 0xD36EBDBD, 0x81D65757, 0xB7D86F6F, 0xEB37DCDC, 0x51441515, 73 | 0xA6DD7B7B, 0x09FEF7F7, 0xB68C3A3A, 0x932FBCBC, 0x0F030C0C, 0x03FCFFFF, 74 | 0xC26BA9A9, 0xBA73C9C9, 0xD96CB5B5, 0xDC6DB1B1, 0x375A6D6D, 0x15504545, 75 | 0xB98F3636, 0x771B6C6C, 0x13ADBEBE, 0xDA904A4A, 0x57B9EEEE, 0xA9DE7777, 76 | 0x4CBEF2F2, 0x837EFDFD, 0x55114444, 0xBDDA6767, 0x2C5D7171, 0x45400505, 77 | 0x631F7C7C, 0x50104040, 0x325B6969, 0xB8DB6363, 0x220A2828, 0xC5C20707, 78 | 0xF531C4C4, 0xA88A2222, 0x31A79696, 0xF9CE3737, 0x977AEDED, 0x49BFF6F6, 79 | 0x992DB4B4, 0xA475D1D1, 0x90D34343, 0x5A124848, 0x58BAE2E2, 0x71E69797, 80 | 0x64B6D2D2, 0x70B2C2C2, 0xAD8B2626, 0xCD68A5A5, 0xCB955E5E, 0x624B2929, 81 | 0x3C0C3030, 0xCE945A5A, 0xAB76DDDD, 0x867FF9F9, 0xF1649595, 0x5DBBE6E6, 82 | 0x35F2C7C7, 0x2D092424, 0xD1C61717, 0xD66FB9B9, 0xDEC51B1B, 0x94861212, 83 | 0x78186060, 0x30F3C3C3, 0x897CF5F5, 0x5CEFB3B3, 0xD23AE8E8, 0xACDF7373, 84 | 0x794C3535, 0xA0208080, 0x9D78E5E5, 0x56EDBBBB, 0x235E7D7D, 0xC63EF8F8, 85 | 0x8BD45F5F, 0xE7C82F2F, 0xDD39E4E4, 0x68492121 }; 86 | 87 | static ossl_inline uint32_t rotl(uint32_t a, uint8_t n) 88 | { 89 | return (a << n) | (a >> (32 - n)); 90 | } 91 | 92 | static ossl_inline uint32_t load_u32_be(const uint8_t *b, uint32_t n) 93 | { 94 | return ((uint32_t)b[4 * n] << 24) | 95 | ((uint32_t)b[4 * n + 1] << 16) | 96 | ((uint32_t)b[4 * n + 2] << 8) | 97 | ((uint32_t)b[4 * n + 3]); 98 | } 99 | 100 | static ossl_inline void store_u32_be(uint32_t v, uint8_t *b) 101 | { 102 | b[0] = (uint8_t)(v >> 24); 103 | b[1] = (uint8_t)(v >> 16); 104 | b[2] = (uint8_t)(v >> 8); 105 | b[3] = (uint8_t)(v); 106 | } 107 | 108 | static ossl_inline uint32_t SM4_T_slow(uint32_t X) 109 | { 110 | uint32_t t = 0; 111 | 112 | t |= ((uint32_t)SM4_S[(uint8_t)(X >> 24)]) << 24; 113 | t |= ((uint32_t)SM4_S[(uint8_t)(X >> 16)]) << 16; 114 | t |= ((uint32_t)SM4_S[(uint8_t)(X >> 8)]) << 8; 115 | t |= SM4_S[(uint8_t)X]; 116 | 117 | /* 118 | * L linear transform 119 | */ 120 | return t ^ rotl(t, 2) ^ rotl(t, 10) ^ rotl(t, 18) ^ rotl(t, 24); 121 | } 122 | 123 | static ossl_inline uint32_t SM4_T(uint32_t X) 124 | { 125 | return SM4_SBOX_T[(uint8_t)(X >> 24)] ^ 126 | rotl(SM4_SBOX_T[(uint8_t)(X >> 16)], 24) ^ 127 | rotl(SM4_SBOX_T[(uint8_t)(X >> 8)], 16) ^ 128 | rotl(SM4_SBOX_T[(uint8_t)X], 8); 129 | } 130 | 131 | int SM4_set_key(const uint8_t *key, SM4_KEY *ks) 132 | { 133 | /* 134 | * Family Key 135 | */ 136 | static const uint32_t FK[4] = 137 | { 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc }; 138 | 139 | /* 140 | * Constant Key 141 | */ 142 | static const uint32_t CK[32] = { 143 | 0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269, 144 | 0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9, 145 | 0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249, 146 | 0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9, 147 | 0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229, 148 | 0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299, 149 | 0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209, 150 | 0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279 151 | }; 152 | 153 | uint32_t K[4]; 154 | int i; 155 | 156 | K[0] = load_u32_be(key, 0) ^ FK[0]; 157 | K[1] = load_u32_be(key, 1) ^ FK[1]; 158 | K[2] = load_u32_be(key, 2) ^ FK[2]; 159 | K[3] = load_u32_be(key, 3) ^ FK[3]; 160 | 161 | for (i = 0; i != SM4_KEY_SCHEDULE; ++i) { 162 | uint32_t X = K[(i + 1) % 4] ^ K[(i + 2) % 4] ^ K[(i + 3) % 4] ^ CK[i]; 163 | uint32_t t = 0; 164 | 165 | t |= ((uint32_t)SM4_S[(uint8_t)(X >> 24)]) << 24; 166 | t |= ((uint32_t)SM4_S[(uint8_t)(X >> 16)]) << 16; 167 | t |= ((uint32_t)SM4_S[(uint8_t)(X >> 8)]) << 8; 168 | t |= SM4_S[(uint8_t)X]; 169 | 170 | t = t ^ rotl(t, 13) ^ rotl(t, 23); 171 | K[i % 4] ^= t; 172 | ks->rk[i] = K[i % 4]; 173 | } 174 | 175 | return 1; 176 | } 177 | 178 | #define SM4_RNDS(k0, k1, k2, k3, F) \ 179 | do { \ 180 | B0 ^= F(B1 ^ B2 ^ B3 ^ ks->rk[k0]); \ 181 | B1 ^= F(B0 ^ B2 ^ B3 ^ ks->rk[k1]); \ 182 | B2 ^= F(B0 ^ B1 ^ B3 ^ ks->rk[k2]); \ 183 | B3 ^= F(B0 ^ B1 ^ B2 ^ ks->rk[k3]); \ 184 | } while(0) 185 | 186 | void SM4_encrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks) 187 | { 188 | uint32_t B0 = load_u32_be(in, 0); 189 | uint32_t B1 = load_u32_be(in, 1); 190 | uint32_t B2 = load_u32_be(in, 2); 191 | uint32_t B3 = load_u32_be(in, 3); 192 | 193 | /* 194 | * Uses byte-wise sbox in the first and last rounds to provide some 195 | * protection from cache based side channels. 196 | */ 197 | SM4_RNDS( 0, 1, 2, 3, SM4_T_slow); 198 | SM4_RNDS( 4, 5, 6, 7, SM4_T); 199 | SM4_RNDS( 8, 9, 10, 11, SM4_T); 200 | SM4_RNDS(12, 13, 14, 15, SM4_T); 201 | SM4_RNDS(16, 17, 18, 19, SM4_T); 202 | SM4_RNDS(20, 21, 22, 23, SM4_T); 203 | SM4_RNDS(24, 25, 26, 27, SM4_T); 204 | SM4_RNDS(28, 29, 30, 31, SM4_T_slow); 205 | 206 | store_u32_be(B3, out); 207 | store_u32_be(B2, out + 4); 208 | store_u32_be(B1, out + 8); 209 | store_u32_be(B0, out + 12); 210 | } 211 | 212 | void SM4_decrypt(const uint8_t *in, uint8_t *out, const SM4_KEY *ks) 213 | { 214 | uint32_t B0 = load_u32_be(in, 0); 215 | uint32_t B1 = load_u32_be(in, 1); 216 | uint32_t B2 = load_u32_be(in, 2); 217 | uint32_t B3 = load_u32_be(in, 3); 218 | 219 | SM4_RNDS(31, 30, 29, 28, SM4_T_slow); 220 | SM4_RNDS(27, 26, 25, 24, SM4_T); 221 | SM4_RNDS(23, 22, 21, 20, SM4_T); 222 | SM4_RNDS(19, 18, 17, 16, SM4_T); 223 | SM4_RNDS(15, 14, 13, 12, SM4_T); 224 | SM4_RNDS(11, 10, 9, 8, SM4_T); 225 | SM4_RNDS( 7, 6, 5, 4, SM4_T); 226 | SM4_RNDS( 3, 2, 1, 0, SM4_T_slow); 227 | 228 | store_u32_be(B3, out); 229 | store_u32_be(B2, out + 4); 230 | store_u32_be(B1, out + 8); 231 | store_u32_be(B0, out + 12); 232 | } 233 | --------------------------------------------------------------------------------