Skip to content

Commit

Permalink
travisci fixes. simplify test and remove suppport for go1.6.
Browse files Browse the repository at this point in the history
  • Loading branch information
cornfeedhobo committed Jun 4, 2018
1 parent 1578f51 commit f1231be
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 158 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
language: go

go:
- 1.6
- 1.7
- 1.8
- 1.9

install: false

script:
- go test -v ./...
- go test -v || travis_terminate 1
- go get github.com/mitchellh/gox
- CGO_ENABLED=0 gox -ldflags "-s" -rebuild

Expand Down
77 changes: 39 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
ssh-keydgen [![Travis](https://img.shields.io/travis/cornfeedhobo/ssh-keydgen.svg)]() [![Github All Releases](https://img.shields.io/github/downloads/cornfeedhobo/ssh-keydgen/total.svg)]()
===========

Generate _Deterministic_ SSH keys

```text
NAME:
ssh-keydgen - Deterministic authentication key generation
USAGE:
ssh-keydgen [-t] [-b] [-c] [-n] [-f] [-a]
AUTHOR:
cornfeedhobo
OPTIONS:
-t type Specifies the type of key to create. The possible values are "dsa", "ecdsa", "rsa", or "ed25519". (default: "ed25519")
-b bits Specifies the number of bits in the key to create. Possible values are restricted by key type. (default: 2048)
-c curve Specifies the elliptic curve to use. The possible values are 256, 384, or 521. (default: 256)
-n factor Specifies the work factor, or "difficulty", applied to the key generation function. (default: 16384)
-f filename Specifies the filename of the key file.
-a Add the generated key to the running ssh-agent.
COPYRIGHT:
(c) 2017 cornfeedhobo
```
Generate _Deterministic_ SSH keys

```text
NAME:
ssh-keydgen - deterministic authentication key generation
USAGE:
ssh-keydgen [-t] [-b] [-c] [-n] [-f] [-a] [-w]
AUTHOR:
cornfeedhobo
OPTIONS:
-t type Specifies the type of key to create. The possible values are "dsa", "ecdsa", "rsa", or "ed25519". (default: "ed25519")
-b bits Specifies the number of bits in the key to create. Possible values are restricted by key type. (default: 2048)
-c curve Specifies the elliptic curve to use. The possible values are 256, 384, or 521. (default: 256)
-n factor Specifies the work factor, or "difficulty", applied to the key generation function. (default: 16384)
-f filename Specifies the filename of the key file.
-a Add the generated key to the running ssh-agent.
-w seed Provides the deterministic seed
COPYRIGHT:
(c) 2018 cornfeedhobo
```



## Usage

1) Generate your keys
```bash
keydgen -f deterministic_key
ls -lh deterministic_key*
```
2) Allow time to pass, hoping an emergency does not arise when you have no access to your keys ...
_If_ the time comes where you need access but can't get to your keys, you can then obtain this
utility and re-generate, or even directly add your key to a running `ssh-agent`.
```bash
keydgen -a
```
3) Profit!
1) Generate your keys
```bash
keydgen -f deterministic_key
ls -lh deterministic_key*
```

2) Allow time to pass, hoping an emergency does not arise when you have no access to your keys ...

_If_ the time comes where you need access but can't get to your keys, you can then obtain this
utility and re-generate, or even directly add your key to a running `ssh-agent`.
```bash
keydgen -a
```
3) Profit!
Expand Down
187 changes: 69 additions & 118 deletions keydgen_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -12,187 +11,95 @@ import (
"github.com/google/goexpect"
)

type keygenCase struct {
name string
expectFailure bool
keydgen *Keydgen
}

func init() {
WorkFactor = 4096
}

