From 3f20868d9cf50e38366af4689a1d3af475f73425 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 23 May 2019 19:47:32 -0700 Subject: [PATCH] refactor: Use BIP66 Library (#88) --- .circleci/script_arduino.sh | 1 + .gitmodules | 3 + CHANGELOG.md | 3 +- extras/ARDUINO_IDE.sh | 5 +- platformio.ini | 4 +- src/CMakeLists.txt | 7 +- src/helpers/crypto.cpp | 22 +- src/helpers/encoding/der.cpp | 111 --------- src/include/cpp-crypto/helpers/encoding/der.h | 35 --- src/lib/BIP66 | 1 + test/CMakeLists.txt | 1 - test/helpers/encoding/der.cpp | 220 ------------------ test/platformio.ini | 4 +- 13 files changed, 30 insertions(+), 387 deletions(-) delete mode 100644 src/helpers/encoding/der.cpp delete mode 100644 src/include/cpp-crypto/helpers/encoding/der.h create mode 160000 src/lib/BIP66 delete mode 100644 test/helpers/encoding/der.cpp diff --git a/.circleci/script_arduino.sh b/.circleci/script_arduino.sh index a0d36579..87107574 100644 --- a/.circleci/script_arduino.sh +++ b/.circleci/script_arduino.sh @@ -6,5 +6,6 @@ mkdir -p ~/Arduino/libraries/cpp-crypto/ mv ~/project/* ~/Arduino/libraries/cpp-crypto arduino-cli lib install "ArduinoJson@6.10.0" +arduino-cli lib install "BIP66" arduino-cli compile --output temp.bin -b esp32:esp32:esp32 ~/Arduino/libraries/cpp-crypto/examples/arduino/ESP32/ESP32.ino --debug diff --git a/.gitmodules b/.gitmodules index 164236c2..398e14cd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "src/lib/ArduinoJson"] path = src/lib/ArduinoJson url = https://github.com/bblanchon/ArduinoJson +[submodule "src/lib/BIP66"] + path = src/lib/BIP66 + url = https://github.com/sleepdefic1t/BIP66 diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a4cd161..d04e81a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed +- changed to BIP66 lib for DER ser/des. ([#88]) - updated vendorField to support 255 bytes in Core v2.4 ([#84]) - updated ArduinoJson package to version v.6.10.0 ([#76]) - updated tests to use Core fixtures ([#74]) @@ -16,7 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed -- properly handle 0 ARKtoshi Transaction amounts. +- properly handle 0 ARKtoshi Transaction amounts. ([#85]) ## [0.3.1] - 2019-02-19 diff --git a/extras/ARDUINO_IDE.sh b/extras/ARDUINO_IDE.sh index d296f211..4ed924ce 100644 --- a/extras/ARDUINO_IDE.sh +++ b/extras/ARDUINO_IDE.sh @@ -112,8 +112,8 @@ if [[ -d ${INCLUDE_DIR} ]]; then mv ${INCLUDE_ENUMS_DIR}/types.h ${SRC_ENUMS_DIR} echo -e "Moving 'helpers' headers.\n" - mv ${INCLUDE_HELPERS_DIR}/encoding/der.h ${SRC_HELPERS_DIR}/encoding - mv ${INCLUDE_HELPERS_DIR}/encoding/hex.h ${SRC_HELPERS_DIR}/encoding + mkdir ${SRC_ENCODING_DIR} + mv ${INCLUDE_HELPERS_DIR}/encoding/hex.h ${SRC_ENCODING_DIR} ## 'bip39' library is not supported in Arduino echo -e "Backing up and removing 'mnemonic.h'.\n" @@ -192,6 +192,7 @@ else echo -e "Moving 'helpers/encoding' headers.\n" mv ${SRC_ENCODING_DIR}/hex.h ${INCLUDE_ENCODING_DIR} + rm ${SRC_ENCODING_DIR} echo -e "Moving 'identities' headers.\n" mv ${SRC_IDENTITIES_DIR}/address.h ${INCLUDE_IDENTITIES_DIR} diff --git a/platformio.ini b/platformio.ini index 522182d1..8cc3220f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,9 +13,9 @@ description = "A simple Cryptography Implementation in C++ for the ARK Blockchai [common] lib_ldf_mode = off -lib_deps = micro-ecc, bip39@^1.1, ArduinoJson@6.10.0 +lib_deps = micro-ecc, bip39@^1.1, ArduinoJson@6.10.0, BIP66 build_flags = -I./src/ -I./src/lib -I./src/include/cpp-crypto -src_filter = +<*> -<.git/> - - - - - +src_filter = +<*> -<.git/> - - - - - - upload_speed = 921600 [env:esp8266] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index efca42da..7e1c1144 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,10 @@ project(Ark-Cpp-Crypto-lib C CXX) add_subdirectory(lib/bip39/src) +set(BIP66_SRC + ${PROJECT_SOURCE_DIR}/lib/BIP66/src/bip66.cpp +) + set(BCL_SRC lib/bcl/Base58Check.cpp lib/bcl/CurvePoint.cpp @@ -26,7 +30,6 @@ set(COMMON_SRC configuration/fee.cpp configuration/network.cpp helpers/crypto.cpp - helpers/encoding/der.cpp identities/address.cpp identities/mnemonic.cpp identities/privatekey.cpp @@ -45,6 +48,7 @@ add_library(${PROJECT_NAME} STATIC ${BCL_SRC} ${uECC_SRC} + ${BIP66_SRC} ${COMMON_SRC} ) @@ -59,6 +63,7 @@ include_directories(${PROJECT_SOURCE_DIR}/lib/bcl) include_directories(${PROJECT_SOURCE_DIR}/lib/bip39) include_directories(${PROJECT_SOURCE_DIR}/lib/rfc6979) include_directories(${PROJECT_SOURCE_DIR}/lib/uECC) +include_directories(${PROJECT_SOURCE_DIR}/lib/BIP66/src) target_include_directories( ${PROJECT_NAME} PUBLIC ${cpp_crypto_build_include_dirs} diff --git a/src/helpers/crypto.cpp b/src/helpers/crypto.cpp index 508c744d..79494fae 100644 --- a/src/helpers/crypto.cpp +++ b/src/helpers/crypto.cpp @@ -4,10 +4,10 @@ #include "bcl/Ecdsa.hpp" #include "bcl/Sha256.hpp" #include "bcl/Uint256.hpp" -#include "helpers/encoding/der.h" #include "helpers/crypto_helpers.h" #include "rfc6979/rfc6979.h" #include "uECC.h" +#include "bip66.h" void cryptoSign(Sha256Hash hash, Ark::Crypto::Identities::PrivateKey privateKey, std::vector& signature) { Uint256 r; @@ -19,13 +19,11 @@ void cryptoSign(Sha256Hash hash, Ark::Crypto::Identities::PrivateKey privateKey, auto ret = Ecdsa::sign(Uint256(privateKey.toBytes()), hash, Uint256(nonce32), r, s); assert(ret); - std::vector r_der(PRIVATEKEY_SIZE); - r.getBigEndianBytes(&r_der[0]); + std::vector rValue(PRIVATEKEY_SIZE), sValue(PRIVATEKEY_SIZE); + r.getBigEndianBytes(&rValue[0]); + s.getBigEndianBytes(&sValue[0]); - std::vector s_der(PRIVATEKEY_SIZE); - s.getBigEndianBytes(&s_der[0]); - - encodeDER(toDER(r_der), toDER(s_der), signature); + BIP66::encode(rValue, sValue, signature); } bool cryptoVerify(Ark::Crypto::Identities::PublicKey publicKey, Sha256Hash hash, std::vector& signature) { @@ -52,12 +50,12 @@ bool cryptoVerify(Ark::Crypto::Identities::PublicKey publicKey, Sha256Hash hash, CurvePoint curvePoint(x, y); /* Decode signature from DER into r & s buffers */ - std::vector r; // create r-value buffer - std::vector s; // create s-value buffer - decodeDER(signature, r, s); + std::vector rValue(PRIVATEKEY_SIZE), sValue(PRIVATEKEY_SIZE); + + BIP66::decode(signature, rValue, sValue); - Uint256 r256(r.data()); // create Uint256/BigNumber from r-value buffer - Uint256 s256(s.data()); // create Uint256/BigNumber from s-value buffer + Uint256 r256(rValue.data()); // create Uint256/BigNumber from r-value buffer + Uint256 s256(sValue.data()); // create Uint256/BigNumber from s-value buffer /* Verify */ return Ecdsa::verify(curvePoint, hash, r256, s256); diff --git a/src/helpers/encoding/der.cpp b/src/helpers/encoding/der.cpp deleted file mode 100644 index 9265a315..00000000 --- a/src/helpers/encoding/der.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#include "helpers/encoding/der.h" - -#include -#include -#include -#include - -/** - * DER Encode/Decode Helpers - **/ -std::vector& toDER(std::vector& buffer) { - // if the sign bit is set, pad with a 0x00 byte - if (buffer.size() > 1 && (buffer[0] & 0x80) != 0) { - buffer.insert(buffer.begin(), 0x00); - } - return buffer; -} - -/**/ - -void decodeDER(std::vector& signature, std::vector& r, std::vector& s) { - // Adapted from https://github.com/bitcoinjs/bip66/blob/master/index.js - assert(signature.size() > 8); // DER sequence length is too short - assert(signature.size() < 72); // DER sequence length is too long - assert(signature[0] == 0x30); // Expected DER sequence - assert(signature[1] == signature.size() - 2); // DER sequence length is invalid - assert(signature[2] == 0x02); // Expected DER integer - - /* Get the length of the signatures R-value (signature 4th-byte/signature[3]) */ - int lenR = signature[3]; - assert(lenR != 0); // R length is zero - assert(5u + lenR <= signature.size()); // R length is too long - assert(signature[4 + lenR] == 0x02); // Expected DER integer (2) - - /* Get the length of the signatures R-value (signature 6th-byte/signature[5]) */ - int lenS = signature[5 + lenR]; - assert(lenS != 0); // S length is zero - assert((6u + lenR + lenS) == signature.size()); // S length is invalid - - assert(signature[4] != 0x80); // R value is negative - assert((lenR > 1)); // && (signature[4] == 0x00) && !(signature[5] == 0x80)); // R value excessively padded - - assert(signature[lenR + 6] != 0x80); // S value is negative - assert(lenS > - 1); // && (signature[lenR + 6] != 0x00) && !(signature[lenR + 7] == 0x80)); // S value excessively padded - - /* non-BIP66 - extract R, S values */ - r = std::vector(&signature[4], &signature[4] + lenR); - s = std::vector(&signature[6 + lenR], &signature[6 + lenR] + lenS); -} - -/**/ - -void encodeDER(const std::vector& r, const std::vector& s, std::vector& signature) { - /* Adapted from https://github.com/bitcoinjs/bip66/blob/master/index.js */ - auto lenR = r.size(); - auto lenS = s.size(); - assert(lenR != 0); // must be non zero - assert(lenS != 0); - assert(lenR <= 33); // must be less than 34 bytes - assert(lenS <= 33); - assert((r[0] & 0x80) == 0); // must not be negative - assert((s[0] & 0x80) == 0); - assert(lenR == 1 || r[0] != 0x00 || (r[1] & 0x80) != 0); // must have zero pad for negative number - assert(lenS == 1 || s[0] != 0x00 || (s[1] & 0x80) != 0); - - auto it = r.begin(); - while (lenR > 1 && *it == 0 && *(it + 1) < 0x80) { - --lenR; - ++it; - } - it = s.begin(); - while (lenS > 1 && *it == 0 && *(it + 1) < 0x80) { - --lenS; - ++it; - } - - signature.clear(); - signature.reserve(6 + lenR + lenS); - - /* 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] */ - signature.push_back(0x30); // [0] - signature.push_back(static_cast(6 + lenR + lenS - 2)); // [1] - signature.push_back(0x02); // [2] - signature.push_back(static_cast(lenR)); // [3] - signature.insert(signature.end(), r.begin(), r.end()); //[4] - signature.push_back(0x02); // [4 + lenR] - signature.push_back(static_cast(lenS)); // [5 + lenR] - signature.insert(signature.end(), s.begin(), s.end()); //[6 + lenR] -} - -/**/ - -void encodeDER(uint8_t packed_signature[DEFAULT_PRIVATEKEY_SIZE * 2], std::vector& signature) { - std::vector r(DEFAULT_PRIVATEKEY_SIZE); - std::vector s(DEFAULT_PRIVATEKEY_SIZE); - - memcpy(&r[0], packed_signature, DEFAULT_PRIVATEKEY_SIZE); - memcpy(&s[0], packed_signature + DEFAULT_PRIVATEKEY_SIZE, DEFAULT_PRIVATEKEY_SIZE); - - encodeDER(toDER(r), toDER(s), signature); -} diff --git a/src/include/cpp-crypto/helpers/encoding/der.h b/src/include/cpp-crypto/helpers/encoding/der.h deleted file mode 100644 index 8d3c67b2..00000000 --- a/src/include/cpp-crypto/helpers/encoding/der.h +++ /dev/null @@ -1,35 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#ifndef DER_H -#define DER_H - -#include -#include - -const auto DEFAULT_PRIVATEKEY_SIZE = 32u; - -/** - * DER Encode/Decode Helpers - **/ -std::vector& toDER(std::vector& buffer); - -/**/ - -void decodeDER(std::vector& signature, std::vector& r, std::vector& s); - -/**/ - -void encodeDER(const std::vector& r, const std::vector& s, std::vector& signature); - -/**/ - -void encodeDER(uint8_t packed_signature[DEFAULT_PRIVATEKEY_SIZE * 2], std::vector& signature); - -#endif diff --git a/src/lib/BIP66 b/src/lib/BIP66 new file mode 160000 index 00000000..5f024b4b --- /dev/null +++ b/src/lib/BIP66 @@ -0,0 +1 @@ +Subproject commit 5f024b4b70fb0d267da483304afb72bb884413cb diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5739a897..9cb1b7d4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,7 +23,6 @@ set (TEST_SRC ${PROJECT_SOURCE_DIR}/configuration/network.cpp ${PROJECT_SOURCE_DIR}/enums/fees.cpp ${PROJECT_SOURCE_DIR}/enums/types.cpp - ${PROJECT_SOURCE_DIR}/helpers/encoding/der.cpp ${PROJECT_SOURCE_DIR}/identities/address.cpp ${PROJECT_SOURCE_DIR}/identities/mnemonic.cpp ${PROJECT_SOURCE_DIR}/identities/privatekey.cpp diff --git a/test/helpers/encoding/der.cpp b/test/helpers/encoding/der.cpp deleted file mode 100644 index 6a94c19c..00000000 --- a/test/helpers/encoding/der.cpp +++ /dev/null @@ -1,220 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#include "gtest/gtest.h" - -#include "helpers/encoding/der.h" -#include "helpers/encoding/hex.h" - -TEST(helpers_encoding_der, decodeDER) { - auto signature = HexToBytes( - "3044022033a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c902206f807982866f785d3f6418d24163ddae117b" - "7db4d5fdf0071de069fa54342262"); - std::vector r; - std::vector s; - - decodeDER(signature, r, s); - ASSERT_STRCASEEQ("33A69CD2065432A30F3D1CE4EB0D59B8AB58C74F27C41A7FDB5696AD4E6108C9", BytesToHex(r).c_str()); - ASSERT_STRCASEEQ("6F807982866F785D3F6418D24163DDAE117B7DB4D5FDF0071DE069FA54342262", BytesToHex(s).c_str()); - - signature = HexToBytes( - "3044022054c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed022007082304410efa6b2943111b6a4e0aaa7b7d" - "b55a07e9861d1fb3cb1f421044a5"); - decodeDER(signature, r, s); - ASSERT_STRCASEEQ("54C4A33C6423D689378F160A7FF8B61330444ABB58FB470F96EA16D99D4A2FED", BytesToHex(r).c_str()); - ASSERT_STRCASEEQ("07082304410EFA6B2943111B6A4E0AAA7B7DB55A07E9861D1FB3CB1F421044A5", BytesToHex(s).c_str()); - - signature = HexToBytes( - "3045022100ff466a9f1b7b273e2f4c3ffe032eb2e814121ed18ef84665d0f515360dab3dd002206fc95f5132e5ecfdc8e5e6e616cc771514" - "55d46ed48f5589b7db7771a332b283"); - decodeDER(signature, r, s); - ASSERT_STRCASEEQ("00FF466A9F1B7B273E2F4C3FFE032EB2E814121ED18EF84665D0F515360DAB3DD0", BytesToHex(r).c_str()); - ASSERT_STRCASEEQ("6FC95F5132E5ECFDC8E5E6E616CC77151455D46ED48F5589B7DB7771A332B283", BytesToHex(s).c_str()); - - signature = HexToBytes( - "3045022100c0dafec8251f1d5010289d210232220b03202cba34ec11fec58b3e93a85b91d3022075afdc06b7d6322a590955bf264e7aaa15" - "5847f614d80078a90292fe205064d3"); - decodeDER(signature, r, s); - ASSERT_STRCASEEQ("00C0DAFEC8251F1D5010289D210232220B03202CBA34EC11FEC58B3E93A85B91D3", BytesToHex(r).c_str()); - ASSERT_STRCASEEQ("75AFDC06B7D6322A590955BF264E7AAA155847F614D80078A90292FE205064D3", BytesToHex(s).c_str()); - - signature = HexToBytes( - "304402207186363571d65e084e7f02b0b77c3ec44fb1b257dee26274c38c928986fea45d02200de0b38e06807e46bda1f1e293f4f6323e85" - "4c86d58abdd00c46c16441085df6"); - decodeDER(signature, r, s); - ASSERT_STRCASEEQ("7186363571D65E084E7F02B0B77C3EC44FB1B257DEE26274C38C928986FEA45D", BytesToHex(r).c_str()); - ASSERT_STRCASEEQ("0DE0B38E06807E46BDA1F1E293F4F6323E854C86D58ABDD00C46C16441085DF6", BytesToHex(s).c_str()); - - signature = HexToBytes( - "3045022100fbfe5076a15860ba8ed00e75e9bd22e05d230f02a936b653eb55b61c99dda48702200e68880ebb0050fe4312b1b1eb0899e1b8" - "2da89baa5b895f612619edf34cbd37"); - decodeDER(signature, r, s); - ASSERT_STRCASEEQ("00FBFE5076A15860BA8ED00E75E9BD22E05D230F02A936B653EB55B61C99DDA487", BytesToHex(r).c_str()); - ASSERT_STRCASEEQ("0E68880EBB0050FE4312B1B1EB0899E1B82DA89BAA5B895F612619EDF34CBD37", BytesToHex(s).c_str()); - - signature = HexToBytes( - "3045022100cde1302d83f8dd835d89aef803c74a119f561fbaef3eb9129e45f30de86abbf9022006ce643f5049ee1f27890467b77a6a8e11" - "ec4661cc38cd8badf90115fbd03cef"); - decodeDER(signature, r, s); - ASSERT_STRCASEEQ("00CDE1302D83F8DD835D89AEF803C74A119F561FBAEF3EB9129E45F30DE86ABBF9", BytesToHex(r).c_str()); - ASSERT_STRCASEEQ("06CE643F5049EE1F27890467B77A6A8E11EC4661CC38CD8BADF90115FBD03CEF", BytesToHex(s).c_str()); -} - -TEST(helpers_encoding_der, encodeDER) { - auto r = HexToBytes( - "33A69CD2065432A30F3D1CE4EB0D59B8AB58C74F27C41A7FDB5696AD4E6108C9"); // dec: - // 23362334225185207751494092901091441011938859014081160902781146257181456271561 - auto s = HexToBytes( - "6F807982866F785D3F6418D24163DDAE117B7DB4D5FDF0071DE069FA54342262"); // dec: - // 50433721247292933944369538617440297985091596895097604618403996029256432099938 - std::vector signature; - encodeDER(toDER(r), toDER(s), signature); - ASSERT_STRCASEEQ( - "3044022033a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c902206f807982866f785d3f6418d24163ddae117b" - "7db4d5fdf0071de069fa54342262", - BytesToHex(signature).c_str()); - - r = HexToBytes( - "54C4A33C6423D689378F160A7FF8B61330444ABB58FB470F96EA16D99D4A2FED"); // dec: - // 38341707918488238920692284707283974715538935465589664377561695343399725051885 - s = HexToBytes( - "07082304410EFA6B2943111B6A4E0AAA7B7DB55A07E9861D1FB3CB1F421044A5"); // dec: - // 3180566392414476763164587487324397066658063772201694230600609996154610926757 - encodeDER(toDER(r), toDER(s), signature); - ASSERT_STRCASEEQ( - "3044022054c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed022007082304410efa6b2943111b6a4e0aaa7b7d" - "b55a07e9861d1fb3cb1f421044a5", - BytesToHex(signature).c_str()); - - r = HexToBytes( - "FF466A9F1B7B273E2F4C3FFE032EB2E814121ED18EF84665D0F515360DAB3DD0"); // dec: - // 115464191557905790016094131873849783294273568009648050793030031933291767741904 - s = HexToBytes( - "6FC95F5132E5ECFDC8E5E6E616CC77151455D46ED48F5589B7DB7771A332B283"); // dec: - // 50562520307781850052192542766631199590053690478900449960232079510155113443971 - encodeDER(toDER(r), toDER(s), signature); - ASSERT_STRCASEEQ( - "3045022100ff466a9f1b7b273e2f4c3ffe032eb2e814121ed18ef84665d0f515360dab3dd002206fc95f5132e5ecfdc8e5e6e616cc771514" - "55d46ed48f5589b7db7771a332b283", - BytesToHex(signature).c_str()); - - r = HexToBytes( - "C0DAFEC8251F1D5010289D210232220B03202CBA34EC11FEC58B3E93A85B91D3"); // dec: - // 87230998027579607140680851455601772643840468630989315269459846730712163783123 - s = HexToBytes( - "75AFDC06B7D6322A590955BF264E7AAA155847F614D80078A90292FE205064D3"); // dec: - // 53231320085894623106179381504478252331065330583563809963303318469380290929875 - encodeDER(toDER(r), toDER(s), signature); - ASSERT_STRCASEEQ( - "3045022100c0dafec8251f1d5010289d210232220b03202cba34ec11fec58b3e93a85b91d3022075afdc06b7d6322a590955bf264e7aaa15" - "5847f614d80078a90292fe205064d3", - BytesToHex(signature).c_str()); - - r = HexToBytes( - "7186363571D65E084E7F02B0B77C3EC44FB1B257DEE26274C38C928986FEA45D"); // dec: - // 51348483531757779992459563033975330355971795607481991320287437101831125115997 - s = HexToBytes( - "0DE0B38E06807E46BDA1F1E293F4F6323E854C86D58ABDD00C46C16441085DF6"); // dec: - // 6277080015686056199074771961940657638578000617958603212944619747099038735862 - encodeDER(toDER(r), toDER(s), signature); - ASSERT_STRCASEEQ( - "304402207186363571d65e084e7f02b0b77c3ec44fb1b257dee26274c38c928986fea45d02200de0b38e06807e46bda1f1e293f4f6323e85" - "4c86d58abdd00c46c16441085df6", - BytesToHex(signature).c_str()); - - r = HexToBytes( - "FBFE5076A15860BA8ED00E75E9BD22E05D230F02A936B653EB55B61C99DDA487"); // dec: - // 113979859486826658566290715281614250298918272782414232881639314569529560769671 - s = HexToBytes( - "0E68880EBB0050FE4312B1B1EB0899E1B82DA89BAA5B895F612619EDF34CBD37"); // dec: - // 6517071009538626957379450615706485096874328019806177698938278220732027419959 - encodeDER(toDER(r), toDER(s), signature); - ASSERT_STRCASEEQ( - "3045022100fbfe5076a15860ba8ed00e75e9bd22e05d230f02a936b653eb55b61c99dda48702200e68880ebb0050fe4312b1b1eb0899e1b8" - "2da89baa5b895f612619edf34cbd37", - BytesToHex(signature).c_str()); - - r = HexToBytes( - "CDE1302D83F8DD835D89AEF803C74A119F561FBAEF3EB9129E45F30DE86ABBF9"); // dec: - // 93122007060065279508564838030979550535085999589142852106617159184757394422777 - s = HexToBytes( - "06CE643F5049EE1F27890467B77A6A8E11EC4661CC38CD8BADF90115FBD03CEF"); // dec: - // 3078539468410661027472930027406594684630312677495124015420811882501887769839 - encodeDER(toDER(r), toDER(s), signature); - ASSERT_STRCASEEQ( - "3045022100cde1302d83f8dd835d89aef803c74a119f561fbaef3eb9129e45f30de86abbf9022006ce643f5049ee1f27890467b77a6a8e11" - "ec4661cc38cd8badf90115fbd03cef", - BytesToHex(signature).c_str()); -} - -TEST(helpers_encoding_der_packed, encodeDER) { - auto packed_signature = HexToBytes( - "33A69CD2065432A30F3D1CE4EB0D59B8AB58C74F27C41A7FDB5696AD4E6108C96F807982866F785D3F6418D24163DDAE117B7DB4D5FDF007" - "1DE069FA54342262"); - std::vector signature; - encodeDER(&packed_signature[0], signature); - ASSERT_STRCASEEQ( - "3044022033a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c902206f807982866f785d3f6418d24163ddae117b" - "7db4d5fdf0071de069fa54342262", - BytesToHex(signature).c_str()); - - packed_signature = HexToBytes( - "54C4A33C6423D689378F160A7FF8B61330444ABB58FB470F96EA16D99D4A2FED07082304410EFA6B2943111B6A4E0AAA7B7DB55A07E9861D" - "1FB3CB1F421044A5"); - encodeDER(&packed_signature[0], signature); - ASSERT_STRCASEEQ( - "3044022054c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed022007082304410efa6b2943111b6a4e0aaa7b7d" - "b55a07e9861d1fb3cb1f421044a5", - BytesToHex(signature).c_str()); - - packed_signature = HexToBytes( - "FF466A9F1B7B273E2F4C3FFE032EB2E814121ED18EF84665D0F515360DAB3DD06FC95F5132E5ECFDC8E5E6E616CC77151455D46ED48F5589" - "B7DB7771A332B283"); - encodeDER(&packed_signature[0], signature); - ASSERT_STRCASEEQ( - "3045022100ff466a9f1b7b273e2f4c3ffe032eb2e814121ed18ef84665d0f515360dab3dd002206fc95f5132e5ecfdc8e5e6e616cc771514" - "55d46ed48f5589b7db7771a332b283", - BytesToHex(signature).c_str()); - - packed_signature = HexToBytes( - "C0DAFEC8251F1D5010289D210232220B03202CBA34EC11FEC58B3E93A85B91D375AFDC06B7D6322A590955BF264E7AAA155847F614D80078" - "A90292FE205064D3"); - encodeDER(&packed_signature[0], signature); - ASSERT_STRCASEEQ( - "3045022100c0dafec8251f1d5010289d210232220b03202cba34ec11fec58b3e93a85b91d3022075afdc06b7d6322a590955bf264e7aaa15" - "5847f614d80078a90292fe205064d3", - BytesToHex(signature).c_str()); - - packed_signature = HexToBytes( - "7186363571D65E084E7F02B0B77C3EC44FB1B257DEE26274C38C928986FEA45D0DE0B38E06807E46BDA1F1E293F4F6323E854C86D58ABDD0" - "0C46C16441085DF6"); - encodeDER(&packed_signature[0], signature); - ASSERT_STRCASEEQ( - "304402207186363571d65e084e7f02b0b77c3ec44fb1b257dee26274c38c928986fea45d02200de0b38e06807e46bda1f1e293f4f6323e85" - "4c86d58abdd00c46c16441085df6", - BytesToHex(signature).c_str()); - - packed_signature = HexToBytes( - "FBFE5076A15860BA8ED00E75E9BD22E05D230F02A936B653EB55B61C99DDA4870E68880EBB0050FE4312B1B1EB0899E1B82DA89BAA5B895F" - "612619EDF34CBD37"); - encodeDER(&packed_signature[0], signature); - ASSERT_STRCASEEQ( - "3045022100fbfe5076a15860ba8ed00e75e9bd22e05d230f02a936b653eb55b61c99dda48702200e68880ebb0050fe4312b1b1eb0899e1b8" - "2da89baa5b895f612619edf34cbd37", - BytesToHex(signature).c_str()); - - packed_signature = HexToBytes( - "CDE1302D83F8DD835D89AEF803C74A119F561FBAEF3EB9129E45F30DE86ABBF906CE643F5049EE1F27890467B77A6A8E11EC4661CC38CD8B" - "ADF90115FBD03CEF"); - encodeDER(&packed_signature[0], signature); - ASSERT_STRCASEEQ( - "3045022100cde1302d83f8dd835d89aef803c74a119f561fbaef3eb9129e45f30de86abbf9022006ce643f5049ee1f27890467b77a6a8e11" - "ec4661cc38cd8badf90115fbd03cef", - BytesToHex(signature).c_str()); -} diff --git a/test/platformio.ini b/test/platformio.ini index a03ad134..bcfd048e 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -15,9 +15,9 @@ lib_dir = .. [common] lib_ldf_mode = off -lib_deps = micro-ecc, bip39@^1.1, AUnit, ArduinoJson@6.10.0 +lib_deps = micro-ecc, bip39@^1.1, AUnit, ArduinoJson@6.10.0, BIP66 build_flags = -I../test/iot/ -I../src -I../src/lib -I../src/include/cpp-crypto -DUNIT_TEST -src_filter = +<*> -<.git/> - - - - -<_3rdParty> - - - - - - - +src_filter = +<*> -<.git/> - - - - -<_3rdParty> - - - - - - - - upload_speed = 921600 # esp8266 unit tests disabled until support is worked out