Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds SRP #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type plainClient struct {
}

func (a *plainClient) Start() (mech string, ir []byte, err error) {
mech = "PLAIN"
mech = Plain
ir = []byte(a.Identity + "\x00" + a.Username + "\x00" + a.Password)
return
}
Expand Down
128 changes: 128 additions & 0 deletions srp/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package srp

import (
"bytes"
"crypto/rand"
"math/big"
"strings"
)

func concat(parts ...[]byte) []byte {
var b []byte
for _, p := range parts {
b = append(b, p...)
}
return b
}

type Client struct {
Username string
Identity string
SID string
Nonce []byte
Password string
}

func NewClient(username, password string) *Client {
return &Client{
Username: username,
Password: password,
}
}

func (c *Client) Start() (mech string, ir []byte, err error) {
mech = "SRP"

ci := &ClientIdentity{
Username: c.Username,
Identity: c.Identity,
SID: c.SID,
Nonce: c.Nonce,
}

var b bytes.Buffer
ci.WriteTo(&b)
ir = b.Bytes()

return
}

func (c *Client) Next(challenge []byte) (response []byte, err error) {
// ErrUnexpectedServerChallenge
r := bytes.NewReader(challenge)

var reused bool
if reused, err = ReadServerReuse(r); err != nil {
return
} else if reused {
ch := &ServerNonce{}
if err = ch.ReadFrom(r); err != nil {
return
}
c.Nonce = ch.Nonce
return
} else {
// See https://tools.ietf.org/html/draft-burdis-cat-srp-sasl-08#section-4.4
ch := &ServerProtocolElements{}
if err = ch.ReadFrom(r); err != nil {
return
}

// TODO: choose hash
var hash func([]byte) []byte

// TODO: implement these
var xor func(_, _ []byte) []byte
var itoa func(*big.Int) []byte
var atoi func([]byte) *big.Int

var clientSecret *big.Int
modulusMinusOne := big.NewInt(0).Sub(ch.Modulus, big.NewInt(1))
if clientSecret, err = rand.Int(rand.Reader, modulusMinusOne); err != nil {
return
}

clientEphemeral := big.NewInt(0).Exp(ch.Generator, clientSecret, ch.Modulus)

x := atoi(hash(concat(ch.Salt, hash([]byte(c.Username + ":" + c.Password)))))
u := atoi(hash(concat(itoa(clientEphemeral), itoa(ch.Ephemeral))))
S := big.NewInt(0).Exp(
big.NewInt(0).Sub(
ch.Ephemeral,
big.NewInt(0).Mul(
big.NewInt(3),
big.NewInt(0).Exp(ch.Generator, x, nil),
),
),
big.NewInt(0).Add(
clientSecret,
big.NewInt(0).Mul(u, x),
),
ch.Modulus,
)
K := hash(itoa(S))

M1 := hash(concat(
xor(hash(itoa(ch.Modulus)), hash(itoa(ch.Generator))),
hash([]byte(c.Username)),
ch.Salt,
itoa(clientEphemeral),
itoa(ch.Ephemeral),
K,
hash([]byte(c.Identity)),
hash([]byte(strings.Join(ch.Options, ","))),
))

ce := &ClientEvidence{
Ephemeral: clientEphemeral,
Evidence: M1,
Options: nil,
}

var b bytes.Buffer
ce.WriteTo(&b)
response = b.Bytes()
}

return
}
99 changes: 99 additions & 0 deletions srp/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package srp

import (
"io"
"encoding/binary"
"math/big"
)

func readMPI(r io.Reader) (*big.Int, error) {
var n uint16
if err := binary.Read(r, binary.BigEndian, &n); err != nil {
return nil, err
}

buf := make([]byte, n)
if _, err := io.ReadFull(r, buf); err != nil {
return nil, err
}

return new(big.Int).SetBytes(buf), nil
}

func writeMPI(w io.Writer, i *big.Int) error {
buf := i.Bytes()
if err := binary.Write(w, binary.BigEndian, uint16(len(buf))); err != nil {
return err
}
_, err := w.Write(buf)
return err
}

func readOS(r io.Reader) ([]byte, error) {
var n uint8
if err := binary.Read(r, binary.BigEndian, &n); err != nil {
return nil, err
}

buf := make([]byte, n)
_, err := io.ReadFull(r, buf)
return buf, err
}

func writeOS(w io.Writer, buf []byte) error {
if err := binary.Write(w, binary.BigEndian, uint8(len(buf))); err != nil {
return err
}
_, err := w.Write(buf)
return err
}

func readEOS(r io.Reader) ([]byte, error) {
var n uint32
if err := binary.Read(r, binary.BigEndian, &n); err != nil {
return nil, err
}

buf := make([]byte, n)
_, err := io.ReadFull(r, buf)
return buf, err
}

func writeEOS(w io.Writer, buf []byte) error {
if err := binary.Write(w, binary.BigEndian, uint32(len(buf))); err != nil {
return err
}
_, err := w.Write(buf)
return err
}

func readText(r io.Reader) (string, error) {
var n uint16
if err := binary.Read(r, binary.BigEndian, &n); err != nil {
return "", err
}

buf := make([]byte, n)
if _, err := io.ReadFull(r, buf); err != nil {
return "", err
}
return string(buf), nil
}

func writeText(w io.Writer, s string) error {
if err := binary.Write(w, binary.BigEndian, uint16(len(s))); err != nil {
return err
}
_, err := io.WriteString(w, s)
return err
}

func readUint(r io.Reader) (uint16, error) {
var n uint16
err := binary.Read(r, binary.BigEndian, &n)
return n, err
}

func writeUint(w io.Writer, i uint16) error {
return binary.Write(w, binary.BigEndian, i)
}
Loading