├── tools ├── README.md ├── reqparse.c ├── certparse.c ├── sm3hmac.c ├── certverify.c └── sm3.c ├── src ├── nginx.c ├── version.c ├── mem.h ├── rand.c ├── sm9_keygen.c ├── rsa.c ├── zuc_eea.c ├── rc4.c ├── sm9_sign.c ├── sm4_lcl.h ├── pem.c ├── endian.h ├── sm4_setkey.c ├── sm2_prn.c ├── sm3_hmac.c ├── chacha20.c ├── debug.c ├── block_cipher.c └── sm4_enc.c ├── README.md ├── tests ├── hextest.c ├── pemtest.c ├── block_ciphertest.c ├── destest.c ├── digesttest.c ├── gf128test.c ├── chacha20test.c ├── sha1test.c ├── base64test.c ├── md5test.c └── x509_strtest.c ├── include └── gmssl │ ├── rand.h │ ├── version.h │ ├── hex.h │ ├── pem.h │ ├── rc4.h │ ├── hkdf.h │ ├── pbkdf2.h │ ├── md5.h │ ├── sha1.h │ ├── hmac.h │ ├── rsa.h │ ├── gf128.h │ ├── sm9.h │ ├── chacha20.h │ ├── error.h │ ├── des.h │ ├── sm3.h │ ├── gcm.h │ ├── ec.h │ ├── base64.h │ ├── digest.h │ ├── block_cipher.h │ ├── x509_str.h │ ├── hash_drbg.h │ └── sm4.h └── .gitignore /tools/README.md: -------------------------------------------------------------------------------- 1 | # 命令行工具 2 | 3 | 命令行工具: 4 | 5 | * `sm3` 计算SM3杂凑值,支持带公钥和ID的Z值计算 6 | * `sm3hmac` 计算SM3-HMAC值 7 | * `sm2keygen` 生成SM2密钥对,以PKCS #8口令加密的PEM格式存储 8 | * `sm2sign`,`sm2verify` SM2签名和验证,生成DER二进制编码的SM2签名值 9 | * `sm2encrypt`,`sm2decrypt` SM2加解密,注意只支持较短的消息加密 10 | * `reqgen` 生成PKCS #10证书签名请求PEM文件 11 | * `reqparse` 解析打印REQ文件 12 | * `reqsign` CA用私钥对REQ文件签名,生成证书 13 | * `certgen`生成自签名证书 14 | * `certparse` 解析打印证书 15 | * `certverify` 验证证书链 16 | 17 | -------------------------------------------------------------------------------- /src/nginx.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | int ssl_init(void) 6 | { 7 | // 不需要这个函数 8 | return 1; 9 | } 10 | 11 | typedef struct { 12 | } SSL_CTX; 13 | 14 | 15 | // nginx中用的是PEM 16 | 17 | int ssl_use_certificate() 18 | { 19 | } 20 | 21 | int ssl_use_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert, ngx_str_t *key, ngx_array_t *passwords) 22 | { 23 | } 24 | 25 | 26 | ngx_ssl_ciphers SSL_CTX_set_cipher_list 27 | 28 | ngx_ssl_client_certificate SSL_CTX_set_verify 29 | SSL_CTX_set_verify_depth 30 | SSL_CTX_load_verify_locations 31 | SSL_load_client_CA_file 32 | 33 | ngx_ssl_trusted_certificate SSL_CTX_set_verify 34 | SSL_CTX_set_verify_depth 35 | SSL_CTX_load_verify_locations 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GmSSL 3.0 Dev 2 | 3 | 注意:本项目已经合并至 GmSSL [develop](https://github.com/guanzhi/GmSSL/tree/develop)分支,后续将在GmSSL [develop](https://github.com/guanzhi/GmSSL/tree/develop)分支进行维护,同时本项目将停止更新。 4 | 5 | 6 | 7 | GmSSL的2.x版本的开发始于2016年,目前主分支在功能上实现了对主要国密算法、标准和协议的覆盖,并成功应用于多种互联网场景中。但是随着GmSSL在物联网、区块链等新场景中的应用,及在密码产品合规检测过程中的实践,我们发现应用对GmSSL提出了一些新的需求。由于很难在基于OpenSSL的GmSSL 2.x版本上满足新需求,因此我们重新设计了GmSSL的架构,GmSSL也迎来第三个大版本——GmSSL 3.0。 8 | 9 | ## 编译与安装 10 | 11 | GmSSL 3.0 采用了cmake构建系统。下载源代码后将其解压缩,进入源码目录,执行: 12 | 13 | ```bash 14 | mkdir build 15 | cd build 16 | cmake .. 17 | make 18 | make test 19 | sudo make install 20 | ``` 21 | 22 | ## 主要新特性 23 | 24 | * 超轻量:GmSSL 3.0大幅度降低了内存需求和二进制代码体积,不依赖动态内存,可以用于无操作系统的低功耗嵌入式环境(MCU、SOC等),开发者也可以更容易地将国密算法和SSL协议嵌入到现有的项目中。 25 | * 更合规:GmSSL 3.0 可以配置为仅包含国密算法和国密协议(TLCP协议),依赖GmSSL 的密码应用更容易满足密码产品型号检测的要求,避免由于混杂非国密算法、不安全算法等导致的安全问题和合规问题。 26 | * 更安全:TLS 1.3在安全性和通信延迟上相对之前的TLS协议有巨大的提升,GmSSL 3.0支持TLS 1.3协议和RFC 8998的国密套件。GmSSL 3.0默认支持密钥的加密保护,提升了密码算法的抗侧信道攻击能力。 27 | * 跨平台:GmSSL 3.0更容易跨平台,构建系统不再依赖Perl,默认的CMake构建系统可以容易地和Visual Studio、Android NDK等默认编译工具配合使用,开发者也可以手工编写Makefile在特殊环境中编译、剪裁。 28 | 29 | ## 主要功能 30 | 31 | ### 密码算法 32 | 33 | * 分组密码:SM4 (CBC, CTR, GCM), AES (GCM) 34 | * 序列密码:ZUC/ZUC-256, ChaCha20, RC4 35 | * 哈希函数: SM3, SHA-224/256/384/512, SHA-1, MD5 36 | * 公钥密码算法:SM2, SM9, ECDH, ECDSA 37 | * 椭圆曲线参数:SM2, NIST-P256 38 | * 伪随机数生成器:HASH_DRBG (NIST.SP.800-90A) 39 | * MAC算法:HMAC, GHASH 40 | * 密钥导出函数:PBKDF2、HKDF 41 | 42 | ### PKI相关标准 43 | 44 | * 数字证书:X.509证书, CRL, CSR (PKCS #10) 45 | * 私钥加密格式:口令加密私钥PEM格式 (PKCS #8) 46 | * 数字信封:SM2加密签名消 (GM/T 0010-2012) 47 | 48 | ### SSL协议 49 | 50 | * TLCP 1.1,支持密码套件:`ECDHE_SM4_CBC_SM3 {0xE0,0x11}` (GB/T 38636-2020、GM/T 0024-2014) 51 | * TLS 1.2,支持密码套件:`ECDHE_SM4_CBC_SM3 {0xE0,0x11}` (GB/T 38636-2020、GM/T 0024-2014、RFC 5246) 52 | * TLS 1.3,支持密码套件:`TLS_SM4_GCM_SM3 {0x00,0xC6}` +ECDHE/SM2 (RFC 8998), `TLS_AES_128_GCM_SHA256` + ECDHE/ECDSA/NIST-P256 53 | 54 | -------------------------------------------------------------------------------- /tests/hextest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | int main(void) 55 | { 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /tests/pemtest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | int main(void) 55 | { 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /tests/block_ciphertest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | int main(void) 55 | { 56 | int err = 0; 57 | return err; 58 | } 59 | -------------------------------------------------------------------------------- /tests/destest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | 55 | 56 | int main(void) 57 | { 58 | int err = 0; 59 | return err; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | 52 | int gmssl_version_num(void) 53 | { 54 | return GMSSL_VERSION_NUM; 55 | } 56 | 57 | const char *gmssl_version_str(void) 58 | { 59 | return GMSSL_VERSION_STR; 60 | } 61 | -------------------------------------------------------------------------------- /include/gmssl/rand.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_RAND_H 50 | #define GMSSL_RAND_H 51 | 52 | #include 53 | #include 54 | 55 | 56 | #ifdef __cplusplus 57 | extern "C" { 58 | #endif 59 | 60 | int rand_bytes(uint8_t *buf, size_t buflen); 61 | 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | #endif 67 | -------------------------------------------------------------------------------- /src/mem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_MEM_H 50 | #define GMSSL_MEM_H 51 | 52 | #include // where size_t from 53 | 54 | 55 | void memxor(void *r, const void *a, size_t len); 56 | void gmssl_memxor(void *r, const void *a, const void *b, size_t len); 57 | int gmssl_memcmp(const void *s1, const void *s2, size_t n); 58 | 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/rand.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | int rand_bytes(uint8_t *buf, size_t len) 57 | { 58 | FILE *fp; 59 | if (!(fp = fopen("/dev/urandom", "rb"))) { 60 | error_print(); 61 | return -1; 62 | } 63 | fread(buf, 1, len, fp); 64 | return 1; 65 | } 66 | -------------------------------------------------------------------------------- /include/gmssl/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #ifndef GMSSL_VERSION_H 51 | #define GMSSL_VERSION_H 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | #define GMSSL_VERSION_NUM 30000 58 | #define GMSSL_VERSION_STR "GmSSL 3.0.0 Alpha" 59 | 60 | int gmssl_version_num(void); 61 | const char *gmssl_version_str(void); 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | #endif 67 | -------------------------------------------------------------------------------- /include/gmssl/hex.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #ifndef GMSSL_HEX_H 51 | #define GMSSL_HEX_H 52 | 53 | 54 | #include 55 | #include 56 | #include 57 | #include 58 | 59 | 60 | #ifdef __cplusplus 61 | extern "C" { 62 | #endif 63 | 64 | int hex_to_bytes(const char *in, size_t inlen, uint8_t *out, size_t *outlen); 65 | 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | #endif 71 | -------------------------------------------------------------------------------- /include/gmssl/pem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_PEM_H 50 | #define GMSSL_PEM_H 51 | 52 | 53 | #include 54 | #include 55 | #include 56 | #include 57 | 58 | 59 | #ifdef __cplusplus 60 | extern "C" { 61 | #endif 62 | 63 | 64 | int pem_read(FILE *fp, const char *name, uint8_t *out, size_t *outlen, size_t maxlen); 65 | int pem_write(FILE *fp, const char *name, const uint8_t *in, size_t inlen); 66 | 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | #endif 72 | -------------------------------------------------------------------------------- /include/gmssl/rc4.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #ifndef GMSSL_RC4_H 51 | #define GMSSL_RC4_H 52 | 53 | 54 | #include 55 | #include 56 | 57 | 58 | #ifdef __cplusplus 59 | extern "C" { 60 | #endif 61 | 62 | 63 | #define RC4_MIN_KEY_BITS 40 64 | #define RC4_STATE_NUM_WORDS 256 65 | 66 | 67 | typedef struct { 68 | uint8_t d[RC4_STATE_NUM_WORDS]; 69 | } RC4_STATE; 70 | 71 | void rc4_init(RC4_STATE *state, const uint8_t *key, size_t keylen); 72 | void rc4_generate_keystream(RC4_STATE *state, size_t outlen, uint8_t *out); 73 | 74 | 75 | #ifdef __cplusplus 76 | } 77 | #endif 78 | #endif 79 | -------------------------------------------------------------------------------- /include/gmssl/hkdf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | // RFC 5869 49 | 50 | #ifndef GMSSL_HKDF_H 51 | #define GMSSL_HKDF_H 52 | 53 | #include 54 | #include 55 | #include 56 | 57 | 58 | #ifdef __cplusplus 59 | extern "C" { 60 | #endif 61 | 62 | 63 | int hkdf_extract(const DIGEST *digest, const uint8_t *salt, size_t saltlen, 64 | const uint8_t *ikm, size_t ikmlen, 65 | uint8_t *prk, size_t *prklen); 66 | 67 | int hkdf_expand(const DIGEST *digest, const uint8_t *prk, size_t prklen, 68 | const uint8_t *opt_info, size_t opt_infolen, 69 | size_t L, uint8_t *okm); 70 | 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | #endif 76 | -------------------------------------------------------------------------------- /src/sm9_keygen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | 51 | int sm9_hash1(bignum_t r, const char *id, size_t idlen, uint8_t hid) 52 | { 53 | bignum_t h; 54 | SM3_CTX ctx1; 55 | SM3_CTX ctx2; 56 | 57 | uint8_t prefix[1] = {0x01}; 58 | uint8_t ct1[4] = {0x00, 0x00, 0x00, 0x01}; 59 | uint8_t ct2[4] = {0x00, 0x00, 0x00, 0x02}; 60 | uint8_t buf[64]; 61 | 62 | sm3_init(&ctx1); 63 | sm3_update(&ctx1, prefix, sizeof(prefix)); 64 | sm3_update(&ctx1, id, idlen); 65 | sm3_update(&ctx1, &hid, 1); 66 | 67 | memcpy(&ctx2, &ctx1, sizeof(SM3_CTX)); 68 | 69 | sm3_update(&ctx1, ct1, sizeof(ct1)); 70 | sm3_update(&ctx2, ct2, sizeof(ct2)); 71 | sm3_finish(&ctx1, buf); 72 | sm3_finish(&ctx2, buf + 32); 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /tests/digesttest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | 55 | const char *digests[] = { 56 | "md5", 57 | "sha1", 58 | "sm3", 59 | "sha224", 60 | "sha256", 61 | "sha384", 62 | "sha512", 63 | "sha512-224", 64 | "sha512-256", 65 | }; 66 | 67 | int main(void) 68 | { 69 | uint8_t dgst[64]; 70 | size_t dgstlen; 71 | size_t i, j; 72 | 73 | for (i = 0; i < sizeof(digests)/sizeof(digests[0]); i++) { 74 | const DIGEST *algor = digest_from_name(digests[i]); 75 | digest(algor, (uint8_t *)"abc", 3, dgst, &dgstlen); 76 | 77 | printf("%s (%zu) ", digests[i], dgstlen); 78 | for (j = 0; j < dgstlen; j++) { 79 | printf("%02x", dgst[j]); 80 | } 81 | printf("\n"); 82 | } 83 | 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /include/gmssl/pbkdf2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_PBKDF2_H 50 | #define GMSSL_PBKDF2_H 51 | 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | 58 | #ifdef __cplusplus 59 | extern "C" { 60 | #endif 61 | 62 | 63 | #define PBKDF2_MIN_ITER 10000 64 | #define PBKDF2_MIN_SALT_SIZE 64 65 | #define PBKDF2_DEFAULT_SALT_SIZE 8 66 | 67 | 68 | int pbkdf2_genkey(const DIGEST *digest, 69 | const char *pass, size_t passlen, const uint8_t *salt, size_t saltlen, size_t iter, 70 | size_t outlen, uint8_t *out); 71 | 72 | int pbkdf2_hmac_sm3_genkey( 73 | const char *pass, size_t passlen, const uint8_t *salt, size_t saltlen, size_t iter, 74 | size_t outlen, uint8_t *out); 75 | 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | #endif 81 | -------------------------------------------------------------------------------- /include/gmssl/md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #ifndef GMSSL_MD5_H 51 | #define GMSSL_MD5_H 52 | 53 | 54 | #include 55 | #include 56 | 57 | 58 | #ifdef __cplusplus 59 | extern "C" { 60 | #endif 61 | 62 | 63 | #define MD5_IS_BIG_ENDIAN 0 64 | 65 | #define MD5_DIGEST_SIZE 16 66 | #define MD5_BLOCK_SIZE 64 67 | #define MD5_STATE_WORDS (MD5_BLOCK_SIZE/sizeof(uint32_t)) 68 | 69 | typedef struct { 70 | uint32_t state[MD5_STATE_WORDS]; 71 | uint64_t nblocks; 72 | uint8_t block[MD5_BLOCK_SIZE]; 73 | size_t num; 74 | } MD5_CTX; 75 | 76 | 77 | void md5_init(MD5_CTX *ctx); 78 | void md5_update(MD5_CTX *ctx, const uint8_t *data, size_t datalen); 79 | void md5_finish(MD5_CTX *ctx, uint8_t dgst[MD5_DIGEST_SIZE]); 80 | void md5_digest(const uint8_t *data, size_t datalen, uint8_t dgst[MD5_DIGEST_SIZE]); 81 | 82 | 83 | #ifdef __cplusplus 84 | } 85 | #endif 86 | #endif 87 | -------------------------------------------------------------------------------- /include/gmssl/sha1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_SHA1_H 50 | #define GMSSL_SHA1_H 51 | 52 | #include 53 | #include 54 | 55 | #ifdef __cplusplus 56 | extern "C" { 57 | #endif 58 | 59 | 60 | #define SHA1_IS_BIG_ENDIAN 1 61 | 62 | #define SHA1_DIGEST_SIZE 20 63 | #define SHA1_BLOCK_SIZE 64 64 | #define SHA1_STATE_WORDS (SHA1_DIGEST_SIZE/sizeof(uint32_t)) 65 | 66 | 67 | typedef struct { 68 | uint32_t state[SHA1_STATE_WORDS]; 69 | uint64_t nblocks; 70 | uint8_t block[SHA1_BLOCK_SIZE]; 71 | size_t num; 72 | } SHA1_CTX; 73 | 74 | void sha1_init(SHA1_CTX *ctx); 75 | void sha1_update(SHA1_CTX *ctx, const uint8_t *data, size_t datalen); 76 | void sha1_finish(SHA1_CTX *ctx, uint8_t dgst[SHA1_DIGEST_SIZE]); 77 | void sha1_digest(const uint8_t *data, size_t datalen, uint8_t dgst[SHA1_DIGEST_SIZE]); 78 | 79 | 80 | #ifdef __cplusplus 81 | } 82 | #endif 83 | #endif 84 | -------------------------------------------------------------------------------- /include/gmssl/hmac.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_HMAC_H 50 | #define GMSSL_HMAC_H 51 | 52 | #include 53 | #include 54 | 55 | 56 | #ifdef __cplusplus 57 | extern "C" { 58 | #endif 59 | 60 | #define HMAC_MAX_SIZE (DIGEST_MAX_SIZE) 61 | 62 | 63 | typedef struct hmac_ctx_st { 64 | const DIGEST *digest; 65 | DIGEST_CTX digest_ctx; 66 | DIGEST_CTX i_ctx; 67 | DIGEST_CTX o_ctx; 68 | } HMAC_CTX; 69 | 70 | 71 | size_t hmac_size(const HMAC_CTX *ctx); 72 | 73 | int hmac_init(HMAC_CTX *ctx, const DIGEST *digest, const uint8_t *key, size_t keylen); 74 | int hmac_update(HMAC_CTX *ctx, const uint8_t *data, size_t datalen); 75 | int hmac_finish(HMAC_CTX *ctx, uint8_t *mac, size_t *maclen); 76 | 77 | int hmac(const DIGEST *md, const uint8_t *key, size_t keylen, 78 | const uint8_t *data, size_t dlen, 79 | uint8_t *mac, size_t *maclen); 80 | 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | #endif 86 | -------------------------------------------------------------------------------- /include/gmssl/rsa.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #ifndef GMSSL_RSA_H 51 | #define GMSSL_RSA_H 52 | 53 | 54 | #include 55 | #include 56 | #include 57 | 58 | 59 | #ifdef __cplusplus 60 | extern "C" { 61 | #endif 62 | 63 | 64 | /* 65 | RSAPublicKey ::= SEQUENCE { 66 | modulus INTEGER, -- n 67 | publicExponent INTEGER -- e 68 | } 69 | 70 | RSAPrivateKey ::= SEQUENCE { 71 | version INTEGER, -- 0 72 | modulus INTEGER, -- n 73 | publicExponent INTEGER, -- e 74 | privateExponent INTEGER, -- d 75 | prime1 INTEGER, -- p 76 | prime2 INTEGER, -- q 77 | exponent1 INTEGER, -- d mod (p-1) 78 | exponent2 INTEGER, -- d mod (q-1) 79 | coefficient INTEGER -- q^-1 mod p 80 | } 81 | */ 82 | 83 | 84 | int rsa_public_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen); 85 | 86 | 87 | 88 | 89 | 90 | 91 | #ifdef __cplusplus 92 | } 93 | #endif 94 | #endif 95 | -------------------------------------------------------------------------------- /src/rsa.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | 58 | 59 | int rsa_public_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen) 60 | { 61 | const uint8_t *d; 62 | size_t dlen; 63 | const uint8_t *p; 64 | size_t len; 65 | int val; 66 | 67 | format_print(fp, fmt, ind, "%s\n", label); 68 | ind += 4; 69 | if (asn1_sequence_from_der(&d, &dlen, &a, &alen) != 1) goto err; 70 | if (asn1_integer_from_der(&p, &len, &d, &dlen) != 1) goto err; 71 | format_bytes(fp, fmt, ind, "modulus", p, len); 72 | if (asn1_int_from_der(&val, &d, &dlen) != 1) goto err; 73 | format_print(fp, fmt, ind, "publicExponent: %d\n",val); 74 | if (asn1_length_is_zero(dlen) != 1) goto err; 75 | if (asn1_length_is_zero(alen) != 1) goto err; 76 | return 1; 77 | err: 78 | error_print(); 79 | return -1; 80 | } 81 | -------------------------------------------------------------------------------- /include/gmssl/gf128.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | /* GF(2^128) defined by f(x) = x^128 + x^7 + x^2 + x + 1 50 | * A + B mod f(x) = a xor b 51 | * A * 2 mod f(x) 52 | */ 53 | 54 | #ifndef GMSSL_GF128_H 55 | #define GMSSL_GF128_H 56 | 57 | 58 | #include 59 | 60 | 61 | #ifdef __cplusplus 62 | extern "C" { 63 | #endif 64 | 65 | #define GMSSL_HAVE_UINT128 66 | #ifdef GMSSL_HAVE_UINT128 67 | typedef unsigned __int128 gf128_t; 68 | #else 69 | typedef struct { 70 | uint64_t hi; 71 | uint64_t lo; 72 | } gf128_t; 73 | #endif 74 | 75 | gf128_t gf128_from_hex(const char *s); 76 | int gf128_equ_hex(gf128_t a, const char *s); 77 | 78 | gf128_t gf128_zero(void); 79 | 80 | gf128_t gf128_add(gf128_t a, gf128_t b); 81 | gf128_t gf128_mul(gf128_t a, gf128_t b); 82 | gf128_t gf128_mul2(gf128_t a); 83 | gf128_t gf128_from_bytes(const uint8_t p[16]); 84 | void gf128_to_bytes(gf128_t a, uint8_t p[16]); 85 | 86 | void gf128_print(const char *s, gf128_t a); 87 | 88 | 89 | #ifdef __cplusplus 90 | } 91 | #endif 92 | #endif 93 | -------------------------------------------------------------------------------- /include/gmssl/sm9.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_SM9_H 50 | #define GMSSL_SM9_H 51 | 52 | #ifdef __cplusplus 53 | extern "C" { 54 | #endif 55 | 56 | 57 | // set the same value as sm2 58 | #define SM9_MAX_ID_BITS 65535 59 | #define SM9_MAX_ID_SIZE (SM9_MAX_ID_BITS/8) 60 | 61 | typedef struct { 62 | uint8_t x[32]; 63 | uint8_t y[32]; 64 | } SM9_POINT; 65 | 66 | typedef struct { 67 | uint8_t x[64]; 68 | uint8_t y[64]; 69 | } SM9_TWIST_POINT; 70 | 71 | typedef struct { 72 | uint8_t ks[32]; 73 | SM9_TWIST_POINT Ppubs; // Ppubs = ks * P2 74 | } SM9_SIGN_MASTER_KEY; 75 | 76 | typedef struct { 77 | SM9_POINT ds; 78 | } SM9_SIGN_KEY; 79 | 80 | typedef struct { 81 | uint8_t h[32]; 82 | SM9_TWIST_POINT S; 83 | } SM9_SIGNATURE; 84 | 85 | int sm9_sign_setup(SM9_SIGN_MASTER_KEY *msk); 86 | int sm9_sign_keygen(SM9_SIGN_MASTER_KEY *msk, const char *id, size_t idlen, SM9_POINT *ds); 87 | 88 | int sm9_do_sign(SM9_SIGN_KEY *key, const uint8_t dgst[32], SM9_SIGNATURE *sig); 89 | int sm9_do_verify(SM9_SIGN_KEY *key, const uint8_t dgst[32], const SM9_SIGNATURE *sig); 90 | 91 | 92 | 93 | # ifdef __cplusplus 94 | } 95 | # endif 96 | # endif 97 | -------------------------------------------------------------------------------- /src/zuc_eea.c: -------------------------------------------------------------------------------- 1 | /* ==================================================================== 2 | * Copyright (c) 2015 - 2019 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | * ==================================================================== 48 | */ 49 | 50 | #include 51 | #include 52 | 53 | static void zuc_set_eea_key(ZUC_STATE *key, const uint8_t user_key[16], 54 | ZUC_UINT32 count, ZUC_UINT5 bearer, ZUC_BIT direction) 55 | { 56 | uint8_t iv[16] = {0}; 57 | iv[0] = iv[8] = count >> 24; 58 | iv[1] = iv[9] = count >> 16; 59 | iv[2] = iv[10] = count >> 8; 60 | iv[3] = iv[11] = count; 61 | iv[4] = iv[12] = ((bearer << 1) | (direction & 1)) << 2; 62 | zuc_init(key, user_key, iv); 63 | } 64 | 65 | void zuc_eea_encrypt(const ZUC_UINT32 *in, ZUC_UINT32 *out, size_t nbits, 66 | const uint8_t key[16], ZUC_UINT32 count, ZUC_UINT5 bearer, 67 | ZUC_BIT direction) 68 | { 69 | ZUC_STATE zuc_key; 70 | size_t nwords = (nbits + 31)/32; 71 | size_t i; 72 | 73 | zuc_set_eea_key(&zuc_key, key, count, bearer, direction); 74 | zuc_generate_keystream(&zuc_key, nwords, out); 75 | for (i = 0; i < nwords; i++) { 76 | out[i] ^= in[i]; 77 | } 78 | 79 | if (nbits % 32 != 0) { 80 | out[nwords - 1] |= (0xffffffff << (32 - (nbits%32))); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /include/gmssl/chacha20.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | /* RFC 8439 "ChaCha20 and Poly1305 for IETF Protocols" */ 50 | 51 | #ifndef GMSSL_CHACHA20_H 52 | #define GMSSL_CHACHA20_H 53 | 54 | #define CHACHA20_IS_BIG_ENDIAN 0 55 | 56 | #include 57 | #include 58 | 59 | #include 60 | 61 | #define CHACHA20_KEY_BITS 256 62 | #define CHACHA20_NONCE_BITS 96 63 | #define CHACHA20_COUNTER_BITS 32 64 | 65 | #define CHACHA20_KEY_SIZE (CHACHA20_KEY_BITS/8) 66 | #define CHACHA20_NONCE_SIZE (CHACHA20_NONCE_BITS/8) 67 | #define CHACHA20_COUNTER_SIZE (CHACHA20_COUNTER_BITS/8) 68 | 69 | #define CHACHA20_KEY_WORDS (CHACHA20_KEY_SIZE/sizeof(uint32_t)) 70 | #define CHACHA20_NONCE_WORDS (CHACHA20_NONCE_SIZE/sizeof(uint32_t)) 71 | #define CHACHA20_COUNTER_WORDS (CHACHA20_COUNTER_SIZE/sizeof(uint32_t)) 72 | 73 | 74 | #ifdef __cplusplus 75 | extern "C" { 76 | #endif 77 | 78 | 79 | typedef struct { 80 | uint32_t d[16]; 81 | } CHACHA20_STATE; 82 | 83 | 84 | void chacha20_init(CHACHA20_STATE *state, 85 | const uint8_t key[CHACHA20_KEY_SIZE], 86 | const uint8_t nonce[CHACHA20_NONCE_SIZE], uint32_t counter); 87 | 88 | void chacha20_generate_keystream(CHACHA20_STATE *state, 89 | size_t counts, uint8_t *out); 90 | 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | #endif 96 | -------------------------------------------------------------------------------- /include/gmssl/error.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #ifndef GMSSL_ERROR_H 51 | #define GMSSL_ERROR_H 52 | 53 | 54 | #include 55 | #include 56 | 57 | 58 | #ifdef __cplusplus 59 | extern "C" { 60 | #endif 61 | 62 | 63 | #define DEBUG 1 64 | 65 | #define error_print() \ 66 | do { if (DEBUG) fprintf(stderr, "%s:%d:%s():\n",__FILE__, __LINE__, __func__); } while (0) 67 | 68 | #define error_print_msg(fmt, ...) \ 69 | do { if (DEBUG) fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__); } while (0) 70 | 71 | #define error_puts(str) \ 72 | do { if (DEBUG) fprintf(stderr, "%s: %d: %s: %s", __FILE__, __LINE__, __func__, str); } while (0) 73 | 74 | 75 | void print_der(const uint8_t *in, size_t inlen); 76 | void print_bytes(const uint8_t *in, size_t inlen); 77 | void print_nodes(const uint32_t *in, size_t inlen); 78 | 79 | #define FMT_CARRAY 0x80 80 | 81 | 82 | int format_print(FILE *fp, int format, int indent, const char *str, ...); 83 | int format_bytes(FILE *fp, int format, int indent, const char *str, const uint8_t *data, size_t datalen); 84 | int format_string(FILE *fp, int format, int indent, const char *str, const uint8_t *data, size_t datalen); 85 | 86 | 87 | 88 | //int tls_trace(int format, int indent, const char *str, ...); 89 | 90 | 91 | #ifdef __cplusplus 92 | } 93 | #endif 94 | #endif 95 | -------------------------------------------------------------------------------- /include/gmssl/des.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | /* FIPS PUB 46-3 "Data Encryption Standard (DES)" */ 50 | 51 | #ifndef GMSSL_DES_H 52 | #define GMSSL_DES_H 53 | 54 | 55 | #include 56 | #include 57 | 58 | 59 | #ifdef __cplusplus 60 | extern "C" { 61 | #endif 62 | 63 | 64 | #define DES_KEY_BITS 56 65 | #define DES_BLOCK_BITS 64 66 | #define DES_KEY_SIZE ((DES_KEY_BITS)/7) 67 | #define DES_BLOCK_SIZE (DES_BLOCK_BITS/8) 68 | 69 | #define DES_RK_BITS 48 70 | #define DES_RK_SIZE (DES_RK_BITS/8) 71 | #define DES_ROUNDS 16 72 | 73 | #define DES_EDE_KEY_SIZE (DES_KEY_SIZE * 3) 74 | 75 | typedef struct { 76 | uint64_t rk[DES_ROUNDS]; 77 | } DES_KEY; 78 | 79 | void des_set_encrypt_key(DES_KEY *key, const uint8_t raw_key[DES_KEY_SIZE]); 80 | void des_set_decrypt_key(DES_KEY *key, const uint8_t raw_key[DES_KEY_SIZE]); 81 | void des_encrypt(DES_KEY *key, const uint8_t in[DES_BLOCK_SIZE], uint8_t out[DES_BLOCK_SIZE]); 82 | 83 | 84 | typedef struct { 85 | DES_KEY K[3]; 86 | } DES_EDE_KEY; 87 | 88 | void des_ede_set_encrypt_key(DES_EDE_KEY *key, const uint8_t raw_key[DES_EDE_KEY_SIZE]); 89 | void des_ede_set_decrypt_key(DES_EDE_KEY *key, const uint8_t raw_key[DES_EDE_KEY_SIZE]); 90 | void des_ede_encrypt(DES_EDE_KEY *key, const uint8_t in[DES_BLOCK_SIZE], uint8_t out[DES_BLOCK_SIZE]); 91 | 92 | 93 | #ifdef __cplusplus 94 | } 95 | #endif 96 | #endif 97 | -------------------------------------------------------------------------------- /include/gmssl/sm3.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_SM3_H 50 | #define GMSSL_SM3_H 51 | 52 | #include 53 | #include 54 | 55 | #ifdef __cplusplus 56 | extern "C" { 57 | #endif 58 | 59 | 60 | #define SM3_IS_BIG_ENDIAN 1 61 | 62 | #define SM3_DIGEST_SIZE 32 63 | #define SM3_BLOCK_SIZE 64 64 | #define SM3_STATE_WORDS 8 65 | #define SM3_HMAC_SIZE (SM3_DIGEST_SIZE) 66 | 67 | 68 | typedef struct { 69 | uint32_t digest[SM3_STATE_WORDS]; 70 | uint64_t nblocks; 71 | uint8_t block[SM3_BLOCK_SIZE]; 72 | size_t num; 73 | } SM3_CTX; 74 | 75 | void sm3_init(SM3_CTX *ctx); 76 | void sm3_update(SM3_CTX *ctx, const uint8_t *data, size_t datalen); 77 | void sm3_finish(SM3_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]); 78 | void sm3_digest(const uint8_t *data, size_t datalen, uint8_t dgst[SM3_DIGEST_SIZE]); 79 | 80 | 81 | typedef struct { 82 | SM3_CTX sm3_ctx; 83 | unsigned char key[SM3_BLOCK_SIZE]; 84 | } SM3_HMAC_CTX; 85 | 86 | void sm3_hmac_init(SM3_HMAC_CTX *ctx, const uint8_t *key, size_t keylen); 87 | void sm3_hmac_update(SM3_HMAC_CTX *ctx, const uint8_t *data, size_t datalen); 88 | void sm3_hmac_finish(SM3_HMAC_CTX *ctx, uint8_t mac[SM3_HMAC_SIZE]); 89 | void sm3_hmac(const uint8_t *key, size_t keylen, 90 | const uint8_t *data, size_t datalen, 91 | uint8_t mac[SM3_HMAC_SIZE]); 92 | 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | #endif 98 | -------------------------------------------------------------------------------- /include/gmssl/gcm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_GCM_H 50 | #define GMSSL_GCM_H 51 | 52 | 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | 59 | 60 | #ifdef __cplusplus 61 | extern "C" { 62 | #endif 63 | 64 | #define GCM_IV_MIN_SIZE 1 65 | #define GCM_IV_MAX_SIZE ((uint64_t)(1 << (64-3))) 66 | #define GCM_IV_DEFAULT_BITS 96 67 | #define GCM_IV_DEFAULT_SIZE 12 68 | 69 | #define GCM_MIN_AAD_SIZE 0 70 | #define GCM_MAX_AAD_SIZE ((uint64_t)(1 << (64-3))) 71 | 72 | #define GCM_MIN_PLAINTEXT_SIZE 0 73 | #define GCM_MAX_PLAINTEXT_SIZE ((((uint64_t)1 << 39) - 256) >> 3) 74 | 75 | 76 | #define GHASH_SIZE (16) 77 | 78 | 79 | #define GCM_IS_LITTLE_ENDIAN 1 80 | 81 | 82 | void ghash(const uint8_t h[16], const uint8_t *aad, size_t aadlen, 83 | const uint8_t *c, size_t clen, uint8_t out[16]); 84 | 85 | int gcm_encrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *iv, size_t ivlen, 86 | const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen, 87 | uint8_t *out, size_t taglen, uint8_t *tag); 88 | 89 | int gcm_decrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *iv, size_t ivlen, 90 | const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen, 91 | const uint8_t *tag, size_t taglen, uint8_t *out); 92 | 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | #endif 98 | -------------------------------------------------------------------------------- /tests/gf128test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | /* 57 | a = de300f9301a499a965f8bf677e99e80d 58 | b = 14b267838ec9ef1bb7b5ce8c19e34bc6 59 | a + b = ca8268108f6d76b2d24d71eb677aa3cb 60 | a - b = ca8268108f6d76b2d24d71eb677aa3cb 61 | a * b = 28e63413cd53b01a3b469375781942c6 62 | a * 2 = bc601f2603493352cbf17ecefd33d09d 63 | */ 64 | 65 | int main(void) 66 | { 67 | gf128_t zero = gf128_from_hex("00000000000000000000000000000000"); 68 | gf128_t one = gf128_from_hex("00000000000000000000000000000001"); 69 | gf128_t ones = gf128_from_hex("11111111111111111111111111111111"); 70 | gf128_t a = gf128_from_hex("de300f9301a499a965f8bf677e99e80d"); 71 | gf128_t b = gf128_from_hex("14b267838ec9ef1bb7b5ce8c19e34bc6"); 72 | gf128_t r; 73 | 74 | /* 75 | r = gf128_add(a, b); 76 | gf128_print("a + b = ", r); 77 | 78 | r = gf128_mul(a, b); 79 | gf128_print("a * b = ", r); 80 | 81 | r = gf128_mul2(a); 82 | gf128_print("a * 2 = ", r); 83 | */ 84 | 85 | gf128_t H = gf128_from_hex("66e94bd4ef8a2c3b884cfa59ca342b2e"); 86 | gf128_t C = gf128_from_hex("0388dace60b6a392f328c2b971b2fe78"); 87 | gf128_t T = gf128_mul(C, H); 88 | 89 | 90 | gf128_print("C = ", C); 91 | gf128_print("H = ", H); 92 | gf128_print("C * H = ", T); 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /tools/reqparse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | 57 | 58 | int main(int argc, char **argv) 59 | { 60 | char *prog = argv[0]; 61 | char *infile = NULL; 62 | FILE *infp = stdin; 63 | uint8_t req[1024]; 64 | size_t reqlen; 65 | 66 | argc--; 67 | argv++; 68 | 69 | while (argc > 0) { 70 | if (!strcmp(*argv, "-help")) { 71 | help: 72 | fprintf(stderr, "usage: %s [-in file]\n", prog); 73 | return -1; 74 | 75 | } else if(!strcmp(*argv, "-in")) { 76 | if (--argc < 1) goto bad; 77 | infile = *(++argv); 78 | 79 | } else { 80 | fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv); 81 | goto help; 82 | } 83 | 84 | argc--; 85 | argv++; 86 | } 87 | 88 | if (infile) { 89 | if (!(infp = fopen(infile, "r"))) { 90 | error_print(); 91 | return -1; 92 | } 93 | } 94 | 95 | if (x509_req_from_pem(req, &reqlen, sizeof(req), infp) != 1) { 96 | error_print(); 97 | return -1; 98 | } 99 | x509_req_print(stdout, 0, 0, "CertificationRequest", req, reqlen); 100 | x509_req_to_pem(req, reqlen, stdout); 101 | return 0; 102 | 103 | bad: 104 | fprintf(stderr, "%s: '%s' option value required\n", prog, *argv); 105 | return -1; 106 | } 107 | -------------------------------------------------------------------------------- /tools/certparse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | static const char *options = "[-in file]"; 57 | 58 | int main(int argc, char **argv) 59 | { 60 | char *prog = argv[0]; 61 | char *infile = NULL; 62 | FILE *infp = stdin; 63 | 64 | uint8_t cert[18192]; 65 | size_t certlen; 66 | 67 | argc--; 68 | argv++; 69 | 70 | while (argc > 0) { 71 | if (!strcmp(*argv, "-help")) { 72 | printf("usage: %s %s\n", prog, options); 73 | return 0; 74 | } else if (!strcmp(*argv, "-in")) { 75 | if (--argc < 1) goto bad; 76 | infile = *(++argv); 77 | } else { 78 | bad: 79 | fprintf(stderr, "%s: llegal option '%s'\n", prog, *argv); 80 | printf("usage: %s %s\n", prog, options); 81 | return 1; 82 | } 83 | 84 | argc--; 85 | argv++; 86 | } 87 | 88 | if (infile) { 89 | if (!(infp = fopen(infile, "r"))) { 90 | error_print(); 91 | return -1; 92 | } 93 | } 94 | 95 | for (;;) { 96 | int ret; 97 | if ((ret = x509_cert_from_pem(cert, &certlen, sizeof(cert), infp)) < 0) { 98 | error_print(); 99 | return -1; 100 | } else if (!ret) { 101 | break; 102 | } 103 | x509_cert_print(stdout, 0, 0, "Certificate", cert, certlen); 104 | x509_cert_to_pem(cert, certlen, stdout); 105 | } 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /tests/chacha20test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | 55 | 56 | int main(void) 57 | { 58 | int err = 0; 59 | int i; 60 | const unsigned char key[] = { 61 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 62 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 63 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 64 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 65 | }; 66 | const unsigned char nonce[] = { 67 | 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 68 | 0x00, 0x00, 0x00, 0x00, 69 | }; 70 | uint32_t counter = 1; 71 | const unsigned char testdata[] = { 72 | 0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15, 73 | 0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71, 0xc4, 74 | 0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03, 75 | 0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e, 76 | 0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09, 77 | 0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2, 78 | 0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9, 79 | 0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e, 80 | }; 81 | unsigned char buf[64]; 82 | 83 | CHACHA20_STATE state; 84 | chacha20_init(&state, key, nonce, counter); 85 | chacha20_generate_keystream(&state, 1, buf); 86 | 87 | printf("chacha20 test "); 88 | if (memcmp(buf, testdata, sizeof(testdata)) != 0) { 89 | printf("failed\n"); 90 | err++; 91 | } else { 92 | printf("ok\n"); 93 | } 94 | 95 | return err; 96 | } 97 | 98 | -------------------------------------------------------------------------------- /include/gmssl/ec.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_EC_H 50 | #define GMSSL_EC_H 51 | 52 | 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | 61 | #ifdef __cplusplus 62 | extern "C" { 63 | #endif 64 | 65 | /* 66 | NamedCurve: 67 | OID_sm2 68 | OID_prime192v1 69 | OID_prime256v1 70 | OID_secp256k1 71 | OID_secp384r1 72 | OID_secp521r1 73 | */ 74 | const char *ec_named_curve_name(int curve); 75 | int ec_named_curve_from_name(const char *name); 76 | int ec_named_curve_to_der(int curve, uint8_t **out, size_t *outlen); 77 | int ec_named_curve_from_der(int *curve, const uint8_t **in, size_t *inlen); 78 | 79 | /* 80 | ECPoint ::= OCTET STRING -- uncompressed point 81 | */ 82 | int ec_point_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen); 83 | 84 | /* 85 | ECPrivateKey ::= SEQUENCE { 86 | version INTEGER, -- value MUST be (1) 87 | privateKey OCTET STRING, -- big endian encoding of integer 88 | parameters [0] EXPLICIT OBJECT IDENTIFIER OPTIONAL, -- namedCurve 89 | publicKey [1] EXPLICIT BIT STRING OPTIONAL -- ECPoint 90 | } 91 | */ 92 | 93 | enum { 94 | EC_private_key_version = 1, 95 | }; 96 | 97 | int ec_private_key_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen); 98 | 99 | #ifdef __cplusplus 100 | } 101 | #endif 102 | #endif 103 | -------------------------------------------------------------------------------- /src/rc4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | void rc4_init(RC4_STATE *state, const uint8_t *key, size_t keylen) 55 | { 56 | int i, j; 57 | uint8_t *s = state->d; 58 | uint8_t k[256]; 59 | uint8_t temp; 60 | 61 | /* expand key */ 62 | for (i = 0; i < keylen; i++) { 63 | k[i] = key[i]; 64 | } 65 | for (i = keylen; i < 256; i++) { 66 | k[i] = key[i % keylen]; 67 | } 68 | 69 | /* init state */ 70 | for (i = 0; i < 256; i++) { 71 | s[i] = i; 72 | } 73 | 74 | /* shuffle state with key */ 75 | j = 0; 76 | for (i = 0; i < 256; i++) { 77 | j = (j + s[i] + k[i]) % 256; 78 | 79 | /* swap(s[i], s[j]) */ 80 | temp = s[j]; 81 | s[j] = s[i]; 82 | s[i] = temp; 83 | } 84 | 85 | /* clean expanded temp key */ 86 | memset(k, 0, sizeof(k)); 87 | } 88 | 89 | void rc4_generate_keystream(RC4_STATE *state, size_t outlen, uint8_t *out) 90 | { 91 | int i = 0, j = 0; 92 | uint8_t *s = state->d; 93 | int oi; 94 | int temp; 95 | 96 | while (outlen > 0) { 97 | i = (i + 1) % 256; 98 | j = (j + s[i]) % 256; 99 | 100 | /* swap(s[i], s[j]) */ 101 | temp = s[j]; 102 | s[j] = s[i]; 103 | s[i] = temp; 104 | 105 | oi = (s[i] + s[j]) % 256; 106 | *out++ = s[oi]; 107 | 108 | outlen--; 109 | } 110 | } 111 | 112 | uint8_t rc4_generate_keybyte(RC4_STATE *state) 113 | { 114 | uint8_t out[1]; 115 | rc4_generate_keystream(state, 1, out); 116 | return out[0]; 117 | } 118 | -------------------------------------------------------------------------------- /include/gmssl/base64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #ifndef GMSSL_BASE64_H 51 | #define GMSSL_BASE64_H 52 | 53 | #include 54 | #include 55 | 56 | #ifdef __cplusplus 57 | extern "C" { 58 | #endif 59 | 60 | typedef struct { 61 | /* number saved in a partial encode/decode */ 62 | int num; 63 | /* 64 | * The length is either the output line length (in input bytes) or the 65 | * shortest input line length that is ok. Once decoding begins, the 66 | * length is adjusted up each time a longer line is decoded 67 | */ 68 | int length; 69 | /* data to encode */ 70 | unsigned char enc_data[80]; 71 | /* number read on current line */ 72 | int line_num; 73 | int expect_nl; 74 | } BASE64_CTX; 75 | 76 | # define BASE64_ENCODE_LENGTH(l) (((l+2)/3*4)+(l/48+1)*2+80) 77 | # define BASE64_DECODE_LENGTH(l) ((l+3)/4*3+80) 78 | 79 | 80 | void base64_encode_init(BASE64_CTX *ctx); 81 | int base64_encode_update(BASE64_CTX *ctx, const uint8_t *in, int inlen, uint8_t *out, int *outlen); 82 | void base64_encode_finish(BASE64_CTX *ctx, uint8_t *out, int *outlen); 83 | 84 | void base64_decode_init(BASE64_CTX *ctx); 85 | int base64_decode_update(BASE64_CTX *ctx, const uint8_t *in, int inlen, uint8_t *out, int *outlen); 86 | int base64_decode_finish(BASE64_CTX *ctx, uint8_t *out, int *outlen); 87 | 88 | 89 | int base64_encode_block(unsigned char *t, const unsigned char *f, int dlen); 90 | int base64_decode_block(unsigned char *t, const unsigned char *f, int n); 91 | 92 | 93 | #ifdef __cplusplus 94 | } 95 | #endif 96 | #endif 97 | -------------------------------------------------------------------------------- /tests/sha1test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | 57 | static char *teststr[] = { 58 | "abc", 59 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 60 | "a", 61 | "0123456701234567012345670123456701234567012345670123456701234567", 62 | }; 63 | 64 | static size_t testcnt[] = { 65 | 1, 66 | 1, 67 | 1000000, 68 | 10, 69 | }; 70 | 71 | static char *dgsthex[] = { 72 | "A9993E364706816ABA3E25717850C26C9CD0D89D", 73 | "84983E441C3BD26EBAAE4AA1F95129E5E54670F1", 74 | "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F", 75 | "DEA356A2CDDD90C7A7ECEDC5EBB563934F460452", 76 | }; 77 | 78 | int main(void) 79 | { 80 | int err = 0; 81 | SHA1_CTX ctx; 82 | uint8_t dgst[20]; 83 | uint8_t dgstbuf[20]; 84 | size_t dgstlen; 85 | size_t i, j; 86 | 87 | for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) { 88 | hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstlen); 89 | 90 | sha1_init(&ctx); 91 | for (j = 0; j < testcnt[i]; j++) { 92 | sha1_update(&ctx, (uint8_t *)teststr[i], strlen(teststr[i])); 93 | } 94 | sha1_finish(&ctx, dgst); 95 | 96 | if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) { 97 | printf("sha1 test %lu failed\n", i+1); 98 | printf("%s\n", dgsthex[i]); 99 | for (j = 0; j < sizeof(dgst); j++) { 100 | printf("%02X", dgst[j]); 101 | } 102 | printf("\n"); 103 | err++; 104 | } else { 105 | printf("sha1 test %lu ok\n", i+1); 106 | } 107 | } 108 | 109 | return err; 110 | } 111 | -------------------------------------------------------------------------------- /tests/base64test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | int test_base64(void) 57 | { 58 | int err = 0; 59 | 60 | uint8_t bin1[50]; 61 | uint8_t bin2[100]; 62 | uint8_t bin3[200]; 63 | uint8_t buf1[8000] = {0}; 64 | uint8_t buf2[8000] = {0}; 65 | 66 | BASE64_CTX ctx; 67 | uint8_t *p; 68 | int len; 69 | 70 | memset(bin1, 0x01, sizeof(bin1)); 71 | memset(bin2, 0xA5, sizeof(bin2)); 72 | memset(bin3, 0xff, sizeof(bin3)); 73 | 74 | 75 | p = buf1; 76 | base64_encode_init(&ctx); 77 | base64_encode_update(&ctx, bin1, sizeof(bin1), p, &len); p += len; 78 | base64_encode_update(&ctx, bin2, sizeof(bin2), p, &len); p += len; 79 | base64_encode_update(&ctx, bin3, sizeof(bin3), p, &len); p += len; 80 | base64_encode_finish(&ctx, p, &len); p += len; 81 | len = (int)(p - buf1); 82 | 83 | p = buf2; 84 | base64_decode_init(&ctx); 85 | base64_decode_update(&ctx, buf1, len, p, &len); p += len; 86 | base64_decode_finish(&ctx, p, &len); p += len; 87 | len = (int)(p - buf2); 88 | 89 | printf("base64 test "); 90 | if (len != sizeof(bin1) + sizeof(bin2) + sizeof(bin3) 91 | || memcmp(buf2, bin1, sizeof(bin1)) != 0 92 | || memcmp(buf2 + sizeof(bin1), bin2, sizeof(bin2)) != 0 93 | || memcmp(buf2 + sizeof(bin1) + sizeof(bin2), bin3, sizeof(bin3)) != 0) { 94 | printf("failed\n"); 95 | err++; 96 | } else { 97 | printf("ok\n"); 98 | } 99 | 100 | return err; 101 | } 102 | 103 | int main(void) 104 | { 105 | int err = 0; 106 | err += test_base64(); 107 | return err; 108 | } 109 | -------------------------------------------------------------------------------- /tests/md5test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | 57 | 58 | static char *teststr[] = { 59 | "", 60 | "a", 61 | "abc", 62 | "message digest", 63 | "abcdefghijklmnopqrstuvwxyz", 64 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 65 | "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 66 | }; 67 | 68 | static char *dgsthex[] = { 69 | "d41d8cd98f00b204e9800998ecf8427e", 70 | "0cc175b9c0f1b6a831c399e269772661", 71 | "900150983cd24fb0d6963f7d28e17f72", 72 | "f96b697d7cb7938d525a2f31aaf161d0", 73 | "c3fcd3d76192e4007dfb496cca67e13b", 74 | "d174ab98d277d9f5a5611c2c9f419d9f", 75 | "57edf4a22be3c955ac49da2e2107b67a", 76 | }; 77 | 78 | int main(int argc, char **argv) 79 | { 80 | int err = 0; 81 | char *p; 82 | uint8_t dgst[16]; 83 | uint8_t dgstbuf[16]; 84 | size_t dgstbuflen; 85 | size_t i; 86 | 87 | for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) { 88 | hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen); 89 | md5_digest((uint8_t *)teststr[i], strlen(teststr[i]), dgst); 90 | 91 | if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) { 92 | int n; 93 | printf("error calculating MD5 on %s\n", teststr[i]); 94 | printf(" digest(corret) = "); 95 | for (n = 0; n < sizeof(dgst); n++) { 96 | printf("%02X", dgst[n]); 97 | } 98 | printf("\n"); 99 | printf(" digest(error) = %s\n", dgsthex[i]); 100 | err++; 101 | } else { 102 | printf("md5 test %lu ok\n", i+1); 103 | } 104 | } 105 | 106 | return err; 107 | } 108 | -------------------------------------------------------------------------------- /src/sm9_sign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | int sm9_sign_setup(SM9_SIGN_MASTER_KEY *msk) 50 | { 51 | 52 | // rand ks in [1, N-1] 53 | fn_rand(ks); 54 | 55 | // Ppubs = ks * P2 56 | twist_point_mul_generator(Ppubs, ks); 57 | } 58 | 59 | 60 | 61 | int sm9_sign_keygen(SM9_SIGN_MASTER_KEY *msk, const char *id, size_t idlen, SM9_POINT *ds) 62 | { 63 | 64 | } 65 | 66 | 67 | int sm9_sign_init(SM3_CTX *ctx) 68 | { 69 | uint8_t prefix[1] = {0x02}; 70 | if (!ctx) { 71 | return -1; 72 | } 73 | 74 | sm3_init(ctx); 75 | sm3_update(ctx, prefix, sizeof(prefix)); 76 | return 0; 77 | } 78 | 79 | int sm9_sign_update(SM3_CTX *ctx, const uint8_t *data, size_t datalen) 80 | { 81 | sm3_update(ctx, data, datalen); 82 | return 1; 83 | } 84 | 85 | int sm9_sign_finish(SM3_CTX *ctx, SM9_SIGNATURE *sig) 86 | { 87 | 88 | fp12_t g; 89 | 90 | sm9_pairing(g, SM9_P1, Ppubs); 91 | 92 | fn_rand(r); 93 | 94 | fp12_pow(w, g, r); 95 | 96 | 97 | fn_sub(l, r, h); 98 | if (fn_is_zero(l)) { 99 | } 100 | 101 | 102 | point_mul(S, l, ds); 103 | 104 | } 105 | 106 | int sm9_verify_init(SM9_SIGN_CTX *ctx) 107 | { 108 | sm3_init(&ctx->sm3_ctx); 109 | sm3_update(&ctx->sm3_ctx, SM9_HASH1_PREFIX, sizeof(SM9_HASH1_PREFIX)); 110 | return 0; 111 | } 112 | 113 | int sm9_verify_update(SM9_SIGN_CTX *ctx, const uint8_t *data, size_t datalen) 114 | { 115 | sm3_update(&ctx->sm3_ctx, data, datalen); 116 | } 117 | 118 | int sm9_verify_finish(SM9_SIGN_CTX *ctx, const char *id, size_t idlen, const SM9_SIGNATURE *sig) 119 | { 120 | 121 | if (bn_is_zero(h) || bn_cmp(h, SM9_N) >= 0) { 122 | } 123 | 124 | if (!point_is_on_curve(S)) { 125 | } 126 | 127 | sm9_pairing(g, SM9_P1, Ppubs); 128 | 129 | fp12_pow(t, g, h); 130 | 131 | 132 | sm9_hash1(h1, id, idlen); 133 | 134 | twist_point_mul_generator(P, h1); 135 | twist_point_add(P, P, Ppubs); 136 | pairing(u, S, P); 137 | } 138 | 139 | 140 | -------------------------------------------------------------------------------- /src/sm4_lcl.h: -------------------------------------------------------------------------------- 1 | /* ==================================================================== 2 | * Copyright (c) 2014 - 2019 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | * ==================================================================== 48 | */ 49 | 50 | #ifndef GMSSL_SM4_LCL_H 51 | #define GMSSL_SM4_LCL_H 52 | 53 | #include 54 | 55 | extern const uint8_t SM4_S[256]; 56 | extern const uint32_t SM4_T[256]; 57 | extern const uint32_t SM4_D[65536]; 58 | 59 | #define S32(A) \ 60 | ((SM4_S[((A) >> 24) ] << 24) ^ \ 61 | (SM4_S[((A) >> 16) & 0xff] << 16) ^ \ 62 | (SM4_S[((A) >> 8) & 0xff] << 8) ^ \ 63 | (SM4_S[((A)) & 0xff])) 64 | 65 | #define ROUNDS(x0, x1, x2, x3, x4) \ 66 | ROUND(x0, x1, x2, x3, x4, 0); \ 67 | ROUND(x1, x2, x3, x4, x0, 1); \ 68 | ROUND(x2, x3, x4, x0, x1, 2); \ 69 | ROUND(x3, x4, x0, x1, x2, 3); \ 70 | ROUND(x4, x0, x1, x2, x3, 4); \ 71 | ROUND(x0, x1, x2, x3, x4, 5); \ 72 | ROUND(x1, x2, x3, x4, x0, 6); \ 73 | ROUND(x2, x3, x4, x0, x1, 7); \ 74 | ROUND(x3, x4, x0, x1, x2, 8); \ 75 | ROUND(x4, x0, x1, x2, x3, 9); \ 76 | ROUND(x0, x1, x2, x3, x4, 10); \ 77 | ROUND(x1, x2, x3, x4, x0, 11); \ 78 | ROUND(x2, x3, x4, x0, x1, 12); \ 79 | ROUND(x3, x4, x0, x1, x2, 13); \ 80 | ROUND(x4, x0, x1, x2, x3, 14); \ 81 | ROUND(x0, x1, x2, x3, x4, 15); \ 82 | ROUND(x1, x2, x3, x4, x0, 16); \ 83 | ROUND(x2, x3, x4, x0, x1, 17); \ 84 | ROUND(x3, x4, x0, x1, x2, 18); \ 85 | ROUND(x4, x0, x1, x2, x3, 19); \ 86 | ROUND(x0, x1, x2, x3, x4, 20); \ 87 | ROUND(x1, x2, x3, x4, x0, 21); \ 88 | ROUND(x2, x3, x4, x0, x1, 22); \ 89 | ROUND(x3, x4, x0, x1, x2, 23); \ 90 | ROUND(x4, x0, x1, x2, x3, 24); \ 91 | ROUND(x0, x1, x2, x3, x4, 25); \ 92 | ROUND(x1, x2, x3, x4, x0, 26); \ 93 | ROUND(x2, x3, x4, x0, x1, 27); \ 94 | ROUND(x3, x4, x0, x1, x2, 28); \ 95 | ROUND(x4, x0, x1, x2, x3, 29); \ 96 | ROUND(x0, x1, x2, x3, x4, 30); \ 97 | ROUND(x1, x2, x3, x4, x0, 31) 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /src/pem.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | 55 | 56 | int pem_write(FILE *fp, const char *name, const uint8_t *data, size_t datalen) 57 | { 58 | int ret = 0; 59 | BASE64_CTX ctx; 60 | uint8_t b64[datalen * 2]; 61 | int len; 62 | 63 | base64_encode_init(&ctx); 64 | base64_encode_update(&ctx, data, (int)datalen, b64, &len); 65 | base64_encode_finish(&ctx, b64 + len, &len); 66 | 67 | ret += fprintf(fp, "-----BEGIN %s-----\n", name); 68 | ret += fprintf(fp, "%s", (char *)b64); 69 | ret += fprintf(fp, "-----END %s-----\n", name); 70 | //return ret; 71 | return 1; 72 | } 73 | 74 | int pem_read(FILE *fp, const char *name, uint8_t *data, size_t *datalen, size_t maxlen) 75 | { 76 | char line[80]; 77 | char begin_line[80]; 78 | char end_line[80]; 79 | int len; 80 | BASE64_CTX ctx; 81 | 82 | snprintf(begin_line, sizeof(begin_line), "-----BEGIN %s-----\n", name); 83 | snprintf(end_line, sizeof(end_line), "-----END %s-----\n", name); 84 | 85 | if (feof(fp)) { 86 | return 0; 87 | } 88 | 89 | if (!fgets(line, sizeof(line), fp)) { 90 | if (feof(fp)) 91 | return 0; 92 | else { 93 | error_print(); 94 | return -1; 95 | } 96 | } 97 | 98 | if (strcmp(line, begin_line) != 0) { 99 | // FIXME: 这里是不是应该容忍一些错误呢? 100 | error_print(); 101 | return -1; 102 | } 103 | 104 | *datalen = 0; 105 | 106 | base64_decode_init(&ctx); 107 | 108 | for (;;) { 109 | if (!fgets(line, sizeof(line), fp)) { 110 | error_print(); 111 | return -1; 112 | } 113 | if (strcmp(line, end_line) == 0) { 114 | break; 115 | } 116 | 117 | base64_decode_update(&ctx, (uint8_t *)line, strlen(line), data, &len); 118 | data += len; 119 | *datalen += len; 120 | } 121 | 122 | base64_decode_finish(&ctx, data, &len); 123 | *datalen += len; 124 | return 1; 125 | } 126 | -------------------------------------------------------------------------------- /src/endian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_ENDIAN_H 50 | #define GMSSL_ENDIAN_H 51 | 52 | 53 | /* Big Endian R/W */ 54 | 55 | #define GETU16(p) \ 56 | ((uint16_t)(p)[0] << 8 | \ 57 | (uint16_t)(p)[1]) 58 | 59 | #define GETU32(p) \ 60 | ((uint32_t)(p)[0] << 24 | \ 61 | (uint32_t)(p)[1] << 16 | \ 62 | (uint32_t)(p)[2] << 8 | \ 63 | (uint32_t)(p)[3]) 64 | 65 | #define GETU64(p) \ 66 | ((uint64_t)(p)[0] << 56 | \ 67 | (uint64_t)(p)[1] << 48 | \ 68 | (uint64_t)(p)[2] << 40 | \ 69 | (uint64_t)(p)[3] << 32 | \ 70 | (uint64_t)(p)[4] << 24 | \ 71 | (uint64_t)(p)[5] << 16 | \ 72 | (uint64_t)(p)[6] << 8 | \ 73 | (uint64_t)(p)[7]) 74 | 75 | 76 | // 注意:PUTU32(buf, val++) 会出错! 77 | #define PUTU16(p,V) \ 78 | ((p)[0] = (uint8_t)((V) >> 8), \ 79 | (p)[1] = (uint8_t)(V)) 80 | 81 | #define PUTU32(p,V) \ 82 | ((p)[0] = (uint8_t)((V) >> 24), \ 83 | (p)[1] = (uint8_t)((V) >> 16), \ 84 | (p)[2] = (uint8_t)((V) >> 8), \ 85 | (p)[3] = (uint8_t)(V)) 86 | 87 | #define PUTU64(p,V) \ 88 | ((p)[0] = (uint8_t)((V) >> 56), \ 89 | (p)[1] = (uint8_t)((V) >> 48), \ 90 | (p)[2] = (uint8_t)((V) >> 40), \ 91 | (p)[3] = (uint8_t)((V) >> 32), \ 92 | (p)[4] = (uint8_t)((V) >> 24), \ 93 | (p)[5] = (uint8_t)((V) >> 16), \ 94 | (p)[6] = (uint8_t)((V) >> 8), \ 95 | (p)[7] = (uint8_t)(V)) 96 | 97 | /* Little Endian R/W */ 98 | 99 | #define GETU16_LE(p) (*(const uint16_t *)(p)) 100 | #define GETU32_LE(p) (*(const uint32_t *)(p)) 101 | #define GETU64_LE(p) (*(const uint64_t *)(p)) 102 | 103 | #define PUTU16_LE(p,V) *(uint16_t *)(p) = (V) 104 | #define PUTU32_LE(p,V) *(uint32_t *)(p) = (V) 105 | #define PUTU64_LE(p,V) *(uint64_t *)(p) = (V) 106 | 107 | /* Rotate */ 108 | 109 | #define ROL32(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) 110 | #define ROL64(a,n) (((a)<<(n))|((a)>>(64-(n)))) 111 | 112 | #define ROR32(a,n) ROL32((a),32-(n)) 113 | #define ROR64(a,n) ROL64(a,64-n) 114 | 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore editor artefacts 2 | /.dir-locals.el 3 | 4 | # Top level excludes 5 | /Makefile.orig 6 | /MINFO 7 | /TABLE 8 | /*.a 9 | /*.pc 10 | /rehash.time 11 | /inc.* 12 | /makefile.* 13 | /out.* 14 | /tmp.* 15 | /configdata.pm 16 | /build 17 | /Makefile 18 | 19 | # *all* Makefiles 20 | #Makefile 21 | *.tmp 22 | 23 | # Java 24 | /java/*.class 25 | /java/Makefile* 26 | 27 | # Links under apps 28 | /apps/CA.pl 29 | /apps/tsget 30 | /apps/tsget.pl 31 | /apps/md4.c 32 | 33 | # Auto generated headers 34 | /crypto/buildinf.h 35 | /crypto/include/internal/*_conf.h 36 | /openssl/include/opensslconf.h 37 | /util/domd 38 | 39 | # Executables 40 | /apps/openssl 41 | /test/sha256t 42 | /test/sha512t 43 | /test/gost2814789t 44 | /test/ssltest_old 45 | /test/*test 46 | /test/fips_aesavs 47 | /test/fips_desmovs 48 | /test/fips_dhvs 49 | /test/fips_drbgvs 50 | /test/fips_dssvs 51 | /test/fips_ecdhvs 52 | /test/fips_ecdsavs 53 | /test/fips_rngvs 54 | /test/fips_test_suite 55 | /test/ssltest_old 56 | /test/x509aux 57 | /test/v3ext 58 | 59 | # Certain files that get created by tests on the fly 60 | /test/*.ss 61 | /test/*.srl 62 | /test/.rnd 63 | /test/test*.pem 64 | /test/newkey.pem 65 | /test/*.log 66 | /test/buildtest_* 67 | /test/*.p 68 | /test/*.dd 69 | /test/*.dp 70 | /test/*.pd 71 | /test/*.p 72 | 73 | /util/shlib_wrap.sh 74 | 75 | # Fuzz stuff. 76 | # Anything without an extension is an executable on Unix, so we keep files 77 | # with extensions. And we keep the corpora subddir versioned as well. 78 | # Anything more generic with extensions that should be ignored will be taken 79 | # care of by general ignores for those extensions (*.o, *.obj, *.exe, ...) 80 | /fuzz/* 81 | !/fuzz/README* 82 | !/fuzz/corpora 83 | !/fuzz/*.* 84 | 85 | # Misc auto generated files 86 | /include/openssl/opensslconf.h 87 | /tools/c_rehash 88 | /tools/c_rehash.pl 89 | /tags 90 | /TAGS 91 | /crypto.map 92 | /ssl.map 93 | 94 | # Windows (legacy) 95 | /tmp32 96 | /tmp32.dbg 97 | /tmp32dll 98 | /tmp32dll.dbg 99 | /out32 100 | /out32.dbg 101 | /out32dll 102 | /out32dll.dbg 103 | /inc32 104 | /MINFO 105 | /ms/.rnd 106 | /ms/bcb.mak 107 | /ms/libeay32.def 108 | /ms/nt.mak 109 | /ms/ntdll.mak 110 | /ms/ssleay32.def 111 | /ms/version32.rc 112 | 113 | # Files created on other branches that are not held in git, and are not 114 | # needed on this branch 115 | /include/openssl/asn1_mac.h 116 | /include/openssl/des_old.h 117 | /include/openssl/fips.h 118 | /include/openssl/fips_rand.h 119 | /include/openssl/krb5_asn.h 120 | /include/openssl/kssl.h 121 | /include/openssl/pq_compat.h 122 | /include/openssl/ssl23.h 123 | /include/openssl/tmdiff.h 124 | /include/openssl/ui_compat.h 125 | /test/fips_aesavs.c 126 | /test/fips_desmovs.c 127 | /test/fips_dsatest.c 128 | /test/fips_dssvs.c 129 | /test/fips_hmactest.c 130 | /test/fips_randtest.c 131 | /test/fips_rngvs.c 132 | /test/fips_rsagtest.c 133 | /test/fips_rsastest.c 134 | /test/fips_rsavtest.c 135 | /test/fips_shatest.c 136 | /test/fips_test_suite.c 137 | /test/shatest.c 138 | 139 | ##### Generic patterns 140 | # Auto generated assembly language source files 141 | *.s 142 | !/crypto/*/asm/*.s 143 | /crypto/arm*.S 144 | /crypto/*/*.S 145 | *.asm 146 | !/crypto/*/asm/*.asm 147 | 148 | # Object files 149 | *.o 150 | *.obj 151 | 152 | # editor artefacts 153 | *.swp 154 | .#* 155 | \#*# 156 | *~ 157 | 158 | # Certificate symbolic links 159 | *.0 160 | 161 | # All kinds of executables 162 | *.so 163 | *.so.* 164 | *.dylib 165 | *.dylib.* 166 | *.dll 167 | *.dll.* 168 | *.exe 169 | *.pyc 170 | *.exp 171 | *.lib 172 | *.pdb 173 | *.ilk 174 | *.def 175 | *.rc 176 | *.res 177 | 178 | # Misc generated stuff 179 | Makefile.save 180 | /crypto/**/lib 181 | /engines/**/lib 182 | /ssl/**/lib 183 | *.bak 184 | cscope.* 185 | *.d 186 | 187 | # macOS 188 | .DS_Store 189 | *.tar.gz 190 | 191 | # add by LiTianjue for GmSSL 192 | # auto create by Configure 193 | crypto/opensslconf.h 194 | tool/c_rehash 195 | # exec file 196 | apps/gmssl 197 | apps/gmca/.ca 198 | 199 | # gmtls 200 | /ssl/ssl_load.c 201 | 202 | # demos 203 | /demos 204 | /demos/kdf 205 | /demos/ssl 206 | /demos/otp 207 | /demos/sm9 208 | 209 | # engines 210 | /engines/e_skf* 211 | /engines/e_sdf* 212 | /engines/sdf 213 | /engines/skf 214 | 215 | include/openssl/srp.h 216 | 217 | /*.sh 218 | 219 | /rust 220 | /python 221 | /build 222 | -------------------------------------------------------------------------------- /include/gmssl/digest.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #ifndef GMSSL_DIGEST_H 51 | #define GMSSL_DIGEST_H 52 | 53 | 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | 61 | 62 | #ifdef __cplusplus 63 | extern "C" { 64 | #endif 65 | 66 | 67 | typedef struct DIGEST DIGEST; 68 | typedef struct DIGEST_CTX DIGEST_CTX; 69 | 70 | 71 | #define DIGEST_MAX_SIZE 64 72 | #define DIGEST_MAX_BLOCK_SIZE (1024/8) 73 | 74 | 75 | struct DIGEST_CTX { 76 | union { 77 | SM3_CTX sm3_ctx; 78 | MD5_CTX md5_ctx; 79 | SHA1_CTX sha1_ctx; 80 | SHA224_CTX sha224_ctx; 81 | SHA256_CTX sha256_ctx; 82 | SHA384_CTX sha384_ctx; 83 | SHA512_CTX sha512_ctx; 84 | } u; 85 | const DIGEST *digest; 86 | }; 87 | 88 | struct DIGEST { 89 | int oid; 90 | size_t digest_size; 91 | size_t block_size; 92 | size_t ctx_size; 93 | int (*init)(DIGEST_CTX *ctx); 94 | int (*update)(DIGEST_CTX *ctx, const uint8_t *data, size_t datalen); 95 | int (*finish)(DIGEST_CTX *ctx, uint8_t *dgst); 96 | }; 97 | 98 | const DIGEST *DIGEST_sm3(void); 99 | const DIGEST *DIGEST_md5(void); 100 | const DIGEST *DIGEST_sha1(void); 101 | const DIGEST *DIGEST_sha224(void); 102 | const DIGEST *DIGEST_sha256(void); 103 | const DIGEST *DIGEST_sha384(void); 104 | const DIGEST *DIGEST_sha512(void); 105 | const DIGEST *DIGEST_sha512_224(void); 106 | const DIGEST *DIGEST_sha512_256(void); 107 | 108 | const DIGEST *digest_from_name(const char *name); 109 | const char *digest_name(const DIGEST *digest); 110 | int digest_init(DIGEST_CTX *ctx, const DIGEST *algor); 111 | int digest_update(DIGEST_CTX *ctx, const uint8_t *data, size_t datalen); 112 | int digest_finish(DIGEST_CTX *ctx, uint8_t *dgst, size_t *dgstlen); 113 | int digest(const DIGEST *digest, const uint8_t *data, size_t datalen, uint8_t *dgst, size_t *dgstlen); 114 | 115 | 116 | #ifdef __cplusplus 117 | } 118 | #endif 119 | #endif 120 | -------------------------------------------------------------------------------- /tools/sm3hmac.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | 57 | int main(int argc, char **argv) 58 | { 59 | int ret = -1; 60 | char *prog = argv[0]; 61 | char *keyhex = NULL; 62 | char *infile = NULL; 63 | uint8_t key[32]; 64 | size_t keylen; 65 | FILE *in = stdin; 66 | SM3_HMAC_CTX ctx; 67 | uint8_t dgst[32]; 68 | uint8_t buf[4096]; 69 | size_t len; 70 | size_t i; 71 | 72 | argc--; 73 | argv++; 74 | 75 | while (argc > 0) { 76 | if (!strcmp(*argv, "-help")) { 77 | help: 78 | fprintf(stderr, "usage: %s -keyhex hex [-in file]\n", prog); 79 | return -1; 80 | 81 | } else if (!strcmp(*argv, "-keyhex")) { 82 | if (--argc < 1) goto bad; 83 | keyhex = *(++argv); 84 | 85 | } else if (!strcmp(*argv, "-in")) { 86 | if (--argc < 1) goto bad; 87 | infile = *(++argv); 88 | 89 | } else { 90 | fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv); 91 | goto help; 92 | } 93 | 94 | argc--; 95 | argv++; 96 | } 97 | 98 | if (!keyhex) { 99 | fprintf(stderr, "%s: option '-keyhex' required\n", prog); 100 | goto help; 101 | } 102 | if (strlen(keyhex) > sizeof(key) * 2) { 103 | error_print(); 104 | return -1; 105 | } 106 | if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) { 107 | error_print(); 108 | return -1; 109 | } 110 | 111 | sm3_hmac_init(&ctx, key, keylen); 112 | 113 | while ((len = fread(buf, 1, sizeof(buf), stdin)) > 0) { 114 | sm3_hmac_update(&ctx, buf, len); 115 | } 116 | sm3_hmac_finish(&ctx, dgst); 117 | 118 | for (i = 0; i < sizeof(dgst); i++) { 119 | printf("%02x", dgst[i]); 120 | } 121 | if (infile) { 122 | printf(" : %s", infile); 123 | } 124 | printf("\n"); 125 | 126 | memset(&ctx, 0, sizeof(ctx)); 127 | memset(key, 0, sizeof(key)); 128 | return 0; 129 | 130 | bad: 131 | fprintf(stderr, "%s: '%s' option value required\n", prog, *argv); 132 | return -1; 133 | } 134 | -------------------------------------------------------------------------------- /tests/x509_strtest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | 58 | static int test_x509_directory_name(void) 59 | { 60 | uint8_t str[] = { 'a', 'b', 'c', 0 }; 61 | uint8_t buf[256]; 62 | uint8_t *p = buf; 63 | const uint8_t *cp = buf; 64 | size_t len = 0; 65 | int tag; 66 | const uint8_t *d; 67 | size_t dlen; 68 | 69 | if (x509_directory_name_check_ex(ASN1_TAG_UTF8String, str, 3, 1, 10) != 1 // str,4 will fail 70 | || x509_directory_name_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1 71 | || x509_directory_name_from_der(&tag, &d, &dlen, &cp, &len) != 1 72 | || asn1_check(tag == ASN1_TAG_UTF8String) != 1 73 | || asn1_check(dlen == 3) != 1 74 | || asn1_check(memcmp(str, d, dlen) == 0) != 1 75 | || asn1_length_is_zero(len) != 1) { 76 | error_print(); 77 | return 1; 78 | } 79 | printf("%s() ok\n", __FUNCTION__); 80 | return 0; 81 | } 82 | 83 | static int test_x509_display_text(void) 84 | { 85 | uint8_t str[] = { 'a', 'b', 'c', 0 }; 86 | uint8_t buf[256]; 87 | uint8_t *p = buf; 88 | const uint8_t *cp = buf; 89 | size_t len = 0; 90 | int tag; 91 | const uint8_t *d; 92 | size_t dlen; 93 | 94 | if (x509_display_text_check(ASN1_TAG_UTF8String, str, 3) != 1 // str,4 will fail 95 | || x509_display_text_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1 96 | || x509_display_text_from_der(&tag, &d, &dlen, &cp, &len) != 1 97 | || asn1_check(tag == ASN1_TAG_UTF8String) != 1 98 | || asn1_check(dlen == 3) != 1 99 | || asn1_check(memcmp(str, d, dlen) == 0) != 1 100 | || asn1_length_is_zero(len) != 1) { 101 | error_print(); 102 | return 1; 103 | } 104 | printf("%s() ok\n", __FUNCTION__); 105 | return 0; 106 | } 107 | 108 | int main(void) 109 | { 110 | int err = 0; 111 | err += test_x509_directory_name(); 112 | err += test_x509_display_text(); 113 | return err; 114 | } 115 | -------------------------------------------------------------------------------- /include/gmssl/block_cipher.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | 51 | #ifndef GMSSL_BLOCK_CIPHER_H 52 | #define GMSSL_BLOCK_CIPHER_H 53 | 54 | 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | 61 | 62 | #ifdef __cplusplus 63 | extern "C" { 64 | #endif 65 | 66 | 67 | #define BLOCK_CIPHER_BLOCK_SIZE 16 68 | #define BLOCK_CIPHER_MIN_KEY_SIZE 16 69 | #define BLOCK_CIPHER_MAX_KEY_SIZE 32 70 | 71 | 72 | typedef struct BLOCK_CIPHER BLOCK_CIPHER; 73 | typedef struct BLOCK_CIPHER_KEY BLOCK_CIPHER_KEY; 74 | 75 | struct BLOCK_CIPHER_KEY { 76 | union { 77 | SM4_KEY sm4_key; 78 | AES_KEY aes_key; 79 | } u; 80 | const BLOCK_CIPHER *cipher; 81 | }; 82 | 83 | typedef void (*block_cipher_set_encrypt_key_func)(BLOCK_CIPHER_KEY *key, const uint8_t *raw_key); 84 | typedef void (*block_cipher_set_decrypt_key_func)(BLOCK_CIPHER_KEY *key, const uint8_t *raw_key); 85 | typedef void (*block_cipher_encrypt_func)(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out); 86 | typedef void (*block_cipher_decrypt_func)(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out); 87 | 88 | struct BLOCK_CIPHER { 89 | int oid; 90 | size_t key_size; 91 | size_t block_size; 92 | block_cipher_set_encrypt_key_func set_encrypt_key; 93 | block_cipher_set_decrypt_key_func set_decrypt_key; 94 | block_cipher_encrypt_func encrypt; 95 | block_cipher_decrypt_func decrypt; 96 | }; 97 | 98 | const BLOCK_CIPHER *BLOCK_CIPHER_sm4(void); 99 | const BLOCK_CIPHER *BLOCK_CIPHER_aes128(void); 100 | 101 | const BLOCK_CIPHER *block_cipher_from_name(const char *name); 102 | const char *block_cipher_name(const BLOCK_CIPHER *cipher); 103 | int block_cipher_set_encrypt_key(BLOCK_CIPHER_KEY *key, const BLOCK_CIPHER *cipher, const uint8_t *raw_key); 104 | int block_cipher_set_decrypt_key(BLOCK_CIPHER_KEY *key, const BLOCK_CIPHER *cipher, const uint8_t *raw_key); 105 | int block_cipher_encrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out); 106 | int block_cipher_decrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out); 107 | 108 | 109 | #ifdef __cplusplus 110 | } 111 | #endif 112 | #endif 113 | -------------------------------------------------------------------------------- /src/sm4_setkey.c: -------------------------------------------------------------------------------- 1 | /* ==================================================================== 2 | * Copyright (c) 2014 - 2019 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | * ==================================================================== 48 | */ 49 | 50 | #include 51 | #include "endian.h" 52 | #include "sm4_lcl.h" 53 | 54 | static uint32_t FK[4] = { 55 | 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc, 56 | }; 57 | 58 | static uint32_t CK[32] = { 59 | 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, 60 | 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, 61 | 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, 62 | 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, 63 | 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, 64 | 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, 65 | 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, 66 | 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279, 67 | }; 68 | 69 | #define L32_(x) \ 70 | ((x) ^ \ 71 | ROL32((x), 13) ^ \ 72 | ROL32((x), 23)) 73 | 74 | #define ENC_ROUND(x0, x1, x2, x3, x4, i) \ 75 | x4 = x1 ^ x2 ^ x3 ^ *(CK + i); \ 76 | x4 = S32(x4); \ 77 | x4 = x0 ^ L32_(x4); \ 78 | *(rk + i) = x4 79 | 80 | #define DEC_ROUND(x0, x1, x2, x3, x4, i) \ 81 | x4 = x1 ^ x2 ^ x3 ^ *(CK + i); \ 82 | x4 = S32(x4); \ 83 | x4 = x0 ^ L32_(x4); \ 84 | *(rk + 31 - i) = x4 85 | 86 | void sm4_set_encrypt_key(SM4_KEY *key, const uint8_t user_key[16]) 87 | { 88 | uint32_t *rk = key->rk; 89 | uint32_t x0, x1, x2, x3, x4; 90 | 91 | x0 = GETU32(user_key ) ^ FK[0]; 92 | x1 = GETU32(user_key + 4) ^ FK[1]; 93 | x2 = GETU32(user_key + 8) ^ FK[2]; 94 | x3 = GETU32(user_key + 12) ^ FK[3]; 95 | 96 | #define ROUND ENC_ROUND 97 | ROUNDS(x0, x1, x2, x3, x4); 98 | #undef ROUND 99 | 100 | x0 = x1 = x2 = x3 = x4 = 0; 101 | } 102 | 103 | void sm4_set_decrypt_key(SM4_KEY *key, const uint8_t user_key[16]) 104 | { 105 | uint32_t *rk = key->rk; 106 | uint32_t x0, x1, x2, x3, x4; 107 | 108 | x0 = GETU32(user_key ) ^ FK[0]; 109 | x1 = GETU32(user_key + 4) ^ FK[1]; 110 | x2 = GETU32(user_key + 8) ^ FK[2]; 111 | x3 = GETU32(user_key + 12) ^ FK[3]; 112 | 113 | #define ROUND DEC_ROUND 114 | ROUNDS(x0, x1, x2, x3, x4); 115 | #undef ROUND 116 | 117 | x0 = x1 = x2 = x3 = x4 = 0; 118 | } 119 | -------------------------------------------------------------------------------- /src/sm2_prn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | 57 | 58 | int sm2_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *key) 59 | { 60 | format_print(fp, fmt, ind, "%s\n", label); 61 | ind += 4; 62 | sm2_public_key_print(fp, fmt, ind, "publicKey", key); 63 | format_bytes(fp, fmt, ind, "privateKey", key->private_key, 32); 64 | return 1; 65 | } 66 | 67 | int sm2_public_key_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY *pub_key) 68 | { 69 | return sm2_point_print(fp, fmt, ind, label, &pub_key->public_key); 70 | } 71 | 72 | int sm2_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_POINT *P) 73 | { 74 | format_print(fp, fmt, ind, "%s\n", label); 75 | ind += 4; 76 | format_bytes(fp, fmt, ind, "x", P->x, 32); 77 | format_bytes(fp, fmt, ind, "y", P->y, 32); 78 | return 1; 79 | } 80 | 81 | int sm2_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen) 82 | { 83 | SM2_SIGNATURE sig; 84 | format_print(fp, fmt, ind, "%s\n", label); 85 | ind += 4; 86 | if (sm2_signature_from_der(&sig, &a, &alen) != 1 87 | || asn1_length_is_zero(alen) != 1) { 88 | error_print(); 89 | return -1; 90 | } 91 | format_bytes(fp, fmt, ind, "r", sig.r, 32); 92 | format_bytes(fp, fmt, ind, "s", sig.s, 32); 93 | return 1; 94 | } 95 | 96 | int sm2_ciphertext_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen) 97 | { 98 | uint8_t buf[512] = {0}; 99 | SM2_CIPHERTEXT *c = (SM2_CIPHERTEXT *)buf; 100 | int i; 101 | 102 | if (sm2_ciphertext_from_der(c, &a, &alen) != 1 103 | || asn1_length_is_zero(alen) != 1) { 104 | error_print(); 105 | return -1; 106 | } 107 | format_print(fp, fmt, ind, "%s\n", label); 108 | ind += 4; 109 | format_bytes(fp, fmt, ind, "XCoordinate", c->point.x, 32); 110 | format_bytes(fp, fmt, ind, "YCoordinate", c->point.y, 32); 111 | format_bytes(fp, fmt, ind, "HASH", c->hash, 32); 112 | format_bytes(fp, fmt, ind, "CipherText", c->ciphertext, c->ciphertext_size); 113 | return 1; 114 | } 115 | -------------------------------------------------------------------------------- /src/sm3_hmac.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | 54 | /** 55 | * HMAC_k(m) = H((k ^ opad) || H((k ^ ipad) || m)) 56 | * pseudo-code: 57 | * function hmac(key, message) 58 | * opad = [0x5c * blocksize] 59 | * ipad = [0x36 * blocksize] 60 | * if (length(key) > blocksize) then 61 | * key = hash(key) 62 | * end if 63 | * for i from 0 to length(key) - 1 step 1 64 | * ipad[i] = ipad[i] XOR key[i] 65 | * opad[i] = opad[i] XOR key[i] 66 | * end for 67 | * return hash(opad || hash(ipad || message)) 68 | * end function 69 | */ 70 | 71 | 72 | #define IPAD 0x36 73 | #define OPAD 0x5C 74 | 75 | void sm3_hmac_init(SM3_HMAC_CTX *ctx, const uint8_t *key, size_t key_len) 76 | { 77 | int i; 78 | 79 | if (key_len <= SM3_BLOCK_SIZE) { 80 | memcpy(ctx->key, key, key_len); 81 | memset(ctx->key + key_len, 0, SM3_BLOCK_SIZE - key_len); 82 | } else { 83 | sm3_init(&ctx->sm3_ctx); 84 | sm3_update(&ctx->sm3_ctx, key, key_len); 85 | sm3_finish(&ctx->sm3_ctx, ctx->key); 86 | memset(ctx->key + SM3_DIGEST_SIZE, 0, 87 | SM3_BLOCK_SIZE - SM3_DIGEST_SIZE); 88 | } 89 | for (i = 0; i < SM3_BLOCK_SIZE; i++) { 90 | ctx->key[i] ^= IPAD; 91 | } 92 | 93 | sm3_init(&ctx->sm3_ctx); 94 | sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE); 95 | } 96 | 97 | void sm3_hmac_update(SM3_HMAC_CTX *ctx, const uint8_t *data, size_t data_len) 98 | { 99 | sm3_update(&ctx->sm3_ctx, data, data_len); 100 | } 101 | 102 | void sm3_hmac_finish(SM3_HMAC_CTX *ctx, uint8_t mac[SM3_HMAC_SIZE]) 103 | { 104 | int i; 105 | for (i = 0; i < SM3_BLOCK_SIZE; i++) { 106 | ctx->key[i] ^= (IPAD ^ OPAD); 107 | } 108 | sm3_finish(&ctx->sm3_ctx, mac); 109 | sm3_init(&ctx->sm3_ctx); 110 | sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE); 111 | sm3_update(&ctx->sm3_ctx, mac, SM3_DIGEST_SIZE); 112 | sm3_finish(&ctx->sm3_ctx, mac); 113 | memset(ctx, 0, sizeof(*ctx)); 114 | } 115 | 116 | void sm3_hmac(const uint8_t *data, size_t data_len, 117 | const uint8_t *key, size_t key_len, 118 | uint8_t mac[SM3_HMAC_SIZE]) 119 | { 120 | SM3_HMAC_CTX ctx; 121 | sm3_hmac_init(&ctx, key, key_len); 122 | sm3_hmac_update(&ctx, data, data_len); 123 | sm3_hmac_finish(&ctx, mac); 124 | } 125 | -------------------------------------------------------------------------------- /src/chacha20.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include "endian.h" 55 | 56 | void chacha20_init(CHACHA20_STATE *state, 57 | const uint8_t key[CHACHA20_KEY_SIZE], 58 | const uint8_t nonce[CHACHA20_NONCE_SIZE], 59 | uint32_t counter) 60 | { 61 | state->d[ 0] = 0x61707865; 62 | state->d[ 1] = 0x3320646e; 63 | state->d[ 2] = 0x79622d32; 64 | state->d[ 3] = 0x6b206574; 65 | state->d[ 4] = GETU32_LE(key ); 66 | state->d[ 5] = GETU32_LE(key + 4); 67 | state->d[ 6] = GETU32_LE(key + 8); 68 | state->d[ 7] = GETU32_LE(key + 12); 69 | state->d[ 8] = GETU32_LE(key + 16); 70 | state->d[ 9] = GETU32_LE(key + 20); 71 | state->d[10] = GETU32_LE(key + 24); 72 | state->d[11] = GETU32_LE(key + 28); 73 | state->d[12] = counter; 74 | state->d[13] = GETU32_LE(nonce); 75 | state->d[14] = GETU32_LE(nonce + 4); 76 | state->d[15] = GETU32_LE(nonce + 8); 77 | } 78 | 79 | /* quarter round */ 80 | #define QR(A, B, C, D) \ 81 | A += B; D ^= A; D = ROL32(D, 16); \ 82 | C += D; B ^= C; B = ROL32(B, 12); \ 83 | A += B; D ^= A; D = ROL32(D, 8); \ 84 | C += D; B ^= C; B = ROL32(B, 7) 85 | 86 | /* double round on state 4x4 matrix: 87 | * four column rounds and and four diagonal rounds 88 | * 89 | * 0 1 2 3 90 | * 4 5 6 7 91 | * 8 9 10 11 92 | * 12 13 14 15 93 | * 94 | */ 95 | #define DR(S) \ 96 | QR(S[0], S[4], S[ 8], S[12]); \ 97 | QR(S[1], S[5], S[ 9], S[13]); \ 98 | QR(S[2], S[6], S[10], S[14]); \ 99 | QR(S[3], S[7], S[11], S[15]); \ 100 | QR(S[0], S[5], S[10], S[15]); \ 101 | QR(S[1], S[6], S[11], S[12]); \ 102 | QR(S[2], S[7], S[ 8], S[13]); \ 103 | QR(S[3], S[4], S[ 9], S[14]) 104 | 105 | void chacha20_generate_keystream(CHACHA20_STATE *state, size_t counts, uint8_t *out) 106 | { 107 | uint32_t working_state[16]; 108 | int i; 109 | 110 | while (counts-- > 0) { 111 | memcpy(working_state, state->d, sizeof(working_state)); 112 | for (i = 0; i < 10; i++) { 113 | DR(working_state); 114 | } 115 | for (i = 0; i < 16; i++) { 116 | working_state[i] += state->d[i]; 117 | PUTU32_LE(out, working_state[i]); 118 | out += sizeof(uint32_t); 119 | } 120 | state->d[12]++; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | 55 | void print_der(const uint8_t *in, size_t inlen) 56 | { 57 | size_t i; 58 | for (i = 0; i < inlen; i++) { 59 | printf("%02x ", in[i]); 60 | } 61 | } 62 | 63 | void print_bytes(const uint8_t *data, size_t datalen) 64 | { 65 | size_t i; 66 | for (i = 0; i < datalen; i++) { 67 | printf("%02X ", data[i]); 68 | if ((i + 1) % 32 == 0) 69 | printf("\n"); 70 | } 71 | printf("\n"); 72 | } 73 | 74 | void print_nodes(const uint32_t *in, size_t inlen) 75 | { 76 | size_t i; 77 | printf("%u", in[0]); 78 | for (i = 1; i < inlen; i++) { 79 | printf(".%u", in[i]); 80 | } 81 | } 82 | 83 | 84 | 85 | int format_print(FILE *fp, int format, int indent, const char *str, ...) 86 | { 87 | va_list args; 88 | int i; 89 | for (i = 0; i < indent; i++) { 90 | fprintf(fp, " "); 91 | } 92 | va_start(args, str); 93 | vfprintf(fp, str, args); 94 | va_end(args); 95 | return 1; 96 | } 97 | 98 | int format_bytes(FILE *fp, int format, int indent, const char *str, const uint8_t *data, size_t datalen) 99 | { 100 | int i; 101 | 102 | if (datalen > 4096) { 103 | error_print(); 104 | return -1; 105 | } 106 | 107 | for (i = 0; i < indent; i++) { 108 | fprintf(fp, " "); 109 | } 110 | fprintf(fp, "%s: ", str); 111 | if (!datalen) { 112 | fprintf(fp, "(null)\n"); 113 | return 1; 114 | } 115 | for (i = 0; i < datalen; i++) { 116 | fprintf(fp, "%02X", data[i]); 117 | } 118 | fprintf(fp, "\n"); 119 | return 1; 120 | } 121 | 122 | 123 | int format_string(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen) 124 | { 125 | while (ind--) { 126 | fprintf(fp, " "); 127 | } 128 | fprintf(fp, "%s: ", label); 129 | while (dlen--) { 130 | fprintf(fp, "%c", *d++); 131 | } 132 | fprintf(fp, "\n"); 133 | return 1; 134 | } 135 | 136 | int tls_trace(int format, int indent, const char *str, ...) 137 | { 138 | FILE *fp = stderr; 139 | va_list args; 140 | int i; 141 | for (i = 0; i < indent; i++) { 142 | fprintf(fp, " "); 143 | } 144 | va_start(args, str); 145 | vfprintf(fp, str, args); 146 | va_end(args); 147 | fprintf(fp, "\n"); 148 | return 1; 149 | } 150 | 151 | -------------------------------------------------------------------------------- /src/block_cipher.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include "endian.h" 56 | 57 | 58 | 59 | int block_cipher_set_encrypt_key(BLOCK_CIPHER_KEY *key, const BLOCK_CIPHER *cipher, const uint8_t *raw_key) 60 | { 61 | memset(key, 0, sizeof(BLOCK_CIPHER_KEY)); 62 | cipher->set_encrypt_key(key, raw_key); 63 | key->cipher = cipher; 64 | return 1; 65 | } 66 | 67 | int block_cipher_set_decrypt_key(BLOCK_CIPHER_KEY *key, const BLOCK_CIPHER *cipher, const uint8_t *raw_key) 68 | { 69 | memset(key, 0, sizeof(BLOCK_CIPHER_KEY)); 70 | cipher->set_decrypt_key(key, raw_key); 71 | key->cipher = cipher; 72 | return 1; 73 | } 74 | 75 | int block_cipher_encrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out) 76 | { 77 | key->cipher->encrypt(key, in, out); 78 | return 1; 79 | } 80 | 81 | int block_cipher_decrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out) 82 | { 83 | key->cipher->decrypt(key, in, out); 84 | return 1; 85 | } 86 | 87 | static const BLOCK_CIPHER sm4_block_cipher_object = { 88 | OID_sm4, 89 | SM4_KEY_SIZE, 90 | SM4_BLOCK_SIZE, 91 | (block_cipher_set_encrypt_key_func)sm4_set_encrypt_key, 92 | (block_cipher_set_decrypt_key_func)sm4_set_decrypt_key, 93 | (block_cipher_encrypt_func)sm4_encrypt, 94 | (block_cipher_decrypt_func)sm4_encrypt, 95 | }; 96 | 97 | const BLOCK_CIPHER *BLOCK_CIPHER_sm4(void) { 98 | return &sm4_block_cipher_object; 99 | } 100 | 101 | static int aes128_set_encrypt_key(AES_KEY *aes_key, const uint8_t key[16]) { 102 | return aes_set_encrypt_key(aes_key, key, 16); 103 | } 104 | 105 | static int aes128_set_decrypt_key(AES_KEY *aes_key, const uint8_t key[16]) { 106 | return aes_set_decrypt_key(aes_key, key, 16); 107 | } 108 | 109 | static const BLOCK_CIPHER aes128_block_cipher_object = { 110 | OID_aes128, 111 | AES128_KEY_SIZE, 112 | AES_BLOCK_SIZE, 113 | (block_cipher_set_encrypt_key_func)aes128_set_encrypt_key, 114 | (block_cipher_set_decrypt_key_func)aes128_set_decrypt_key, 115 | (block_cipher_encrypt_func)aes_encrypt, 116 | (block_cipher_decrypt_func)aes_encrypt, 117 | }; 118 | 119 | const BLOCK_CIPHER *BLOCK_CIPHER_aes128(void) { 120 | return &aes128_block_cipher_object; 121 | } 122 | -------------------------------------------------------------------------------- /tools/certverify.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | 58 | // 验证证书链是一个重量级的功能,应准备相应的文档,列举所有验证项目 59 | // 比如最基本的是证书中的签名、有效期、各个扩展等 60 | // 外部相关的:证书链、CRL等 61 | 62 | int main(int argc, char **argv) 63 | { 64 | int ret = 0; 65 | char *prog = argv[0]; 66 | char *certfile = NULL; 67 | FILE *certfp = NULL; 68 | 69 | char *cacertfile = NULL; 70 | FILE *cacertfp = NULL; 71 | 72 | uint8_t cert[1024]; 73 | size_t certlen; 74 | uint8_t cacert[1024]; 75 | size_t cacertlen; 76 | char *signer_id = SM2_DEFAULT_ID; 77 | 78 | SM2_KEY ca_pubkey; 79 | 80 | argc--; 81 | argv++; 82 | while (argc >= 1) { 83 | if (!strcmp(*argv, "-help")) { 84 | printf("Usage: %s [-cert pem] -cacert pem\n", prog); 85 | return 0; 86 | 87 | } else if (!strcmp(*argv, "-cert")) { 88 | if (--argc < 1) goto bad; 89 | certfile = *(++argv); 90 | if (!(certfp = fopen(certfile, "r"))) { 91 | error_print(); 92 | return -1; 93 | } 94 | } else if (!strcmp(*argv, "-cacert")) { 95 | if (--argc < 1) goto bad; 96 | cacertfile = *(++argv); 97 | if (!(cacertfp = fopen(cacertfile, "r"))) { 98 | error_print(); 99 | return -1; 100 | } 101 | } else { 102 | printf("Usage: %s [-cert pem] -cacert pem\n", prog); 103 | return 0; 104 | break; 105 | } 106 | 107 | argc--; 108 | argv++; 109 | } 110 | 111 | if (!certfp || !cacertfp) { 112 | error_print(); 113 | return -1; 114 | } 115 | 116 | 117 | if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1) { 118 | error_print(); 119 | return -1; 120 | } 121 | if (x509_cert_from_pem(cacert, &cacertlen, sizeof(cacert), cacertfp) != 1) { 122 | error_print(); 123 | return -1; 124 | } 125 | if (x509_cert_verify_by_ca_cert(cert, certlen, cacert, cacertlen, signer_id, strlen(signer_id)) != 1) { 126 | error_print(); 127 | return -1; 128 | } 129 | ret = 1; 130 | printf("Verification %s\n", ret ? "success" : "failure"); 131 | 132 | ret = 0; 133 | goto end; 134 | 135 | bad: 136 | fprintf(stderr, "%s: commands should not be used together\n", prog); 137 | 138 | end: 139 | return ret; 140 | } 141 | -------------------------------------------------------------------------------- /include/gmssl/x509_str.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_X509_STR_H 50 | #define GMSSL_X509_STR_H 51 | 52 | 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | 61 | #ifdef __cplusplus 62 | extern "C" { 63 | #endif 64 | 65 | /* 66 | DirectoryString or DirectoryName 67 | 68 | DirectoryName ::= CHOICE { 69 | teletexString TeletexString (SIZE (1..MAX)), 70 | printableString PrintableString (SIZE (1..MAX)), 71 | universalString UniversalString (SIZE (1..MAX)), 72 | utf8String UTF8String (SIZE (1..MAX)), 73 | bmpString BMPString (SIZE (1..MAX)), 74 | } 75 | */ 76 | int x509_directory_name_check(int tag, const uint8_t *d, size_t dlen); 77 | int x509_directory_name_check_ex(int tag, const uint8_t *d, size_t dlen, size_t minlen, size_t maxlen); 78 | int x509_directory_name_to_der(int tag, const uint8_t *d, size_t dlen, uint8_t **out, size_t *outlen); 79 | int x509_directory_name_from_der(int *tag, const uint8_t **d, size_t *dlen, const uint8_t **in, size_t *inlen); 80 | int x509_explicit_directory_name_to_der(int index, int tag, const uint8_t *d, size_t dlen, uint8_t **out, size_t *outlen); 81 | int x509_explicit_directory_name_from_der(int index, int *tag, const uint8_t **d, size_t *dlen, const uint8_t **in, size_t *inlen); 82 | int x509_directory_name_print(FILE *fp, int fmt, int ind, const char *label, int tag, const uint8_t *d, size_t dlen); 83 | 84 | 85 | /* 86 | DisplayText ::= CHOICE { 87 | ia5String IA5String (SIZE (1..200)), 88 | visibleString VisibleString (SIZE (1..200)), 89 | bmpString BMPString (SIZE (1..200)), 90 | utf8String UTF8String (SIZE (1..200)) 91 | } 92 | */ 93 | #define X509_DISPLAY_TEXT_MIN_LEN 1 94 | #define X509_DISPLAY_TEXT_MAX_LEN 200 95 | 96 | int x509_display_text_check(int tag, const uint8_t *d, size_t dlen); 97 | int x509_display_text_to_der(int tag, const uint8_t *d, size_t dlen, uint8_t **out, size_t *outlen); 98 | int x509_display_text_from_der(int *tag, const uint8_t **d, size_t *dlen, const uint8_t **in, size_t *inlen); 99 | int x509_display_text_print(FILE *fp, int fmt, int ind, const char *label, int tag, const uint8_t *d, size_t dlen); 100 | 101 | 102 | #ifdef __cplusplus 103 | } 104 | #endif 105 | #endif 106 | -------------------------------------------------------------------------------- /src/sm4_enc.c: -------------------------------------------------------------------------------- 1 | /* ==================================================================== 2 | * Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | * ==================================================================== 48 | */ 49 | 50 | #include 51 | #include "endian.h" 52 | #include "sm4_lcl.h" 53 | 54 | 55 | #define L32(x) \ 56 | ((x) ^ \ 57 | ROL32((x), 2) ^ \ 58 | ROL32((x), 10) ^ \ 59 | ROL32((x), 18) ^ \ 60 | ROL32((x), 24)) 61 | 62 | #define ROUND_SBOX(x0, x1, x2, x3, x4, i) \ 63 | x4 = x1 ^ x2 ^ x3 ^ *(rk + i); \ 64 | x4 = S32(x4); \ 65 | x4 = x0 ^ L32(x4) 66 | 67 | #define ROUND_TBOX(x0, x1, x2, x3, x4, i) \ 68 | x4 = x1 ^ x2 ^ x3 ^ *(rk + i); \ 69 | t0 = ROL32(SM4_T[(uint8_t)x4], 8); \ 70 | x4 >>= 8; \ 71 | x0 ^= t0; \ 72 | t0 = ROL32(SM4_T[(uint8_t)x4], 16); \ 73 | x4 >>= 8; \ 74 | x0 ^= t0; \ 75 | t0 = ROL32(SM4_T[(uint8_t)x4], 24); \ 76 | x4 >>= 8; \ 77 | x0 ^= t0; \ 78 | t1 = SM4_T[x4]; \ 79 | x4 = x0 ^ t1 80 | 81 | #define ROUND ROUND_TBOX 82 | 83 | 84 | void sm4_encrypt(const SM4_KEY *key, const unsigned char in[16], unsigned char out[16]) 85 | { 86 | const uint32_t *rk = key->rk; 87 | uint32_t x0, x1, x2, x3, x4; 88 | uint32_t t0, t1; 89 | 90 | x0 = GETU32(in ); 91 | x1 = GETU32(in + 4); 92 | x2 = GETU32(in + 8); 93 | x3 = GETU32(in + 12); 94 | ROUNDS(x0, x1, x2, x3, x4); 95 | PUTU32(out , x0); 96 | PUTU32(out + 4, x4); 97 | PUTU32(out + 8, x3); 98 | PUTU32(out + 12, x2); 99 | } 100 | 101 | /* caller make sure counter not overflow */ 102 | void sm4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, 103 | size_t blocks, const SM4_KEY *key, const unsigned char iv[16]) 104 | { 105 | const uint32_t *rk = key->rk; 106 | unsigned int c0 = GETU32(iv ); 107 | unsigned int c1 = GETU32(iv + 4); 108 | unsigned int c2 = GETU32(iv + 8); 109 | unsigned int c3 = GETU32(iv + 12); 110 | uint32_t x0, x1, x2, x3, x4; 111 | uint32_t t0, t1; 112 | 113 | while (blocks--) { 114 | x0 = c0; 115 | x1 = c1; 116 | x2 = c2; 117 | x3 = c3; 118 | ROUNDS(x0, x1, x2, x3, x4); 119 | PUTU32(out , GETU32(in ) ^ x0); 120 | PUTU32(out + 4, GETU32(in + 4) ^ x4); 121 | PUTU32(out + 8, GETU32(in + 8) ^ x3); 122 | PUTU32(out + 12, GETU32(in + 12) ^ x2); 123 | in += 16; 124 | out += 16; 125 | c3++; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /include/gmssl/hash_drbg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | /* NIST SP800-90A Rev.1 "Recommendation for Random Number Generation 50 | * Using Deterministic Random Bit Generators", 10.1.1 Hash_DRBG */ 51 | 52 | #ifndef GMSSL_HASH_DRBG_H 53 | #define GMSSL_HASH_DRBG_H 54 | 55 | 56 | #include 57 | #include 58 | #include 59 | 60 | 61 | /* seedlen for hash_drgb, table 2 of nist sp 800-90a rev.1 */ 62 | #define HASH_DRBG_SM3_SEED_BITS 440 /* 55 bytes */ 63 | #define HASH_DRBG_SHA1_SEED_BITS 440 64 | #define HASH_DRBG_SHA224_SEED_BITS 440 65 | #define HASH_DRBG_SHA512_224_SEED_BITS 440 66 | #define HASH_DRBG_SHA256_SEED_BITS 440 67 | #define HASH_DRBG_SHA512_256_SEED_BITS 440 68 | #define HASH_DRBG_SHA384_SEED_BITS 888 /* 110 bytes */ 69 | #define HASH_DRBG_SHA512_SEED_BITS 888 70 | #define HASH_DRBG_MAX_SEED_BITS 888 71 | 72 | #define HASH_DRBG_SM3_SEED_SIZE (HASH_DRBG_SM3_SEED_BITS/8) 73 | #define HASH_DRBG_SHA1_SEED_SIZE (HASH_DRBG_SHA1_SEED_BITS/8) 74 | #define HASH_DRBG_SHA224_SEED_SIZE (HASH_DRBG_SHA224_SEED_BITS/8) 75 | #define HASH_DRBG_SHA512_224_SEED_SIZE (HASH_DRBG_SHA512_224_SEED_BITS/8) 76 | #define HASH_DRBG_SHA256_SEED_SIZE (HASH_DRBG_SHA256_SEED_BITS/8) 77 | #define HASH_DRBG_SHA512_256_SEED_SIZE (HASH_DRBG_SHA512_256_SEED_BITS/8) 78 | #define HASH_DRBG_SHA384_SEED_SIZE (HASH_DRBG_SHA384_SEED_BITS/8) 79 | #define HASH_DRBG_SHA512_SEED_SIZE (HASH_DRBG_SHA512_SEED_BITS/8) 80 | #define HASH_DRBG_MAX_SEED_SIZE (HASH_DRBG_MAX_SEED_BITS/8) 81 | 82 | #define HASH_DRBG_RESEED_INTERVAL ((uint64_t)1 << 48) 83 | 84 | #ifdef __cplusplus 85 | extern "C" { 86 | #endif 87 | 88 | 89 | typedef struct { 90 | const DIGEST *digest; 91 | uint8_t V[HASH_DRBG_MAX_SEED_SIZE]; 92 | uint8_t C[HASH_DRBG_MAX_SEED_SIZE]; 93 | size_t seedlen; 94 | uint64_t reseed_counter; 95 | } HASH_DRBG; 96 | 97 | 98 | int hash_drbg_init(HASH_DRBG *drbg, 99 | const DIGEST *digest, 100 | const uint8_t *entropy, size_t entropy_len, 101 | const uint8_t *nonce, size_t nonce_len, 102 | const uint8_t *personalstr, size_t personalstr_len); 103 | 104 | int hash_drbg_reseed(HASH_DRBG *drbg, 105 | const uint8_t *entropy, size_t entropy_len, 106 | const uint8_t *additional, size_t additional_len); 107 | 108 | int hash_drbg_generate(HASH_DRBG *drbg, 109 | const uint8_t *additional, size_t additional_len, 110 | size_t outlen, uint8_t *out); 111 | 112 | 113 | #ifdef __cplusplus 114 | } 115 | #endif 116 | #endif 117 | -------------------------------------------------------------------------------- /include/gmssl/sm4.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #ifndef GMSSL_SM4_H 50 | #define GMSSL_SM4_H 51 | 52 | #include 53 | #include 54 | 55 | #ifdef __cplusplus 56 | extern "C" { 57 | #endif 58 | 59 | 60 | #define SM4_KEY_SIZE (16) 61 | #define SM4_BLOCK_SIZE (16) 62 | #define SM4_NUM_ROUNDS (32) 63 | 64 | 65 | typedef struct { 66 | uint32_t rk[SM4_NUM_ROUNDS]; 67 | } SM4_KEY; 68 | 69 | void sm4_set_encrypt_key(SM4_KEY *key, const uint8_t raw_key[SM4_KEY_SIZE]); 70 | void sm4_set_decrypt_key(SM4_KEY *key, const uint8_t raw_key[SM4_KEY_SIZE]); 71 | void sm4_encrypt(const SM4_KEY *key, const uint8_t in[SM4_BLOCK_SIZE], uint8_t out[SM4_BLOCK_SIZE]); 72 | #define sm4_decrypt(key,in,out) sm4_encrypt(key,in,out) 73 | 74 | 75 | void sm4_cbc_encrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE], 76 | const uint8_t *in, size_t nblocks, uint8_t *out); 77 | void sm4_cbc_decrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE], 78 | const uint8_t *in, size_t nblocks, uint8_t *out); 79 | int sm4_cbc_padding_encrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE], 80 | const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen); 81 | int sm4_cbc_padding_decrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE], 82 | const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen); 83 | 84 | void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[SM4_BLOCK_SIZE], 85 | const uint8_t *in, size_t inlen, uint8_t *out); 86 | #define sm4_ctr_decrypt(key,ctr,in,inlen,out) sm4_ctr_encrypt(key,ctr,in,inlen,out) 87 | 88 | 89 | #define SM4_GCM_IV_MIN_SIZE 1 90 | #define SM4_GCM_IV_MAX_SIZE ((uint64_t)(1 << (64-3))) 91 | #define SM4_GCM_IV_DEFAULT_BITS 96 92 | #define SM4_GCM_IV_DEFAULT_SIZE 12 93 | 94 | #define SM4_GCM_MIN_AAD_SIZE 0 95 | #define SM4_GCM_MAX_AAD_SIZE ((uint64_t)(1 << (64-3))) 96 | 97 | #define SM4_GCM_MIN_PLAINTEXT_SIZE 0 98 | #define SM4_GCM_MAX_PLAINTEXT_SIZE ((((uint64_t)1 << 39) - 256) >> 3) 99 | 100 | int sm4_gcm_encrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen, 101 | const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen, 102 | uint8_t *out, size_t taglen, uint8_t *tag); 103 | int sm4_gcm_decrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen, 104 | const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen, 105 | const uint8_t *tag, size_t taglen, uint8_t *out); 106 | 107 | 108 | #ifdef __cplusplus 109 | } 110 | #endif 111 | #endif 112 | -------------------------------------------------------------------------------- /tools/sm3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * 3. All advertising materials mentioning features or use of this 17 | * software must display the following acknowledgment: 18 | * "This product includes software developed by the GmSSL Project. 19 | * (http://gmssl.org/)" 20 | * 21 | * 4. The name "GmSSL Project" must not be used to endorse or promote 22 | * products derived from this software without prior written 23 | * permission. For written permission, please contact 24 | * guanzhi1980@gmail.com. 25 | * 26 | * 5. Products derived from this software may not be called "GmSSL" 27 | * nor may "GmSSL" appear in their names without prior written 28 | * permission of the GmSSL Project. 29 | * 30 | * 6. Redistributions of any form whatsoever must retain the following 31 | * acknowledgment: 32 | * "This product includes software developed by the GmSSL Project 33 | * (http://gmssl.org/)" 34 | * 35 | * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. 47 | */ 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | 57 | int main(int argc, char **argv) 58 | { 59 | char *prog = argv[0]; 60 | char *pubkeyfile = NULL; 61 | char *infile = NULL; 62 | char *id = NULL; 63 | FILE *pubkeyfp = NULL; 64 | FILE *infp = stdin; 65 | SM3_CTX sm3_ctx; 66 | uint8_t dgst[32]; 67 | uint8_t buf[4096]; 68 | ssize_t len; 69 | int i; 70 | 71 | argc--; 72 | argv++; 73 | 74 | while (argc > 0) { 75 | if (!strcmp(*argv, "-help")) { 76 | help: 77 | fprintf(stderr, "usage: %s [-pubkey pem [-id str]] [-in file]\n", prog); 78 | fprintf(stderr, "usage: echo -n \"abc\" | %s\n", prog); 79 | return -1; 80 | 81 | } else if (!strcmp(*argv, "-pubkey")) { 82 | if (--argc < 1) goto bad; 83 | pubkeyfile = *(++argv); 84 | 85 | } else if (!strcmp(*argv, "-id")) { 86 | if (--argc < 1) goto bad; 87 | id = *(++argv); 88 | 89 | } else if (!strcmp(*argv, "-in")) { 90 | if (--argc < 1) goto bad; 91 | infile = *(++argv); 92 | 93 | } else { 94 | fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv); 95 | goto help; 96 | } 97 | 98 | argc--; 99 | argv++; 100 | } 101 | 102 | sm3_init(&sm3_ctx); 103 | 104 | if (pubkeyfile) { 105 | SM2_KEY sm2_key; 106 | uint8_t z[32]; 107 | 108 | if (!(pubkeyfp = fopen(pubkeyfile, "r"))) { 109 | error_print(); 110 | return -1; 111 | } 112 | if (sm2_public_key_info_from_pem(&sm2_key, pubkeyfp) != 1) { 113 | error_print(); 114 | return -1; 115 | } 116 | if (!id) { 117 | id = SM2_DEFAULT_ID; 118 | } 119 | 120 | sm2_compute_z(z, (SM2_POINT *)&sm2_key, id, strlen(id)); 121 | sm3_update(&sm3_ctx, z, sizeof(z)); 122 | 123 | } else { 124 | if (id) { 125 | fprintf(stderr, "%s: option '-id' must be with '-pubkey'\n", prog); 126 | goto help; 127 | } 128 | } 129 | 130 | if (infile) { 131 | if (!(infp = fopen(infile, "r"))) { 132 | error_print(); 133 | return -1; 134 | } 135 | } 136 | while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) { 137 | sm3_update(&sm3_ctx, buf, len); 138 | } 139 | 140 | sm3_finish(&sm3_ctx, dgst); 141 | for (i = 0; i < sizeof(dgst); i++) { 142 | printf("%02x", dgst[i]); 143 | } 144 | printf("\n"); 145 | 146 | if (infile) { 147 | fclose(infp); 148 | } 149 | return 0; 150 | 151 | bad: 152 | fprintf(stderr, "%s: '%s' option value required\n", prog, *argv); 153 | return -1; 154 | } 155 | --------------------------------------------------------------------------------