Skip to content
This repository was archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Add support for Ed25519
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Mar 11, 2019
1 parent 401e496 commit 5c6ce22
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.12
require (
github.com/emersion/go-milter v0.0.0-20190311184326-c3095a41a6fe
github.com/emersion/go-msgauth v0.0.0-20190307192406-8646172ce7a5
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
)
31 changes: 28 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"net"
"strings"

"golang.org/x/crypto/ed25519"
)

type verifier interface {
Expand All @@ -19,14 +22,29 @@ type rsaVerifier struct {
*rsa.PublicKey
}

func (v *rsaVerifier) Public() crypto.PublicKey {
func (v rsaVerifier) Public() crypto.PublicKey {
return v.PublicKey
}

func (v *rsaVerifier) Verify(hash crypto.Hash, hashed, sig []byte) error {
func (v rsaVerifier) Verify(hash crypto.Hash, hashed, sig []byte) error {
return rsa.VerifyPKCS1v15(v.PublicKey, hash, hashed, sig)
}

type ed25519Verifier struct {
ed25519.PublicKey
}

func (v ed25519Verifier) Public() crypto.PublicKey {
return v.PublicKey
}

func (v ed25519Verifier) Verify(hash crypto.Hash, hashed, sig []byte) error {
if !ed25519.Verify(v.PublicKey, hashed, sig) {
return errors.New("dkim: invalid Ed25519 signature")
}
return nil
}

type queryResult struct {
Verifier verifier
KeyAlgo string
Expand Down Expand Up @@ -102,8 +120,15 @@ func parsePublicKey(s string) (*queryResult, error) {
if rsaPub.Size() * 8 < 1024 {
return nil, permFailError(fmt.Sprintf("key is too short: want 1024 bits, has %v bits", rsaPub.Size() * 8))
}
res.Verifier = &rsaVerifier{rsaPub}
res.Verifier = rsaVerifier{rsaPub}
res.KeyAlgo = "rsa"
case "ed25519":
if len(b) != ed25519.PublicKeySize {
return nil, permFailError(fmt.Sprintf("invalid Ed25519 public key size: %v", len(b)))
}
ed25519Pub := ed25519.PublicKey(b)
res.Verifier = ed25519Verifier{ed25519Pub}
res.KeyAlgo = "ed25519"
default:
return nil, permFailError("unsupported key algorithm")
}
Expand Down
4 changes: 4 additions & 0 deletions sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strconv"
"strings"
"time"

"golang.org/x/crypto/ed25519"
)

var randReader io.Reader = rand.Reader
Expand Down Expand Up @@ -87,6 +89,8 @@ func Sign(w io.Writer, r io.Reader, options *SignOptions) error {
switch options.Signer.Public().(type) {
case *rsa.PublicKey:
keyAlgo = "rsa"
case ed25519.PublicKey:
keyAlgo = "ed25519"
default:
return fmt.Errorf("dkim: unsupported key algorithm %T", options.Signer.Public())
}
Expand Down

0 comments on commit 5c6ce22

Please sign in to comment.