Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 19
sidebar_position: 2
slug: /features/SM2-No-Za
---
# SM2 无 Za 实战

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
sidebar_position: 3
slug: /features/zuc/ZUC-128-EIA3
---
# ZUC 128-EIA3 例子
## 构建
构建 Tongsuo 时需要开启 ZUC 算法,例如:
Expand Down
6 changes: 6 additions & 0 deletions docs/features/algorithm-practice/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"label": "算法实战",
"position": 5,
"collapsible": true,
"link": null
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 6
sidebar_position: 1
slug: /features/sm2-sm3-sm4-cli
---

# SM2&SM3&SM4 命令行实战
Expand All @@ -26,7 +27,7 @@ echo -n "hello tongsuo" | /opt/tongsuo/bin/tongsuo dgst -sm3
```

结果如下:
![image.png](img/cli-result.png)
![image.png](../img/cli-result.png)

## 实战 SM2 签名和验签

Expand All @@ -44,7 +45,7 @@ dd if=/dev/urandom of=msg.bin bs=1024 count=1

签名正确时,验证成功可以看到:

![image.png](img/cli-result2.png)
![image.png](../img/cli-result2.png)

## 实战 SM2 加密和解密

Expand All @@ -61,4 +62,4 @@ echo "hello tongsuo" > msg.bin
diff msg.bin msg2.bin
```

![image.png](img/cli-result3.png)
![image.png](../img/cli-result3.png)
6 changes: 6 additions & 0 deletions docs/features/ecosystem-integrations/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"label": "生态集成",
"position": 9,
"collapsible": true,
"link": null
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 5
slug: /features/angie
---

# Nginx 衍生版 Angie 支持铜锁和国密功能
Expand All @@ -9,7 +10,7 @@ sidebar_position: 5
近日,铜锁社区收到 Angie 核心成员 VBart (真名:Valentin V. Bartenev) 的来信,他很高兴地说,Angie 已经支持了铜锁库和国密功能,希望我们在铜锁项目主页上『互粉』一下。这让我们欣喜万分,因为我们之前尝试去让 Nginx 支持铜锁和国密,但由于 Nginx 被 F5 收购之后就不那么 open 了,此事就没了后续,而今天了解到 Angie 是 Nginx 的几个核心成员 fork Nginx 的衍生版,因为 F5 是美国公司,由于政治原因关闭了俄罗斯的业务,所以 Nginx 的核心成员就出去创业了自己的公司,开始开发 Angie,也算是延续 Nginx 的生命吧。同时,也让我们意识到,解决卡脖子问题刻不容缓。另外,也想不到一个俄罗斯软件会主动来集成铜锁和支持国密,我想这可能是开源社区的魅力吧。
下面是 VBart 来信的原文截图:

![image.png](img/angie.png)
![image.png](../img/angie.png)

## 关于 Angie

