This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 275
Pad z value to proper size after P-521 scalar multiplication. #245
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,10 @@ | |
package josecipher | ||
|
||
import ( | ||
"bytes" | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"encoding/binary" | ||
) | ||
|
||
|
@@ -44,16 +46,34 @@ func DeriveECDHES(alg string, apuData, apvData []byte, priv *ecdsa.PrivateKey, p | |
panic("public key not on same curve as private key") | ||
} | ||
|
||
z, _ := priv.PublicKey.Curve.ScalarMult(pub.X, pub.Y, priv.D.Bytes()) | ||
reader := NewConcatKDF(crypto.SHA256, z.Bytes(), algID, ptyUInfo, ptyVInfo, supPubInfo, []byte{}) | ||
z, _ := priv.Curve.ScalarMult(pub.X, pub.Y, priv.D.Bytes()) | ||
zBytes := z.Bytes() | ||
|
||
octSize := dSize(priv.Curve) | ||
if len(zBytes) != octSize { | ||
zBytes = append(bytes.Repeat([]byte{0}, octSize-len(zBytes)), zBytes...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we care about any timing info leak here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly, though I’m not sure how we’d fix it, and regardless P-521 in Go isn’t constant time anyway (only P-256 is afaik). |
||
} | ||
|
||
reader := NewConcatKDF(crypto.SHA256, zBytes, algID, ptyUInfo, ptyVInfo, supPubInfo, []byte{}) | ||
key := make([]byte, size) | ||
|
||
// Read on the KDF will never fail | ||
_, _ = reader.Read(key) | ||
|
||
return key | ||
} | ||
|
||
// dSize returns the size in octets for a coordinate on a elliptic curve. | ||
func dSize(curve elliptic.Curve) int { | ||
order := curve.Params().P | ||
bitLen := order.BitLen() | ||
size := bitLen / 8 | ||
if bitLen%8 != 0 { | ||
size++ | ||
} | ||
return size | ||
} | ||
|
||
func lengthPrefixed(data []byte) []byte { | ||
out := make([]byte, len(data)+4) | ||
binary.BigEndian.PutUint32(out, uint32(len(data))) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the comment from #228 (comment) would be great here