Skip to content

Commit

Permalink
Add SHE commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Mar 16, 2024
1 parent 6e6936a commit a103eeb
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/hash/sha.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var ShaCmd = &cmd.Command{
Run: runSha,
Brief: "Generate cryptographic hash using SHA",

Usage: `Usage: pocryp hash-sha -alg [-bin] [-in INPUT] [-out OUTPUT]
Usage: `Usage: pocryp sha -alg [-bin] [-in INPUT] [-out OUTPUT]
Compute SHA digest of INPUT to OUTPUT.
Expand Down
2 changes: 1 addition & 1 deletion internal/kdf/pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Pbkdf2Cmd = &cmd.Command{
Run: runPbkdf2,
Brief: "Derive key using PBKDF2",

Usage: `Usage: pocryp kdf-pbkdf2 [-bin] -key|-key-file -salt|-salt-file -iter -len -hash [-out OUTPUT]
Usage: `Usage: pocryp pbkdf2 [-bin] -key|-key-file -salt|-salt-file -iter -len -hash [-out OUTPUT]
Derive a new key from the given key using PBKDF2.
Expand Down
2 changes: 1 addition & 1 deletion internal/kem/rsa/cmd/kem.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Cmd = &cmd.Command{
Run: run,
Brief: "Encapsulate/Decapsulate using RSA-KEM",

Usage: `Usage: pocryp kem-rsa [-bin] [-e/-d] -key [-in INPUT] [-out OUTPUT]
Usage: `Usage: pocryp rsa-kem [-bin] [-e/-d] -key [-in INPUT] [-out OUTPUT]
Encapsulate/Decapsulate INPUT to OUTPUT using RSA-KEM.
Expand Down
2 changes: 1 addition & 1 deletion internal/keygen/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var AesCmd = &cmd.Command{
Run: runAes,
Brief: "Generate AES key",

Usage: `Usage: pocryp gen-aes [-out OUTPUT] [-bin] NUM_BITS
Usage: `Usage: pocryp aes-keygen [-out OUTPUT] [-bin] NUM_BITS
Generate AES key.
Valid NUM_BITS: 128, 192, 256.
Expand Down
2 changes: 1 addition & 1 deletion internal/keygen/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var Ed25519Cmd = &cmd.Command{
Run: runEd25519,
Brief: "Generate ED25519 key",

Usage: `Usage: pocryp gen-ed25519 [-out OUTPUT] [-bin]
Usage: `Usage: pocryp ed25519-keygen [-out OUTPUT] [-bin]
Generate ED25519 key.
Expand Down
2 changes: 1 addition & 1 deletion internal/keygen/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var RsaCmd = &cmd.Command{
Run: runRsa,
Brief: "Generate RSA key",

Usage: `Usage: pocryp gen-rsa [-out OUTPUT] NUM_BITS
Usage: `Usage: pocryp rsa-keygen [-out OUTPUT] NUM_BITS
Generate RSA key.
Valid NUM_BITS: 2048, 3072, 4096.
Expand Down
66 changes: 66 additions & 0 deletions internal/she/cmd/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"strings"

"bandr.me/p/pocryp/internal/cli/cmd"
"bandr.me/p/pocryp/internal/she/mup"
)

var DecodeCmd = &cmd.Command{
Name: "she-decode",
Run: runDecode,
Brief: "Decode M1,M2,M3,M4,M5 to JSON input",

Usage: `Usage: pocryp she-decode [hex_string]
Decode the M1,M2,M3,M4,M5 given as a hex string to its JSON form.
If no argument given, stdin will be read.
`,
}

func runDecode(cmd *cmd.Command) error {
keyHex := cmd.Flags.String("key", "", "Secret key as hex")

if err := cmd.Parse(); err != nil {
return err
}

input := cmd.Flags.Arg(0)
if cmd.Flags.NArg() == 0 {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("failed to read stdin: %w", err)
}
input = string(data)
}
input = strings.NewReplacer(" ", "", "\t", "", "\r", "", "\n", "").Replace(input)

key, err := hex.DecodeString(*keyHex)
if err != nil {
return fmt.Errorf("failed to decode key: %w", err)
}
if len(key) != 16 {
return fmt.Errorf("invalid key size: %d", len(key))
}

m1m2m3, err := hex.DecodeString(input)
if err != nil {
return fmt.Errorf("failed to decode input: %w", err)
}

result, err := mup.Decode(m1m2m3, key)
if err != nil {
return err
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")

return enc.Encode(result)
}
64 changes: 64 additions & 0 deletions internal/she/cmd/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"encoding/hex"
"encoding/json"
"fmt"
"os"

"bandr.me/p/pocryp/internal/cli/cmd"
"bandr.me/p/pocryp/internal/she/mup"
)

var EncodeCmd = &cmd.Command{
Name: "she-encode",
Run: runEncode,
Brief: "Encode JSON input to M1,M2,M3,M4,M5",

Usage: `Usage: pocryp she-encode [input.json]
Encode the given JSON input file to M1,M2,M3,M4,M5.
If no argument given, stdin will be read.
`,
}

func runEncode(cmd *cmd.Command) error {
oneLine := cmd.Flags.Bool("l", false, "Print everything on one line")

if err := cmd.Parse(); err != nil {
return err
}

inFile := os.Stdin
if cmd.Flags.NArg() > 0 {
f, err := os.Open(cmd.Flags.Arg(0))
if err != nil {
return err
}
defer f.Close()
inFile = f
}

var input mup.Input
if err := json.NewDecoder(inFile).Decode(&input); err != nil {
return err
}

result, err := input.Encode()
if err != nil {
return err
}

if *oneLine {
fmt.Println(hex.EncodeToString(result[:]))
} else {
m1, m2, m3, m4, m5 := mup.SliceMs(result)
fmt.Println(hex.EncodeToString(m1))
fmt.Println(hex.EncodeToString(m2))
fmt.Println(hex.EncodeToString(m3))
fmt.Println(hex.EncodeToString(m4))
fmt.Println(hex.EncodeToString(m5))
}

return nil
}
13 changes: 13 additions & 0 deletions internal/she/cmd/encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cmd

import (
"testing"
)

func TestEncode(t *testing.T) {
EncodeCmd.Args = []string{"-foo"}
EncodeCmd.Init()
if err := runEncode(EncodeCmd); err == nil {
t.Fatal("expected error")
}
}
39 changes: 39 additions & 0 deletions internal/she/cmd/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"encoding/json"
"os"

"bandr.me/p/pocryp/internal/cli/cmd"
"bandr.me/p/pocryp/internal/she/mup"
)

var ExampleCmd = &cmd.Command{
Name: "she-example",
Run: runExample,
Brief: "Print example JSON input",

Usage: `Usage: pocryp she-example
Print example JSON input to stdout.
`,
}

func runExample(cmd *cmd.Command) error {
if err := cmd.Parse(); err != nil {
return err
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")

input := mup.Input{
UID: "000000000000000000000000000000",
ID: 0,
AuthID: 0,
AuthKey: "00000000000000000000000000000000",
NewKey: "00000000000000000000000000000000",
}

return enc.Encode(input)
}
15 changes: 15 additions & 0 deletions internal/she/cmd/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"UID": "000000000000000000000000000001",
"AuthID": 1,
"AuthKey": "000102030405060708090a0b0c0d0e0f",
"ID": 4,
"NewKey": "0f0e0d0c0b0a09080706050403020100",
"Counter": 1,
"Flags": {
"Write": false,
"Boot": false,
"Debugger": false,
"KeyUsage": false,
"Wildcard": false
}
}
13 changes: 13 additions & 0 deletions internal/she/cmd/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cmd

import (
"testing"
)

func TestExample(t *testing.T) {
ExampleCmd.Args = []string{"-foo"}
ExampleCmd.Init()
if err := runExample(ExampleCmd); err == nil {
t.Fatal("expected error")
}
}
65 changes: 53 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,75 @@ import (
encoding_rsa "bandr.me/p/pocryp/internal/encoding/rsa"
kem_rsa "bandr.me/p/pocryp/internal/kem/rsa/cmd"
keywrap_aes "bandr.me/p/pocryp/internal/keywrap/aes/cmd"
she "bandr.me/p/pocryp/internal/she/cmd"
)

func main() {
var a cli.App

a.Add("Key Generation", keygen.AesCmd, keygen.RsaCmd, keygen.Ed25519Cmd)
a.Add(
"Key Generation",
keygen.AesCmd,
keygen.RsaCmd,
keygen.Ed25519Cmd,
)

a.Add("Key Encoding",
a.Add(
"Key Encoding",
encoding_rsa.Priv2PubCmd,
encoding_rsa.Raw2DerCmd, encoding_rsa.Der2RawCmd,
encoding_rsa.Pem2DerCmd, encoding_rsa.Der2PemCmd,
encoding_rsa.Raw2DerCmd,
encoding_rsa.Der2RawCmd,
encoding_rsa.Pem2DerCmd,
encoding_rsa.Der2PemCmd,
)

a.Add("Block Cipher", aes.EcbCmd)
a.Add(
"Block Cipher",
aes.EcbCmd,
)

a.Add("Stream Cipher", aes.CbcCmd)
a.Add(
"Stream Cipher",
aes.CbcCmd,
)

a.Add("Message Authentication Code(MAC)", aes.CmacGenerateCmd, aes.CmacVerifyCmd)
a.Add(
"Message Authentication Code(MAC)",
aes.CmacGenerateCmd,
aes.CmacVerifyCmd,
)

a.Add("Authenticated Encryption(AEAD)", aes.GcmCmd)
a.Add(
"Authenticated Encryption(AEAD)",
aes.GcmCmd,
)

a.Add("Key Wrap", keywrap_aes.Cmd)
a.Add(
"Key Wrap",
keywrap_aes.Cmd,
)

a.Add("Key Derivation Function(KDF)", kdf.Pbkdf2Cmd)
a.Add(
"Key Derivation Function(KDF)",
kdf.Pbkdf2Cmd,
)

a.Add("Hash Function", hash.ShaCmd)
a.Add(
"Hash Function",
hash.ShaCmd,
)

a.Add("Key Encapsulation Mechanism(KEM)", kem_rsa.Cmd)
a.Add(
"Key Encapsulation Mechanism(KEM)",
kem_rsa.Cmd,
)

a.Add(
"Secured Hardware Extensions(AUTOSAR)",
she.ExampleCmd,
she.EncodeCmd,
she.DecodeCmd,
)

if err := a.Run(os.Args[1:]...); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
Expand Down

0 comments on commit a103eeb

Please sign in to comment.