func testKeygenImport(k *Keydgen) error {

if _, err := k.GenerateKey(); err != nil {
return err
}

pub, err := k.MarshalPublicKey()
if err != nil {
return err
}

tmpFile, err := ioutil.TempFile("", "keydgen_test")
if err != nil {
return err
}

if err := writeKeyToFile(k, tmpFile.Name()); err != nil {
return err
}
defer func() {
os.Remove(tmpFile.Name())
os.Remove(tmpFile.Name() + ".pub")
}()

e, _, err := expect.Spawn("ssh-keygen -y -f "+tmpFile.Name(), -1)
if err != nil {
return err
}
defer e.Close()

expr := regexp.MustCompilePOSIX(regexp.QuoteMeta(string(pub)))
if _, _, eErr := e.Expect(expr, -1); eErr != nil {
err = errors.New("unable to verify generated public key with ssh-keygen")
}

return err

}
func TestKeydgen_GenerateKey(t *testing.T) {

func TestKeydgen_GenerateKey_DSA(t *testing.T) {
WorkFactor = 1024 * 1

cases := []keygenCase{
cases := []struct {
name string
expectFailure bool
keydgen *Keydgen
}{
// DSA
{
name: "InvalidKeyLength",
expectFailure: true,
name: "DSA_InvalidKeyLength",
keydgen: &Keydgen{
Type: DSA,
Bits: 100,
Seed: []byte("keydgen"),
},
expectFailure: true,
},
{
name: "1024",
name: "DSA_1024",
keydgen: &Keydgen{
Type: DSA,
Bits: 1024,
Seed: []byte("keydgen"),
},
},
{
name: "2048",
name: "DSA_2048",
keydgen: &Keydgen{
Type: DSA,
Bits: 2048,
Seed: []byte("keydgen"),
},
},
{
name: "3072",
name: "DSA_3072",
keydgen: &Keydgen{
Type: DSA,
Bits: 3072,
Seed: []byte("keydgen"),
},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
start := time.Now()
fmt.Print(t.Name())
if err := testKeygenImport(c.keydgen); err != nil && !c.expectFailure {
t.Fatal(err)
}
fmt.Printf(" PASS %s\n", time.Since(start))
})
}

}

func TestKeydgen_GenerateKey_ECDSA(t *testing.T) {

cases := []keygenCase{
// ECDSA
{
name: "InvalidCurve",
expectFailure: true,
name: "ECDSA_InvalidCurve",
keydgen: &Keydgen{
Type: ECDSA,
Curve: 128,
Seed: []byte("keydgen"),
},
expectFailure: true,
},
{
name: "256",
name: "ECDSA_256",
keydgen: &Keydgen{
Type: ECDSA,
Curve: 256,
Seed: []byte("keydgen"),
},
},
{
name: "384",
name: "ECDSA_384",
keydgen: &Keydgen{
Type: ECDSA,
Curve: 384,
Seed: []byte("keydgen"),
},
},
{
name: "521",
name: "ECDSA_521",
keydgen: &Keydgen{
Type: ECDSA,
Curve: 521,
Seed: []byte("keydgen"),
},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
start := time.Now()
fmt.Print(t.Name())
if err := testKeygenImport(c.keydgen); err != nil && !c.expectFailure {
t.Fatal(err)
}
fmt.Printf(" PASS %s\n", time.Since(start))
})
}

}

func TestKeydgen_GenerateKey_RSA(t *testing.T) {

cases := []keygenCase{
// RSA
{
name: "2048",
name: "RSA_2048",
keydgen: &Keydgen{
Type: RSA,
Bits: 2048,
Seed: []byte("keydgen"),
},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
start := time.Now()
fmt.Print(t.Name())
if err := testKeygenImport(c.keydgen); err != nil && !c.expectFailure {
t.Fatal(err)
}
fmt.Printf(" PASS %s\n", time.Since(start))
})
}

}

func TestKeydgen_GenerateKey_ED25519(t *testing.T) {

cases := []keygenCase{
// ED25519
{
name: "ED25519",
keydgen: &Keydgen{
Type: ED25519,
Seed: []byte("keydgen"),
Expand All @@ -202,12 +109,56 @@ func TestKeydgen_GenerateKey_ED25519(t *testing.T) {

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {

start := time.Now()
fmt.Print(t.Name())
if err := testKeygenImport(c.keydgen); err != nil && !c.expectFailure {
fmt.Print(c.name)

if _, err := c.keydgen.GenerateKey(); err != nil && !c.expectFailure {
t.Fatal(err)
}

if !c.expectFailure {

pub, err := c.keydgen.MarshalPublicKey()
if err != nil {
t.Fatal(err)
}

filename := fmt.Sprintf("keydgen_test_%s_", c.keydgen.Type)
if c.keydgen.Type == RSA || c.keydgen.Type == DSA {
filename += fmt.Sprintf("%d_", c.keydgen.Bits)
} else if c.keydgen.Type == ECDSA {
filename += fmt.Sprintf("%d_", c.keydgen.Curve)
}

tmpFile, err := ioutil.TempFile("", filename)
if err != nil {
t.Fatal(err)
}

if err := writeKeyToFile(c.keydgen, tmpFile.Name()); err != nil {
t.Fatal(err)
}
defer func() {
os.Remove(tmpFile.Name())
os.Remove(tmpFile.Name() + ".pub")
}()

e, _, err := expect.Spawn("ssh-keygen -y -f "+tmpFile.Name(), -1)
if err != nil {
t.Fatal(err)
}
defer e.Close()

expr := regexp.MustCompilePOSIX(regexp.QuoteMeta(string(pub)))
if out, _, err := e.Expect(expr, -1); err != nil {
t.Fatal("unable to verify generated public key with ssh-keygen: " + out)
}

}

fmt.Printf(" PASS %s\n", time.Since(start))

})
}

Expand Down

0 comments on commit f1231be

Please sign in to comment.