Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Aug 19, 2024
1 parent 19a0d56 commit 43a4e80
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 0 deletions.
204 changes: 204 additions & 0 deletions cipher/belt/belt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package belt

import (
"fmt"
"crypto/cipher"

"github.com/deatil/go-cryptobin/tool/alias"
)

// BELT block cipher, defined in STB 34.101.31

const BlockSize = 16

// KeySizeError is returned when key size in bytes
// isn't one of 16, 24, or 32.
type KeySizeError int

func (k KeySizeError) Error() string {
return fmt.Sprintf("cryptobin/belt: invalid key size %d", int(k))
}

type beltCipher struct {
ks [32]byte
}

// NewCipher creates and returns a new cipher.Block.
func NewCipher(key []byte) (cipher.Block, error) {
k := len(key)
switch k {
case 16, 24, 32:
break
default:
return nil, KeySizeError(k)
}

c := new(beltCipher)
c.expandKey(key)

return c, nil
}

func (c *beltCipher) BlockSize() int {
return BlockSize
}

func (c *beltCipher) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/belt: input not full block")
}

if len(dst) < BlockSize {
panic("cryptobin/belt: output not full block")
}

if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/belt: invalid buffer overlap")
}

c.encrypt(dst, src)
}

func (c *beltCipher) Decrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/belt: input not full block")
}

if len(dst) < BlockSize {
panic("cryptobin/belt: output not full block")
}

if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/belt: invalid buffer overlap")
}

c.decrypt(dst, src)
}

func (cc *beltCipher) encrypt(out, in []byte) {
var a, b, c, d, e uint32
var i uint32

a = getu32(in[0:])
b = getu32(in[4:])
c = getu32(in[8:])
d = getu32(in[12:])

ks := cc.ks

var key uint32

for i = 0; i < 8; i++ {
key = getu32(ks[4*KIdx[i][0]:])
b ^= G(a + key, 5)

key = getu32(ks[4*KIdx[i][1]:])
c ^= G(d + key, 21)

key = getu32(ks[4*KIdx[i][2]:])
a = (a - G(b + key, 13))

key = getu32(ks[4*KIdx[i][3]:])
e = G(b + c + key, 21) ^ (i + 1)

b += e
c = uint32(c - e)

key = getu32(ks[4*KIdx[i][4]:])
d += G(c + key, 13)

key = getu32(ks[4*KIdx[i][5]:])
b ^= G(a + key, 21)

key = getu32(ks[4*KIdx[i][6]:])
c ^= G(d + key, 5)

a, b = b, a
c, d = d, c
b, c = c, b
}

putu32(out[0:], b)
putu32(out[4:], d)
putu32(out[8:], a)
putu32(out[12:], c)
}

func (cc *beltCipher) decrypt(out, in []byte) {
var a, b, c, d, e uint32
var i uint32

a = getu32(in[0:])
b = getu32(in[4:])
c = getu32(in[8:])
d = getu32(in[12:])

ks := cc.ks

for i = 0; i < 8; i++ {
var key uint32

j := 7 - i

key = getu32(ks[4*KIdx[j][6]:])
b ^= G(a + key, 5)

key = getu32(ks[4*KIdx[j][5]:])
c ^= G(d + key, 21)

key = getu32(ks[4*KIdx[j][4]:])
a = uint32(a - G(b + key, 13))

key = getu32(ks[4*KIdx[j][3]:])
e = G(b + c + key, 21) ^ (j + 1)

b += e
c = uint32(c - e)

key = getu32(ks[4*KIdx[j][2]:])
d += G(c + key, 13)

key = getu32(ks[4*KIdx[j][1]:])
b ^= G(a + key, 21)

key = getu32(ks[4*KIdx[j][0]:])
c ^= G(d + key, 5)

a, b = b, a
c, d = d, c
a, d = d, a
}

putu32(out[0:], c)
putu32(out[4:], a)
putu32(out[8:], d)
putu32(out[12:], b)
}

func (c *beltCipher) expandKey(k []byte) {
var i int

kLen := len(k)

switch (kLen) {
case 16:
for i = 0; i < 16; i++ {
c.ks[i] = k[i]
c.ks[i + 16] = k[i]
}

case 24:
for i = 0; i < 24; i++ {
c.ks[i] = k[i]
}

for i = 24; i < 32; i++ {
c.ks[i] = k[i - 24] ^ k[i - 20] ^ k[i - 16]
}

case 32:
for i = 0; i < 32; i++ {
c.ks[i] = k[i]
}
}
}
125 changes: 125 additions & 0 deletions cipher/belt/belt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package belt

import (
"bytes"
"testing"
"math/rand"
"encoding/hex"
)

func fromHex(s string) []byte {
h, _ := hex.DecodeString(s)
return h
}

