From c4eb65e9052a76b39dee7d0ae37f81cbcf9b63eb Mon Sep 17 00:00:00 2001 From: Marc Worrell Date: Thu, 17 Jun 2021 09:37:32 +0200 Subject: [PATCH 1/3] OTP24 compatibility --- .github/workflows/test.yml | 36 +++++++++++++++++++++++++++++ .gitignore | 4 +++- Makefile | 40 +++++++++++++++++---------------- rebar.config | 6 +++++ rebar.lock | 1 + src/termit.erl | 46 +++++--------------------------------- 6 files changed, 72 insertions(+), 61 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 rebar.lock diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..fbd2a96 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,36 @@ +# This workflow checks the tests and dialyzer. + +name: Test + +# Controls when the action will run. Triggers the workflow on push or pull request +# events but only for the master branch +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + linux: + name: Test on OTP ${{ matrix.otp_version }} + runs-on: ${{ matrix.os }} + + strategy: + matrix: + otp_version: [21.3, 22.3, 23.2.5, 24.0.1] + os: [ubuntu-latest] + + container: + image: erlang:${{ matrix.otp_version }} + + steps: + - uses: actions/checkout@v2 + - name: Compile + run: make + - name: Test + run: make test + - name: XRef + run: make xref + - name: Dialyzer + run: make dialyzer diff --git a/.gitignore b/.gitignore index a476804..c5b9e8a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,9 @@ ebin deps .eunit .ct -.rebar +.rebar3 +rebar3 logs test/*.beam erl_crash.dump +_build diff --git a/Makefile b/Makefile index ee9c8cf..f113927 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,31 @@ -all: deps compile check test +ERL ?= erl +REBAR := ./rebar3 +REBAR_URL := https://s3.amazonaws.com/rebar3/rebar3 -deps: - rebar get-deps +.PHONY: all compile check test clean -compile: - rebar compile +all: ./rebar3 compile check test + +$(REBAR): + $(ERL) -noshell -s inets -s ssl \ + -eval '{ok, saved_to_file} = httpc:request(get, {"$(REBAR_URL)", []}, [{ssl, [ {verify, verify_none} ]}], [{stream, "$(REBAR)"}])' \ + -s init stop + chmod +x $(REBAR) -run: compile - sh start.sh +compile: + $(REBAR) compile clean: - rebar clean - rm -fr ebin .ct test/*.beam + $(REBAR) clean check: - rebar eunit skip_deps=true - -test: deps compile check - #rebar ct - mkdir -p .ct - ct_run -dir test -logdir .ct -pa ebin + $(REBAR) eunit -dist: deps compile - echo TODO +test: compile check + $(REBAR) ct -.PHONY: all deps compile check test run clean dist -.SILENT: +dialyzer: + $(REBAR) dialyzer +xref: + $(REBAR) xref diff --git a/rebar.config b/rebar.config index 17effc6..6cf888b 100644 --- a/rebar.config +++ b/rebar.config @@ -15,3 +15,9 @@ ]}. {cover_enabled, true}. + +{xref_checks, [ + undefined_function_calls, + locals_not_used, + deprecated_function_calls +]}. diff --git a/rebar.lock b/rebar.lock new file mode 100644 index 0000000..57afcca --- /dev/null +++ b/rebar.lock @@ -0,0 +1 @@ +[]. diff --git a/src/termit.erl b/src/termit.erl index 7c5cd4c..89bc22f 100644 --- a/src/termit.erl +++ b/src/termit.erl @@ -75,47 +75,12 @@ decode(Bin, _) when is_binary(Bin) -> {error, forged}. - --ifdef(crypto_compatibility). --spec key( - Secret :: binary()) -> - MAC16 :: binary(). - -key(Secret) -> - crypto:md5_mac(Secret, []). - --spec sign( - Data :: binary(), - Secret :: binary()) -> - MAC20 :: binary(). - -sign(Data, Key) -> - crypto:sha_mac(Key, Data). - --spec encrypt( - Data :: binary(), - Key :: binary(), - IV :: binary()) -> - Cipher :: binary(). - -encrypt(Data, Key, IV) -> - Crypt = crypto:aes_cfb_128_encrypt(Key, IV, Data), - << IV/binary, Crypt/binary>>. - --spec uncrypt( - Cipher :: binary(), - Key :: binary()) -> - Uncrypted :: binary(). - -uncrypt(<< IV:16/binary, Data/binary >>, Key) -> - crypto:aes_cfb_128_decrypt(Key, IV, Data). --else -spec key( Secret :: binary()) -> MAC16 :: binary(). key(Secret) -> - crypto:hmac(md5, Secret, []). + crypto:mac(hmac, md5, Secret, []). -spec sign( Data :: binary(), @@ -123,7 +88,7 @@ key(Secret) -> MAC20 :: binary(). sign(Data, Key) -> - crypto:hmac(sha, Key, Data). + crypto:mac(hmac, sha, Key, Data). -spec encrypt( Data :: binary(), Key :: binary(), @@ -131,7 +96,7 @@ sign(Data, Key) -> Cipher :: binary(). encrypt(Data, Key, IV) -> - Crypt = crypto:block_encrypt(aes_cfb128, Key, IV, Data), + Crypt = crypto:crypto_one_time(aes_cfb128, Key, IV, Data, true), << IV/binary, Crypt/binary>>. -spec uncrypt( @@ -140,9 +105,8 @@ encrypt(Data, Key, IV) -> Uncrypted :: binary(). uncrypt(<< IV:16/binary, Data/binary >>, Key) -> - crypto:block_decrypt(aes_cfb128, Key, IV, Data). + crypto:crypto_one_time(aes_cfb128, Key, IV, Data, false). --endif. %% %% ----------------------------------------------------------------------------- @@ -286,7 +250,7 @@ rand_uniform(N) -> encrypt_test() -> IV = crypto:strong_rand_bytes(16), - Secret = crypto:hmac(md5, <<"Make It Elegant">>, []), + Secret = crypto:mac(hmac, md5, <<"Make It Elegant">>, []), << Secret15:15/binary, _/binary >> = Secret, Bin = <<"Transire Benefaciendo">>, ?assertEqual(Bin, uncrypt(encrypt(Bin, Secret, IV), Secret)), From 1f4f56d0bdd8b10633091cbe1c7c81eebcc28dfc Mon Sep 17 00:00:00 2001 From: Marc Worrell Date: Thu, 17 Jun 2021 09:46:05 +0200 Subject: [PATCH 2/3] Remove OTP 21 as test target. rebar3 does not work on OTP < 22 --- .github/workflows/hex-publish.yml | 18 ++++++++++++++++++ .github/workflows/test.yml | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/hex-publish.yml diff --git a/.github/workflows/hex-publish.yml b/.github/workflows/hex-publish.yml new file mode 100644 index 0000000..4592c83 --- /dev/null +++ b/.github/workflows/hex-publish.yml @@ -0,0 +1,18 @@ +name: Hex Publish + +on: + push: + tags: + - '*' + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - name: Check out + uses: actions/checkout@v2 + + - name: Publish to Hex.pm + uses: erlangpack/github-action@v1 + env: + HEX_API_KEY: ${{ secrets.HEX_API_KEY }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fbd2a96..8dba323 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ jobs: strategy: matrix: - otp_version: [21.3, 22.3, 23.2.5, 24.0.1] + otp_version: [22.3, 23.2.5, 24.0.1] os: [ubuntu-latest] container: From aafaeeb66df5a1eb7a98042c986c15cfdc518855 Mon Sep 17 00:00:00 2001 From: Marc Worrell Date: Thu, 17 Jun 2021 09:56:15 +0200 Subject: [PATCH 3/3] Specify key length for cipher, as needed in OTP22. Use aes_128_cfb128 instead of aes_cfb128 --- src/termit.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/termit.erl b/src/termit.erl index 89bc22f..f86f1a7 100644 --- a/src/termit.erl +++ b/src/termit.erl @@ -96,7 +96,7 @@ sign(Data, Key) -> Cipher :: binary(). encrypt(Data, Key, IV) -> - Crypt = crypto:crypto_one_time(aes_cfb128, Key, IV, Data, true), + Crypt = crypto:crypto_one_time(aes_128_cfb128, Key, IV, Data, true), << IV/binary, Crypt/binary>>. -spec uncrypt( @@ -105,7 +105,7 @@ encrypt(Data, Key, IV) -> Uncrypted :: binary(). uncrypt(<< IV:16/binary, Data/binary >>, Key) -> - crypto:crypto_one_time(aes_cfb128, Key, IV, Data, false). + crypto:crypto_one_time(aes_128_cfb128, Key, IV, Data, false). %%