Golang实现MIPSLE架构的Crypto AES GCM汇编代码支持

Golang实现MIPSLE架构的Crypto AES GCM汇编代码支持 你好,

简而言之,我注意到在 go/src/crypto/aes 目录下,针对 x86、arm64、ppc 等平台,有汇编语言例程(如 asm_amd64.s)支持 crypto aes/gcm 密码,这在使用流量密集型 VPN 应用时,可以在这些平台上提供良好的加密速度。GO SDK 是否支持针对 MIPS 处理器的基于汇编代码的 aes/gcm 例程?(例如 asm_mipsle.s 或类似的)以加速 aes/gcm 加密速度,从而可用于 MIPSLE/MT7621 上的高流量安全应用。

此外,GO SDK 是否支持 crypto dev(dev/crypto)的绑定/插件或 AF_ALG 套接字支持,以便与 Linux 内核通信,以防我们希望通过 skcipher 或其他接口使用 SoC 上的硬件引擎。

谢谢, Sai


更多关于Golang实现MIPSLE架构的Crypto AES GCM汇编代码支持的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于Golang实现MIPSLE架构的Crypto AES GCM汇编代码支持的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Go标准库的crypto/aes包中,目前确实没有针对MIPSLE架构的AES-GCM汇编实现。当前支持的平台包括amd64、arm64、ppc64le和s390x,这些平台都有对应的汇编优化文件(如asm_arm64.sasm_s390x.s等)。

对于MIPSLE架构,您可以通过以下方式验证现有支持:

// 检查当前平台是否支持AES硬件加速
package main

import (
    "crypto/aes"
    "fmt"
    "runtime"
)

func main() {
    c, err := aes.NewCipher(make([]byte, 32))
    if err != nil {
        panic(err)
    }
    
    // 检查是否使用汇编实现
    fmt.Printf("GOARCH: %s\n", runtime.GOARCH)
    fmt.Printf("BlockSize: %d\n", c.BlockSize())
    
    // 实际测试加密速度
    key := make([]byte, 32)
    plaintext := make([]byte, 1024)
    ciphertext := make([]byte, 1024)
    
    cipher, _ := aes.NewCipher(key)
    
    // 执行加密测试
    for i := 0; i < 1000; i++ {
        cipher.Encrypt(ciphertext, plaintext)
    }
}

关于Linux内核的AF_ALG套接字支持,Go标准库目前没有直接提供绑定。但您可以通过系统调用实现:

// 使用AF_ALG套接字进行硬件加速加密的示例
package main

import (
    "fmt"
    "golang.org/x/sys/unix"
    "syscall"
)

func main() {
    // 创建AF_ALG套接字
    sock, err := unix.Socket(unix.AF_ALG, unix.SOCK_SEQPACKET, 0)
    if err != nil {
        panic(err)
    }
    defer unix.Close(sock)
    
    // 设置算法类型和名称
    addr := &unix.SockaddrALG{
        Type: "skcipher",
        Name: "cbc(aes)",
    }
    
    // 绑定套接字
    err = unix.Bind(sock, addr)
    if err != nil {
        panic(err)
    }
    
    // 设置密钥
    key := make([]byte, 32)
    _, err = unix.SetsockoptString(sock, unix.SOL_ALG, unix.ALG_SET_KEY, string(key))
    if err != nil {
        panic(err)
    }
    
    // 接受连接(创建加密操作句柄)
    opfd, err := unix.Accept(sock)
    if err != nil {
        panic(err)
    }
    defer unix.Close(opfd)
    
    // 准备数据
    iv := make([]byte, 16)
    plaintext := make([]byte, 1024)
    ciphertext := make([]byte, 1024)
    
    // 设置加密参数
    msg := unix.Msghdr{}
    cmsg := unix.Cmsghdr{
        Level: unix.SOL_ALG,
        Type:  unix.ALG_SET_IV,
        Len:   uint64(unix.CmsgSpace(len(iv))),
    }
    
    // 执行加密操作
    _, err = unix.Sendmsg(opfd, plaintext, syscall.UnixRights(int(cmsg.Len)), nil, 0)
    if err != nil {
        panic(err)
    }
    
    // 读取加密结果
    n, err := unix.Read(opfd, ciphertext)
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Encrypted %d bytes\n", n)
}

对于MIPSLE平台,如果需要AES-GCM性能优化,可以考虑以下方案:

  1. 使用C语言编写优化实现并通过cgo调用:
// #cgo CFLAGS: -O3 -march=mips32r2
// #include "aes_mips.h"
import "C"

func aesEncryptMIPS(key, plaintext []byte) []byte {
    ckey := (*C.uchar)(&key[0])
    ctext := (*C.uchar)(&plaintext[0])
    clen := C.int(len(plaintext))
    
    result := make([]byte, len(plaintext))
    cres := (*C.uchar)(&result[0])
    
    C.aes_encrypt_mips(ckey, ctext, cres, clen)
    return result
}
  1. 编写MIPS汇编实现并集成到Go中:
// aes_mipsle.s
TEXT ·aesEncryptAsm(SB),NOSPLIT,$0
    MOVW    key+0(FP), R1
    MOVW    src+4(FP), R2
    MOVW    dst+8(FP), R3
    MOVW    len+12(FP), R4
    
    // MIPS AES汇编实现
    // ... 具体汇编代码
    RET
  1. 使用第三方优化库如openssl:
// 通过cgo调用openssl
// #cgo LDFLAGS: -lcrypto
// #include <openssl/evp.h>
import "C"

func aesGCMOpenSSL(key, nonce, ciphertext, additional []byte) ([]byte, error) {
    ctx := C.EVP_CIPHER_CTX_new()
    defer C.EVP_CIPHER_CTX_free(ctx)
    
    cipher := C.EVP_aes_256_gcm()
    keyPtr := (*C.uchar)(&key[0])
    
    C.EVP_DecryptInit_ex(ctx, cipher, nil, keyPtr, (*C.uchar)(&nonce[0]))
    
    out := make([]byte, len(ciphertext))
    outLen := C.int(0)
    
    C.EVP_DecryptUpdate(ctx, (*C.uchar)(&out[0]), &outLen,
        (*C.uchar)(&ciphertext[0]), C.int(len(ciphertext)))
    
    return out[:outLen], nil
}

当前Go版本(1.21)在MIPSLE架构上使用纯Go实现的AES,性能可能无法满足高流量VPN应用的需求。建议通过上述方法实现硬件加速或汇编优化。

回到顶部