This commit is contained in:
Peter Rounce 2023-09-16 12:27:02 +01:00
parent 2da9275c9a
commit 3a1262db82
2 changed files with 44 additions and 44 deletions

BIN
cli/cli

Binary file not shown.

View file

@ -1,10 +1,10 @@
package main package main
import ( import (
"os" "encoding/hex"
"fmt" "fmt"
"encoding/hex"
"github.com/boltcard/boltcard/crypto" "github.com/boltcard/boltcard/crypto"
"os"
) )
// inspired by parse_request() in lnurlw_request.go // inspired by parse_request() in lnurlw_request.go
@ -40,50 +40,50 @@ func check_cmac(uid []byte, ctr []byte, k2_cmac_key []byte, cmac []byte) (bool,
func main() { func main() {
fmt.Println("-- bolt card crypto test vectors --") fmt.Println("-- bolt card crypto test vectors --")
fmt.Println() fmt.Println()
args := os.Args[1:] args := os.Args[1:]
if(len(args) != 4) { if len(args) != 4 {
fmt.Println("error: should have arguments for: p c aes_decrypt_key aes_cmac_key") fmt.Println("error: should have arguments for: p c aes_decrypt_key aes_cmac_key")
os.Exit(1) os.Exit(1)
} }
// get from args // get from args
p_hex := args[0] p_hex := args[0]
c_hex := args[1] c_hex := args[1]
aes_decrypt_key_hex := args[2] aes_decrypt_key_hex := args[2]
aes_cmac_key_hex := args[3] aes_cmac_key_hex := args[3]
fmt.Println("p = ", p_hex) fmt.Println("p = ", p_hex)
fmt.Println("c = ", c_hex) fmt.Println("c = ", c_hex)
fmt.Println("aes_decrypt_key = ", aes_decrypt_key_hex) fmt.Println("aes_decrypt_key = ", aes_decrypt_key_hex)
fmt.Println("aes_cmac_key = ", aes_cmac_key_hex) fmt.Println("aes_cmac_key = ", aes_cmac_key_hex)
fmt.Println() fmt.Println()
p, err := hex.DecodeString(p_hex) p, err := hex.DecodeString(p_hex)
if err != nil { if err != nil {
fmt.Println("ERROR: p not valid hex", err) fmt.Println("ERROR: p not valid hex", err)
os.Exit(1) os.Exit(1)
} }
c, err := hex.DecodeString(c_hex) c, err := hex.DecodeString(c_hex)
if err != nil { if err != nil {
fmt.Println("ERROR: c not valid hex", err) fmt.Println("ERROR: c not valid hex", err)
os.Exit(1) os.Exit(1)
} }
if len(p) != 16 { if len(p) != 16 {
fmt.Println("ERROR: p length not valid") fmt.Println("ERROR: p length not valid")
os.Exit(1) os.Exit(1)
} }
if len(c) != 8 { if len(c) != 8 {
fmt.Println("ERROR: c length not valid") fmt.Println("ERROR: c length not valid")
os.Exit(1) os.Exit(1)
} }
// decrypt p with aes_decrypt_key // decrypt p with aes_decrypt_key
@ -91,20 +91,20 @@ func main() {
aes_decrypt_key, err := hex.DecodeString(aes_decrypt_key_hex) aes_decrypt_key, err := hex.DecodeString(aes_decrypt_key_hex)
if err != nil { if err != nil {
fmt.Println("ERROR: DecodeString() returned an error", err) fmt.Println("ERROR: DecodeString() returned an error", err)
os.Exit(1) os.Exit(1)
} }
dec_p, err := crypto.Aes_decrypt(aes_decrypt_key, p) dec_p, err := crypto.Aes_decrypt(aes_decrypt_key, p)
if err != nil { if err != nil {
fmt.Println("ERROR: Aes_decrypt() returned an error", err) fmt.Println("ERROR: Aes_decrypt() returned an error", err)
os.Exit(1) os.Exit(1)
} }
if dec_p[0] != 0xC7 { if dec_p[0] != 0xC7 {
fmt.Println("ERROR: decrypted data does not start with 0xC7 so is invalid") fmt.Println("ERROR: decrypted data does not start with 0xC7 so is invalid")
os.Exit(1) os.Exit(1)
} }
uid := dec_p[1:8] uid := dec_p[1:8]
@ -115,29 +115,29 @@ func main() {
uid_str := hex.EncodeToString(uid) uid_str := hex.EncodeToString(uid)
ctr_str := hex.EncodeToString(ctr) ctr_str := hex.EncodeToString(ctr)
fmt.Println("decrypted card data : uid", uid_str, ", ctr", ctr_str) fmt.Println("decrypted card data : uid", uid_str, ", ctr", ctr_str)
// check cmac // check cmac
aes_cmac_key, err := hex.DecodeString(aes_cmac_key_hex) aes_cmac_key, err := hex.DecodeString(aes_cmac_key_hex)
if err != nil { if err != nil {
fmt.Println("ERROR: aes_cmac_key is not valid hex", err) fmt.Println("ERROR: aes_cmac_key is not valid hex", err)
os.Exit(1) os.Exit(1)
} }
cmac_valid, err := check_cmac(uid, ctr, aes_cmac_key, c) cmac_valid, err := check_cmac(uid, ctr, aes_cmac_key, c)
if err != nil { if err != nil {
fmt.Println("ERROR: check_cmac() returned an error", err) fmt.Println("ERROR: check_cmac() returned an error", err)
os.Exit(1) os.Exit(1)
} }
if cmac_valid == false { if cmac_valid == false {
fmt.Println("ERROR: cmac incorrect") fmt.Println("ERROR: cmac incorrect")
os.Exit(1) os.Exit(1)
} }
fmt.Println("cmac validates ok") fmt.Println("cmac validates ok")
os.Exit(0) os.Exit(0)
} }