Skip to content

Commit

Permalink
Add support for hx hmac-sha{224,256,384,512} checked with the test ve…
Browse files Browse the repository at this point in the history
…ctors
  • Loading branch information
np committed Nov 20, 2014
1 parent a9b6289 commit b5b0abf
Show file tree
Hide file tree
Showing 61 changed files with 1,271 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ executable hx
ghc-options: -Wall
build-depends: base >=4.6, haskoin, bytestring, base16-bytestring,
scientific, binary, RFC1751 >= 0.3, containers,
aeson
aeson, cryptohash
default-language: Haskell2010
-- other-modules:
-- other-extensions:
18 changes: 18 additions & 0 deletions hx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import System.Environment
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as LBS
import Crypto.MAC.HMAC (hmac)
import qualified Crypto.Hash.SHA224 as SHA224 (hash)
import qualified Crypto.Hash.SHA384 as SHA384 (hash)

import Network.Haskoin.Crypto
import Network.Haskoin.Internals ( curveP, curveN, curveG, integerA, integerB
Expand Down Expand Up @@ -429,6 +432,10 @@ hx_showtx ("-j":xs) = hx_showtx xs
hx_showtx ("--json":xs) = hx_showtx xs
hx_showtx _ = error "Usage: hx showtx [-j|--json] [<TXFILE>]"

hx_hmac :: String -> (BS -> BS) -> Int -> [BS] -> BS
hx_hmac _ h s [key,input] = encodeHex $ hmac h s (decodeHex "hmac key" key) (decodeHex "hmac data" input)
hx_hmac m _ _ _ = error $ "hx hmac-" ++ m ++ " <KEY> [<INPUT>]"

chksum32_encode :: BS -> BS
chksum32_encode d = d <> encode' (chksum32 d)

Expand Down Expand Up @@ -537,6 +544,11 @@ mainArgs ["sha1"] = interactHex hashSha1BS
mainArgs ["hash256"] = interactHex $ hash256BS . hash256BS
mainArgs ["hash160"] = interactHex $ hash160BS . hash256BS

mainArgs ("hmac-sha224":args) = interactArgsLn (hx_hmac "sha224" SHA224.hash 64) args
mainArgs ("hmac-sha256":args) = interactArgsLn (hx_hmac "sha256" hash256BS 64) args
mainArgs ("hmac-sha384":args) = interactArgsLn (hx_hmac "sha384" SHA384.hash 128) args
mainArgs ("hmac-sha512":args) = interactArgsLn (hx_hmac "sha512" hash512BS 128) args

mainArgs ("chksum32":args) = interactArgs hx_chksum32 args
mainArgs ("chksum32-encode":args) = interactArgs hx_chksum32_encode args
mainArgs ("chksum32-decode":args) = interactArgs hx_chksum32_decode args
Expand Down Expand Up @@ -678,6 +690,12 @@ mainArgs _ = error $ unlines ["Unexpected arguments."
,"hx hash160 [0]"
,"hx hash256 [0]"
,""
,"# HASH BASED MACs"
,"hx hmac-sha224 <KEY> [<INPUT>] [0]"
,"hx hmac-sha256 <KEY> [<INPUT>] [0]"
,"hx hmac-sha384 <KEY> [<INPUT>] [0]"
,"hx hmac-sha512 <KEY> [<INPUT>] [0]"
,""
,"[0]: Not available in sx"
,"[1]: `hx showtx` is always using JSON output,"
," `-j` and `--json` are ignored."
Expand Down
42 changes: 42 additions & 0 deletions tests/hex-decode-hmac-test-vector-data-7.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=hex-decode-hmac-test-vector-data-7.t
command=hx
args=( hex-decode )
exit_code=0
stdin_file=stdin
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/hex-decode-hmac-test-vector-data-7.t/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e
1 change: 1 addition & 0 deletions tests/hex-decode-hmac-test-vector-data-7.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.
42 changes: 42 additions & 0 deletions tests/hmac-sha224-test-vector-1.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=hmac-sha224-test-vector-1.t
command=hx
args=( hmac-sha224 0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b 4869205468657265 )
exit_code=0
stdin_file=/dev/null
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/hmac-sha224-test-vector-1.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22
42 changes: 42 additions & 0 deletions tests/hmac-sha224-test-vector-2.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=hmac-sha224-test-vector-2.t
command=hx
args=( hmac-sha224 4a656665 7768617420646f2079612077616e7420666f72206e6f7468696e673f )
exit_code=0
stdin_file=/dev/null
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/hmac-sha224-test-vector-2.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44
42 changes: 42 additions & 0 deletions tests/hmac-sha224-test-vector-3.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=hmac-sha224-test-vector-3.t
command=hx
args=( hmac-sha224 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd )
exit_code=0
stdin_file=/dev/null
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/hmac-sha224-test-vector-3.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea
42 changes: 42 additions & 0 deletions tests/hmac-sha224-test-vector-4.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=hmac-sha224-test-vector-4.t
command=hx
args=( hmac-sha224 0102030405060708090a0b0c0d0e0f10111213141516171819 cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd )
exit_code=0
stdin_file=/dev/null
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/hmac-sha224-test-vector-4.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a
43 changes: 43 additions & 0 deletions tests/hmac-sha224-test-vector-5.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

testname=hmac-sha224-test-vector-5.t
command=hx
args=( hmac-sha224 0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c 546573742057697468205472756e636174696f6e )
exit_code=0
stdin_file=/dev/null
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
head -c32 <stdout | sponge stdout
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/hmac-sha224-test-vector-5.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0e2aea68a90c8d37c988bcdb9fca6fa8
42 changes: 42 additions & 0 deletions tests/hmac-sha224-test-vector-6.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=hmac-sha224-test-vector-6.t
command=hx
args=( hmac-sha224 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374 )
exit_code=0
stdin_file=/dev/null
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/hmac-sha224-test-vector-6.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e
Loading

0 comments on commit b5b0abf

Please sign in to comment.