-
Notifications
You must be signed in to change notification settings - Fork 22
/
square.go
123 lines (97 loc) · 2.76 KB
/
square.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package square
import (
"fmt"
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
/**
* The Square block cipher.
*
* Algorithm developed by Joan Daemen <daemen.j@protonworld.com> and
* Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>. Description available
* from http://www.esat.kuleuven.ac.be/~cosicart/pdf/VR-9700.PDF
*
* This implementation is in the public domain.
*
* @author Paulo S.L.M. Barreto <pbarreto@nw.com.br>
* @author George Barwood <george.barwood@dial.pipex.com>
* @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
*
* Caveat: this code assumes 32-bit words and probably will not work
* otherwise.
*
* To correctly visualize this file, please set tabstop = 4.
*
* @version 2.7 (1999.06.29)
*
*/
const BlockSize = 16
type KeySizeError int
func (k KeySizeError) Error() string {
return fmt.Sprintf("cryptobin/square: invalid key size %d", int(k))
}
/*
* This a byte level implementation.
* a[0] corresponds with the first byte of the plaintext block,
* a[0xF] with the last one. same for the ciphertext and the key.
*/
type squareCipher struct {
ekey [R+1][4]uint32
dkey [R+1][4]uint32
}
// NewCipher creates and returns a new cipher.Block.
func NewCipher(key []byte) (cipher.Block, error) {
k := len(key)
switch k {
case 16:
break
default:
return nil, KeySizeError(len(key))
}
c := new(squareCipher)
c.expandKey(key)
return c, nil
}
func (this *squareCipher) BlockSize() int {
return BlockSize
}
func (this *squareCipher) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/square: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/square: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/square: invalid buffer overlap")
}
var a [4]uint32
aUints := bytesToUint32s(src)
copy(a[:], aUints)
squareEncrypt(&a, this.ekey)
dstBytes := uint32sToBytes(a[:])
copy(dst, dstBytes)
}
func (this *squareCipher) Decrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/square: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/square: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/square: invalid buffer overlap")
}
var a [4]uint32
aUints := bytesToUint32s(src)
copy(a[:], aUints)
squareDecrypt(&a, this.dkey)
dstBytes := uint32sToBytes(a[:])
copy(dst, dstBytes)
}
func (this *squareCipher) expandKey(key []byte) {
k := bytesToUint32s(key)
var a [4]uint32
copy(a[:], k)
squareGenerateRoundKeys(a, &this.ekey, &this.dkey)
}