From 87e27ec31d1ef86ff2449e2c0c3b8a5cb0f85222 Mon Sep 17 00:00:00 2001 From: Arun M Date: Fri, 3 Nov 2017 09:57:12 +0530 Subject: [PATCH] HMAC algo added --- include/jwt/algorithm.hpp | 233 ++++++++++++++++++++++++++++++++++ include/jwt/jwt.hpp | 3 + include/jwt/test/test_hmac | Bin 0 -> 16764 bytes include/jwt/test/test_hmac.cc | 16 +++ 4 files changed, 252 insertions(+) create mode 100644 include/jwt/algorithm.hpp create mode 100755 include/jwt/test/test_hmac create mode 100644 include/jwt/test/test_hmac.cc diff --git a/include/jwt/algorithm.hpp b/include/jwt/algorithm.hpp new file mode 100644 index 0000000..f9904f4 --- /dev/null +++ b/include/jwt/algorithm.hpp @@ -0,0 +1,233 @@ +#ifndef CPP_JWT_ALGORITHM_HPP +#define CPP_JWT_ALGORITHM_HPP + +#include +#include + +#include +#include +#include +#include + +#include "jwt/string_view.hpp" + +namespace jwt { + +/// The result type of the signing function +using sign_result_t = std::pair; +/// The result type of verification function +using verify_result_t = std::pair; +/// The function pointer type of the signing function +using sign_func_t = sign_result_t (*) (string_view key, string_view data); + +namespace algo { + +//TODO: All these can be done using code generaion. +// NO. NEVER. I hate Macros. +// You can use templates too. +// No. I would rather prefer explicit. +// Ok. You win. + +/*! + */ +struct HS256 +{ + const EVP_MD* operator()() noexcept + { + return EVP_sha256(); + } +}; + +/*! + */ +struct HS384 +{ + const EVP_MD* operator()() noexcept + { + return EVP_sha384(); + } +}; + +/*! + */ +struct HS512 +{ + const EVP_MD* operator()() noexcept + { + return EVP_sha512(); + } +}; + +/*! + */ +struct NONE +{ + void operator()() noexcept + { + return; + } +}; + +/*! + */ +struct RS256 +{ + static const int type = EVP_PKEY_RSA; + + const EVP_MD* operator()() noexcept + { + return EVP_sha256(); + } +}; + +/*! + */ +struct RS384 +{ + static const int type = EVP_PKEY_RSA; + + const EVP_MD* operator()() noexcept + { + return EVP_sha384(); + } +}; + +/*! + */ +struct RS512 +{ + static const int type = EVP_PKEY_RSA; + + const EVP_MD* operator()() noexcept + { + return EVP_sha512(); + } +}; + +/*! + */ +struct ES256 +{ + static const int type = EVP_PKEY_EC; + + const EVP_MD* operator()() noexcept + { + return EVP_sha256(); + } +}; + +/*! + */ +struct ES384 +{ + static const int type = EVP_PKEY_EC; + + const EVP_MD* operator()() noexcept + { + return EVP_sha384(); + } +}; + +/*! + */ +struct ES512 +{ + static const int type = EVP_PKEY_EC; + + const EVP_MD* operator()() noexcept + { + return EVP_sha512(); + } +}; + +} //END Namespace algo + + +/*! + */ +template +struct HMACSign +{ + /// The type of Hashing algorithm + using hasher_type = Hasher; + + /*! + */ + static sign_result_t sign(string_view key, string_view data) + { + std::string sign; + sign.resize(EVP_MAX_MD_SIZE); + std::error_code ec{}; + + uint32_t len = 0; + + unsigned char* res = HMAC(Hasher{}(), + key.data(), + key.length(), + reinterpret_cast(data.data()), + data.length(), + reinterpret_cast(&sign[0]), + &len); + + if (!res) { + //TODO: Set the appropriate error code + } + sign.resize(len); + + return {std::move(sign), ec}; + } + + /*! + */ + static verify_result_t + verify(string_view key, string_view head, string_view sign) + { + int compare_res = 0; + std::error_code ec{}; + + return {compare_res, ec}; + } + +}; + +/*! + */ +template <> +struct HMACSign +{ + using hasher_type = algo::NONE; + + /*! + */ + static sign_result_t sign(string_view key, string_view data) + { + std::string sign; + std::error_code ec{}; + + //TODO: Set the appropriate error code for none + return {sign, ec}; + } + + /*! + */ + static verify_result_t + verify(string_view key, string_view head, string_view sign) + { + int compare_res = 0; + std::error_code ec{}; + + //TODO: Set the appropriate error code for none + return {compare_res, ec}; + } + +}; + + +/*! + */ + + +} // END namespace jwt + + +#endif diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 884dbf0..9ec6efc 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -149,6 +149,9 @@ struct write_interface }; /*! + * Provides the functionality for doing + * base64 encoding and decoding from the + * json string. */ template struct base64_enc_dec diff --git a/include/jwt/test/test_hmac b/include/jwt/test/test_hmac new file mode 100755 index 0000000000000000000000000000000000000000..e2518551bce2b2213f1153f6f9f8e45f2343a301 GIT binary patch literal 16764 zcmeHOeQ;dWb$@FCmibyiAh_hi3)o=3jAUCz4uQ(p51#Qyg=HvVFwd*i+FEDz;oX%j zQ$%*Lx3kYL3u0Pz+?XcnWSVN)G*zaEaXQE&Vi&1y8&t{=H)V>-kj_)aOcjPEDy68u z-?{g#c2_br4u52(dZT;KJ?EZ#?z!jQd+xbc(yJf5`_V$f2vitGdVygWS0dG4WEk1> zbtoCeZy;Gnw%xGqe)Cat+i#It`UOhSIWfxVLI<|J-F#$wDT}lZmC_82LpS3>Btm|+ zor-p)R9WGr@)|f8RJd0u9^x0wuoFoaqqdz)bv7jnq;mb<{G5{an&$9vzd@yz2b0OC zCJct8IM(i2Kd!qvC}7(g)@@%`#1>L|8C9AFTt1C+y-}_& z+qT={cJe@LQ+wR*NF~lH&lhKeX4+CN*T)Sk=+Cxyw-+trS@NnvO25ULpSZ6JNtCyY z1^wA}Jp6I`Ee~mVoD6)#i{){=%UPknnSH-`H7t=4u6X-44up1eXq%F$g2 z;d=NLVL?_2N+n>6VwJoDcp8dKu2O!6YlCBcWVK=7$2f_JK8A*upxl7OkGe5Ye{3J& z2bl9aa;ITznxp1&&_YO;BHhuMOxzKVHQmvCAl?j|AGW>7z3%P<J@8O)UALb zu_kGKF6|x>r#>T{je9S%kU7aA z?Kr{i9W*u5PX0->_P(vyj(FcdVd83~VSfqBONwR8+ph}aUK3tT8bU^G$&=XxaQOd zxJ5|=mEHqrV $>`=uvYd65BiRiok3Vr znYU?~%m>oS$)1L2%-0w|@X#F~T5gsIMm!2hqmYCxh~#WJ4T!X{3jCvBnYg=zy${uW zVCx6$2OIz#ure>RJD|sa9s?W(9M%%h7X?`rdP`=9rRQ-gpQjBirxI47*(`UErgFtN zur&>W^+@G6W0e7T8TeeRo?|Hc`L@BRM8H7@vy2iZLdYtJh1$ryNjFsvM$d3!io+fa!LWH7%4G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^aK-`^{1Y>|#Fj2~{QLilH* z)8anMw63X%?!{dJ|E-Jbk{OGA4C|_B`z~1c{`KpN{=+vOt)4|$Ig3&|i?VhWrDoM^ z(wf<%JE`Fu5ua_(4h?OI8jJ65>f%-&cyb4w!ws&8tzh1ns~c`Yv*w5VXLB?xT+2{bdLP46NBKoelV)yUb3cdpH?LW5B8;E@7c~+?K>z>% literal 0 HcmV?d00001 diff --git a/include/jwt/test/test_hmac.cc b/include/jwt/test/test_hmac.cc new file mode 100644 index 0000000..a4f485a --- /dev/null +++ b/include/jwt/test/test_hmac.cc @@ -0,0 +1,16 @@ +#include +#include "jwt/algorithm.hpp" + +void basic_hmac_test() +{ + jwt::string_view sv = "secret" ; + jwt::string_view d = "Some random data string"; + auto res = jwt::HMACSign::sign(sv, d); + + std::cout << res.first << std::endl; +} + +int main() { + basic_hmac_test(); + return 0; +}