]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost28147/mac.go
1.1 release is ready
[gogost.git] / src / cypherpunks.ru / gogost / gost28147 / mac.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2016 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, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package gost28147
18
19 import (
20         "errors"
21 )
22
23 var (
24         SeqMAC = Seq([]uint8{
25                 0, 1, 2, 3, 4, 5, 6, 7,
26                 0, 1, 2, 3, 4, 5, 6, 7,
27         })
28 )
29
30 type MAC struct {
31         c    *Cipher
32         size int
33         iv   []byte
34         prev []byte
35         buf  []byte
36         n1   nv
37         n2   nv
38 }
39
40 // Create MAC with given tag size and initial initialization vector.
41 // Size is in bytes and must be between 1 and 8. To be RFC conformant,
42 // iv must be the first block of the authenticated data, second and
43 // following ones are fed to Write function.
44 func (c *Cipher) NewMAC(size int, iv [BlockSize]byte) (*MAC, error) {
45         if size == 0 || size > 8 {
46                 return nil, errors.New("Invalid tag size")
47         }
48         m := MAC{c: c, size: size, iv: iv[:]}
49         n2, n1 := block2nvs(iv[:])
50         m.iv = make([]byte, BlockSize)
51         nvs2block(n1, n2, m.iv)
52         m.prev = make([]byte, BlockSize)
53         m.Reset()
54         return &m, nil
55 }
56
57 func (m *MAC) Reset() {
58         copy(m.prev, m.iv)
59         m.buf = nil
60 }
61
62 func (m *MAC) BlockSize() int {
63         return BlockSize
64 }
65
66 func (m *MAC) Size() int {
67         return m.size
68 }
69
70 func (m *MAC) Write(b []byte) (int, error) {
71         m.buf = append(m.buf, b...)
72         for len(m.buf) >= BlockSize {
73                 for i := 0; i < BlockSize; i++ {
74                         m.prev[i] ^= m.buf[i]
75                 }
76                 m.n1, m.n2 = block2nvs(m.prev)
77                 m.n1, m.n2 = m.c.xcrypt(SeqMAC, m.n1, m.n2)
78                 nvs2block(m.n2, m.n1, m.prev)
79                 m.buf = m.buf[8:]
80         }
81         return len(b), nil
82 }
83
84 func (m *MAC) Sum(b []byte) []byte {
85         if len(m.buf) == 0 {
86                 return append(b, m.prev[0:m.size]...)
87         }
88         buf := m.buf
89         var i int
90         for i = 0; i < BlockSize-len(m.buf); i++ {
91                 buf = append(buf, byte(0))
92         }
93         for i = 0; i < BlockSize; i++ {
94                 buf[i] ^= m.prev[i]
95         }
96         m.n1, m.n2 = block2nvs(buf)
97         m.n1, m.n2 = m.c.xcrypt(SeqMAC, m.n1, m.n2)
98         nvs2block(m.n2, m.n1, buf)
99         return append(b, buf[0:m.size]...)
100 }