Skip to content

Commit

Permalink
Fix #524: Include version in the token digest
Browse files Browse the repository at this point in the history
  • Loading branch information
petrdvorak committed Sep 6, 2023
1 parent 89192d7 commit a8a452b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,22 @@ public byte[] generateTokenTimestamp() {
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException {
return tokenUtils.computeTokenDigest(nonce, timestamp, tokenSecret);
return this.computeTokenDigest(nonce, timestamp, null, tokenSecret);
}

/**
* Compute the digest of provided token information using given token secret.
*
* @param nonce Token nonce, 16 random bytes.
* @param timestamp Token timestamp, Unix timestamp format encoded as bytes (from string representation).
* @param version Protocol version.
* @param tokenSecret Token secret, 16 random bytes.
* @return Token digest computed using provided data bytes with given token secret.
* @throws GenericCryptoException In case digest computation fails.
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, byte[] version, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException {
return tokenUtils.computeTokenDigest(nonce, timestamp, version, tokenSecret);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,33 @@ public byte[] convertTokenTimestamp(long timestamp) {
* Compute the digest of provided token information using given token secret.
* @param nonce Token nonce, 16 random bytes.
* @param timestamp Token timestamp, Unix timestamp format encoded as bytes (string representation).
* @param version Protocol version.
* @param tokenSecret Token secret, 16 random bytes.
* @return Token digest computed using provided data bytes with given token secret.
* @throws GenericCryptoException In case digest computation fails.
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException {
byte[] amp = "&".getBytes(StandardCharsets.UTF_8);
byte[] data = ByteUtils.concat(nonce, amp, timestamp);
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, byte[] version, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException {
final byte[] amp = "&".getBytes(StandardCharsets.UTF_8);
final byte[] data = (version != null)
? ByteUtils.concat(nonce, amp, timestamp, amp, version)
: ByteUtils.concat(nonce, amp, timestamp);
return hmac.hash(tokenSecret, data);
}

/**
* Validate provided token digest for given input data and provided token secret.
* @param nonce Token nonce, 16 random bytes.
* @param timestamp Token timestamp, Unix timestamp format encoded as bytes (string representation).
* @param version Protocol version.
* @param tokenSecret Token secret, 16 random bytes.
* @param tokenDigest Token digest, 32 bytes to be validated.
* @return Token digest computed using provided data bytes with given token secret.
* @throws GenericCryptoException In case digest computation fails.
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException {
return SideChannelUtils.constantTimeAreEqual(computeTokenDigest(nonce, timestamp, tokenSecret), tokenDigest);
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, byte[] version, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException {
return SideChannelUtils.constantTimeAreEqual(computeTokenDigest(nonce, timestamp, version, tokenSecret), tokenDigest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,22 @@ public byte[] convertTokenTimestamp(long timestamp) {
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException {
return tokenUtils.validateTokenDigest(nonce, timestamp, tokenSecret, tokenDigest);
return this.validateTokenDigest(nonce, timestamp, null, tokenSecret, tokenDigest);
}

/**
* Validate provided token digest for given input data and provided token secret.
* @param nonce Token nonce, 16 random bytes.
* @param timestamp Token timestamp, Unix timestamp format encoded as bytes (from string representation).
* @param version Protocol version.
* @param tokenSecret Token secret, 16 random bytes.
* @param tokenDigest Token digest, 32 bytes to be validated.
* @return Token digest computed using provided data bytes with given token secret.
* @throws GenericCryptoException In case digest computation fails.
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, byte[] version, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException {
return tokenUtils.validateTokenDigest(nonce, timestamp, version, tokenSecret, tokenDigest);
}

}

0 comments on commit a8a452b

Please sign in to comment.