Skip to content

Commit

Permalink
Major rewrite and API design changes
Browse files Browse the repository at this point in the history
Signed-off-by: Steffen Vogel <post@steffenvogel.de>
  • Loading branch information
stv0g committed Oct 9, 2023
1 parent 236bf16 commit c70835b
Show file tree
Hide file tree
Showing 62 changed files with 3,299 additions and 1,386 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ hawkes: ./assets/entitlements.xml
go build -o $@ ./cmd
codesign -f -s ${CODESIGN_IDENTITY} --entitlements ./assets/entitlements.xml $@

.PHONY: all hawkes
provider-test:
go test -c -o $@ ./provider
codesign -f -s ${CODESIGN_IDENTITY} --entitlements ./assets/entitlements.xml $@

.PHONY: all hawkes provider-test
109 changes: 97 additions & 12 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,117 @@ package main

import (
"encoding/base64"
"flag"
"fmt"
"log"
"log/slog"
"os"

"cunicu.li/hawkes/providers/ecdh/se"
)

var (
label = flag.String("label", "my-label", "User visible label of the generated key pair")
tag = flag.String("tag", "my-tag", "An internal tag")
se "cunicu.li/hawkes/ecdh/applese"
"cunicu.li/hawkes/ecdh/sw"
)

func main() {
if len(os.Args) < 2 {
panic("invalid usage")
slog.Error("Usage: hawkes (list|remove|genkey)")
os.Exit(-1)
}

switch os.Args[1] {
case "genkey":
pk, err := se.GenerateKeyPair(*label, *tag)
label := "label"
if len(os.Args) >= 3 {
label = os.Args[2]
}

sk, err := se.GenerateKey(label)
if err != nil {
slog.Error("Failed to generate key", slog.Any("error", err))
os.Exit(-1)
}

fmt.Println(sk.Label())

case "diffie-helman", "dh":
if len(os.Args) < 4 {
slog.Error("Usage: hawkes dh [label] [public-key]")
os.Exit(-1)
}

labelBytes, err := base64.StdEncoding.DecodeString(os.Args[2])
if err != nil {
panic(err)
slog.Error("Failed to decode", slog.Any("error", err))
os.Exit(-1)
}

fmt.Println(base64.StdEncoding.EncodeToString(pk))
label := se.KeyLabel(labelBytes)

sk, err := se.PrivateKeyByLabel(label)
if err != nil {
slog.Error("Failed to get private key", slog.Any("error", err))
os.Exit(-1)
}

pkBytes, err := base64.StdEncoding.DecodeString(os.Args[3])
if err != nil {
slog.Error("Failed to decode", slog.Any("error", err))
os.Exit(-1)
}

pk, err := sw.P256.ParsePublicKey(pkBytes)
if err != nil {
slog.Error("Failed to load public key", slog.Any("error", err))
os.Exit(-1)
}

case "shared-secret":
ss, err := sk.DH(pk)
if err != nil {
slog.Error("Failed to calc shared secret", slog.Any("error", err))
os.Exit(-1)
}

fmt.Println(base64.StdEncoding.EncodeToString(ss))

case "remove", "rm":
var err error
var hash []byte

if len(os.Args) < 3 {
slog.Error("Usage: hawkes remove [label]")
os.Exit(-1)
}

if hash, err = base64.StdEncoding.DecodeString(os.Args[2]); err != nil {
slog.Error("Failed to decode key label", slog.Any("error", err))
os.Exit(-1)
}

if ok, err := se.RemoveKey(se.KeyLabel(hash)); err != nil {
slog.Error("Failed to remove key", slog.Any("error", err))
os.Exit(-1)
} else if !ok {
slog.Warn("No matching key found")
}

case "list", "ls":
var err error
var hash []byte

if len(os.Args) > 2 {
if hash, err = base64.StdEncoding.DecodeString(os.Args[2]); err != nil {
slog.Error("Failed to decode key label", slog.Any("error", err))
os.Exit(-1)
}
}

keys, err := se.Keys(hash)
if err != nil {
slog.Error("Failed to enumerate keys", slog.Any("error", err))
os.Exit(-1)
}

for _, key := range keys {
pkStr := base64.StdEncoding.EncodeToString(key.Public().Bytes())
log.Println(key.Label(), pkStr)
}
}
}
Loading

0 comments on commit c70835b

Please sign in to comment.