Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(514)

Delta Between Two Patch Sets: src/pkg/crypto/sha1/sha1block.go

Issue 6820096: code review 6820096: crypto/sha1: Make sha-1 do block mixup in place (Closed)
Left Patch Set: diff -r c33545ae0ec0 https://code.google.com/p/go/ Created 11 years, 4 months ago
Right Patch Set: diff -r c33545ae0ec0 https://code.google.com/p/go/ Created 11 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // SHA1 block step. 5 // SHA1 block step.
6 // In its own file so that a faster assembly or C version 6 // In its own file so that a faster assembly or C version
7 // can be substituted easily. 7 // can be substituted easily.
8 8
9 package sha1 9 package sha1
10 10
11 const ( 11 const (
12 _K0 = 0x5A827999 12 _K0 = 0x5A827999
13 _K1 = 0x6ED9EBA1 13 _K1 = 0x6ED9EBA1
14 _K2 = 0x8F1BBCDC 14 _K2 = 0x8F1BBCDC
15 _K3 = 0xCA62C1D6 15 _K3 = 0xCA62C1D6
16 ) 16 )
17 17
18 func block(dig *digest, p []byte) { 18 func block(dig *digest, p []byte) {
19 var w [16]uint32 19 var w [16]uint32
20 20
21 h0, h1, h2, h3, h4 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] 21 h0, h1, h2, h3, h4 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4]
22 for len(p) >= chunk { 22 for len(p) >= chunk {
23 // Can interlace the computation of w with the 23 // Can interlace the computation of w with the
24 // rounds below if needed for speed. 24 // rounds below if needed for speed.
25 for i := 0; i < 16; i++ { 25 for i := 0; i < 16; i++ {
26 j := i * 4 26 j := i * 4
dfc 2012/11/06 22:04:24 j := i << 2 avoids the imul on intel. The compiler
27 w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[ j+2])<<8 | uint32(p[j+3]) 27 w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[ j+2])<<8 | uint32(p[j+3])
28 } 28 }
29 29
30 a, b, c, d, e := h0, h1, h2, h3, h4 30 a, b, c, d, e := h0, h1, h2, h3, h4
31 31
32 // Each of the four 20-iteration rounds 32 // Each of the four 20-iteration rounds
33 // differs only in the computation of f and 33 // differs only in the computation of f and
34 // the choice of K (_K0, _K1, etc). 34 // the choice of K (_K0, _K1, etc).
35 » » for i := 0; i < 16; i++ { 35 » » i := 0
36 » » for ; i < 16; i++ {
36 f := b&c | (^b)&d 37 f := b&c | (^b)&d
37 a5 := a<<5 | a>>(32-5) 38 a5 := a<<5 | a>>(32-5)
38 b30 := b<<30 | b>>(32-30) 39 b30 := b<<30 | b>>(32-30)
39 » » » t := a5 + f + e + w[i] + _K0 40 » » » t := a5 + f + e + w[i&0xf] + _K0
40 a, b, c, d, e = t, a, b30, c, d 41 a, b, c, d, e = t, a, b30, c, d
41 } 42 }
42 » » for i := 16; i < 20; i++ { 43 » » for ; i < 20; i++ {
43 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf] 44 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf]
44 w[i&0xf] = tmp<<1 | tmp>>(32-1) 45 w[i&0xf] = tmp<<1 | tmp>>(32-1)
45 46
46 f := b&c | (^b)&d 47 f := b&c | (^b)&d
47 a5 := a<<5 | a>>(32-5) 48 a5 := a<<5 | a>>(32-5)
48 b30 := b<<30 | b>>(32-30) 49 b30 := b<<30 | b>>(32-30)
49 t := a5 + f + e + w[i&0xf] + _K0 50 t := a5 + f + e + w[i&0xf] + _K0
50 a, b, c, d, e = t, a, b30, c, d 51 a, b, c, d, e = t, a, b30, c, d
51 } 52 }
52 » » for i := 20; i < 40; i++ { 53 » » for ; i < 40; i++ {
53 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf] 54 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf]
54 w[i&0xf] = tmp<<1 | tmp>>(32-1) 55 w[i&0xf] = tmp<<1 | tmp>>(32-1)
55 f := b ^ c ^ d 56 f := b ^ c ^ d
56 a5 := a<<5 | a>>(32-5) 57 a5 := a<<5 | a>>(32-5)
57 b30 := b<<30 | b>>(32-30) 58 b30 := b<<30 | b>>(32-30)
58 t := a5 + f + e + w[i&0xf] + _K1 59 t := a5 + f + e + w[i&0xf] + _K1
59 a, b, c, d, e = t, a, b30, c, d 60 a, b, c, d, e = t, a, b30, c, d
60 } 61 }
61 » » for i := 40; i < 60; i++ { 62 » » for ; i < 60; i++ {
62 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf] 63 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf]
63 w[i&0xf] = tmp<<1 | tmp>>(32-1) 64 w[i&0xf] = tmp<<1 | tmp>>(32-1)
64 » » » f := b&c | b&d | c&d 65 » » » f := ((b | c) & d) | (b & c)
66
65 a5 := a<<5 | a>>(32-5) 67 a5 := a<<5 | a>>(32-5)
66 b30 := b<<30 | b>>(32-30) 68 b30 := b<<30 | b>>(32-30)
67 t := a5 + f + e + w[i&0xf] + _K2 69 t := a5 + f + e + w[i&0xf] + _K2
68 a, b, c, d, e = t, a, b30, c, d 70 a, b, c, d, e = t, a, b30, c, d
69 } 71 }
70 » » for i := 60; i < 80; i++ { 72 » » for ; i < 80; i++ {
71 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf] 73 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf]
72 w[i&0xf] = tmp<<1 | tmp>>(32-1) 74 w[i&0xf] = tmp<<1 | tmp>>(32-1)
73 f := b ^ c ^ d 75 f := b ^ c ^ d
74 a5 := a<<5 | a>>(32-5) 76 a5 := a<<5 | a>>(32-5)
75 b30 := b<<30 | b>>(32-30) 77 b30 := b<<30 | b>>(32-30)
76 t := a5 + f + e + w[i&0xf] + _K3 78 t := a5 + f + e + w[i&0xf] + _K3
77 a, b, c, d, e = t, a, b30, c, d 79 a, b, c, d, e = t, a, b30, c, d
78 } 80 }
79 81
80 h0 += a 82 h0 += a
81 h1 += b 83 h1 += b
82 h2 += c 84 h2 += c
83 h3 += d 85 h3 += d
84 h4 += e 86 h4 += e
85 87
86 p = p[chunk:] 88 p = p[chunk:]
87 } 89 }
88 90
89 dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] = h0, h1, h2, h3, h4 91 dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] = h0, h1, h2, h3, h4
90 } 92 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b