-
Notifications
You must be signed in to change notification settings - Fork 85
ases-cbs-hmac-support #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karen-avetisyan-mc
wants to merge
2
commits into
main
Choose a base branch
from
ases-cbs-hmac-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+376
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 54 additions & 5 deletions
59
src/main/java/com/mastercard/developer/encryption/aes/AESCBC.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,84 @@ | ||
| package com.mastercard.developer.encryption.aes; | ||
|
|
||
| import com.mastercard.developer.encryption.EncryptionException; | ||
| import com.mastercard.developer.encryption.jwe.JweObject; | ||
| import com.mastercard.developer.utils.ByteUtils; | ||
| import com.mastercard.developer.utils.EncodingUtils; | ||
|
|
||
| import javax.crypto.Cipher; | ||
| import javax.crypto.Mac; | ||
| import javax.crypto.spec.IvParameterSpec; | ||
| import javax.crypto.spec.SecretKeySpec; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.GeneralSecurityException; | ||
| import java.security.Key; | ||
| import java.security.MessageDigest; | ||
| import java.security.spec.AlgorithmParameterSpec; | ||
|
|
||
| public class AESCBC { | ||
|
|
||
| private AESCBC() { | ||
| } | ||
|
|
||
| private static final String CYPHER = "AES/CBC/PKCS5Padding"; | ||
| private static final String CIPHER = "AES/CBC/PKCS5Padding"; | ||
| private static final String HMAC_ALGORITHM = "HmacSHA256"; | ||
|
|
||
| @java.lang.SuppressWarnings("squid:S3329") | ||
| public static byte[] decrypt(Key secretKey, JweObject object) throws GeneralSecurityException { | ||
| // First 16 bytes are the MAC key, so we only use the second 16 bytes | ||
| SecretKeySpec aesKey = new SecretKeySpec(secretKey.getEncoded(), 16, 16, "AES"); | ||
| public static byte[] decrypt(Key secretKey, JweObject object, boolean enableHmacVerification) throws GeneralSecurityException, EncryptionException { | ||
| byte[] cek = secretKey.getEncoded(); | ||
|
|
||
| // For A128CBC-HS256: First 16 bytes are HMAC key, second 16 bytes are AES key | ||
| int keyLength = cek.length / 2; | ||
| SecretKeySpec aesKey = new SecretKeySpec(cek, keyLength, keyLength, "AES"); | ||
|
|
||
| byte[] cipherText = EncodingUtils.base64Decode(object.getCipherText()); | ||
| byte[] iv = EncodingUtils.base64Decode(object.getIv()); | ||
|
|
||
| // Only verify authentication tag if enabled | ||
| if (enableHmacVerification) { | ||
| SecretKeySpec hmacKey = new SecretKeySpec(cek, 0, keyLength, HMAC_ALGORITHM); | ||
| byte[] authTag = EncodingUtils.base64Decode(object.getAuthTag()); | ||
| byte[] aad = object.getRawHeader().getBytes(StandardCharsets.US_ASCII); | ||
|
|
||
| byte[] expectedTag = computeAuthTag(hmacKey, aad, iv, cipherText, keyLength); | ||
| if (!MessageDigest.isEqual(authTag, expectedTag)) { | ||
| throw new EncryptionException("Authentication tag verification failed"); | ||
| } | ||
| } | ||
|
|
||
| return cipher(aesKey, new IvParameterSpec(iv), cipherText, Cipher.DECRYPT_MODE); | ||
| } | ||
|
|
||
| public static byte[] cipher(Key key, AlgorithmParameterSpec iv, byte[] bytes, int mode) throws GeneralSecurityException { | ||
| Cipher cipher = Cipher.getInstance(CYPHER); | ||
| Cipher cipher = Cipher.getInstance(CIPHER); | ||
| cipher.init(mode, key, iv); | ||
| return cipher.doFinal(bytes); | ||
| } | ||
|
|
||
| /** | ||
| * Computes the authentication tag for AES-CBC-HMAC-SHA2 | ||
| * HMAC is computed over: AAD || IV || Ciphertext || AL | ||
| * where AL is the length of AAD in bits expressed as a 64-bit big-endian integer | ||
| */ | ||
| private static byte[] computeAuthTag(SecretKeySpec hmacKey, byte[] aad, byte[] iv, byte[] cipherText, int tagLength) | ||
| throws GeneralSecurityException { | ||
| Mac mac = Mac.getInstance(HMAC_ALGORITHM); | ||
| mac.init(hmacKey); | ||
|
|
||
| // Compute AL (AAD Length in bits as 64-bit big-endian) | ||
| long aadLengthBits = (long) aad.length * 8; | ||
| byte[] al = ByteBuffer.allocate(8).putLong(aadLengthBits).array(); | ||
|
|
||
| // HMAC input: AAD || IV || Ciphertext || AL | ||
| mac.update(aad); | ||
| mac.update(iv); | ||
| mac.update(cipherText); | ||
| mac.update(al); | ||
|
|
||
| byte[] hmacOutput = mac.doFinal(); | ||
|
|
||
| // Return first half (tagLength bytes) as the authentication tag | ||
| return ByteUtils.subArray(hmacOutput, 0, tagLength); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should confirm that cek.length == 32 before using it. The rest of the calculations could work in unexpected ways of cek.length is not 32.