pax_global_header00006660000000000000000000000064142270763740014526gustar00rootroot0000000000000052 comment=848274684a52026a2e7b6815760fe7158d5ca899 md4-0.1.2/000077500000000000000000000000001422707637400122125ustar00rootroot00000000000000md4-0.1.2/.github/000077500000000000000000000000001422707637400135525ustar00rootroot00000000000000md4-0.1.2/.github/workflows/000077500000000000000000000000001422707637400156075ustar00rootroot00000000000000md4-0.1.2/.github/workflows/ci.yml000066400000000000000000000032711422707637400167300ustar00rootroot00000000000000name: ci permissions: contents: read on: push: branches: - main pull_request: schedule: - cron: "33 11 * * 6" jobs: test: strategy: matrix: go-version: [1.17.x, 1.18.x] platform: [ubuntu-latest] tags: ["", "purego"] runs-on: ${{ matrix.platform }} env: GOFLAGS: "-tags=${{ matrix.tags }}" steps: - name: Install Go uses: actions/setup-go@37335c7bb261b353407cff977110895fa0b4f7d8 # v2.1.3 with: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # v2.3.4 with: persist-credentials: false - name: Build run: go build - name: Test run: go test -v -bench . -benchtime=1x - name: Comparison Tests working-directory: internal/tests run: go test -v -bench . -benchtime=1x lint: runs-on: ubuntu-latest steps: - name: Install Go uses: actions/setup-go@37335c7bb261b353407cff977110895fa0b4f7d8 # v2.1.3 with: go-version: 1.18.x - name: Configure Go Environment run: | echo GOPATH=${{ runner.workspace }} >> $GITHUB_ENV echo ${{ runner.workspace }}/bin >> $GITHUB_PATH - name: Checkout code uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # v2.3.4 with: persist-credentials: false - name: Bootstrap run: ./script/bootstrap - name: Lint run: ./script/lint - name: Generate run: ./script/generate - name: Git Status run: | git diff test -z "$(git status --porcelain)" md4-0.1.2/.golangci.yml000066400000000000000000000015101422707637400145730ustar00rootroot00000000000000linters: disable-all: true enable: - asciicheck - deadcode - depguard - dupl - errcheck - errorlint - exhaustive - exportloopref - forbidigo - forcetypeassert - gci - gocognit - gocritic - gocyclo - godot - godox - gofmt - gofumpt - goimports - goprintffuncname - gosec - gosimple - govet - importas - ineffassign - makezero - misspell - nakedret - nestif - nilerr - predeclared - revive - staticcheck - structcheck - stylecheck - thelper - typecheck - unconvert - unparam - unused - varcheck - wastedassign - whitespace linters-settings: depguard: list-type: whitelist packages: - github.com/mmcloughlin/md4 issues: exclude-use-default: false md4-0.1.2/LICENSE000066400000000000000000000027071422707637400132250ustar00rootroot00000000000000Copyright (c) 2009 The Go Authors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Google Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. md4-0.1.2/README.md000066400000000000000000000036711422707637400135000ustar00rootroot00000000000000# md4 [![Go Reference](https://pkg.go.dev/badge/github.com/mmcloughlin/md4.svg)](https://pkg.go.dev/github.com/mmcloughlin/md4) MD4 hash algorithm in Go. Assembly-optimized for amd64 platforms. MD4 is cryptographically broken and should should only be used where compatibility with legacy systems, not security, is the goal. ## Thanks This package is based up the [MD4 implementation in `x/crypto`](https://github.com/golang/crypto/tree/32db794688a5a24a23a43f2a984cecd5b3d8da58/md4) together with some internals borrowed from [`crypto/md5`](https://github.com/golang/go/tree/a1938435d6361dcbc93a15ce0ace28748a45b85d/src/crypto/md5), which is structurally very similar. For assembly micro-optimization it was very helpful to consult the implementations of MD5 in [Go](https://github.com/golang/go/blob/a1938435d6361dcbc93a15ce0ace28748a45b85d/src/crypto/md5/md5block_amd64.s) and [OpenSSL](https://github.com/openssl/openssl/blob/d4458e59f62b0d102069e53da41f1d5305a66912/crypto/md5/asm/md5-x86_64.pl), [Marc Bevand](https://www.zorinaq.com/)'s assembly [MD5](http://www.zorinaq.com/papers/md5-amd64.html), and [Nayuki](https://www.nayuki.io)'s optimized [MD4](https://github.com/nayuki/Native-hashes-for-Java/blob/0f4a908b8b2edc3c0e4f778dee4de2c189465304/native/md4-compress-x8664.S)/[MD5](https://www.nayuki.io/page/fast-md5-hash-implementation-in-x86-assembly). [Ricardo Branco](https://github.com/ricardobranco777)'s [OpenSSL bindings](https://github.com/ricardobranco777/go-openssl) were very helpful for differential testing and providing a performance baseline. Lastly, thanks to [Michael Stapelberg](https://michael.stapelberg.ch) for providing the [motivation for this package](https://twitter.com/zekjur/status/1433878940154843145), to optimize his [rsync daemon](https://github.com/gokrazy/rsync). ## License `md4` is available under the [BSD 3-Clause License](LICENSE). The license retains the copyright notice from [`x/crypto`](https://golang.org/x/crypto). md4-0.1.2/asm/000077500000000000000000000000001422707637400127725ustar00rootroot00000000000000md4-0.1.2/asm/asm.go000066400000000000000000000042401422707637400141010ustar00rootroot00000000000000// Copyright 2021 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. package main import ( . "github.com/mmcloughlin/avo/build" . "github.com/mmcloughlin/avo/operand" . "github.com/mmcloughlin/avo/reg" ) func main() { ConstraintExpr("amd64,!purego") TEXT("block", 0, "func(h *[4]uint32, m []byte)") Doc("block SHA-1 hashes the 64-byte message m into the running state h.") h := Mem{Base: Load(Param("h"), GP64())} m := Mem{Base: Load(Param("m").Base(), GP64())} X := func(i int) Mem { return m.Offset(i * 4) } Comment("Load initial hash.") hash := [4]Register{GP32(), GP32(), GP32(), GP32()} for i, r := range hash { MOVL(h.Offset(4*i), r) } Comment("Initialize registers.") a, b, c, d := GP32(), GP32(), GP32(), GP32() for i, r := range []Register{a, b, c, d} { MOVL(hash[i], r) } // Generate round updates. third := []struct { i []int s []int F func(Register, Register, Register) Register K uint32 }{ { i: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, s: []int{3, 7, 11, 19}, F: choose, K: 0, }, { i: []int{0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15}, s: []int{3, 5, 9, 13}, F: majority, K: 0x5a827999, }, { i: []int{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}, s: []int{3, 9, 11, 15}, F: xor, K: 0x6ed9eba1, }, } for r := 0; r < 48; r++ { Commentf("Round %d.", r) t := third[r/16] ADDL(X(t.i[r%16]), a) if t.K != 0 { ADDL(U32(t.K), a) } ADDL(t.F(b, c, d), a) ROLL(U8(t.s[r%4]), a) a, b, c, d = d, a, b, c } Comment("Final add.") for i, r := range []Register{a, b, c, d} { ADDL(r, hash[i]) } Comment("Store results back.") for i, r := range hash { MOVL(r, h.Offset(4*i)) } RET() Generate() } func choose(b, c, d Register) Register { r := GP32() MOVL(d, r) XORL(c, r) ANDL(b, r) XORL(d, r) return r } func majority(b, c, d Register) Register { t, r := GP32(), GP32() MOVL(c, t) ORL(d, t) ANDL(b, t) MOVL(c, r) ANDL(d, r) ORL(t, r) return r } func xor(b, c, d Register) Register { r := GP32() MOVL(c, r) XORL(d, r) XORL(b, r) return r } md4-0.1.2/asm/go.mod000066400000000000000000000005341422707637400141020ustar00rootroot00000000000000module github.com/mmcloughlin/md4/asm go 1.17 require github.com/mmcloughlin/avo v0.4.0 require ( golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect golang.org/x/tools v0.1.10 // indirect golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect ) md4-0.1.2/asm/go.sum000066400000000000000000000104601422707637400141260ustar00rootroot00000000000000github.com/mmcloughlin/avo v0.4.0 h1:jeHDRktVD+578ULxWpQHkilor6pkdLF7u7EiTzDbfcU= github.com/mmcloughlin/avo v0.4.0/go.mod h1:RW9BfYA3TgO9uCdNrKU2h6J8cPD8ZLznvfgHAeszb1s= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o= golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211030160813-b3129d9d1021/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20= golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U= golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= md4-0.1.2/block.go000066400000000000000000000034351422707637400136400ustar00rootroot00000000000000// 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<>(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<>(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<>(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 } md4-0.1.2/block_amd64.go000066400000000000000000000004001422707637400146200ustar00rootroot00000000000000// Code generated by command: go run asm.go -out block_amd64.s -stubs block_amd64.go -pkg md4. DO NOT EDIT. //go:build amd64 && !purego package md4 // block SHA-1 hashes the 64-byte message m into the running state h. func block(h *[4]uint32, m []byte) md4-0.1.2/block_amd64.s000066400000000000000000000162541422707637400144730ustar00rootroot00000000000000// Code generated by command: go run asm.go -out block_amd64.s -stubs block_amd64.go -pkg md4. DO NOT EDIT. //go:build amd64 && !purego // func block(h *[4]uint32, m []byte) TEXT ·block(SB), $0-32 MOVQ h+0(FP), AX MOVQ m_base+8(FP), CX // Load initial hash. MOVL (AX), DX MOVL 4(AX), BX MOVL 8(AX), SI MOVL 12(AX), DI // Initialize registers. MOVL DX, R8 MOVL BX, R9 MOVL SI, R10 MOVL DI, R11 // Round 0. ADDL (CX), R8 MOVL R11, R12 XORL R10, R12 ANDL R9, R12 XORL R11, R12 ADDL R12, R8 ROLL $0x03, R8 // Round 1. ADDL 4(CX), R11 MOVL R10, R12 XORL R9, R12 ANDL R8, R12 XORL R10, R12 ADDL R12, R11 ROLL $0x07, R11 // Round 2. ADDL 8(CX), R10 MOVL R9, R12 XORL R8, R12 ANDL R11, R12 XORL R9, R12 ADDL R12, R10 ROLL $0x0b, R10 // Round 3. ADDL 12(CX), R9 MOVL R8, R12 XORL R11, R12 ANDL R10, R12 XORL R8, R12 ADDL R12, R9 ROLL $0x13, R9 // Round 4. ADDL 16(CX), R8 MOVL R11, R12 XORL R10, R12 ANDL R9, R12 XORL R11, R12 ADDL R12, R8 ROLL $0x03, R8 // Round 5. ADDL 20(CX), R11 MOVL R10, R12 XORL R9, R12 ANDL R8, R12 XORL R10, R12 ADDL R12, R11 ROLL $0x07, R11 // Round 6. ADDL 24(CX), R10 MOVL R9, R12 XORL R8, R12 ANDL R11, R12 XORL R9, R12 ADDL R12, R10 ROLL $0x0b, R10 // Round 7. ADDL 28(CX), R9 MOVL R8, R12 XORL R11, R12 ANDL R10, R12 XORL R8, R12 ADDL R12, R9 ROLL $0x13, R9 // Round 8. ADDL 32(CX), R8 MOVL R11, R12 XORL R10, R12 ANDL R9, R12 XORL R11, R12 ADDL R12, R8 ROLL $0x03, R8 // Round 9. ADDL 36(CX), R11 MOVL R10, R12 XORL R9, R12 ANDL R8, R12 XORL R10, R12 ADDL R12, R11 ROLL $0x07, R11 // Round 10. ADDL 40(CX), R10 MOVL R9, R12 XORL R8, R12 ANDL R11, R12 XORL R9, R12 ADDL R12, R10 ROLL $0x0b, R10 // Round 11. ADDL 44(CX), R9 MOVL R8, R12 XORL R11, R12 ANDL R10, R12 XORL R8, R12 ADDL R12, R9 ROLL $0x13, R9 // Round 12. ADDL 48(CX), R8 MOVL R11, R12 XORL R10, R12 ANDL R9, R12 XORL R11, R12 ADDL R12, R8 ROLL $0x03, R8 // Round 13. ADDL 52(CX), R11 MOVL R10, R12 XORL R9, R12 ANDL R8, R12 XORL R10, R12 ADDL R12, R11 ROLL $0x07, R11 // Round 14. ADDL 56(CX), R10 MOVL R9, R12 XORL R8, R12 ANDL R11, R12 XORL R9, R12 ADDL R12, R10 ROLL $0x0b, R10 // Round 15. ADDL 60(CX), R9 MOVL R8, R12 XORL R11, R12 ANDL R10, R12 XORL R8, R12 ADDL R12, R9 ROLL $0x13, R9 // Round 16. ADDL (CX), R8 ADDL $0x5a827999, R8 MOVL R10, R12 ORL R11, R12 ANDL R9, R12 MOVL R10, R13 ANDL R11, R13 ORL R12, R13 ADDL R13, R8 ROLL $0x03, R8 // Round 17. ADDL 16(CX), R11 ADDL $0x5a827999, R11 MOVL R9, R12 ORL R10, R12 ANDL R8, R12 MOVL R9, R13 ANDL R10, R13 ORL R12, R13 ADDL R13, R11 ROLL $0x05, R11 // Round 18. ADDL 32(CX), R10 ADDL $0x5a827999, R10 MOVL R8, R12 ORL R9, R12 ANDL R11, R12 MOVL R8, R13 ANDL R9, R13 ORL R12, R13 ADDL R13, R10 ROLL $0x09, R10 // Round 19. ADDL 48(CX), R9 ADDL $0x5a827999, R9 MOVL R11, R12 ORL R8, R12 ANDL R10, R12 MOVL R11, R13 ANDL R8, R13 ORL R12, R13 ADDL R13, R9 ROLL $0x0d, R9 // Round 20. ADDL 4(CX), R8 ADDL $0x5a827999, R8 MOVL R10, R12 ORL R11, R12 ANDL R9, R12 MOVL R10, R13 ANDL R11, R13 ORL R12, R13 ADDL R13, R8 ROLL $0x03, R8 // Round 21. ADDL 20(CX), R11 ADDL $0x5a827999, R11 MOVL R9, R12 ORL R10, R12 ANDL R8, R12 MOVL R9, R13 ANDL R10, R13 ORL R12, R13 ADDL R13, R11 ROLL $0x05, R11 // Round 22. ADDL 36(CX), R10 ADDL $0x5a827999, R10 MOVL R8, R12 ORL R9, R12 ANDL R11, R12 MOVL R8, R13 ANDL R9, R13 ORL R12, R13 ADDL R13, R10 ROLL $0x09, R10 // Round 23. ADDL 52(CX), R9 ADDL $0x5a827999, R9 MOVL R11, R12 ORL R8, R12 ANDL R10, R12 MOVL R11, R13 ANDL R8, R13 ORL R12, R13 ADDL R13, R9 ROLL $0x0d, R9 // Round 24. ADDL 8(CX), R8 ADDL $0x5a827999, R8 MOVL R10, R12 ORL R11, R12 ANDL R9, R12 MOVL R10, R13 ANDL R11, R13 ORL R12, R13 ADDL R13, R8 ROLL $0x03, R8 // Round 25. ADDL 24(CX), R11 ADDL $0x5a827999, R11 MOVL R9, R12 ORL R10, R12 ANDL R8, R12 MOVL R9, R13 ANDL R10, R13 ORL R12, R13 ADDL R13, R11 ROLL $0x05, R11 // Round 26. ADDL 40(CX), R10 ADDL $0x5a827999, R10 MOVL R8, R12 ORL R9, R12 ANDL R11, R12 MOVL R8, R13 ANDL R9, R13 ORL R12, R13 ADDL R13, R10 ROLL $0x09, R10 // Round 27. ADDL 56(CX), R9 ADDL $0x5a827999, R9 MOVL R11, R12 ORL R8, R12 ANDL R10, R12 MOVL R11, R13 ANDL R8, R13 ORL R12, R13 ADDL R13, R9 ROLL $0x0d, R9 // Round 28. ADDL 12(CX), R8 ADDL $0x5a827999, R8 MOVL R10, R12 ORL R11, R12 ANDL R9, R12 MOVL R10, R13 ANDL R11, R13 ORL R12, R13 ADDL R13, R8 ROLL $0x03, R8 // Round 29. ADDL 28(CX), R11 ADDL $0x5a827999, R11 MOVL R9, R12 ORL R10, R12 ANDL R8, R12 MOVL R9, R13 ANDL R10, R13 ORL R12, R13 ADDL R13, R11 ROLL $0x05, R11 // Round 30. ADDL 44(CX), R10 ADDL $0x5a827999, R10 MOVL R8, R12 ORL R9, R12 ANDL R11, R12 MOVL R8, R13 ANDL R9, R13 ORL R12, R13 ADDL R13, R10 ROLL $0x09, R10 // Round 31. ADDL 60(CX), R9 ADDL $0x5a827999, R9 MOVL R11, R12 ORL R8, R12 ANDL R10, R12 MOVL R11, R13 ANDL R8, R13 ORL R12, R13 ADDL R13, R9 ROLL $0x0d, R9 // Round 32. ADDL (CX), R8 ADDL $0x6ed9eba1, R8 MOVL R10, R12 XORL R11, R12 XORL R9, R12 ADDL R12, R8 ROLL $0x03, R8 // Round 33. ADDL 32(CX), R11 ADDL $0x6ed9eba1, R11 MOVL R9, R12 XORL R10, R12 XORL R8, R12 ADDL R12, R11 ROLL $0x09, R11 // Round 34. ADDL 16(CX), R10 ADDL $0x6ed9eba1, R10 MOVL R8, R12 XORL R9, R12 XORL R11, R12 ADDL R12, R10 ROLL $0x0b, R10 // Round 35. ADDL 48(CX), R9 ADDL $0x6ed9eba1, R9 MOVL R11, R12 XORL R8, R12 XORL R10, R12 ADDL R12, R9 ROLL $0x0f, R9 // Round 36. ADDL 8(CX), R8 ADDL $0x6ed9eba1, R8 MOVL R10, R12 XORL R11, R12 XORL R9, R12 ADDL R12, R8 ROLL $0x03, R8 // Round 37. ADDL 40(CX), R11 ADDL $0x6ed9eba1, R11 MOVL R9, R12 XORL R10, R12 XORL R8, R12 ADDL R12, R11 ROLL $0x09, R11 // Round 38. ADDL 24(CX), R10 ADDL $0x6ed9eba1, R10 MOVL R8, R12 XORL R9, R12 XORL R11, R12 ADDL R12, R10 ROLL $0x0b, R10 // Round 39. ADDL 56(CX), R9 ADDL $0x6ed9eba1, R9 MOVL R11, R12 XORL R8, R12 XORL R10, R12 ADDL R12, R9 ROLL $0x0f, R9 // Round 40. ADDL 4(CX), R8 ADDL $0x6ed9eba1, R8 MOVL R10, R12 XORL R11, R12 XORL R9, R12 ADDL R12, R8 ROLL $0x03, R8 // Round 41. ADDL 36(CX), R11 ADDL $0x6ed9eba1, R11 MOVL R9, R12 XORL R10, R12 XORL R8, R12 ADDL R12, R11 ROLL $0x09, R11 // Round 42. ADDL 20(CX), R10 ADDL $0x6ed9eba1, R10 MOVL R8, R12 XORL R9, R12 XORL R11, R12 ADDL R12, R10 ROLL $0x0b, R10 // Round 43. ADDL 52(CX), R9 ADDL $0x6ed9eba1, R9 MOVL R11, R12 XORL R8, R12 XORL R10, R12 ADDL R12, R9 ROLL $0x0f, R9 // Round 44. ADDL 12(CX), R8 ADDL $0x6ed9eba1, R8 MOVL R10, R12 XORL R11, R12 XORL R9, R12 ADDL R12, R8 ROLL $0x03, R8 // Round 45. ADDL 44(CX), R11 ADDL $0x6ed9eba1, R11 MOVL R9, R12 XORL R10, R12 XORL R8, R12 ADDL R12, R11 ROLL $0x09, R11 // Round 46. ADDL 28(CX), R10 ADDL $0x6ed9eba1, R10 MOVL R8, R12 XORL R9, R12 XORL R11, R12 ADDL R12, R10 ROLL $0x0b, R10 // Round 47. ADDL 60(CX), R9 ADDL $0x6ed9eba1, R9 MOVL R11, CX XORL R8, CX XORL R10, CX ADDL CX, R9 ROLL $0x0f, R9 // Final add. ADDL R8, DX ADDL R9, BX ADDL R10, SI ADDL R11, DI // Store results back. MOVL DX, (AX) MOVL BX, 4(AX) MOVL SI, 8(AX) MOVL DI, 12(AX) RET md4-0.1.2/example_test.go000066400000000000000000000006351422707637400152370ustar00rootroot00000000000000// Copyright 2017 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. package md4_test import ( "fmt" "github.com/mmcloughlin/md4" ) func ExampleNew() { h := md4.New() data := "These pretzels are making me thirsty." h.Write([]byte(data)) fmt.Printf("%x", h.Sum(nil)) // Output: 48c4e365090b30a32f084c4888deceaa } md4-0.1.2/go.mod000066400000000000000000000000531422707637400133160ustar00rootroot00000000000000module github.com/mmcloughlin/md4 go 1.17 md4-0.1.2/internal/000077500000000000000000000000001422707637400140265ustar00rootroot00000000000000md4-0.1.2/internal/tests/000077500000000000000000000000001422707637400151705ustar00rootroot00000000000000md4-0.1.2/internal/tests/bench_test.go000066400000000000000000000021301422707637400176310ustar00rootroot00000000000000// Copyright 2021 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. package tests import ( "fmt" "hash" "testing" mmcloughlin "github.com/mmcloughlin/md4" openssl "github.com/ricardobranco777/go-openssl/md4" xcrypto "golang.org/x/crypto/md4" ) var buf = make([]byte, 1<<20) func BenchmarkHash(b *testing.B) { hashes := []struct { Name string New func() hash.Hash }{ {"xcrypto", xcrypto.New}, {"mmcloughlin", mmcloughlin.New}, {"openssl", openssl.New}, } sizes := []struct { Name string Bytes int64 }{ {"8Bytes", 8}, {"1K", 1 << 10}, {"8K", 8 << 10}, {"16K", 16 << 10}, {"32K", 32 << 10}, {"1M", 1 << 20}, } for _, size := range sizes { for _, impl := range hashes { name := fmt.Sprintf("size=%s/pkg=%s", size.Name, impl.Name) b.Run(name, func(b *testing.B) { h := impl.New() b.SetBytes(int64(size.Bytes)) sum := make([]byte, h.Size()) for i := 0; i < b.N; i++ { h.Reset() h.Write(buf[:size.Bytes]) h.Sum(sum[:0]) } }) } } } md4-0.1.2/internal/tests/cmp_test.go000066400000000000000000000012711422707637400173360ustar00rootroot00000000000000// Copyright 2021 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. package tests import ( "bytes" "hash" "math/rand" "testing" "github.com/mmcloughlin/md4" openssl "github.com/ricardobranco777/go-openssl/md4" ) func TestLengths(t *testing.T) { N := 1 << 14 buf := make([]byte, N) for n := 0; n < N; n++ { // Initialize random buffer. b := buf[:n] rand.Read(b) // Expected hash. expect := sum(openssl.New(), b) got := sum(md4.New(), b) if !bytes.Equal(expect, got) { t.FailNow() } } } func sum(h hash.Hash, b []byte) []byte { h.Reset() h.Write(b) return h.Sum(nil) } md4-0.1.2/internal/tests/go.mod000066400000000000000000000004661422707637400163040ustar00rootroot00000000000000module github.com/mmcloughlin/md4/internal/tests go 1.17 require ( github.com/mmcloughlin/md4 v0.0.0-00010101000000-000000000000 github.com/ricardobranco777/go-openssl v0.0.0-20200811151806-76963704f0b8 golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 ) replace github.com/mmcloughlin/md4 => ../.. md4-0.1.2/internal/tests/go.sum000066400000000000000000000020721422707637400163240ustar00rootroot00000000000000github.com/ricardobranco777/go-openssl v0.0.0-20200811151806-76963704f0b8 h1:0Y2Z+xvCjtgJpIodNh4nnz74wXuOyUp8iR6GbcswZvs= github.com/ricardobranco777/go-openssl v0.0.0-20200811151806-76963704f0b8/go.mod h1:kNLi9TxwiUIMVhx/N7YT0lcO/kzP0eN8vAijOFN0pjA= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= md4-0.1.2/md4.go000066400000000000000000000060161422707637400132300ustar00rootroot00000000000000// 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. // Package md4 implements the MD4 hash algorithm as defined in RFC 1320. // // MD4 is cryptographically broken and should should only be used where // compatibility with legacy systems, not security, is the goal. Instead, use a // secure hash like SHA-256 (from crypto/sha256). package md4 import ( "crypto" "encoding/binary" "hash" ) //go:generate go run -modfile=asm/go.mod asm/asm.go -out block_amd64.s -stubs block_amd64.go -pkg md4 func init() { crypto.RegisterHash(crypto.MD4, New) } // Size of an MD4 checksum in bytes. const Size = 16 // BlockSize is the block size of MD4 in bytes. const BlockSize = 64 const ( init0 = 0x67452301 init1 = 0xEFCDAB89 init2 = 0x98BADCFE init3 = 0x10325476 ) // digest represents the partial evaluation of a checksum. type digest struct { s [4]uint32 x [BlockSize]byte nx int len uint64 } func (d *digest) Reset() { d.s[0] = init0 d.s[1] = init1 d.s[2] = init2 d.s[3] = init3 d.nx = 0 d.len = 0 } // New returns a new hash.Hash computing the MD4 checksum. func New() hash.Hash { d := new(digest) d.Reset() return d } func (d *digest) Size() int { return Size } func (d *digest) BlockSize() int { return BlockSize } func (d *digest) Write(p []byte) (int, error) { return d.write(p), nil } func (d *digest) write(p []byte) int { nn := len(p) d.len += uint64(nn) if d.nx > 0 { n := len(p) if n > BlockSize-d.nx { n = BlockSize - d.nx } for i := 0; i < n; i++ { d.x[d.nx+i] = p[i] } d.nx += n if d.nx == BlockSize { block(&d.s, d.x[0:]) d.nx = 0 } p = p[n:] } for len(p) >= BlockSize { block(&d.s, p) p = p[BlockSize:] } if len(p) > 0 { d.nx = copy(d.x[:], p) } return nn } func (d *digest) Sum(in []byte) []byte { // Make a copy of d so that caller can keep writing and summing. d0 := *d hash := d0.checkSum() return append(in, hash[:]...) } func (d *digest) checkSum() [Size]byte { // Append 0x80 to the end of the message and then append zeros // until the length is a multiple of 56 bytes. Finally append // 8 bytes representing the message length in bits. // // 1 byte end marker :: 0-63 padding bytes :: 8 byte length tmp := [1 + 63 + 8]byte{0x80} pad := (55 - d.len) % 64 // calculate number of padding bytes binary.LittleEndian.PutUint64(tmp[1+pad:], d.len<<3) // append length in bits d.write(tmp[:1+pad+8]) // The previous write ensures that a whole number of // blocks (i.e. a multiple of 64 bytes) have been hashed. if d.nx != 0 { panic("d.nx != 0") } var digest [Size]byte binary.LittleEndian.PutUint32(digest[0:], d.s[0]) binary.LittleEndian.PutUint32(digest[4:], d.s[1]) binary.LittleEndian.PutUint32(digest[8:], d.s[2]) binary.LittleEndian.PutUint32(digest[12:], d.s[3]) return digest } // Sum returns the MD4 checksum of the data. func Sum(data []byte) [Size]byte { var d digest d.Reset() d.write(data) return d.checkSum() } md4-0.1.2/md4_test.go000066400000000000000000000103711422707637400142660ustar00rootroot00000000000000// 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. package md4 import ( "fmt" "testing" ) type md4Test struct { out string in string } var golden = []md4Test{ {"31d6cfe0d16ae931b73c59d7e0c089c0", ""}, {"bde52cb31de33e46245e05fbdbd6fb24", "a"}, {"ec388dd78999dfc7cf4632465693b6bf", "ab"}, {"a448017aaf21d8525fc10ae87aa6729d", "abc"}, {"41decd8f579255c5200f86a4bb3ba740", "abcd"}, {"9803f4a34e8eb14f96adba49064a0c41", "abcde"}, {"804e7f1c2586e50b49ac65db5b645131", "abcdef"}, {"752f4adfe53d1da0241b5bc216d098fc", "abcdefg"}, {"ad9daf8d49d81988590a6f0e745d15dd", "abcdefgh"}, {"1e4e28b05464316b56402b3815ed2dfd", "abcdefghi"}, {"dc959c6f5d6f9e04e4380777cc964b3d", "abcdefghij"}, {"1b5701e265778898ef7de5623bbe7cc0", "Discard medicine more than two years old."}, {"d7f087e090fe7ad4a01cb59dacc9a572", "He who has a shady past knows that nice guys finish last."}, {"a6f8fd6df617c72837592fc3570595c9", "I wouldn't marry him with a ten foot pole."}, {"c92a84a9526da8abc240c05d6b1a1ce0", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"}, {"f6013160c4dcb00847069fee3bb09803", "The days of the digital watch are numbered. -Tom Stoppard"}, {"2c3bb64f50b9107ed57640fe94bec09f", "Nepal premier won't resign."}, {"45b7d8a32c7806f2f7f897332774d6e4", "For every action there is an equal and opposite government program."}, {"b5b4f9026b175c62d7654bdc3a1cd438", "His money is twice tainted: 'taint yours and 'taint mine."}, {"caf44e80f2c20ce19b5ba1cab766e7bd", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"}, {"191fae6707f496aa54a6bce9f2ecf74d", "It's a tiny change to the code and not completely disgusting. - Bob Manchek"}, {"9ddc753e7a4ccee6081cd1b45b23a834", "size: a.out: bad magic"}, {"8d050f55b1cadb9323474564be08a521", "The major problem is with sendmail. -Mark Horton"}, {"ad6e2587f74c3e3cc19146f6127fa2e3", "Give me a rock, paper and scissors and I will move the world. CCFestoon"}, {"1d616d60a5fabe85589c3f1566ca7fca", "If the enemy is within range, then so are you."}, {"aec3326a4f496a2ced65a1963f84577f", "It's well we cannot hear the screams/That we create in others' dreams."}, {"77b4fd762d6b9245e61c50bf6ebf118b", "You remind me of a TV show, but that's all right: I watch it anyway."}, {"e8f48c726bae5e516f6ddb1a4fe62438", "C is as portable as Stonehedge!!"}, {"a3a84366e7219e887423b01f9be7166e", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"}, {"a6b7aa35157e984ef5d9b7f32e5fbb52", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"}, {"75661f0545955f8f9abeeb17845f3fd6", "How can you write a big system without C++? -Paul Glick"}, } func TestGolden(t *testing.T) { for i := 0; i < len(golden); i++ { g := golden[i] c := New() for j := 0; j < 3; j++ { if j < 2 { c.Write([]byte(g.in)) } else { c.Write([]byte(g.in[0 : len(g.in)/2])) c.Sum(nil) c.Write([]byte(g.in[len(g.in)/2:])) } s := fmt.Sprintf("%x", c.Sum(nil)) if s != g.out { t.Fatalf("md4[%d](%s) = %s want %s", j, g.in, s, g.out) } c.Reset() } } } func TestSumGolden(t *testing.T) { for _, g := range golden { got := fmt.Sprintf("%x", Sum([]byte(g.in))) if got != g.out { t.Fatalf("Sum(%s) = %s want %s", g.in, got, g.out) } } } var ( bench = New() buf = make([]byte, 8<<20) sum = make([]byte, bench.Size()) ) func benchmarkSize(b *testing.B, size int) { b.Helper() b.SetBytes(int64(size)) for i := 0; i < b.N; i++ { bench.Reset() bench.Write(buf[:size]) bench.Sum(sum[:0]) } } func BenchmarkHash8Bytes(b *testing.B) { benchmarkSize(b, 8) } func BenchmarkHash64(b *testing.B) { benchmarkSize(b, 64) } func BenchmarkHash128(b *testing.B) { benchmarkSize(b, 128) } func BenchmarkHash256(b *testing.B) { benchmarkSize(b, 256) } func BenchmarkHash512(b *testing.B) { benchmarkSize(b, 512) } func BenchmarkHash1K(b *testing.B) { benchmarkSize(b, 1024) } func BenchmarkHash8K(b *testing.B) { benchmarkSize(b, 8192) } func BenchmarkHash1M(b *testing.B) { benchmarkSize(b, 1024*1024) } func BenchmarkHash8M(b *testing.B) { benchmarkSize(b, 8*1024*1024) } md4-0.1.2/script/000077500000000000000000000000001422707637400135165ustar00rootroot00000000000000md4-0.1.2/script/bootstrap000077500000000000000000000003701422707637400154610ustar00rootroot00000000000000#!/usr/bin/env bash set -exuo pipefail # Install golangci-lint curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b "${GOPATH}/bin" v1.42.0 # addlicense tool. go install github.com/google/addlicense@v1.0.0 md4-0.1.2/script/generate000077500000000000000000000005731422707637400152430ustar00rootroot00000000000000#!/usr/bin/env bash set -exuo pipefail # Licenses. license="$(mktemp)" cat > "${license}" <