Debian Codegraph @

cmac/cmac.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file.
3
4 // Package cmac implements the fast CMac MAC based on
5 // a block cipher. This mode of operation fixes security
6 // deficiencies of CBC-MAC (CBC-MAC is secure only for
7 // fixed-length messages). CMac is equal to OMAC1.
8 // This implementations supports block ciphers with a
9 // block size of:
10 // - 64 bit (8 byte)
11 // - 128 bit (16 byte)
12 // - 256 bit (32 byte)
13 // - 512 bit (64 byte)
14 // - 1024 bit (128 byte)
15 // Common ciphers like AES, Serpent etc. operate on 128 bit
16 // blocks. 256, 512 and 1024 are supported for the Threefish
17 // tweakable block cipher. Ciphers with 64 bit blocks are
18 // supported, but not recommened.
19 // CMac (using AES) is specified in RFC 4493.
20 package cmac
21
22 import (
23 "crypto/cipher"
24 "crypto/subtle"
25 "errors"
26 "hash"
27
28 "github.com/enceve/crypto"
29 )
30
31 const (
32 // minimal irreducible polynomial for blocksize
33 p64 = 0x1b // for 64 bit block ciphers
34 p128 = 0x87 // for 128 bit block ciphers (like AES)
35 p256 = 0x425 // special for large block ciphers (Threefish)
36 p512 = 0x125 // special for large block ciphers (Threefish)
37 p1024 = 0x80043 // special for large block ciphers (Threefish)
38 )
39
40 // Sum computes the CMac checksum of msg using the cipher.Block.
41 // If the block cipher is not supported by CMac (see package doc),
42 // a non-nil error is returned.
43 func Sum(msg []byte, c cipher.Block) ([]byte, error) {
44 mac, err := New(c)
45 if err != nil {
46 return nil, err
47 }
48 mac.Write(msg)
49 return mac.Sum(nil), nil
50 }
51
52 // Verify computes the CMac checksum of msg and compares it with the
53 // given mac. This functions returns true if and only if the given mac
54 // is equal to computed one. If the block cipher is not supported
55 // by CMac (see package doc), this function returns false.
56 func Verify(mac, msg []byte, c cipher.Block) bool {
57 sum, err := Sum(msg, c)
58 if err != nil {
59 return false
60 }
61 return subtle.ConstantTimeCompare(mac, sum) == 1
62 }
63
64 // New returns a hash.Hash computing the CMac checksum.
65 // If the block cipher is not supported by CMac
66 // (see package doc), a non-nil error is returned.
67 func New(c cipher.Block) (hash.Hash, error) {
68 if c == nil {
69 return nil, errors.New("the cipher.Block must not be nil")
70 }
71 bs := c.BlockSize()
72
73 var p int
74 switch bs {
75 default:
76 return nil, errors.New("cipher block size not supported")
77 case 8:
78 p = p64
79 case 16:
80 p = p128
81 case 32:
82 p = p256
83 case 64:
84 p = p512
85 case 128:
86 p = p1024
87 }
88
89 m := &macFunc{
90 cipher: c,
91 k0: make([]byte, bs),
92 k1: make([]byte, bs),
93 buf: make([]byte, bs),
94 }
95 c.Encrypt(m.k0, m.k0)
96
97 v := shift(m.k0, m.k0)
98 m.k0[bs-1] ^= byte(subtle.ConstantTimeSelect(v, p, 0))
99
100 v = shift(m.k1, m.k0)
101 m.k1[bs-1] ^= byte(subtle.ConstantTimeSelect(v, p, 0))
102
103 return m, nil
104 }
105
106 // The CMac message auth. function
107 type macFunc struct {
108 cipher cipher.Block
109 k0, k1 []byte
110 buf []byte
111 off int
112 }
113
114 func (h *macFunc) Size() int { return h.cipher.BlockSize() }
115
116 func (h *macFunc) BlockSize() int { return h.cipher.BlockSize() }
117
118 func (h *macFunc) Reset() {
119 for i := range h.buf {
120 h.buf[i] = 0
121 }
122 h.off = 0
123 }
124
125 func (h *macFunc) Write(msg []byte) (int, error) {
126 bs := h.BlockSize()
127 n := len(msg)
128
129 if h.off > 0 {
130 dif := bs - h.off
131 if n > dif {
132 crypto.XOR(h.buf[h.off:], h.buf[h.off:], msg[:dif])
133 msg = msg[dif:]
134 h.cipher.Encrypt(h.buf, h.buf)
135 h.off = 0
136 } else {
137 crypto.XOR(h.buf[h.off:], h.buf[h.off:], msg)
138 h.off += n
139 return n, nil
140 }
141 }
142
143 if length := len(msg); length > bs {
144 nn := length & (^(bs - 1))
145 if length == nn {
146 nn -= bs
147 }
148 for i := 0; i < nn; i += bs {
149 crypto.XOR(h.buf, h.buf, msg[i:i+bs])
150 h.cipher.Encrypt(h.buf, h.buf)
151 }
152 msg = msg[nn:]
153 }
154
155 if length := len(msg); length > 0 {
156 crypto.XOR(h.buf[h.off:], h.buf[h.off:], msg)
157 h.off += length
158 }
159
160 return n, nil
161 }
162
163 func (h *macFunc) Sum(b []byte) []byte {
164 bs := h.cipher.BlockSize()
165
166 // Don't change the buffer so the
167 // caller can keep writing and suming.
168 hash := make([]byte, bs)
169
170 k := h.k0
171 if h.off < bs {
172 k = h.k1
173 }
174 crypto.XOR(hash, k, h.buf)
175 if h.off < h.cipher.BlockSize() {
176 hash[h.off] ^= 0x80
177 }
178
179 h.cipher.Encrypt(hash, hash)
180 return append(b, hash...)
181 }
182
183 func shift(dst, src []byte) int {
184 var b, bit byte
185 for i := len(src) - 1; i >= 0; i-- { // a range would be nice
186 bit = src[i] >> 7
187 dst[i] = src[i]<<1 | b
188 b = bit
189 }
190 return int(b)
191 }