Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Mar 17, 2024
1 parent 3e4034e commit 000aaf9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 107 deletions.
155 changes: 51 additions & 104 deletions cipher/hight/hight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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]
}
}
}
6 changes: 3 additions & 3 deletions cipher/hight/vars.go → cipher/hight/utils.go
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 000aaf9

Please sign in to comment.