]> Cypherpunks.ru repositories - gogost.git/blob - mgm/mode.go
Separate GF^64 and GF^128 multiplier implementations
[gogost.git] / mgm / mode.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2021 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 // Multilinear Galois Mode (MGM) block cipher mode.
17 package mgm
18
19 import (
20         "crypto/cipher"
21         "crypto/hmac"
22         "encoding/binary"
23         "errors"
24 )
25
26 type Mul interface {
27         Mul(x, y []byte) []byte
28 }
29
30 type MGM struct {
31         MaxSize   uint64
32         BlockSize int
33         TagSize   int
34         cipher    cipher.Block
35         icn       []byte
36         bufP      []byte
37         bufC      []byte
38         padded    []byte
39         sum       []byte
40         mul       Mul
41 }
42
43 func NewMGM(cipher cipher.Block, tagSize int) (cipher.AEAD, error) {
44         blockSize := cipher.BlockSize()
45         if !(blockSize == 8 || blockSize == 16) {
46                 return nil, errors.New("gogost/mgm: only 64/128 blocksizes allowed")
47         }
48         if tagSize < 4 || tagSize > blockSize {
49                 return nil, errors.New("gogost/mgm: invalid tag size")
50         }
51         mgm := MGM{
52                 MaxSize:   uint64(1<<uint(blockSize*8/2) - 1),
53                 BlockSize: blockSize,
54                 TagSize:   tagSize,
55                 cipher:    cipher,
56                 icn:       make([]byte, blockSize),
57                 bufP:      make([]byte, blockSize),
58                 bufC:      make([]byte, blockSize),
59                 padded:    make([]byte, blockSize),
60                 sum:       make([]byte, blockSize),
61         }
62         if blockSize == 8 {
63                 mgm.mul = newMul64()
64         } else {
65                 mgm.mul = newMul128()
66         }
67         return &mgm, nil
68 }
69
70 func (mgm *MGM) NonceSize() int {
71         return mgm.BlockSize
72 }
73
74 func (mgm *MGM) Overhead() int {
75         return mgm.TagSize
76 }
77
78 func incr(data []byte) {
79         for i := len(data) - 1; i >= 0; i-- {
80                 data[i]++
81                 if data[i] != 0 {
82                         return
83                 }
84         }
85 }
86
87 func xor(dst, src1, src2 []byte) {
88         for i := 0; i < len(src1); i++ {
89                 dst[i] = src1[i] ^ src2[i]
90         }
91 }
92
93 func (mgm *MGM) validateNonce(nonce []byte) {
94         if len(nonce) != mgm.BlockSize {
95                 panic("nonce length must be equal to cipher's blocksize")
96         }
97         if nonce[0]&0x80 > 0 {
98                 panic("nonce must not have higher bit set")
99         }
100 }
101
102 func (mgm *MGM) validateSizes(text, additionalData []byte) {
103         if len(text) == 0 && len(additionalData) == 0 {
104                 panic("at least either *text or additionalData must be provided")
105         }
106         if uint64(len(additionalData)) > mgm.MaxSize {
107                 panic("additionalData is too big")
108         }
109         if uint64(len(text)+len(additionalData)) > mgm.MaxSize {
110                 panic("*text with additionalData are too big")
111         }
112 }
113
114 func (mgm *MGM) auth(out, text, ad []byte) {
115         for i := 0; i < mgm.BlockSize; i++ {
116                 mgm.sum[i] = 0
117         }
118         adLen := len(ad) * 8
119         textLen := len(text) * 8
120         mgm.icn[0] |= 0x80
121         mgm.cipher.Encrypt(mgm.bufP, mgm.icn) // Z_1 = E_K(1 || ICN)
122         for len(ad) >= mgm.BlockSize {
123                 mgm.cipher.Encrypt(mgm.bufC, mgm.bufP) // H_i = E_K(Z_i)
124                 xor(                                   // sum (xor)= H_i (x) A_i
125                         mgm.sum,
126                         mgm.sum,
127                         mgm.mul.Mul(mgm.bufC, ad[:mgm.BlockSize]),
128                 )
129                 incr(mgm.bufP[:mgm.BlockSize/2]) // Z_{i+1} = incr_l(Z_i)
130                 ad = ad[mgm.BlockSize:]
131         }
132         if len(ad) > 0 {
133                 copy(mgm.padded, ad)
134                 for i := len(ad); i < mgm.BlockSize; i++ {
135                         mgm.padded[i] = 0
136                 }
137                 mgm.cipher.Encrypt(mgm.bufC, mgm.bufP)
138                 xor(mgm.sum, mgm.sum, mgm.mul.Mul(mgm.bufC, mgm.padded))
139                 incr(mgm.bufP[:mgm.BlockSize/2])
140         }
141
142         for len(text) >= mgm.BlockSize {
143                 mgm.cipher.Encrypt(mgm.bufC, mgm.bufP) // H_{h+j} = E_K(Z_{h+j})
144                 xor(                                   // sum (xor)= H_{h+j} (x) C_j
145                         mgm.sum,
146                         mgm.sum,
147                         mgm.mul.Mul(mgm.bufC, text[:mgm.BlockSize]),
148                 )
149                 incr(mgm.bufP[:mgm.BlockSize/2]) // Z_{h+j+1} = incr_l(Z_{h+j})
150                 text = text[mgm.BlockSize:]
151         }
152         if len(text) > 0 {
153                 copy(mgm.padded, text)
154                 for i := len(text); i < mgm.BlockSize; i++ {
155                         mgm.padded[i] = 0
156                 }
157                 mgm.cipher.Encrypt(mgm.bufC, mgm.bufP)
158                 xor(mgm.sum, mgm.sum, mgm.mul.Mul(mgm.bufC, mgm.padded))
159                 incr(mgm.bufP[:mgm.BlockSize/2])
160         }
161
162         mgm.cipher.Encrypt(mgm.bufP, mgm.bufP) // H_{h+q+1} = E_K(Z_{h+q+1})
163         // len(A) || len(C)
164         if mgm.BlockSize == 8 {
165                 binary.BigEndian.PutUint32(mgm.bufC, uint32(adLen))
166                 binary.BigEndian.PutUint32(mgm.bufC[mgm.BlockSize/2:], uint32(textLen))
167         } else {
168                 binary.BigEndian.PutUint64(mgm.bufC, uint64(adLen))
169                 binary.BigEndian.PutUint64(mgm.bufC[mgm.BlockSize/2:], uint64(textLen))
170         }
171         // sum (xor)= H_{h+q+1} (x) (len(A) || len(C))
172         xor(mgm.sum, mgm.sum, mgm.mul.Mul(mgm.bufC, mgm.bufP))
173         mgm.cipher.Encrypt(mgm.bufP, mgm.sum) // E_K(sum)
174         copy(out, mgm.bufP[:mgm.TagSize])     // MSB_S(E_K(sum))
175 }
176
177 func (mgm *MGM) crypt(out, in []byte) {
178         mgm.icn[0] &= 0x7F
179         mgm.cipher.Encrypt(mgm.bufP, mgm.icn) // Y_1 = E_K(0 || ICN)
180         for len(in) >= mgm.BlockSize {
181                 mgm.cipher.Encrypt(mgm.bufC, mgm.bufP) // E_K(Y_i)
182                 xor(out, mgm.bufC, in)                 // C_i = P_i (xor) E_K(Y_i)
183                 incr(mgm.bufP[mgm.BlockSize/2:])       // Y_i = incr_r(Y_{i-1})
184                 out = out[mgm.BlockSize:]
185                 in = in[mgm.BlockSize:]
186         }
187         if len(in) > 0 {
188                 mgm.cipher.Encrypt(mgm.bufC, mgm.bufP)
189                 xor(out, in, mgm.bufC)
190         }
191 }
192
193 func (mgm *MGM) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
194         mgm.validateNonce(nonce)
195         mgm.validateSizes(plaintext, additionalData)
196         if uint64(len(plaintext)) > mgm.MaxSize {
197                 panic("plaintext is too big")
198         }
199         ret, out := sliceForAppend(dst, len(plaintext)+mgm.TagSize)
200         copy(mgm.icn, nonce)
201         mgm.crypt(out, plaintext)
202         mgm.auth(
203                 out[len(plaintext):len(plaintext)+mgm.TagSize],
204                 out[:len(plaintext)],
205                 additionalData,
206         )
207         return ret
208 }
209
210 func (mgm *MGM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
211         mgm.validateNonce(nonce)
212         mgm.validateSizes(ciphertext, additionalData)
213         if len(ciphertext) < mgm.TagSize {
214                 return nil, errors.New("ciphertext is too short")
215         }
216         if uint64(len(ciphertext)-mgm.TagSize) > mgm.MaxSize {
217                 panic("ciphertext is too big")
218         }
219         ret, out := sliceForAppend(dst, len(ciphertext)-mgm.TagSize)
220         ct := ciphertext[:len(ciphertext)-mgm.TagSize]
221         copy(mgm.icn, nonce)
222         mgm.auth(mgm.sum, ct, additionalData)
223         if !hmac.Equal(mgm.sum[:mgm.TagSize], ciphertext[len(ciphertext)-mgm.TagSize:]) {
224                 return nil, errors.New("gogost/mgm: invalid authentication tag")
225         }
226         mgm.crypt(out, ct)
227         return ret, nil
228 }