Changed base64 to hex encoding.

This commit is contained in:
Ravin Perera
2019-10-16 20:14:35 +05:30
parent 8a22748c8d
commit 0fb9ebf79f
11 changed files with 147 additions and 157 deletions

View File

@@ -61,7 +61,7 @@ static const char *CHALLENGE_MSGTYPE = "public_challenge";
// Message type for the user challenge response.
static const char *CHALLENGE_RESP_MSGTYPE = "challenge_response";
// Length of user random challenge bytes.
static const int CHALLENGE_LEN = 16;
static const size_t CHALLENGE_LEN = 16;
/**
* Initializes the usr subsystem. Must be called once during application startup.
@@ -93,20 +93,20 @@ void deinit()
* {
* "version": "<HP version>",
* "type": "public_challenge",
* "challenge": "<base64 challenge string>"
* "challenge": "<hex challenge string>"
* }
* @param challenge String reference to copy the generated base64 challenge string into.
* @param challenge String reference to copy the generated hex challenge string into.
*/
void create_user_challenge(std::string &msg, std::string &challengeb64)
void create_user_challenge(std::string &msg, std::string &challengehex)
{
//Use libsodium to generate the random challenge bytes.
unsigned char challenge_bytes[CHALLENGE_LEN];
randombytes_buf(challenge_bytes, CHALLENGE_LEN);
//We pass the b64 challenge string separately to the caller even though
//We pass the hex challenge string separately to the caller even though
//we also include it in the challenge msg as well.
util::base64_encode(challengeb64, challenge_bytes, CHALLENGE_LEN);
util::bin2hex(challengehex, challenge_bytes, CHALLENGE_LEN);
//Construct the challenge msg json.
// We do not use RapidJson here in favour of performance because this is a simple json message.
@@ -118,7 +118,7 @@ void create_user_challenge(std::string &msg, std::string &challengeb64)
msg.append("{\"version\":\"")
.append(util::HP_VERSION)
.append("\",\"type\":\"public_challenge\",\"challenge\":\"")
.append(challengeb64)
.append(challengehex)
.append("\"}");
}
@@ -126,19 +126,19 @@ void create_user_challenge(std::string &msg, std::string &challengeb64)
* Verifies the user challenge response with the original challenge issued to the user
* and the user public key contained in the response.
*
* @param extracted_pubkeyb64 The base64 public key extracted from the response.
* @param extracted_pubkeyhex The hex public key extracted from the response.
* @param response The response bytes to verify. This will be parsed as json.
* Accepted response format:
* {
* "type": "challenge_response",
* "challenge": "<original base64 challenge the user received>",
* "sig": "<Base64 signature of the challenge>",
* "pubkey": "<Base64 public key of the user>"
* "challenge": "<original hex challenge the user received>",
* "sig": "<hex signature of the challenge>",
* "pubkey": "<hex public key of the user>"
* }
* @param original_challenge The original base64 challenge string issued to the user.
* @param original_challenge The original hex challenge string issued to the user.
* @return 0 if challenge response is verified. -1 if challenge not met or an error occurs.
*/
int verify_user_challenge_response(std::string &extracted_pubkeyb64, std::string_view response, std::string_view original_challenge)
int verify_user_challenge_response(std::string &extracted_pubkeyhex, std::string_view response, std::string_view original_challenge)
{
// We load response raw bytes into json document.
rapidjson::Document d;
@@ -179,7 +179,7 @@ int verify_user_challenge_response(std::string &extracted_pubkeyb64, std::string
// Verify the challenge signature. We do this last due to signature verification cost.
std::string_view pubkeysv = util::getsv(d[CHALLENGE_RESP_PUBKEY]);
if (crypto::verify_b64(
if (crypto::verify_hex(
original_challenge,
util::getsv(d[CHALLENGE_RESP_SIG]),
pubkeysv) != 0)
@@ -188,7 +188,7 @@ int verify_user_challenge_response(std::string &extracted_pubkeyb64, std::string
return -1;
}
extracted_pubkeyb64 = pubkeysv;
extracted_pubkeyhex = pubkeysv;
return 0;
}