forked from haskell-crypto/cryptonite
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Deterministic Nonce Generation for ECDSA #46
Open
julmb
wants to merge
4
commits into
kazu-yamamoto:main
Choose a base branch
from
julmb:deterministic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−3
Open
Changes from all commits
Commits
Show all changes
4 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 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Crypto.Random.HmacDRG (HmacDRG, initial, update) where | ||
|
||
import Data.Maybe | ||
import qualified Data.ByteString as B | ||
import Data.ByteArray (ByteArrayAccess) | ||
import qualified Data.ByteArray as M | ||
import Crypto.Hash | ||
import Crypto.MAC.HMAC (HMAC (..), hmac) | ||
import Crypto.Random.Types | ||
|
||
-- | HMAC-based Deterministic Random Generator | ||
-- | ||
-- Adapted from NIST Special Publication 800-90A Revision 1, Section 10.1.2 | ||
data HmacDRG hash = HmacDRG (Digest hash) (Digest hash) | ||
|
||
-- | The initial DRG state. It should be seeded via 'update' before use. | ||
initial :: HashAlgorithm hash => hash -> HmacDRG hash | ||
initial algorithm = HmacDRG (constant 0x00) (constant 0x01) where | ||
constant = fromJust . digestFromByteString . B.replicate (hashDigestSize algorithm) | ||
|
||
-- | Update the DRG state with optional provided data. | ||
update :: ByteArrayAccess input => HashAlgorithm hash => input -> HmacDRG hash -> HmacDRG hash | ||
update input state0 = if M.null input then state1 else state2 where | ||
state1 = step 0x00 state0 | ||
state2 = step 0x01 state1 | ||
step byte (HmacDRG key value) = HmacDRG keyNew valueNew where | ||
keyNew = hmacGetDigest $ hmac key $ M.convert value <> B.singleton byte <> M.convert input | ||
valueNew = hmacGetDigest $ hmac keyNew value | ||
|
||
instance HashAlgorithm hash => DRG (HmacDRG hash) where | ||
randomBytesGenerate count (HmacDRG key value) = (output, state) where | ||
output = M.take count result | ||
state = update B.empty $ HmacDRG key new | ||
(result, new) = go M.empty value | ||
go buffer current | ||
| M.length buffer >= count = (buffer, current) | ||
| otherwise = go (buffer <> M.convert next) next | ||
where next = hmacGetDigest $ hmac key current |
This file contains 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
This file contains 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
Oops, something went wrong.
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.
`unsafeShiftR` 3
can be used here.