Expand All @@ -26,7 +27,7 @@ http 模块支持国密的commit:[https://github.com/webserver-llc/angie/commi
stream 模块支持国密的commit:[https://github.com/webserver-llc/angie/commit/fa83ee606cf7d55910e28772f5914d773e1fb635](https://github.com/webserver-llc/angie/commit/fa83ee606cf7d55910e28772f5914d773e1fb635)

下面是 Angie 的特性截图:
![image.png](img/angie2.png)
![image.png](../img/angie2.png)

Angie 更多信息请移步其官网:[https://angie.software/en/](https://angie.software/en/)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
sidebar_position: 28
---
---
sidebar_position: 8
slug: /features/cpython_ntls
---

# cpython + tongsuo

Expand Down Expand Up @@ -296,4 +297,4 @@ if __name__ == "__main__":

bash
sudo tcpdump -i lo -s 0 -w localhost_8443.pcap 'host 127.0.0.1 and port 8443'
然后在 Windows 系统下用 Wireshark 打开并分析 pcap 文件。
然后在 Windows 系统下用 Wireshark 打开并分析 pcap 文件。
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 10
sidebar_position: 1
slug: /features/curl
---

# curl + 铜锁,支持国密HTTPS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
sidebar_position: 27
---
---
sidebar_position: 7
slug: /features/gRPC_NTLS
---

# gRPC + Tongsuo

Expand Down Expand Up @@ -475,4 +476,4 @@ int main(int argc, char** argv) {

bash
tcpdump -i lo -s 0 -w localhost_50051.pcap '(host 127.0.0.1 or host ::1) and port 50051'
然后在 Windows 系统下用 Wireshark 打开并分析 pcap 文件。
然后在 Windows 系统下用 Wireshark 打开并分析 pcap 文件。
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 8
sidebar_position: 6
slug: /features/php
---

# PHP 使用 Tongsuo + 国密说明文档
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 13
sidebar_position: 4
slug: /features/tengine
---
# Tengine + Tongsuo 国密使用教程
## 版本
Expand Down Expand Up @@ -270,7 +271,7 @@ ECDHE-SM2套件需要设置客户端的签名和加密证书。
1. 下载和安装360安全浏览器: [http://jinjiu.oss.aliyuncs.com/360se10.1.1670.0.exe](http://jinjiu.oss.aliyuncs.com/360se10.1.1670.0.exe)  (注意:360安全浏览器其他版本没有测试通过,可能是用法不对,也可能是客户端 bug)
2. 启用国密

![image.png](./img/tengine.png)
![image.png](../img/tengine.png)

3. 配置信任的根证书

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
sidebar_position: 10
slug: /features/tongsuo-java-sdk-engine
---
# Tongsuo-Java-SDK + 海光CPU密码协处理器使用教程

## 概述
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
sidebar_position: 9
slug: /features/tongsuo-java-sdk-tls13
---
# Tongsuo-Java-SDK, TLS 1.3 + 商密套件示例代码

## 客户端,TLS 1.3 + 商密套件
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 9
sidebar_position: 2
slug: /features/wget
---

# wget + 铜锁,支持从国密服务器下载文件
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 11
sidebar_position: 3
slug: /features/wrk
---
# wrk + 铜锁,测试国密性能

Expand Down
6 changes: 6 additions & 0 deletions docs/features/getting-started/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"label": "综合使用教程",
"position": 3,
"collapsible": true,
"link": null
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 3
sidebar_position: 1
slug: /features/handbook
---
# 铜锁探“密”训练营实验手册

Expand Down Expand Up @@ -66,13 +67,13 @@ make install
```bash
ls -l /opt/tongsuo
```
![image.png](img/handbook1.png)
![image.png](../img/handbook1.png)

产看铜锁版本,执行如下命令:
```bash
/opt/tongsuo/bin/tongsuo version
```
![image.png](img/handbook2.png)
![image.png](../img/handbook2.png)

## SM2&SM3&SM4算法实战

Expand All @@ -96,7 +97,7 @@ diff msg.bin msg2.bin
echo -n "hello tongsuo" | /opt/tongsuo/bin/tongsuo dgst -sm3
```
结果如下:
![image.png](img/handbook3.png)
![image.png](../img/handbook3.png)

### 实战SM2签名和验签

Expand All @@ -112,7 +113,7 @@ dd if=/dev/urandom of=msg.bin bs=1024 count=1

```
签名正确时,验证成功可以看到:
![image.png](img/handbook4.png)
![image.png](../img/handbook4.png)

## SM2&SM3&SM4算法编程入门

Expand Down Expand Up @@ -186,7 +187,7 @@ gcc sm4_enc.c -I/opt/tongsuo/include -L/opt/tongsuo/lib64 -lcrypto -Wl,-rpath=/o
./a.out
```
输出明文消息的密文如下:
![image.png](img/handbook5.png)
![image.png](../img/handbook5.png)

SM4解密:
```c
Expand Down Expand Up @@ -309,7 +310,7 @@ gcc sm3.c -I/opt/tongsuo/include -L/opt/tongsuo/lib64 -lcrypto -Wl,-rpath=/opt/t
./a.out
```
运行结果如下:
![image.png](img/handbook6.png)
![image.png](../img/handbook6.png)

### SM2签名算法编程入门

Expand Down Expand Up @@ -387,7 +388,7 @@ gcc sm2_sign.c -I/opt/tongsuo/include -L/opt/tongsuo/lib64 -lcrypto -Wl,-rpath=/
./a.out
```
输出签名结果:
![image.png](img/handbook7.png)
![image.png](../img/handbook7.png)

SM2验签:
```c
Expand Down Expand Up @@ -653,7 +654,7 @@ gcc sm2_dec.c -I/opt/tongsuo/include -L/opt/tongsuo/lib64 -lcrypto -Wl,-rpath=/o
./a.out
```
解密出来的明文应该是hello tongsuo,
![image.png](img/handbook8.png)
![image.png](../img/handbook8.png)

## 自签发国密证书

Expand Down Expand Up @@ -1020,22 +1021,22 @@ mkdir certs/server
/opt/tongsuo/bin/tongsuo pkey -in certs/server/sm2_sign.key -text -noout
```
结果如下:
![image.png](img/handbook9.png)
![image.png](../img/handbook9.png)

命令行查看证书:
```bash
/opt/tongsuo/bin/tongsuo x509 -in certs/server/sm2_sign.crt -text -noout
```
结果如下:
![image.png](img/handbook10.png)
![image.png](../img/handbook10.png)

浏览器查看证书,使用支持360浏览器访问[https://ebssec.boc.cn/boc15/login.html](https://ebssec.boc.cn/boc15/login.html),或者其他支持国密协议的浏览器也可以,查看国密证书,截图如下:

![image.png](img/handbook11.png)
![image.png](../img/handbook11.png)

点击证书信息,可以查看详细的信息:

![image.png](img/handbook12.png)
![image.png](../img/handbook12.png)

## 实战国密传输协议

Expand Down Expand Up @@ -1391,13 +1392,13 @@ SSL-Session:

通过360浏览器访问[https://ebssec.boc.cn/](https://ebssec.boc.cn/),如图所示:

![image.png](img/handbook13.png)
![image.png](../img/handbook13.png)

可以从截图中看出使用的国密传输协议。

同时使用 Wireshark 进行抓包,可以看到 TLCP 的握手消息的详细内容。

![image.png](img/handbook14.png)
![image.png](../img/handbook14.png)

## 国密传输协议编程入门

Expand Down Expand Up @@ -1588,7 +1589,7 @@ gcc server.c -I/opt/tongsuo/include/ -L/opt/tongsuo/lib64/ -lssl -lcrypto -Wl,-
```
并在客户端发送消息,服务端截图:

![image.png](img/handbook15.png)
![image.png](../img/handbook15.png)

### 国密客户端

Expand Down Expand Up @@ -1724,7 +1725,7 @@ gcc client.c -I/opt/tongsuo/include/ -L/opt/tongsuo/lib64/ -lssl -lcrypto -Wl,-
```
可以看到连接成功,发送消息,并收到应答:

![image.png](img/handbook16.png)
![image.png](../img/handbook16.png)

## Tengine + 铜锁,搭建国密服务器

Expand Down Expand Up @@ -2244,7 +2245,7 @@ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
/usr/local/mysql/bin/mysql -uroot -p --ssl-mode=required
```
查看status:
![image.png](img/handbook17.png)
![image.png](../img/handbook17.png)

## 结营作业说明

Expand All @@ -2267,7 +2268,7 @@ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
- 以组号或者小组名字在t-camp下创建目录,将所有代码放到该目录下,避免和其他小组冲突;

fork项目:
![image.png](img/handbook18.png)
![image.png](../img/handbook18.png)

提交变更请求:
![image.png](img/handbook19.png)
![image.png](../img/handbook19.png)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 12
sidebar_position: 3
slug: /features/sm2-sm3-sm4-algorithms
---
# 在铜锁中使用 SM2&SM3&SM4 算法的教程

Expand Down
6 changes: 6 additions & 0 deletions docs/features/performance-optimization/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"label": "国密性能优化",
"position": 7,
"collapsible": true,
"link": null
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 3
slug: /features/sm2/Implement-2
---
# 铜锁SM2算法性能优化实践:(三)快速模逆元算法实现

Expand All @@ -18,7 +19,7 @@ sidebar_position: 3
用于求解最大公约数的欧几里得算法最早在欧几里得的《几何原本》中被提出,拓展欧几里得算法是对这一古老算法的扩展。在求解最大公约数的基础上,拓展欧几里得算法通过收集辗转相除过程中的余式,求得线性方程$ax + by = gcd(a, b)$的整数解。由于阶数$n$是一个质数,因此$gcd(a, n) = 1$,那 么该线性方程即转化为$ax + ny = 1$,恰巧是同余线性方程 $ax \equiv 1 \pmod{n}$ 的一般表示形式,所求的$x$即为$a$的模$n$逆元。
对于现代计算机而言,拓展欧几里得算法的一个缺点是在计算过程中存在大量除法运算,而CPU在处理除法运算时的效率通常比其他基本运算(如加、减、乘)要低得多。针对这一缺陷,约瑟夫 · 斯提芬于1967年提出了[二进制拓展欧几里得算法](https://www.sciencedirect.com/science/article/abs/pii/0021999167900472?via%3Dihub), 该算法用简单的移位操作和减法代替了复杂的除法运算。 下面是利用二进制拓展欧几里得算法求模$n$逆元的伪代码:

![image.png](../img/pseudocode.png)
![image.png](../../img/pseudocode.png)

拓展欧几里得算法求解模逆元的优点是:**可以求解任意模数下的逆元**,不受模数是否为素数的限制;算法效率高,相较于费马小定理求模逆元有一定的**性能优势**。但是,拓展欧几里得算法相应的也存在一些缺点:**实现代码较为复杂**,容易出错;代码中有大量的分支和判断语句,**难以实现恒定时间(Constant time)算法**,对于侧信道攻击的抵抗较弱,在密码学算法中可能会导致关于私钥或明文的信息发生泄漏。

Expand Down Expand Up @@ -50,7 +51,7 @@ $a^{2^{256} − 2^{224} - 2^{96} + 2^{64} - 3} \pmod{p}$
该数的幂次非常大,如果采用常规方式构造,效率极低,这里我们采用加法链的思想以实现快速求幂。加法链求幂是一种快速求幂的方法,它的基本思想是将指数按二进制分解,并将幂运算分解为多个小幂数相乘的形式,从而减少幂运算的次数。前文提到,费马小定理求解模逆元的运算过程可以分解为模乘法和模平方运算,这里我们记模乘法次数为$xM$,模平方次数为$yS$,那么加法链求幂的时间复杂度可以用$xM + yS$来衡量。
尽管使加法链求幂时间复杂度最优的问题是一个NP-hard问题,且证明某求解链路是否为最优解也非常困难,但密码学界针对常见的椭圆曲线参数已经提出了许多较优的加法链。我们通过比较同一曲线的不同加法链路和相近曲线的较优链路,再进一步比较不同方案所需的中间值数量,可以比选出一个当前最优解。目前,在针对SM2曲线参数$p$的模逆元加法链研究中,一个较优解是朱辉等人提出的[算法](https://jeit.ac.cn/cn/article/doi/10.11999/JEIT211049),**此算法的时间复杂度为**$255M + 14S$**,需要4个变量作为中间值**,具体如下:

![image.png](../img/sca-256.png)
![image.png](../../img/sca-256.png)
在计算$a^{p-2}$时,另一个需要仔细考虑的是中间值溢出问题。在本系列(二)中提到,快速模约减的输入必须小于$p^2$,由于$0 < a < p$,因此在每一次乘法或平方运算后,都需要立刻调用快速模约减函数将中间值约化到$[0,p)$范围内,以避免中间值溢出导致结果出错的情况。最终铜锁实现的快速模逆元算法如下所示:
```c
static void felem_inv(felem out, const felem in)
Expand Down
Loading
Loading