Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ginkoid committed Jun 18, 2021
0 parents commit 687e3c9
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: CI
on:
push:
tags:
- v*
pull_request:
jobs:
linux:
runs-on: ubuntu-20.04
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.16.5
- uses: actions/checkout@v2
- name: go build
run: go build -v -ldflags '-w -s -extldflags -static' -o redpwnpow-linux-amd64 ./cmd/redpwnpow
- uses: actions/upload-artifact@v2
with:
name: linux
path: redpwnpow-linux-*
darwin:
runs-on: macos-10.15
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.16.5
- uses: actions/checkout@v2
- name: go build
run: |
lib_tmp=$(mktemp -d)
cp /usr/local/opt/gmp/lib/libgmp.a "$lib_tmp"
go build -v -ldflags "-w -s -extldflags -L$lib_tmp" -o redpwnpow-darwin-amd64 ./cmd/redpwnpow
- uses: actions/upload-artifact@v2
with:
name: darwin
path: redpwnpow-darwin-*
windows:
runs-on: windows-2019
steps:
- uses: msys2/setup-msys2@v2
with:
install: mingw-w64-x86_64-go mingw-w64-x86_64-gcc mingw-w64-x86_64-gmp
- uses: actions/checkout@v2
- name: go build
shell: msys2 {0}
run: go build -v -ldflags '-w -s -extldflags -static' -o redpwnpow-windows-amd64.exe ./cmd/redpwnpow
- uses: actions/upload-artifact@v2
with:
name: windows
path: redpwnpow-windows-*
release:
runs-on: ubuntu-20.04
if: startsWith(github.ref, 'refs/tags/v')
needs:
- linux
- darwin
- windows
steps:
- uses: actions/download-artifact@v2
- uses: softprops/action-gh-release@v1
with:
files: */redpwnpow-*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2021, redpwn
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. 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.

3. Neither the name of the copyright holder 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 HOLDER 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.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# redpwn/pow

redpwn/pow is used by [redpwn/jail](https://github.com/redpwn/jail).
28 changes: 28 additions & 0 deletions cmd/redpwnpow/redpwnpow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/redpwn/pow"
)

func run() error {
if len(os.Args) < 2 {
return errors.New("usage: redpwnpow challenge")
}
c, err := pow.DecodeChallenge(os.Args[1])
if err != nil {
return fmt.Errorf("decode challenge: %w", err)
}
fmt.Println(c.Solve())
return nil
}

func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
21 changes: 21 additions & 0 deletions cmd/redpwnpow/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh
# redpwnpow proof of work runner
# https://github.com/redpwn/pow/blob/master/cmd/redpwnpow/run.sh

set -e
version=v0.0.1
challenge=$1
run() {
cache_root=$HOME/.cache/redpwnpow
mkdir -p "$cache_root"
cache_path="$cache_root/redpwnpow-$version"
case $(uname | tr '[:upper:]' '[:lower:]') in
linux*) release=linux-amd64;;
darwin*) release=darwin-amd64;;
msys*|mingw*|cygwin*) release=windows-amd64.exe;;
*) echo unknown operating system >&2; exit 1
esac
[ -e "$cache_path" ] || curl -sSfLo "$cache_path" "https://github.com/redpwn/pow/releases/download/$version/redpwnpow-$release" && chmod u+x "$cache_path"
"$cache_path" "$challenge"
}
run
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/redpwn/pow

go 1.16

require github.com/ncw/gmp v1.0.4
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/ncw/gmp v1.0.4 h1:/f+vRpbpMIqDWfTGqYgCIuhoVfiyVf0ygsnwayqjGwU=
github.com/ncw/gmp v1.0.4/go.mod h1:cDbCx93DFhzP32H3rnwwt6QnIXNL5wu4jLPCNaExheI=
108 changes: 108 additions & 0 deletions pow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package pow

import (
"crypto/rand"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"strings"

"github.com/ncw/gmp"
)

const version = "s"

var (
mod = gmp.NewInt(0)
exp = gmp.NewInt(0)
one = gmp.NewInt(1)
two = gmp.NewInt(2)
)

func init() {
mod.Lsh(two, 1278)
exp.Div(mod, gmp.NewInt(4))
mod.Sub(mod, one)
}

type challenge struct {
d uint32
x *gmp.Int
}

func DecodeChallenge(v string) (*challenge, error) {
parts := strings.SplitN(v, ".", 3)
if len(parts) != 3 || parts[0] != version {
return nil, errors.New("incorrect version")
}
dBytes, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
if len(dBytes) > 4 {
return nil, errors.New("difficulty too long")
}
// pad start with 0s to 4 bytes
dBytes = append(make([]byte, 4-len(dBytes)), dBytes...)
xBytes, err := base64.StdEncoding.DecodeString(parts[2])
if err != nil {
return nil, err
}
d := binary.BigEndian.Uint32(dBytes)
x := gmp.NewInt(0).SetBytes(xBytes)
return &challenge{d: d, x: x}, nil
}

func GenerateChallenge(d uint32) *challenge {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
panic(err)
}
return &challenge{
x: gmp.NewInt(0).SetBytes(b),
d: d,
}
}

func (c *challenge) String() string {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, c.d)
return fmt.Sprintf("%s.%s.%s", version, base64.StdEncoding.EncodeToString(b), base64.StdEncoding.EncodeToString(c.x.Bytes()))
}

func (c challenge) Solve() string {
for i := uint32(0); i < c.d; i += 1 {
c.x.Exp(c.x, exp, mod)
c.x.Xor(c.x, one)
}
return fmt.Sprintf("%s.%s", version, base64.StdEncoding.EncodeToString(c.x.Bytes()))
}

func decodeSolution(s string) (*gmp.Int, error) {
parts := strings.SplitN(s, ".", 2)
if len(parts) != 2 || parts[0] != version {
return nil, errors.New("incorrect version")
}
yBytes, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
return gmp.NewInt(0).SetBytes(yBytes), nil
}

func (c challenge) Check(s string) (bool, error) {
y, err := decodeSolution(s)
if err != nil {
return false, fmt.Errorf("decode solution: %w", err)
}
for i := uint32(0); i < c.d; i += 1 {
y.Xor(y, one)
y.Exp(y, two, mod)
}
if c.x.Cmp(y) == 0 {
return true, nil
}
c.x.Sub(mod, c.x)
return c.x.Cmp(y) == 0, nil
}

0 comments on commit 687e3c9

Please sign in to comment.