interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / argon2 / test / gen / oracle / main.go

78 lines · 2364 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Test referee only (never part of the om/argon2 implementation). Reads lines
3// T M P TL PW SALT KEY AD (decimal, decimal, decimal, decimal, then hex or "-")
4// tka SEC (hex or "-")
5// and prints the Argon2i tag in lowercase hex, one line per input line.
6// Lines without KEY and AD (both "-") use the public golang.org/x/crypto/argon2.Key; lines with a
7// secret K or associated data X call the package's internal deriveKey (the function Key wraps)
8// through go:linkname, because the public API has no K and X inputs. Both paths are cross-checked
9// on every K = X = empty line. "tka SEC" is argon2.Key(SEC, "tailscale network-lock disablement
10// salt", 4, 16*1024, 4, 32), the Tailnet Lock disablement value (tailscale tka/disablement.go).
11package main
12
13import (
14 "bufio"
15 "bytes"
16 "encoding/hex"
17 "fmt"
18 "os"
19 "strconv"
20 "strings"
21 _ "unsafe"
22
23 "golang.org/x/crypto/argon2"
24)
25
26//go:linkname deriveKey golang.org/x/crypto/argon2.deriveKey
27func deriveKey(mode int, password, salt, secret, data []byte, time, memory uint32, threads uint8, keyLen uint32) []byte
28
29const argon2i = 1 // golang.org/x/crypto/argon2: argon2d = 0, argon2i = 1, argon2id = 2
30
31func field(s string) []byte {
32 if s == "-" {
33 return nil
34 }
35 b, err := hex.DecodeString(s)
36 if err != nil {
37 panic(err)
38 }
39 return b
40}
41
42func num(s string) uint32 {
43 v, err := strconv.ParseUint(s, 10, 32)
44 if err != nil {
45 panic(err)
46 }
47 return uint32(v)
48}
49
50func main() {
51 in := bufio.NewScanner(os.Stdin)
52 in.Buffer(make([]byte, 1<<20), 1<<20)
53 out := bufio.NewWriter(os.Stdout)
54 defer out.Flush()
55 for in.Scan() {
56 f := strings.Fields(in.Text())
57 if len(f) == 2 && f[0] == "tka" {
58 k := argon2.Key(field(f[1]), []byte("tailscale network-lock disablement salt"), 4, 16*1024, 4, 32)
59 fmt.Fprintf(out, "%x\n", k)
60 continue
61 }
62 if len(f) != 8 {
63 panic("bad line: " + in.Text())
64 }
65 t, m, p, tl := num(f[0]), num(f[1]), num(f[2]), num(f[3])
66 pw, salt, key, ad := field(f[4]), field(f[5]), field(f[6]), field(f[7])
67 k := deriveKey(argon2i, pw, salt, key, ad, t, m, uint8(p), tl)
68 if key == nil && ad == nil {
69 if pub := argon2.Key(pw, salt, t, m, uint8(p), tl); !bytes.Equal(pub, k) {
70 panic("argon2.Key and deriveKey disagree")
71 }
72 }
73 fmt.Fprintf(out, "%x\n", k)
74 }
75 if err := in.Err(); err != nil {
76 panic(err)
77 }
78}