1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2023 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
16 // Multilinear Galois Mode (MGM) block cipher mode.
27 Mul(x, y []byte) []byte
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")
48 if tagSize < 4 || tagSize > blockSize {
49 return nil, errors.New("gogost/mgm: invalid tag size")
52 MaxSize: uint64(1<<uint(blockSize*8/2) - 1),
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),
70 func (mgm *MGM) NonceSize() int {
74 func (mgm *MGM) Overhead() int {
78 func incr(data []byte) {
79 for i := len(data) - 1; i >= 0; i-- {
87 func xor(dst, src1, src2 []byte) {
88 for i := 0; i < len(src1); i++ {
89 dst[i] = src1[i] ^ src2[i]
93 func (mgm *MGM) validateNonce(nonce []byte) {
94 if len(nonce) != mgm.BlockSize {
95 panic("nonce length must be equal to cipher's blocksize")
97 if nonce[0]&0x80 > 0 {
98 panic("nonce must not have higher bit set")
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")
106 if uint64(len(additionalData)) > mgm.MaxSize {
107 panic("additionalData is too big")
109 if uint64(len(text)+len(additionalData)) > mgm.MaxSize {
110 panic("*text with additionalData are too big")
114 func (mgm *MGM) auth(out, text, ad []byte) {
115 for i := 0; i < mgm.BlockSize; i++ {
119 textLen := len(text) * 8
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
127 mgm.mul.Mul(mgm.bufC, ad[:mgm.BlockSize]),
129 incr(mgm.bufP[:mgm.BlockSize/2]) // Z_{i+1} = incr_l(Z_i)
130 ad = ad[mgm.BlockSize:]
134 for i := len(ad); i < mgm.BlockSize; i++ {
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])
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
147 mgm.mul.Mul(mgm.bufC, text[:mgm.BlockSize]),
149 incr(mgm.bufP[:mgm.BlockSize/2]) // Z_{h+j+1} = incr_l(Z_{h+j})
150 text = text[mgm.BlockSize:]
153 copy(mgm.padded, text)
154 for i := len(text); i < mgm.BlockSize; i++ {
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])
162 mgm.cipher.Encrypt(mgm.bufP, mgm.bufP) // H_{h+q+1} = E_K(Z_{h+q+1})
164 if mgm.BlockSize == 8 {
165 binary.BigEndian.PutUint32(mgm.bufC, uint32(adLen))
166 binary.BigEndian.PutUint32(mgm.bufC[mgm.BlockSize/2:], uint32(textLen))
168 binary.BigEndian.PutUint64(mgm.bufC, uint64(adLen))
169 binary.BigEndian.PutUint64(mgm.bufC[mgm.BlockSize/2:], uint64(textLen))
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))
177 func (mgm *MGM) crypt(out, in []byte) {
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:]
188 mgm.cipher.Encrypt(mgm.bufC, mgm.bufP)
189 xor(out, in, mgm.bufC)
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")
199 ret, out := sliceForAppend(dst, len(plaintext)+mgm.TagSize)
201 mgm.crypt(out, plaintext)
203 out[len(plaintext):len(plaintext)+mgm.TagSize],
204 out[:len(plaintext)],
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")
216 if uint64(len(ciphertext)-mgm.TagSize) > mgm.MaxSize {
217 panic("ciphertext is too big")
219 ret, out := sliceForAppend(dst, len(ciphertext)-mgm.TagSize)
220 ct := ciphertext[:len(ciphertext)-mgm.TagSize]
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")