-
Notifications
You must be signed in to change notification settings - Fork 22
/
block.go
54 lines (44 loc) · 1.01 KB
/
block.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
package pmac
import (
"crypto/cipher"
"crypto/subtle"
)
const (
// R is the minimal irreducible polynomial for a 128-bit block size
R = 0x87
)
// Block is a 128-bit array used by certain block ciphers (i.e. AES)
type Block struct {
Data []byte
Size int
}
func NewBlock(size int) Block {
return Block{
Data: make([]byte, size),
Size: size,
}
}
// Clear zeroes out the contents of the block
func (b *Block) Clear() {
for i := range b.Data {
b.Data[i] = 0
}
}
// Dbl performs a doubling of a block over GF(2^128):
//
// a<<1 if firstbit(a)=0
// (a<<1) ⊕ 0¹²⁰10000111 if firstbit(a)=1
//
func (b *Block) Dbl() {
var z byte
for i := b.Size - 1; i >= 0; i-- {
zz := b.Data[i] >> 7
b.Data[i] = b.Data[i]<<1 | z
z = zz
}
b.Data[b.Size-1] ^= byte(subtle.ConstantTimeSelect(int(z), R, 0))
}
// Encrypt a block with the given block cipher
func (b *Block) Encrypt(c cipher.Block) {
c.Encrypt(b.Data, b.Data)
}