func Test_Key(t *testing.T) {
random := rand.New(rand.NewSource(99))
max := 100

var encrypted [16]byte
var decrypted [16]byte

for i := 0; i < max; i++ {
key := make([]byte, 32)
random.Read(key)
value := make([]byte, 16)
random.Read(value)

cipher1, err := NewCipher(key)
if err != nil {
t.Fatal(err.Error())
}

cipher1.Encrypt(encrypted[:], value)

cipher2, err := NewCipher(key)
if err != nil {
t.Fatal(err.Error())
}

cipher2.Decrypt(decrypted[:], encrypted[:])

if !bytes.Equal(decrypted[:], value[:]) {
t.Errorf("encryption/decryption failed: got %x, want %x", decrypted, value)
}
}
}

type testData struct {
pt []byte
ct []byte
key []byte
}

func Test_Check_List(t *testing.T) {
tests := []testData{
// 32 bytes
{
fromHex("000000000000000000000000bdf4e311"),
fromHex("74bc468e2d40f5839633370e7d67bd23"),
fromHex("2342bb9efa38542cbed0ac83940ac2988d7c47ce264908461cc1b5137ae6b604"),
},
{
fromHex("000000000000000000000000cf05f422"),
fromHex("05ceae72f09bc0d7dfa4978903a5c936"),
fromHex("2342bb9efa38542cbed0ac83940ac2988d7c47ce264908461cc1b5137ae6b604"),
},
{
fromHex("000000000000000000000000f0271543"),
fromHex("47b67fd2f8a41cf50f6526d874f9e692"),
fromHex("2342bb9efa38542cbed0ac83940ac2988d7c47ce264908461cc1b5137ae6b604"),
},

// 24 bytes
{
fromHex("000000000000000000000000de255aff"),
fromHex("312de1e84c285f8a4c3ee45de9a8bacc"),
fromHex("2342bb9efa38542cbed0ac83940ac298bac77a7717942863"),
},
{
fromHex("000000000000000000000000e2295f03"),
fromHex("79172557ae7da54f7d5adb4dcb0ec0d3"),
fromHex("2342bb9efa38542cbed0ac83940ac298bac77a7717942863"),
},

// 16 bytes
{
fromHex("0000000000000000000000000c9b2807"),
fromHex("c362d0c8e930486e00df76023439047d"),
fromHex("2342bb9efa38542c0af75647f29f615d"),
},
{
fromHex("0000000000000000000000002cbb4827"),
fromHex("98c02f6582e88b61ed3e0235a361c18a"),
fromHex("2342bb9efa38542c0af75647f29f615d"),
},
}

for i, test := range tests {
c, err := NewCipher(test.key)
if err != nil {
t.Fatal(err.Error())
}

tmp := make([]byte, BlockSize)
c.Encrypt(tmp, test.pt)

if !bytes.Equal(tmp, test.ct) {
t.Errorf("[%d] Check error: got %x, want %x", i, tmp, test.ct)
}

// ===========

c2, err := NewCipher(test.key)
if err != nil {
t.Fatal(err.Error())
}

tmp2 := make([]byte, BlockSize)
c2.Decrypt(tmp2, test.ct)

if !bytes.Equal(tmp2, test.pt) {
t.Errorf("[%d] Check Decrypt error: got %x, want %x", i, tmp2, test.pt)
}
}
}
31 changes: 31 additions & 0 deletions cipher/belt/sboxs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package belt

var S = [256]byte{
0xB1, 0x94, 0xBA, 0xC8, 0x0A, 0x08, 0xF5, 0x3B, 0x36, 0x6D, 0x00, 0x8E, 0x58, 0x4A, 0x5D, 0xE4,
0x85, 0x04, 0xFA, 0x9D, 0x1B, 0xB6, 0xC7, 0xAC, 0x25, 0x2E, 0x72, 0xC2, 0x02, 0xFD, 0xCE, 0x0D,
0x5B, 0xE3, 0xD6, 0x12, 0x17, 0xB9, 0x61, 0x81, 0xFE, 0x67, 0x86, 0xAD, 0x71, 0x6B, 0x89, 0x0B,
0x5C, 0xB0, 0xC0, 0xFF, 0x33, 0xC3, 0x56, 0xB8, 0x35, 0xC4, 0x05, 0xAE, 0xD8, 0xE0, 0x7F, 0x99,
0xE1, 0x2B, 0xDC, 0x1A, 0xE2, 0x82, 0x57, 0xEC, 0x70, 0x3F, 0xCC, 0xF0, 0x95, 0xEE, 0x8D, 0xF1,
0xC1, 0xAB, 0x76, 0x38, 0x9F, 0xE6, 0x78, 0xCA, 0xF7, 0x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 5, 6, 7, 0, 1, 2, 3 },
{ 4, 5, 6, 7, 0, 1, 2 },
{ 3, 4, 5, 6, 7, 0, 1 },
{ 2, 3, 4, 5, 6, 7, 0 },
{ 1, 2, 3, 4, 5, 6, 7 },
}
Loading

0 comments on commit 43a4e80

Please sign in to comment.