- 由于mbedtls主要支持TCP的TLS传输实现,由ARM公司加持,目前这个库的代码质量非常高,也可以用在其他加密解密领域,比如AES、DES、chacha20等加密,当然和libsodium实现互补,有些实现也必须用libsodium更好一些,不过这两个库的代码质量都很高。
- 示例代码展现了AES-256-ECB的加密如何使用,由于库里面有很多加密支持,可以参考mbedtls github原始代码的cipher_wrap.c实现,这里面实现了统一API封装,方便使用,只需要初始化时给出对应的类型参数,即可获取相应handle方便使用。
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "mbedtls/cipher.h"
#include "mbedtls/platform.h"
uint8_t key[16] =
{
0x06, 0xa9, 0x21, 0x40, 0x36, 0xb8, 0xa1, 0x5b,
0x51, 0x2e, 0x03, 0xd5, 0x34, 0x12, 0x00, 0x06
};
uint8_t iv[16] =
{
0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30,
0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41
};
static void dump_buf(char *info, uint8_t *buf