当前位置: 首页 > article >正文

STM32嵌入式移植GmSSL库

前言

最近在做一个换电柜的项目,需要和云端平台对接json协议,由于服务端规定了,需要采用sm2 sm3 sm4用来加密。在嵌入式方面只能用北京大学的GmSSL了。

下载GmSSL

https://github.com/guanzhi/GmSSL下载库
也可以通过git命令下载,下载不了,请注意科学上网。
git clone https://github.com/guanzhi/GmSSL
我采用的3.0.0版本
git checkout 3.0.0

在linux下测试一下库是否能用

编译
mkdir build
cd build
cmake ..
cmake --build .
在build\bin可以看到有aestest block_ciphertest cmstest ectest gmssl hkdftest...等等可执行文件了。
测试通过后下面可以移植到嵌入式平台了。

添加一些缺少的C标准库函数

添加syscalls.c文件放到src目录下

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>  // 包含timegm需要的头文件
#include <stdio.h> // 用于assert


// 用于断言的简单实现
void _exit(int status) { return; }

void *_sbrk_r(struct _reent *r, int nbytes)
{
    extern char _end; // 末尾符号
    static char *heap_end;
    char *prev_heap_end;

    if (heap_end == 0)
    {
        heap_end = &_end; // 初始化堆指针
    }

    prev_heap_end = heap_end;
    heap_end += nbytes;

    return (char *)prev_heap_end;
}

int _write(int file, char *ptr, int len)
{
    // 将数据写入特定的输出设备,通常是串口
    return -1;
}

int _open(const char *pathname, int flags)
{
    return -1;
}
int _close(int file) { return-1; }

int _fstat(int file, struct stat *st) { return -1; }

int _isatty(int file)
{
    return 1; // 默认所有文件为终端
}

int _lseek(int file, int ptr, int dir) { return -1; }

int _read(int file, char *ptr, int len)
{
    return -1;
}

// 添加 _assert 函数
void _assert(const char *expr, const char *file, int line)
{
    // 这里可以根据需要处理
    // 例如:输出错误信息,或进入死循环
    while (1)
    {
        // 进入死循环,或者根据需要重启系统
    }
}

// 添加 timegm 函数
time_t timegm(struct tm *tm)
{
    // 这里可以实现根据 tm 结构计算 UTC 时间
    // 一个简单的实现示例,具体实现可能需要根据需要调整
    time_t seconds = 0;

    // 计算总秒数(这是一个简化的示例,实际可能需要更多处理)
    seconds += tm->tm_sec;
    seconds += tm->tm_min * 60;
    seconds += tm->tm_hour * 3600;
    seconds += (tm->tm_year - 70) * 31536000; // 1970年起的年数
    seconds += (tm->tm_mon) * 2592000;        // 简化的月份处理
    seconds += (tm->tm_mday - 1) * 86400;     // 日数

    return seconds;
}

GmSSL依赖随机数

修改src/rand.c为如下伪随机数函数

/*
 *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the License); you may
 *  not use this file except in compliance with the License.
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 */



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>

#define RAND_MAX_BUF_SIZE 4096
static uint32_t seed = 123456789; // 初始种子

static uint32_t lcg_rand()
{
    seed = (1103515245 * seed + 12345) % (1U << 31);
    return seed;
}

int rand_bytes(uint8_t *buf, size_t len)
{
    if (!buf) {
        return -1;
    }
    if (len > RAND_MAX_BUF_SIZE) {
        return -1;
    }
    if (!len) {
        return 0;
    }

    for (size_t i = 0; i < len; i++) {
        buf[i] = lcg_rand() & 0xFF; // 取低8位
    }
    return 1;
}

编译通过,可以生成sm2test.elf没有链接错误了。


http://www.kler.cn/news/353344.html

相关文章:

  • wait和sleep的区别
  • 渗透测试 工具OneForAll
  • 听泉鉴宝在三个月前已布局商标注册!
  • 单片机输出方波
  • QT--文本框 QLineEdit、qtextedit
  • SpringBoot实现的智能个人理财平台
  • 利用Spring Boot构建大创项目资源规划平台
  • 医药追溯码是什么?
  • Java多线程--实现跑马小游戏
  • 《学习方法报》是什么级别的报纸?
  • 鸿蒙网络编程系列4-实现Smtp邮件发送客户端
  • 海康NVR管理平台EasyNVR多品牌NVR管理工具实现智能化视频管理介入现代化工厂
  • vue 音频播放控件封装
  • [已解决]DockerTarBuilder永久解决镜像docker拉取异常问题
  • Redis --- 第四讲 --- 常用数据结构 --- string类型
  • IntelliJ IDEA插件开发-开发环境搭建
  • 关于C语言中局部变量与全局变量——超详细解释篇
  • 元数据 -BWF 广播音频扩展 (bext)
  • redis--过期策略和内存淘汰策略
  • 灵当CRM index.php 任意文件上传漏洞复现
  • 我也要!赚钱是分层的:这就是你又累又穷的原因——早读(逆天打工人爬取热门微信文章解读)
  • 小猿口算炸鱼脚本
  • Python爬虫中的多线程技术:提升数据采集效率
  • 利用FnOS搭建虚拟云桌面,并搭建前端开发环境(一)
  • Free RTOS实时操作系统
  • 随机掉落的项目足迹:修改组件库默认样式