Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Fix generation of D.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsha committed Dec 5, 2018
1 parent 3185494 commit 25e77d5
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,17 @@ func fromEcPrivateKey(ec *ecdsa.PrivateKey) (*rawJSONWebKey, error) {
return nil, fmt.Errorf("square/go-jose: invalid EC private key")
}

raw.D = newBuffer(ec.D.Bytes())
// Calculating the size of D:
// The length of this octet string MUST be ceiling(log-base-2(n)/8)
// octets (where n is the order of the curve).
// https://tools.ietf.org/html/rfc7518#section-6.2.2.1
order := ec.PublicKey.Curve.Params().P
bitLen := order.BitLen()
size := bitLen / 8
if bitLen%8 != 0 {
size = size + 1
}
raw.D = newFixedSizeBuffer(ec.D.Bytes(), size)

return raw, nil
}
Expand Down

0 comments on commit 25e77d5

Please sign in to comment.