从密码 aes 256 GCM Golang 中提取标签

Extract tag from cipher aes 256 GCM Golang

我在Ruby有加解密,尝试用Go改写。我一步一步地尝试,所以从 ruby 中的加密开始,然后尝试在 go 中解密,它是有效的。但是当我尝试在 Go 中编写 encryption 并在 ruby 中解密时。我在尝试提取标签时卡住了,我解释了我需要提取授权标签的原因

加密 ruby

plaintext = "Foo bar"
cipher = OpenSSL::Cipher.new('aes-256-gcm')
cipher.encrypt
cipher.iv = iv # string 12 len
cipher.key = key # string 32 len
cipher.auth_data = # string 12 len
cipherText = cipher.update(JSON.generate({value: plaintext})) + cipher.final
authTag = cipher.auth_tag
hexString = (iv + cipherText + authTag).unpack('H*').first

我尝试连接一个初始向量、一个密文和身份验证标签,所以在解密之前我可以提取它们,尤其是身份验证标签,因为我需要在调用 Cipher#final 之前设置它 Ruby

auth_tag

The tag must be set after calling Cipher#decrypt, Cipher#key= and Cipher#iv=, but before calling Cipher#final. After all decryption is performed, the tag is verified automatically in the call to Cipher#final

这里是golang中的函数加密

ciphertext := aesgcm.Seal(nil, []byte(iv), []byte(plaintext), []byte(authData))
src := iv + string(ciphertext) // + try to add authentication tag here
fmt.Printf(hex.EncodeToString([]byte(src)))

如何提取认证标签并与iv和密文连接,这样我就可以用ruby

中的解密函数解密了
raw_data = [hexString].pack('H*')
cipher_text = raw_data.slice(12, raw_data.length - 28)
auth_tag = raw_data.slice(raw_data.length - 16, 16)

cipher = OpenSSL::Cipher.new('aes-256-gcm').decrypt
cipher.iv = iv # string 12 len
cipher.key = key # string 32 len
cipher.auth_data = # string 12 len
cipher.auth_tag = auth_tag
JSON.parse(cipher.update(cipher_text) + cipher.final)

我希望能够在 Go 中进行加密,并尝试在 Ruby 中进行解密。

您希望您的加密流程是这样的:

func encrypt(in []byte, key []byte) (out []byte, err error) {

    c, err := aes.NewCipher(key)
    if err != nil {
        return
    }

    gcm, err := cipher.NewGCM(c)
    if err != nil {
        return
    }

    nonce := make([]byte, gcm.NonceSize())
    if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
        return
    }

    out = gcm.Seal(nonce, nonce, in, nil) // include the nonce in the preable of 'out'
    return
}

根据 aes.NewCipher 文档,您的输入 key 长度应为 16、24 或 32 字节。

来自上述函数的加密 out 字节将包含 nonce 前缀(长度为 16、24 或 32 字节)——因此它可以在解密阶段轻松提取,如下所示:

// `in` here is ciphertext
nonce, ciphertext := in[:ns], in[ns:]

其中 ns 的计算方式如下:

c, err := aes.NewCipher(key)
if err != nil {
    return
}

gcm, err := cipher.NewGCM(c)
if err != nil {
    return
}

ns := gcm.NonceSize()
if len(in) < ns {
    err = fmt.Errorf("missing nonce - input shorter than %d bytes", ns)
    return
}

编辑:

如果您在 go 端使用默认密码设置(见上文)进行加密:

gcm, err := cipher.NewGCM(c)

the source 开始,标签字节大小将为 16

注意:如果使用 cipher.NewGCMWithTagSize - 那么大小将明显不同(基本上在 1216 字节之间)

所以让我们假设标签大小为 16,有了这些知识,并且知道完整的有效负载排列是:

IV/nonce + raw_ciphertext + auth_tag

解密的Ruby端的auth_tag,是payload的最后16字节; raw_ciphertext 是 IV/nonce 之后的所有字节,直到 auth_tag 开始。

aesgcm.Seal自动在密文末尾附加GCM标签。您可以在 source:

中看到它
    var tag [gcmTagSize]byte
    g.auth(tag[:], out[:len(plaintext)], data, &tagMask)
    copy(out[len(plaintext):], tag[:])                   // <---------------- here

大功告成,不需要其他任何东西。 gcm.Seal 已经 returns 末尾附加了 auth 标签的密文。

同样,您不需要提取 gcm.Open 的授权标签,它会自动完成,too:

    tag := ciphertext[len(ciphertext)-g.tagSize:]        // <---------------- here
    ciphertext = ciphertext[:len(ciphertext)-g.tagSize]

所以你在解密过程中所要做的就是提取 IV (nonce) 并将其余部分作为密文传递。