File: block.go

package info (click to toggle)
golang-github-mmcloughlin-md4 0.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152 kB
  • sloc: asm: 404; sh: 19; makefile: 3
file content (81 lines) | stat: -rw-r--r-- 1,821 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !amd64 || purego
// +build !amd64 purego

package md4

var shift1 = []uint{3, 7, 11, 19}
var shift2 = []uint{3, 5, 9, 13}
var shift3 = []uint{3, 9, 11, 15}

var xIndex2 = []uint{0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15}
var xIndex3 = []uint{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}

func block(h *[4]uint32, m []byte) {
	a := h[0]
	b := h[1]
	c := h[2]
	d := h[3]
	var X [16]uint32
	aa, bb, cc, dd := a, b, c, d

	j := 0
	for i := 0; i < 16; i++ {
		X[i] = uint32(m[j]) | uint32(m[j+1])<<8 | uint32(m[j+2])<<16 | uint32(m[j+3])<<24
		j += 4
	}

	// If this needs to be made faster in the future,
	// the usual trick is to unroll each of these
	// loops by a factor of 4; that lets you replace
	// the shift[] lookups with constants and,
	// with suitable variable renaming in each
	// unrolled body, delete the a, b, c, d = d, a, b, c
	// (or you can let the optimizer do the renaming).
	//
	// The index variables are uint so that % by a power
	// of two can be optimized easily by a compiler.

	// Round 1.
	for i := uint(0); i < 16; i++ {
		x := i
		s := shift1[i%4]
		f := ((c ^ d) & b) ^ d
		a += f + X[x]
		a = a<<s | a>>(32-s)
		a, b, c, d = d, a, b, c
	}

	// Round 2.
	for i := uint(0); i < 16; i++ {
		x := xIndex2[i]
		s := shift2[i%4]
		g := (b & c) | (b & d) | (c & d)
		a += g + X[x] + 0x5a827999
		a = a<<s | a>>(32-s)
		a, b, c, d = d, a, b, c
	}

	// Round 3.
	for i := uint(0); i < 16; i++ {
		x := xIndex3[i]
		s := shift3[i%4]
		h := b ^ c ^ d
		a += h + X[x] + 0x6ed9eba1
		a = a<<s | a>>(32-s)
		a, b, c, d = d, a, b, c
	}

	a += aa
	b += bb
	c += cc
	d += dd

	h[0] = a
	h[1] = b
	h[2] = c
	h[3] = d
}