blob: 5ea1ba966ea4d55d41e5b66be752ae4314d40601 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001// 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// MD4 block step.
6// In its own file so that a faster assembly or C version
7// can be substituted easily.
8
9package md4
10
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053011import "math/bits"
12
13var shift1 = []int{3, 7, 11, 19}
14var shift2 = []int{3, 5, 9, 13}
15var shift3 = []int{3, 9, 11, 15}
Holger Hildebrandtfa074992020-03-27 15:42:06 +000016
17var xIndex2 = []uint{0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15}
18var xIndex3 = []uint{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}
19
20func _Block(dig *digest, p []byte) int {
21 a := dig.s[0]
22 b := dig.s[1]
23 c := dig.s[2]
24 d := dig.s[3]
25 n := 0
26 var X [16]uint32
27 for len(p) >= _Chunk {
28 aa, bb, cc, dd := a, b, c, d
29
30 j := 0
31 for i := 0; i < 16; i++ {
32 X[i] = uint32(p[j]) | uint32(p[j+1])<<8 | uint32(p[j+2])<<16 | uint32(p[j+3])<<24
33 j += 4
34 }
35
36 // If this needs to be made faster in the future,
37 // the usual trick is to unroll each of these
38 // loops by a factor of 4; that lets you replace
39 // the shift[] lookups with constants and,
40 // with suitable variable renaming in each
41 // unrolled body, delete the a, b, c, d = d, a, b, c
42 // (or you can let the optimizer do the renaming).
43 //
44 // The index variables are uint so that % by a power
45 // of two can be optimized easily by a compiler.
46
47 // Round 1.
48 for i := uint(0); i < 16; i++ {
49 x := i
50 s := shift1[i%4]
51 f := ((c ^ d) & b) ^ d
52 a += f + X[x]
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053053 a = bits.RotateLeft32(a, s)
Holger Hildebrandtfa074992020-03-27 15:42:06 +000054 a, b, c, d = d, a, b, c
55 }
56
57 // Round 2.
58 for i := uint(0); i < 16; i++ {
59 x := xIndex2[i]
60 s := shift2[i%4]
61 g := (b & c) | (b & d) | (c & d)
62 a += g + X[x] + 0x5a827999
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053063 a = bits.RotateLeft32(a, s)
Holger Hildebrandtfa074992020-03-27 15:42:06 +000064 a, b, c, d = d, a, b, c
65 }
66
67 // Round 3.
68 for i := uint(0); i < 16; i++ {
69 x := xIndex3[i]
70 s := shift3[i%4]
71 h := b ^ c ^ d
72 a += h + X[x] + 0x6ed9eba1
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053073 a = bits.RotateLeft32(a, s)
Holger Hildebrandtfa074992020-03-27 15:42:06 +000074 a, b, c, d = d, a, b, c
75 }
76
77 a += aa
78 b += bb
79 c += cc
80 d += dd
81
82 p = p[_Chunk:]
83 n += _Chunk
84 }
85
86 dig.s[0] = a
87 dig.s[1] = b
88 dig.s[2] = c
89 dig.s[3] = d
90 return n
91}