Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit edf9136

Browse files
authored
Fix exception management (#36)
1 parent 6cd01ea commit edf9136

File tree

8 files changed

+260
-207
lines changed

8 files changed

+260
-207
lines changed

GeneXusCryptography/src/main/java/com/genexus/cryptography/mac/Cmac.java

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,60 @@
44
import org.bouncycastle.crypto.CipherParameters;
55
import org.bouncycastle.crypto.macs.CMac;
66
import org.bouncycastle.crypto.params.KeyParameter;
7-
import org.bouncycastle.util.encoders.Hex;
87

98
import com.genexus.cryptography.commons.CmacObject;
109
import com.genexus.cryptography.symmetric.SymmetricBlockCipher;
1110
import com.genexus.cryptography.symmetric.utils.SymmetricBlockAlgorithm;
1211
import com.genexus.securityapicommons.config.EncodingUtil;
1312
import com.genexus.securityapicommons.utils.SecurityUtils;
1413

15-
public class Cmac extends CmacObject{
14+
public class Cmac extends CmacObject {
1615

17-
public Cmac()
18-
{
16+
public Cmac() {
1917
super();
2018
}
2119

22-
23-
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
20+
/******** EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
2421

2522
@Override
2623
public String calculate(String plainText, String key, String algorithm, int macSize) {
27-
if(!isValidAlgorithm(algorithm))
28-
{
24+
if (!isValidAlgorithm(algorithm)) {
2925
this.error.setError("CM001", "Invalid Symmetric block algorithm for CMAC");
3026
return "";
3127
}
3228
SymmetricBlockAlgorithm symmetricBlockAlgorithm = SymmetricBlockAlgorithm.getSymmetricBlockAlgorithm(algorithm,
3329
this.error);
3430
SymmetricBlockCipher symCipher = new SymmetricBlockCipher();
3531
BlockCipher blockCipher = symCipher.getCipherEngine(symmetricBlockAlgorithm);
36-
if(symCipher.hasError()) {
32+
if (symCipher.hasError()) {
3733
this.error = symCipher.getError();
3834
return "";
3935
}
40-
if(macSize>blockCipher.getBlockSize()*8)
41-
{
36+
if (macSize > blockCipher.getBlockSize() * 8) {
4237
this.error.setError("CM002", "The mac length must be less or equal than the algorithm block size.");
4338
return "";
4439
}
45-
byte[] byteKey = Hex.decode(key);
40+
byte[] byteKey = SecurityUtils.getHexa(key, "CM003", this.error);
41+
if (this.hasError()) {
42+
return "";
43+
}
4644
EncodingUtil eu = new EncodingUtil();
4745
byte[] byteInput = eu.getBytes(plainText);
48-
46+
4947
CipherParameters params = new KeyParameter(byteKey);
5048

5149
org.bouncycastle.crypto.macs.CMac mac = null;
52-
if(macSize!=0)
53-
{
54-
mac= new CMac(blockCipher,macSize );
55-
}else {
56-
mac= new CMac(blockCipher);
50+
if (macSize != 0) {
51+
mac = new CMac(blockCipher, macSize);
52+
} else {
53+
mac = new CMac(blockCipher);
54+
}
55+
try {
56+
mac.init(params);
57+
} catch (Exception e) {
58+
this.error.setError("CM004", e.getMessage());
59+
return "";
5760
}
58-
mac.init(params);
5961
byte[] resBytes = new byte[mac.getMacSize()];
6062
mac.update(byteInput, 0, byteInput.length);
6163
mac.doFinal(resBytes, 0);
@@ -64,7 +66,7 @@ public String calculate(String plainText, String key, String algorithm, int macS
6466
return result;
6567
}
6668
return "";
67-
69+
6870
}
6971

7072
@Override
@@ -73,12 +75,10 @@ public boolean verify(String plainText, String key, String mac, String algorithm
7375
return SecurityUtils.compareStrings(res, mac);
7476
}
7577

76-
77-
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/
78-
78+
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
79+
7980
/**
80-
* @param digest
81-
* byte array
81+
* @param digest byte array
8282
* @return String Hexa respresentation of the byte array digest
8383
*/
8484
private String toHexaString(byte[] digest) {
@@ -99,21 +99,20 @@ private String toHexaString(byte[] digest) {
9999
return result.trim();
100100

101101
}
102-
102+
103103
private boolean isValidAlgorithm(String algorithm) {
104104
SymmetricBlockAlgorithm symmetricBlockAlgorithm = SymmetricBlockAlgorithm.getSymmetricBlockAlgorithm(algorithm,
105105
this.error);
106106
int blockSize = SymmetricBlockAlgorithm.getBlockSize(symmetricBlockAlgorithm, this.error);
107-
if(this.hasError()) {
108-
107+
if (this.hasError()) {
108+
109109
return false;
110110
}
111-
if(blockSize != 64 && blockSize != 128)
112-
{
113-
111+
if (blockSize != 64 && blockSize != 128) {
112+
114113
return false;
115114
}
116-
115+
117116
return true;
118117
}
119118
}

GeneXusCryptography/src/main/java/com/genexus/cryptography/mac/Hmac.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.bouncycastle.crypto.Digest;
44
import org.bouncycastle.crypto.macs.HMac;
55
import org.bouncycastle.crypto.params.KeyParameter;
6-
import org.bouncycastle.util.encoders.Hex;
76

87
import com.genexus.cryptography.commons.HmacObject;
98
import com.genexus.cryptography.hash.Hashing;
@@ -17,10 +16,13 @@ public Hmac() {
1716
super();
1817
}
1918

20-
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
19+
/******** EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
2120
@Override
2221
public String calculate(String plainText, String password, String algorithm) {
23-
byte[] pass = Hex.decode(password);
22+
byte[] pass = SecurityUtils.getHexa(password, "HS002", this.error);
23+
if (this.hasError()) {
24+
return "";
25+
}
2426
EncodingUtil eu = new EncodingUtil();
2527
byte[] inputBytes = eu.getBytes(plainText);
2628
if (this.hasError()) {
@@ -33,7 +35,12 @@ public String calculate(String plainText, String password, String algorithm) {
3335
}
3436
Digest digest = hash.createHash(alg);
3537
HMac engine = new HMac(digest);
36-
engine.init(new KeyParameter(pass));
38+
try {
39+
engine.init(new KeyParameter(pass));
40+
} catch (Exception e) {
41+
this.error.setError("HS003", e.getMessage());
42+
return "";
43+
}
3744
byte[] resBytes = new byte[engine.getMacSize()];
3845
engine.update(inputBytes, 0, inputBytes.length);
3946
engine.doFinal(resBytes, 0);
@@ -51,12 +58,11 @@ public boolean verify(String plainText, String password, String mac, String algo
5158
String res = calculate(plainText, password, algorithm);
5259
return SecurityUtils.compareStrings(res, mac);
5360
}
54-
55-
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/
61+
62+
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
5663

5764
/**
58-
* @param digest
59-
* byte array
65+
* @param digest byte array
6066
* @return String Hexa respresentation of the byte array digest
6167
*/
6268
private String toHexaString(byte[] digest) {

0 commit comments

Comments
 (0)