OSDN Git Service

7e9f330e594a4c08c212d2d47ea28ebd09d9c802
[pf3gnuchains/gcc-fork.git] / libgo / go / crypto / sha512 / sha512.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // This package implements the SHA384 and SHA512 hash algorithms as defined in FIPS 180-2.
6 package sha512
7
8 import (
9         "crypto"
10         "hash"
11         "os"
12 )
13
14 func init() {
15         crypto.RegisterHash(crypto.SHA384, New384)
16         crypto.RegisterHash(crypto.SHA512, New)
17 }
18
19 // The size of a SHA512 checksum in bytes.
20 const Size = 64
21
22 // The size of a SHA384 checksum in bytes.
23 const Size384 = 48
24
25 const (
26         _Chunk     = 128
27         _Init0     = 0x6a09e667f3bcc908
28         _Init1     = 0xbb67ae8584caa73b
29         _Init2     = 0x3c6ef372fe94f82b
30         _Init3     = 0xa54ff53a5f1d36f1
31         _Init4     = 0x510e527fade682d1
32         _Init5     = 0x9b05688c2b3e6c1f
33         _Init6     = 0x1f83d9abfb41bd6b
34         _Init7     = 0x5be0cd19137e2179
35         _Init0_384 = 0xcbbb9d5dc1059ed8
36         _Init1_384 = 0x629a292a367cd507
37         _Init2_384 = 0x9159015a3070dd17
38         _Init3_384 = 0x152fecd8f70e5939
39         _Init4_384 = 0x67332667ffc00b31
40         _Init5_384 = 0x8eb44a8768581511
41         _Init6_384 = 0xdb0c2e0d64f98fa7
42         _Init7_384 = 0x47b5481dbefa4fa4
43 )
44
45 // digest represents the partial evaluation of a checksum.
46 type digest struct {
47         h     [8]uint64
48         x     [_Chunk]byte
49         nx    int
50         len   uint64
51         is384 bool // mark if this digest is SHA-384
52 }
53
54 func (d *digest) Reset() {
55         if !d.is384 {
56                 d.h[0] = _Init0
57                 d.h[1] = _Init1
58                 d.h[2] = _Init2
59                 d.h[3] = _Init3
60                 d.h[4] = _Init4
61                 d.h[5] = _Init5
62                 d.h[6] = _Init6
63                 d.h[7] = _Init7
64         } else {
65                 d.h[0] = _Init0_384
66                 d.h[1] = _Init1_384
67                 d.h[2] = _Init2_384
68                 d.h[3] = _Init3_384
69                 d.h[4] = _Init4_384
70                 d.h[5] = _Init5_384
71                 d.h[6] = _Init6_384
72                 d.h[7] = _Init7_384
73         }
74         d.nx = 0
75         d.len = 0
76 }
77
78 // New returns a new hash.Hash computing the SHA512 checksum.
79 func New() hash.Hash {
80         d := new(digest)
81         d.Reset()
82         return d
83 }
84
85 // New384 returns a new hash.Hash computing the SHA384 checksum.
86 func New384() hash.Hash {
87         d := new(digest)
88         d.is384 = true
89         d.Reset()
90         return d
91 }
92
93 func (d *digest) Size() int {
94         if !d.is384 {
95                 return Size
96         }
97         return Size384
98 }
99
100 func (d *digest) Write(p []byte) (nn int, err os.Error) {
101         nn = len(p)
102         d.len += uint64(nn)
103         if d.nx > 0 {
104                 n := len(p)
105                 if n > _Chunk-d.nx {
106                         n = _Chunk - d.nx
107                 }
108                 for i := 0; i < n; i++ {
109                         d.x[d.nx+i] = p[i]
110                 }
111                 d.nx += n
112                 if d.nx == _Chunk {
113                         _Block(d, d.x[0:])
114                         d.nx = 0
115                 }
116                 p = p[n:]
117         }
118         n := _Block(d, p)
119         p = p[n:]
120         if len(p) > 0 {
121                 d.nx = copy(d.x[:], p)
122         }
123         return
124 }
125
126 func (d0 *digest) Sum() []byte {
127         // Make a copy of d0 so that caller can keep writing and summing.
128         d := new(digest)
129         *d = *d0
130
131         // Padding.  Add a 1 bit and 0 bits until 112 bytes mod 128.
132         len := d.len
133         var tmp [128]byte
134         tmp[0] = 0x80
135         if len%128 < 112 {
136                 d.Write(tmp[0 : 112-len%128])
137         } else {
138                 d.Write(tmp[0 : 128+112-len%128])
139         }
140
141         // Length in bits.
142         len <<= 3
143         for i := uint(0); i < 16; i++ {
144                 tmp[i] = byte(len >> (120 - 8*i))
145         }
146         d.Write(tmp[0:16])
147
148         if d.nx != 0 {
149                 panic("d.nx != 0")
150         }
151
152         p := make([]byte, 64)
153         j := 0
154         for _, s := range d.h {
155                 p[j+0] = byte(s >> 56)
156                 p[j+1] = byte(s >> 48)
157                 p[j+2] = byte(s >> 40)
158                 p[j+3] = byte(s >> 32)
159                 p[j+4] = byte(s >> 24)
160                 p[j+5] = byte(s >> 16)
161                 p[j+6] = byte(s >> 8)
162                 p[j+7] = byte(s >> 0)
163                 j += 8
164         }
165         if d.is384 {
166                 return p[0:48]
167         }
168         return p
169 }