From 000aaf9cf8eb76382b29858ea48ff226dfe97fc1 Mon Sep 17 00:00:00 2001 From: deatil <2217957370@qq.com> Date: Sun, 17 Mar 2024 10:46:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cipher/hight/hight.go | 155 ++++++++++------------------- cipher/hight/{vars.go => utils.go} | 6 +- 2 files changed, 54 insertions(+), 107 deletions(-) rename cipher/hight/{vars.go => utils.go} (98%) diff --git a/cipher/hight/hight.go b/cipher/hight/hight.go index 65b9cea4..39045cb0 100644 --- a/cipher/hight/hight.go +++ b/cipher/hight/hight.go @@ -35,11 +35,11 @@ func NewCipher(key []byte) (cipher.Block, error) { return c, nil } -func (s *hightCipher) BlockSize() int { +func (this *hightCipher) BlockSize() int { return BlockSize } -func (s *hightCipher) Encrypt(dst, src []byte) { +func (this *hightCipher) Encrypt(dst, src []byte) { if len(src) < BlockSize { panic("cryptobin/hight: input not full block") } @@ -52,10 +52,10 @@ func (s *hightCipher) Encrypt(dst, src []byte) { panic("cryptobin/hight: invalid buffer overlap") } - s.encrypt(dst, src) + this.encrypt(dst, src) } -func (s *hightCipher) Decrypt(dst, src []byte) { +func (this *hightCipher) Decrypt(dst, src []byte) { if len(src) < BlockSize { panic("cryptobin/hight: input not full block") } @@ -68,145 +68,92 @@ func (s *hightCipher) Decrypt(dst, src []byte) { panic("cryptobin/hight: invalid buffer overlap") } - s.decrypt(dst, src) + this.decrypt(dst, src) } -func (s *hightCipher) encryptStep(XX []byte, k, i0, i1, i2, i3, i4, i5, i6, i7 int) { - XX[i0] = (XX[i0] ^ (hight_F0[XX[i1]] + s.roundKey[4*k+3])) - XX[i2] = (XX[i2] + (hight_F1[XX[i3]] ^ s.roundKey[4*k+2])) - XX[i4] = (XX[i4] ^ (hight_F0[XX[i5]] + s.roundKey[4*k+1])) - XX[i6] = (XX[i6] + (hight_F1[XX[i7]] ^ s.roundKey[4*k+0])) +func (this *hightCipher) encryptStep(XX []byte, k, i0, i1, i2, i3, i4, i5, i6, i7 int) { + XX[i0] = (XX[i0] ^ (f0[XX[i1]] + this.roundKey[4*k+3])) + XX[i2] = (XX[i2] + (f1[XX[i3]] ^ this.roundKey[4*k+2])) + XX[i4] = (XX[i4] ^ (f0[XX[i5]] + this.roundKey[4*k+1])) + XX[i6] = (XX[i6] + (f1[XX[i7]] ^ this.roundKey[4*k+0])) } -func (s *hightCipher) encrypt(dst, src []byte) { +func (this *hightCipher) encrypt(dst, src []byte) { XX := []byte{ - src[0] + s.roundKey[0], + src[0] + this.roundKey[0], src[1], - src[2] ^ s.roundKey[1], + src[2] ^ this.roundKey[1], src[3], - src[4] + s.roundKey[2], + src[4] + this.roundKey[2], src[5], - src[6] ^ s.roundKey[3], + src[6] ^ this.roundKey[3], src[7], } - s.encryptStep(XX, 2, 7, 6, 5, 4, 3, 2, 1, 0) - s.encryptStep(XX, 3, 6, 5, 4, 3, 2, 1, 0, 7) - s.encryptStep(XX, 4, 5, 4, 3, 2, 1, 0, 7, 6) - s.encryptStep(XX, 5, 4, 3, 2, 1, 0, 7, 6, 5) - s.encryptStep(XX, 6, 3, 2, 1, 0, 7, 6, 5, 4) - s.encryptStep(XX, 7, 2, 1, 0, 7, 6, 5, 4, 3) - s.encryptStep(XX, 8, 1, 0, 7, 6, 5, 4, 3, 2) - s.encryptStep(XX, 9, 0, 7, 6, 5, 4, 3, 2, 1) - s.encryptStep(XX, 10, 7, 6, 5, 4, 3, 2, 1, 0) - s.encryptStep(XX, 11, 6, 5, 4, 3, 2, 1, 0, 7) - s.encryptStep(XX, 12, 5, 4, 3, 2, 1, 0, 7, 6) - s.encryptStep(XX, 13, 4, 3, 2, 1, 0, 7, 6, 5) - s.encryptStep(XX, 14, 3, 2, 1, 0, 7, 6, 5, 4) - s.encryptStep(XX, 15, 2, 1, 0, 7, 6, 5, 4, 3) - s.encryptStep(XX, 16, 1, 0, 7, 6, 5, 4, 3, 2) - s.encryptStep(XX, 17, 0, 7, 6, 5, 4, 3, 2, 1) - s.encryptStep(XX, 18, 7, 6, 5, 4, 3, 2, 1, 0) - s.encryptStep(XX, 19, 6, 5, 4, 3, 2, 1, 0, 7) - s.encryptStep(XX, 20, 5, 4, 3, 2, 1, 0, 7, 6) - s.encryptStep(XX, 21, 4, 3, 2, 1, 0, 7, 6, 5) - s.encryptStep(XX, 22, 3, 2, 1, 0, 7, 6, 5, 4) - s.encryptStep(XX, 23, 2, 1, 0, 7, 6, 5, 4, 3) - s.encryptStep(XX, 24, 1, 0, 7, 6, 5, 4, 3, 2) - s.encryptStep(XX, 25, 0, 7, 6, 5, 4, 3, 2, 1) - s.encryptStep(XX, 26, 7, 6, 5, 4, 3, 2, 1, 0) - s.encryptStep(XX, 27, 6, 5, 4, 3, 2, 1, 0, 7) - s.encryptStep(XX, 28, 5, 4, 3, 2, 1, 0, 7, 6) - s.encryptStep(XX, 29, 4, 3, 2, 1, 0, 7, 6, 5) - s.encryptStep(XX, 30, 3, 2, 1, 0, 7, 6, 5, 4) - s.encryptStep(XX, 31, 2, 1, 0, 7, 6, 5, 4, 3) - s.encryptStep(XX, 32, 1, 0, 7, 6, 5, 4, 3, 2) - s.encryptStep(XX, 33, 0, 7, 6, 5, 4, 3, 2, 1) - - dst[0] = XX[1] + s.roundKey[4] + var j = 0; + for i := 2; i <= 33; i++ { + this.encryptStep(XX, i, 7-j%8, 7-(j+1)%8, 7-(j+2)%8, 7-(j+3)%8, 7-(j+4)%8, 7-(j+5)%8, 7-(j+6)%8, 7-(j+7)%8) + j++ + } + + dst[0] = XX[1] + this.roundKey[4] dst[1] = XX[2] - dst[2] = XX[3] ^ s.roundKey[5] + dst[2] = XX[3] ^ this.roundKey[5] dst[3] = XX[4] - dst[4] = XX[5] + s.roundKey[6] + dst[4] = XX[5] + this.roundKey[6] dst[5] = XX[6] - dst[6] = XX[7] ^ s.roundKey[7] + dst[6] = XX[7] ^ this.roundKey[7] dst[7] = XX[0] } -func (s *hightCipher) decryptStep(XX []byte, k, i0, i1, i2, i3, i4, i5, i6, i7 int) { - XX[i1] = (XX[i1] - (hight_F1[XX[i2]] ^ s.roundKey[4*k+2])) - XX[i3] = (XX[i3] ^ (hight_F0[XX[i4]] + s.roundKey[4*k+1])) - XX[i5] = (XX[i5] - (hight_F1[XX[i6]] ^ s.roundKey[4*k+0])) - XX[i7] = (XX[i7] ^ (hight_F0[XX[i0]] + s.roundKey[4*k+3])) +func (this *hightCipher) decryptStep(XX []byte, k, i0, i1, i2, i3, i4, i5, i6, i7 int) { + XX[i1] = (XX[i1] - (f1[XX[i2]] ^ this.roundKey[4*k+2])) + XX[i3] = (XX[i3] ^ (f0[XX[i4]] + this.roundKey[4*k+1])) + XX[i5] = (XX[i5] - (f1[XX[i6]] ^ this.roundKey[4*k+0])) + XX[i7] = (XX[i7] ^ (f0[XX[i0]] + this.roundKey[4*k+3])) } -func (s *hightCipher) decrypt(dst, src []byte) { +func (this *hightCipher) decrypt(dst, src []byte) { XX := []byte{ src[7], - src[0] - s.roundKey[4], + src[0] - this.roundKey[4], src[1], - src[2] ^ s.roundKey[5], + src[2] ^ this.roundKey[5], src[3], - src[4] - s.roundKey[6], + src[4] - this.roundKey[6], src[5], - src[6] ^ s.roundKey[7], + src[6] ^ this.roundKey[7], } - s.decryptStep(XX, 33, 7, 6, 5, 4, 3, 2, 1, 0) - s.decryptStep(XX, 32, 0, 7, 6, 5, 4, 3, 2, 1) - s.decryptStep(XX, 31, 1, 0, 7, 6, 5, 4, 3, 2) - s.decryptStep(XX, 30, 2, 1, 0, 7, 6, 5, 4, 3) - s.decryptStep(XX, 29, 3, 2, 1, 0, 7, 6, 5, 4) - s.decryptStep(XX, 28, 4, 3, 2, 1, 0, 7, 6, 5) - s.decryptStep(XX, 27, 5, 4, 3, 2, 1, 0, 7, 6) - s.decryptStep(XX, 26, 6, 5, 4, 3, 2, 1, 0, 7) - s.decryptStep(XX, 25, 7, 6, 5, 4, 3, 2, 1, 0) - s.decryptStep(XX, 24, 0, 7, 6, 5, 4, 3, 2, 1) - s.decryptStep(XX, 23, 1, 0, 7, 6, 5, 4, 3, 2) - s.decryptStep(XX, 22, 2, 1, 0, 7, 6, 5, 4, 3) - s.decryptStep(XX, 21, 3, 2, 1, 0, 7, 6, 5, 4) - s.decryptStep(XX, 20, 4, 3, 2, 1, 0, 7, 6, 5) - s.decryptStep(XX, 19, 5, 4, 3, 2, 1, 0, 7, 6) - s.decryptStep(XX, 18, 6, 5, 4, 3, 2, 1, 0, 7) - s.decryptStep(XX, 17, 7, 6, 5, 4, 3, 2, 1, 0) - s.decryptStep(XX, 16, 0, 7, 6, 5, 4, 3, 2, 1) - s.decryptStep(XX, 15, 1, 0, 7, 6, 5, 4, 3, 2) - s.decryptStep(XX, 14, 2, 1, 0, 7, 6, 5, 4, 3) - s.decryptStep(XX, 13, 3, 2, 1, 0, 7, 6, 5, 4) - s.decryptStep(XX, 12, 4, 3, 2, 1, 0, 7, 6, 5) - s.decryptStep(XX, 11, 5, 4, 3, 2, 1, 0, 7, 6) - s.decryptStep(XX, 10, 6, 5, 4, 3, 2, 1, 0, 7) - s.decryptStep(XX, 9, 7, 6, 5, 4, 3, 2, 1, 0) - s.decryptStep(XX, 8, 0, 7, 6, 5, 4, 3, 2, 1) - s.decryptStep(XX, 7, 1, 0, 7, 6, 5, 4, 3, 2) - s.decryptStep(XX, 6, 2, 1, 0, 7, 6, 5, 4, 3) - s.decryptStep(XX, 5, 3, 2, 1, 0, 7, 6, 5, 4) - s.decryptStep(XX, 4, 4, 3, 2, 1, 0, 7, 6, 5) - s.decryptStep(XX, 3, 5, 4, 3, 2, 1, 0, 7, 6) - s.decryptStep(XX, 2, 6, 5, 4, 3, 2, 1, 0, 7) - - dst[0] = XX[0] - s.roundKey[0] + var j = 0; + for i := 33; i >= 2; i-- { + this.decryptStep(XX, i, (7+j)%8, (6+j)%8, (5+j)%8, (4+j)%8, (3+j)%8, (2+j)%8, (1+j)%8, j%8) + j++ + } + + dst[0] = XX[0] - this.roundKey[0] dst[1] = XX[1] - dst[2] = XX[2] ^ s.roundKey[1] + dst[2] = XX[2] ^ this.roundKey[1] dst[3] = XX[3] - dst[4] = XX[4] - s.roundKey[2] + dst[4] = XX[4] - this.roundKey[2] dst[5] = XX[5] - dst[6] = XX[6] ^ s.roundKey[3] + dst[6] = XX[6] ^ this.roundKey[3] dst[7] = XX[7] } -func (s *hightCipher) expandKey(key []byte) { +func (this *hightCipher) expandKey(key []byte) { for i := 0; i < 4; i++ { - s.roundKey[i+0] = key[i+12] - s.roundKey[i+4] = key[i+0] + this.roundKey[i+0] = key[i+12] + this.roundKey[i+4] = key[i+0] } for i := 0; i < 8; i++ { for k := 0; k < 8; k++ { - s.roundKey[8+16*i+k+0] = key[((k-i)&7)+0] + delta[16*i+k+0] + this.roundKey[8+16*i+k+0] = key[((k-i)&7)+0] + delta[16*i+k+0] } + for k := 0; k < 8; k++ { - s.roundKey[8+16*i+k+8] = key[((k-i)&7)+8] + delta[16*i+k+8] + this.roundKey[8+16*i+k+8] = key[((k-i)&7)+8] + delta[16*i+k+8] } } } diff --git a/cipher/hight/vars.go b/cipher/hight/utils.go similarity index 98% rename from cipher/hight/vars.go rename to cipher/hight/utils.go index ca2d57f7..2ce9a31e 100644 --- a/cipher/hight/vars.go +++ b/cipher/hight/utils.go @@ -1,7 +1,7 @@ package hight var ( - delta = [...]byte{ + delta = []byte{ 0x5A, 0x6D, 0x36, 0x1B, 0x0D, 0x06, 0x03, 0x41, 0x60, 0x30, 0x18, 0x4C, 0x66, 0x33, 0x59, 0x2C, 0x56, 0x2B, 0x15, 0x4A, 0x65, 0x72, 0x39, 0x1C, @@ -20,7 +20,7 @@ var ( 0x74, 0x3A, 0x5D, 0x2E, 0x57, 0x6B, 0x35, 0x5A, } - hight_F0 = [...]byte{ + f0 = []byte{ 0x00, 0x86, 0x0D, 0x8B, 0x1A, 0x9C, 0x17, 0x91, 0x34, 0xB2, 0x39, 0xBF, 0x2E, 0xA8, 0x23, 0xA5, 0x68, 0xEE, 0x65, 0xE3, 0x72, 0xF4, 0x7F, 0xF9, @@ -55,7 +55,7 @@ var ( 0x6E, 0xE8, 0x63, 0xE5, 0x74, 0xF2, 0x79, 0xFF, } - hight_F1 = [...]byte{ + f1 = []byte{ 0x00, 0x58, 0xB0, 0xE8, 0x61, 0x39, 0xD1, 0x89, 0xC2, 0x9A, 0x72, 0x2A, 0xA3, 0xFB, 0x13, 0x4B, 0x85, 0xDD, 0x35, 0x6D, 0xE4, 0xBC, 0x54, 0x0C,