[SM4] refactor to support different concurrent blocks

This commit is contained in:
Emman 2022-01-06 11:48:38 +08:00
parent b25e199b57
commit acabed56bd
6 changed files with 78 additions and 51 deletions

View File

@ -45,22 +45,20 @@ func (x *cbc) CryptBlocks(dst, src []byte) {
}
end := len(src)
copy(x.tmp, src[end-BlockSize:end])
start := end - FourBlocksSize
var temp []byte = make([]byte, FourBlocksSize)
var src64 []byte = make([]byte, FourBlocksSize)
start := end - x.b.blocksSize
var temp []byte = make([]byte, x.b.blocksSize)
var batchSrc []byte = make([]byte, x.b.blocksSize)
for start > 0 {
encryptBlocksAsm(&x.b.dec[0], &temp[0], &src[start:end][0])
xor.XorBytes(dst[end-BlockSize:end], temp[FourBlocksSize-BlockSize:FourBlocksSize], src[end-2*BlockSize:end-BlockSize])
xor.XorBytes(dst[end-2*BlockSize:end-BlockSize], temp[FourBlocksSize-2*BlockSize:FourBlocksSize-BlockSize], src[end-3*BlockSize:end-2*BlockSize])
xor.XorBytes(dst[end-3*BlockSize:end-2*BlockSize], temp[FourBlocksSize-3*BlockSize:FourBlocksSize-2*BlockSize], src[end-4*BlockSize:end-3*BlockSize])
xor.XorBytes(dst[end-4*BlockSize:end-3*BlockSize], temp[:BlockSize], src[end-5*BlockSize:end-4*BlockSize])
for i := 0; i < x.b.batchBlocks; i++ {
xor.XorBytes(dst[end-(i+1)*BlockSize:end-i*BlockSize], temp[x.b.blocksSize-(i+1)*BlockSize:x.b.blocksSize-i*BlockSize], src[end-(i+2)*BlockSize:end-(i+1)*BlockSize])
}
end = start
start -= FourBlocksSize
start -= x.b.blocksSize
}
copy(src64, src[:end])
encryptBlocksAsm(&x.b.dec[0], &temp[0], &src[:end][0])
copy(batchSrc, src[:end])
encryptBlocksAsm(&x.b.dec[0], &temp[0], &batchSrc[0])
count := end / BlockSize
for i := count; i > 1; i-- {
xor.XorBytes(dst[end-BlockSize:end], temp[end-BlockSize:end], src[end-2*BlockSize:end-BlockSize])

View File

@ -25,13 +25,15 @@ func expandKeyAsm(key *byte, ck, enc, dec *uint32)
type sm4CipherAsm struct {
sm4Cipher
batchBlocks int
blocksSize int
}
func newCipher(key []byte) (cipher.Block, error) {
if !supportsAES {
return newCipherGeneric(key)
}
c := sm4CipherAsm{sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}}
c := sm4CipherAsm{sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}, 4, 4 * BlockSize}
expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0])
if supportsAES && supportsGFMUL {
return &sm4CipherGCM{c}, nil
@ -39,13 +41,9 @@ func newCipher(key []byte) (cipher.Block, error) {
return &c, nil
}
const FourBlocksSize = 64
const BatchBlocks = 4
func (c *sm4CipherAsm) BlockSize() int { return BlockSize }
func (c *sm4CipherAsm) Concurrency() int { return BatchBlocks }
func (c *sm4CipherAsm) Concurrency() int { return c.batchBlocks }
func (c *sm4CipherAsm) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
@ -61,13 +59,13 @@ func (c *sm4CipherAsm) Encrypt(dst, src []byte) {
}
func (c *sm4CipherAsm) EncryptBlocks(dst, src []byte) {
if len(src) < FourBlocksSize {
if len(src) < c.blocksSize {
panic("sm4: input not full blocks")
}
if len(dst) < FourBlocksSize {
if len(dst) < c.blocksSize {
panic("sm4: output not full blocks")
}
if subtle.InexactOverlap(dst[:FourBlocksSize], src[:FourBlocksSize]) {
if subtle.InexactOverlap(dst[:c.blocksSize], src[:c.blocksSize]) {
panic("sm4: invalid buffer overlap")
}
encryptBlocksAsm(&c.enc[0], &dst[0], &src[0])
@ -87,13 +85,13 @@ func (c *sm4CipherAsm) Decrypt(dst, src []byte) {
}
func (c *sm4CipherAsm) DecryptBlocks(dst, src []byte) {
if len(src) < FourBlocksSize {
if len(src) < c.blocksSize {
panic("sm4: input not full blocks")
}
if len(dst) < FourBlocksSize {
if len(dst) < c.blocksSize {
panic("sm4: output not full blocks")
}
if subtle.InexactOverlap(dst[:FourBlocksSize], src[:FourBlocksSize]) {
if subtle.InexactOverlap(dst[:c.blocksSize], src[:c.blocksSize]) {
panic("sm4: invalid buffer overlap")
}
encryptBlocksAsm(&c.dec[0], &dst[0], &src[0])

View File

@ -34,14 +34,14 @@ func (c *sm4CipherAsm) NewCTR(iv []byte) cipher.Stream {
}
s := &ctr{
b: c,
ctr: make([]byte, 4*len(iv)),
ctr: make([]byte, c.batchBlocks*len(iv)),
out: make([]byte, 0, bufSize),
outUsed: 0,
}
copy(s.ctr, iv)
s.genCtr(BlockSize)
s.genCtr(2 * BlockSize)
s.genCtr(3 * BlockSize)
for i := 1; i < c.batchBlocks; i++ {
s.genCtr(i * BlockSize)
}
return s
}
@ -66,15 +66,14 @@ func (x *ctr) refill() {
remain := len(x.out) - x.outUsed
copy(x.out, x.out[x.outUsed:])
x.out = x.out[:cap(x.out)]
for remain <= len(x.out)-FourBlocksSize {
for remain <= len(x.out)-x.b.blocksSize {
encryptBlocksAsm(&x.b.enc[0], &x.out[remain:][0], &x.ctr[0])
remain += FourBlocksSize
remain += x.b.blocksSize
// Increment counter
x.genCtr(0)
x.genCtr(BlockSize)
x.genCtr(2 * BlockSize)
x.genCtr(3 * BlockSize)
for i := 0; i < x.b.batchBlocks; i++ {
x.genCtr(i * BlockSize)
}
}
x.out = x.out[:remain]
x.outUsed = 0

View File

@ -265,32 +265,27 @@ func gcmInc32(counterBlock *[16]byte) {
// counterCrypt crypts in to out using g.cipher in counter mode.
func (g *gcm) counterCrypt(out, in []byte, counter *[gcmBlockSize]byte) {
var mask [FourBlocksSize]byte
var couters [FourBlocksSize]byte
mask := make([]byte, g.cipher.blocksSize)
counters := make([]byte, g.cipher.blocksSize)
for len(in) >= FourBlocksSize {
copy(couters[:], counter[:])
gcmInc32(counter)
copy(couters[gcmBlockSize:], counter[:])
gcmInc32(counter)
copy(couters[2*gcmBlockSize:], counter[:])
gcmInc32(counter)
copy(couters[3*gcmBlockSize:], counter[:])
encryptBlocksAsm(&g.cipher.enc[0], &mask[0], &couters[0])
for len(in) >= g.cipher.blocksSize {
for i := 0; i < g.cipher.batchBlocks; i++ {
copy(counters[i*gcmBlockSize:(i+1)*gcmBlockSize], counter[:])
gcmInc32(counter)
}
encryptBlocksAsm(&g.cipher.enc[0], &mask[0], &counters[0])
xor.XorWords(out, in, mask[:])
out = out[FourBlocksSize:]
in = in[FourBlocksSize:]
out = out[g.cipher.blocksSize:]
in = in[g.cipher.blocksSize:]
}
if len(in) > 0 {
blocks := (len(in) + gcmBlockSize - 1) / gcmBlockSize
for i := 0; i < blocks; i++ {
copy(couters[i*gcmBlockSize:], counter[:])
copy(counters[i*gcmBlockSize:], counter[:])
gcmInc32(counter)
}
encryptBlocksAsm(&g.cipher.enc[0], &mask[0], &couters[0])
encryptBlocksAsm(&g.cipher.enc[0], &mask[0], &counters[0])
xor.XorBytes(out, in, mask[:blocks*gcmBlockSize])
}
}

View File

@ -10,7 +10,7 @@ import (
func genPrecomputeTable() *gcmAsm {
key := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
c := sm4CipherAsm{sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}}
c := sm4CipherAsm{sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}, 4, 64}
expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0])
c1 := &sm4CipherGCM{c}
g := &gcmAsm{}

View File

@ -75,6 +75,37 @@ var cbcSM4Tests = []struct {
0x34, 0x6e, 0x9d, 0xad, 0xe1, 0x8a, 0xf4, 0xa1, 0x83, 0x69, 0x57, 0xb9, 0x37, 0x26, 0x7e, 0x03,
},
},
{
"Five blocks",
[]byte("0123456789ABCDEF"),
[]byte("0123456789ABCDEF"),
[]byte("Hello World Hello World Hello World Hello World Hello World Hello World Hello Wo"),
[]byte{
0xd3, 0x1e, 0x36, 0x83, 0xe4, 0xfc, 0x9b, 0x51, 0x6a, 0x2c, 0x0f, 0x98, 0x36, 0x76, 0xa9, 0xeb,
0x1f, 0xdc, 0xc3, 0x2a, 0xf3, 0x84, 0x08, 0x97, 0x81, 0x57, 0xa2, 0x06, 0x5d, 0xe3, 0x4c, 0x6a,
0xe0, 0x02, 0xd6, 0xe4, 0xf5, 0x66, 0x87, 0xc4, 0xcc, 0x54, 0x1d, 0x1f, 0x1c, 0xc4, 0x2f, 0xe6,
0xe5, 0x1d, 0xea, 0x52, 0xb8, 0x0c, 0xc8, 0xbe, 0xae, 0xcc, 0x44, 0xa8, 0x51, 0x81, 0x08, 0x60,
0xb6, 0x09, 0x7b, 0xb8, 0x7e, 0xdb, 0x53, 0x4b, 0xea, 0x2a, 0xc6, 0xa1, 0xe5, 0xa0, 0x2a, 0xe9,
0x62, 0xb5, 0xe7, 0x50, 0x44, 0xea, 0x24, 0xcc, 0x9b, 0x5e, 0x07, 0x48, 0x04, 0x89, 0xa2, 0x74,
},
},
{
"9 blocks",
[]byte("0123456789ABCDEF"),
[]byte("0123456789ABCDEF"),
[]byte("Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World"),
[]byte{
0xd3, 0x1e, 0x36, 0x83, 0xe4, 0xfc, 0x9b, 0x51, 0x6a, 0x2c, 0x0f, 0x98, 0x36, 0x76, 0xa9, 0xeb,
0x1f, 0xdc, 0xc3, 0x2a, 0xf3, 0x84, 0x08, 0x97, 0x81, 0x57, 0xa2, 0x06, 0x5d, 0xe3, 0x4c, 0x6a,
0xe0, 0x02, 0xd6, 0xe4, 0xf5, 0x66, 0x87, 0xc4, 0xcc, 0x54, 0x1d, 0x1f, 0x1c, 0xc4, 0x2f, 0xe6,
0xe5, 0x1d, 0xea, 0x52, 0xb8, 0x0c, 0xc8, 0xbe, 0xae, 0xcc, 0x44, 0xa8, 0x51, 0x81, 0x08, 0x60,
0xb6, 0x09, 0x7b, 0xb8, 0x7e, 0xdb, 0x53, 0x4b, 0xea, 0x2a, 0xc6, 0xa1, 0xe5, 0xa0, 0x2a, 0xe9,
0x22, 0x65, 0x5b, 0xa3, 0xb9, 0xcc, 0x63, 0x92, 0x16, 0x0e, 0x2f, 0xf4, 0x3b, 0x93, 0x06, 0x82,
0xb3, 0x8c, 0x26, 0x2e, 0x06, 0x51, 0x34, 0x2c, 0xe4, 0x3d, 0xd0, 0xc7, 0x2b, 0x8f, 0x31, 0x15,
0x30, 0xa8, 0x96, 0x1c, 0xbc, 0x8e, 0xf7, 0x4f, 0x6b, 0x69, 0x9d, 0xc9, 0x40, 0x89, 0xd7, 0xe8,
0xf7, 0x90, 0x47, 0x74, 0xaf, 0x40, 0xfd, 0x72, 0xc6, 0x17, 0xeb, 0xc0, 0x8b, 0x01, 0x71, 0x5c,
},
},
{
"A.1",
[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
@ -103,6 +134,12 @@ func TestCBCEncrypterSM4(t *testing.T) {
encrypter.CryptBlocks(data, data)
if !bytes.Equal(test.out, data) {
t.Errorf("%s: CBCEncrypter\nhave %s\nwant %x", test.name, hex.EncodeToString(data), test.out)
for i := 0; i < len(data); i++ {
fmt.Printf("0x%02x, ", data[i])
if (i+1)%16 == 0 {
fmt.Println()
}
}
}
}
}