From b5b0abf282eee56818fcb51f9305306e156a3e3e Mon Sep 17 00:00:00 2001 From: Nicolas Pouillard Date: Thu, 20 Nov 2014 20:58:44 +0100 Subject: [PATCH] Add support for hx hmac-sha{224,256,384,512} checked with the test vectors --- hx.cabal | 2 +- hx.hs | 18 ++++++++ .../TESTRECIPE | 42 ++++++++++++++++++ .../stdin | 1 + .../stdout | 1 + tests/hmac-sha224-test-vector-1.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha224-test-vector-1.t/stdout | 1 + tests/hmac-sha224-test-vector-2.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha224-test-vector-2.t/stdout | 1 + tests/hmac-sha224-test-vector-3.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha224-test-vector-3.t/stdout | 1 + tests/hmac-sha224-test-vector-4.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha224-test-vector-4.t/stdout | 1 + tests/hmac-sha224-test-vector-5.t/TESTRECIPE | 43 +++++++++++++++++++ tests/hmac-sha224-test-vector-5.t/stdout | 1 + tests/hmac-sha224-test-vector-6.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha224-test-vector-6.t/stdout | 1 + tests/hmac-sha224-test-vector-7.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha224-test-vector-7.t/stdout | 1 + tests/hmac-sha256-test-vector-1.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha256-test-vector-1.t/stdout | 1 + tests/hmac-sha256-test-vector-2.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha256-test-vector-2.t/stdout | 1 + tests/hmac-sha256-test-vector-3.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha256-test-vector-3.t/stdout | 1 + tests/hmac-sha256-test-vector-4.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha256-test-vector-4.t/stdout | 1 + tests/hmac-sha256-test-vector-5.t/TESTRECIPE | 43 +++++++++++++++++++ tests/hmac-sha256-test-vector-5.t/stdout | 1 + tests/hmac-sha256-test-vector-6.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha256-test-vector-6.t/stdout | 1 + tests/hmac-sha256-test-vector-7.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha256-test-vector-7.t/stdout | 1 + tests/hmac-sha384-test-vector-1.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha384-test-vector-1.t/stdout | 1 + tests/hmac-sha384-test-vector-2.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha384-test-vector-2.t/stdout | 1 + tests/hmac-sha384-test-vector-3.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha384-test-vector-3.t/stdout | 1 + tests/hmac-sha384-test-vector-4.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha384-test-vector-4.t/stdout | 1 + tests/hmac-sha384-test-vector-5.t/TESTRECIPE | 43 +++++++++++++++++++ tests/hmac-sha384-test-vector-5.t/stdout | 1 + tests/hmac-sha384-test-vector-6.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha384-test-vector-6.t/stdout | 1 + tests/hmac-sha384-test-vector-7.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha384-test-vector-7.t/stdout | 1 + tests/hmac-sha512-test-vector-1.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha512-test-vector-1.t/stdout | 1 + tests/hmac-sha512-test-vector-2.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha512-test-vector-2.t/stdout | 1 + tests/hmac-sha512-test-vector-3.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha512-test-vector-3.t/stdout | 1 + tests/hmac-sha512-test-vector-4.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha512-test-vector-4.t/stdout | 1 + tests/hmac-sha512-test-vector-5.t/TESTRECIPE | 43 +++++++++++++++++++ tests/hmac-sha512-test-vector-5.t/stdout | 1 + tests/hmac-sha512-test-vector-6.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha512-test-vector-6.t/stdout | 1 + tests/hmac-sha512-test-vector-7.t/TESTRECIPE | 42 ++++++++++++++++++ tests/hmac-sha512-test-vector-7.t/stdout | 1 + 61 files changed, 1271 insertions(+), 1 deletion(-) create mode 100644 tests/hex-decode-hmac-test-vector-data-7.t/TESTRECIPE create mode 100644 tests/hex-decode-hmac-test-vector-data-7.t/stdin create mode 100644 tests/hex-decode-hmac-test-vector-data-7.t/stdout create mode 100644 tests/hmac-sha224-test-vector-1.t/TESTRECIPE create mode 100644 tests/hmac-sha224-test-vector-1.t/stdout create mode 100644 tests/hmac-sha224-test-vector-2.t/TESTRECIPE create mode 100644 tests/hmac-sha224-test-vector-2.t/stdout create mode 100644 tests/hmac-sha224-test-vector-3.t/TESTRECIPE create mode 100644 tests/hmac-sha224-test-vector-3.t/stdout create mode 100644 tests/hmac-sha224-test-vector-4.t/TESTRECIPE create mode 100644 tests/hmac-sha224-test-vector-4.t/stdout create mode 100644 tests/hmac-sha224-test-vector-5.t/TESTRECIPE create mode 100644 tests/hmac-sha224-test-vector-5.t/stdout create mode 100644 tests/hmac-sha224-test-vector-6.t/TESTRECIPE create mode 100644 tests/hmac-sha224-test-vector-6.t/stdout create mode 100644 tests/hmac-sha224-test-vector-7.t/TESTRECIPE create mode 100644 tests/hmac-sha224-test-vector-7.t/stdout create mode 100644 tests/hmac-sha256-test-vector-1.t/TESTRECIPE create mode 100644 tests/hmac-sha256-test-vector-1.t/stdout create mode 100644 tests/hmac-sha256-test-vector-2.t/TESTRECIPE create mode 100644 tests/hmac-sha256-test-vector-2.t/stdout create mode 100644 tests/hmac-sha256-test-vector-3.t/TESTRECIPE create mode 100644 tests/hmac-sha256-test-vector-3.t/stdout create mode 100644 tests/hmac-sha256-test-vector-4.t/TESTRECIPE create mode 100644 tests/hmac-sha256-test-vector-4.t/stdout create mode 100644 tests/hmac-sha256-test-vector-5.t/TESTRECIPE create mode 100644 tests/hmac-sha256-test-vector-5.t/stdout create mode 100644 tests/hmac-sha256-test-vector-6.t/TESTRECIPE create mode 100644 tests/hmac-sha256-test-vector-6.t/stdout create mode 100644 tests/hmac-sha256-test-vector-7.t/TESTRECIPE create mode 100644 tests/hmac-sha256-test-vector-7.t/stdout create mode 100644 tests/hmac-sha384-test-vector-1.t/TESTRECIPE create mode 100644 tests/hmac-sha384-test-vector-1.t/stdout create mode 100644 tests/hmac-sha384-test-vector-2.t/TESTRECIPE create mode 100644 tests/hmac-sha384-test-vector-2.t/stdout create mode 100644 tests/hmac-sha384-test-vector-3.t/TESTRECIPE create mode 100644 tests/hmac-sha384-test-vector-3.t/stdout create mode 100644 tests/hmac-sha384-test-vector-4.t/TESTRECIPE create mode 100644 tests/hmac-sha384-test-vector-4.t/stdout create mode 100644 tests/hmac-sha384-test-vector-5.t/TESTRECIPE create mode 100644 tests/hmac-sha384-test-vector-5.t/stdout create mode 100644 tests/hmac-sha384-test-vector-6.t/TESTRECIPE create mode 100644 tests/hmac-sha384-test-vector-6.t/stdout create mode 100644 tests/hmac-sha384-test-vector-7.t/TESTRECIPE create mode 100644 tests/hmac-sha384-test-vector-7.t/stdout create mode 100644 tests/hmac-sha512-test-vector-1.t/TESTRECIPE create mode 100644 tests/hmac-sha512-test-vector-1.t/stdout create mode 100644 tests/hmac-sha512-test-vector-2.t/TESTRECIPE create mode 100644 tests/hmac-sha512-test-vector-2.t/stdout create mode 100644 tests/hmac-sha512-test-vector-3.t/TESTRECIPE create mode 100644 tests/hmac-sha512-test-vector-3.t/stdout create mode 100644 tests/hmac-sha512-test-vector-4.t/TESTRECIPE create mode 100644 tests/hmac-sha512-test-vector-4.t/stdout create mode 100644 tests/hmac-sha512-test-vector-5.t/TESTRECIPE create mode 100644 tests/hmac-sha512-test-vector-5.t/stdout create mode 100644 tests/hmac-sha512-test-vector-6.t/TESTRECIPE create mode 100644 tests/hmac-sha512-test-vector-6.t/stdout create mode 100644 tests/hmac-sha512-test-vector-7.t/TESTRECIPE create mode 100644 tests/hmac-sha512-test-vector-7.t/stdout diff --git a/hx.cabal b/hx.cabal index 86944cf..501458c 100644 --- a/hx.cabal +++ b/hx.cabal @@ -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: diff --git a/hx.hs b/hx.hs index 2cf3c75..7bf81d7 100644 --- a/hx.hs +++ b/hx.hs @@ -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 @@ -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] []" +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 ++ " []" + chksum32_encode :: BS -> BS chksum32_encode d = d <> encode' (chksum32 d) @@ -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 @@ -678,6 +690,12 @@ mainArgs _ = error $ unlines ["Unexpected arguments." ,"hx hash160 [0]" ,"hx hash256 [0]" ,"" + ,"# HASH BASED MACs" + ,"hx hmac-sha224 [] [0]" + ,"hx hmac-sha256 [] [0]" + ,"hx hmac-sha384 [] [0]" + ,"hx hmac-sha512 [] [0]" + ,"" ,"[0]: Not available in sx" ,"[1]: `hx showtx` is always using JSON output," ," `-j` and `--json` are ignored." diff --git a/tests/hex-decode-hmac-test-vector-data-7.t/TESTRECIPE b/tests/hex-decode-hmac-test-vector-data-7.t/TESTRECIPE new file mode 100644 index 0000000..0edf344 --- /dev/null +++ b/tests/hex-decode-hmac-test-vector-data-7.t/TESTRECIPE @@ -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 +} diff --git a/tests/hex-decode-hmac-test-vector-data-7.t/stdin b/tests/hex-decode-hmac-test-vector-data-7.t/stdin new file mode 100644 index 0000000..514912c --- /dev/null +++ b/tests/hex-decode-hmac-test-vector-data-7.t/stdin @@ -0,0 +1 @@ +5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e diff --git a/tests/hex-decode-hmac-test-vector-data-7.t/stdout b/tests/hex-decode-hmac-test-vector-data-7.t/stdout new file mode 100644 index 0000000..401151d --- /dev/null +++ b/tests/hex-decode-hmac-test-vector-data-7.t/stdout @@ -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. \ No newline at end of file diff --git a/tests/hmac-sha224-test-vector-1.t/TESTRECIPE b/tests/hmac-sha224-test-vector-1.t/TESTRECIPE new file mode 100644 index 0000000..ef55e45 --- /dev/null +++ b/tests/hmac-sha224-test-vector-1.t/TESTRECIPE @@ -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 +} diff --git a/tests/hmac-sha224-test-vector-1.t/stdout b/tests/hmac-sha224-test-vector-1.t/stdout new file mode 100644 index 0000000..b130494 --- /dev/null +++ b/tests/hmac-sha224-test-vector-1.t/stdout @@ -0,0 +1 @@ +896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22 diff --git a/tests/hmac-sha224-test-vector-2.t/TESTRECIPE b/tests/hmac-sha224-test-vector-2.t/TESTRECIPE new file mode 100644 index 0000000..31ea674 --- /dev/null +++ b/tests/hmac-sha224-test-vector-2.t/TESTRECIPE @@ -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 +} diff --git a/tests/hmac-sha224-test-vector-2.t/stdout b/tests/hmac-sha224-test-vector-2.t/stdout new file mode 100644 index 0000000..9c2c390 --- /dev/null +++ b/tests/hmac-sha224-test-vector-2.t/stdout @@ -0,0 +1 @@ +a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44 diff --git a/tests/hmac-sha224-test-vector-3.t/TESTRECIPE b/tests/hmac-sha224-test-vector-3.t/TESTRECIPE new file mode 100644 index 0000000..3d36d1e --- /dev/null +++ b/tests/hmac-sha224-test-vector-3.t/TESTRECIPE @@ -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 +} diff --git a/tests/hmac-sha224-test-vector-3.t/stdout b/tests/hmac-sha224-test-vector-3.t/stdout new file mode 100644 index 0000000..39ad474 --- /dev/null +++ b/tests/hmac-sha224-test-vector-3.t/stdout @@ -0,0 +1 @@ +7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea diff --git a/tests/hmac-sha224-test-vector-4.t/TESTRECIPE b/tests/hmac-sha224-test-vector-4.t/TESTRECIPE new file mode 100644 index 0000000..c41f15a --- /dev/null +++ b/tests/hmac-sha224-test-vector-4.t/TESTRECIPE @@ -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 +} diff --git a/tests/hmac-sha224-test-vector-4.t/stdout b/tests/hmac-sha224-test-vector-4.t/stdout new file mode 100644 index 0000000..5b9911b --- /dev/null +++ b/tests/hmac-sha224-test-vector-4.t/stdout @@ -0,0 +1 @@ +6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a diff --git a/tests/hmac-sha224-test-vector-5.t/TESTRECIPE b/tests/hmac-sha224-test-vector-5.t/TESTRECIPE new file mode 100644 index 0000000..267f2cb --- /dev/null +++ b/tests/hmac-sha224-test-vector-5.t/TESTRECIPE @@ -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