Livt packages
Livt.Crypto
Version 1.0.5
Fixed-size cryptographic components for Livt, including AES, ChaCha20, SHA, MACs, and key derivation.
Published files (69)
tests/aead/Aes256GcmTest.lvt
namespace Livt.Crypto.Tests.Aead
using Livt.Crypto.Aead
// AES-256 GCM test suite.
// Reference vectors:
// key = feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308
// iv = cafebabefacedbaddecaf888
// pt = d9313225f88406e5a55909c5aff5269a
// CT = 522dc1f099567d07f47f37a32a84427d
// Tag = 7ea353da7e9241a1d90d693a4954186b
@Test
component Aes256GcmTest
{
gcm: Aes256Gcm
new()
{
this.gcm = new Aes256Gcm()
}
@Test
fn TestEncryptNoAAD()
{
var key: byte[32] = [
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08]
var nonce: byte[12] = [
0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
0xde, 0xca, 0xf8, 0x88]
var pt: byte[16] = [
0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a]
this.gcm.SetKey(key)
this.gcm.SetNonce(nonce)
this.gcm.LoadBlock(pt)
this.gcm.EncryptBlock()
this.gcm.FinalizeTag(0, 16)
var ct: byte[16] = this.gcm.GetBlock()
assert ct[0] == 0x52
assert ct[1] == 0x2d
assert ct[2] == 0xc1
assert ct[3] == 0xf0
assert ct[4] == 0x99
assert ct[5] == 0x56
assert ct[6] == 0x7d
assert ct[7] == 0x07
assert ct[8] == 0xf4
assert ct[9] == 0x7f
assert ct[10] == 0x37
assert ct[11] == 0xa3
assert ct[12] == 0x2a
assert ct[13] == 0x84
assert ct[14] == 0x42
assert ct[15] == 0x7d
var tag: byte[16] = this.gcm.GetTag()
assert tag[0] == 0x7e
assert tag[1] == 0xa3
assert tag[2] == 0x53
assert tag[3] == 0xda
assert tag[4] == 0x7e
assert tag[5] == 0x92
assert tag[6] == 0x41
assert tag[7] == 0xa1
assert tag[8] == 0xd9
assert tag[9] == 0x0d
assert tag[10] == 0x69
assert tag[11] == 0x3a
assert tag[12] == 0x49
assert tag[13] == 0x54
assert tag[14] == 0x18
assert tag[15] == 0x6b
var ok: int = this.gcm.VerifyTag(tag)
assert ok == 1
tag[0] = tag[0] ^ 0x01
var bad: int = this.gcm.VerifyTag(tag)
assert bad == 0
}
@Test
fn TestDecryptRoundtrip()
{
var key: byte[32] = [
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08]
var nonce: byte[12] = [
0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
0xde, 0xca, 0xf8, 0x88]
var pt: byte[16] = [
0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a]
this.gcm.SetKey(key)
this.gcm.SetNonce(nonce)
this.gcm.LoadBlock(pt)
this.gcm.EncryptBlock()
var ct: byte[16] = this.gcm.GetBlock()
this.gcm.SetNonce(nonce)
this.gcm.LoadBlock(ct)
this.gcm.DecryptBlock()
var recovered: byte[16] = this.gcm.GetBlock()
assert recovered[0] == pt[0]
assert recovered[1] == pt[1]
assert recovered[2] == pt[2]
assert recovered[3] == pt[3]
assert recovered[4] == pt[4]
assert recovered[5] == pt[5]
assert recovered[6] == pt[6]
assert recovered[7] == pt[7]
assert recovered[8] == pt[8]
assert recovered[9] == pt[9]
assert recovered[10] == pt[10]
assert recovered[11] == pt[11]
assert recovered[12] == pt[12]
assert recovered[13] == pt[13]
assert recovered[14] == pt[14]
assert recovered[15] == pt[15]
}
}