Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,60 @@
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.macs.CMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;

import com.genexus.cryptography.commons.CmacObject;
import com.genexus.cryptography.symmetric.SymmetricBlockCipher;
import com.genexus.cryptography.symmetric.utils.SymmetricBlockAlgorithm;
import com.genexus.securityapicommons.config.EncodingUtil;
import com.genexus.securityapicommons.utils.SecurityUtils;

public class Cmac extends CmacObject{
public class Cmac extends CmacObject {

public Cmac()
{
public Cmac() {
super();
}


/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
/******** EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/

@Override
public String calculate(String plainText, String key, String algorithm, int macSize) {
if(!isValidAlgorithm(algorithm))
{
if (!isValidAlgorithm(algorithm)) {
this.error.setError("CM001", "Invalid Symmetric block algorithm for CMAC");
return "";
}
SymmetricBlockAlgorithm symmetricBlockAlgorithm = SymmetricBlockAlgorithm.getSymmetricBlockAlgorithm(algorithm,
this.error);
SymmetricBlockCipher symCipher = new SymmetricBlockCipher();
BlockCipher blockCipher = symCipher.getCipherEngine(symmetricBlockAlgorithm);
if(symCipher.hasError()) {
if (symCipher.hasError()) {
this.error = symCipher.getError();
return "";
}
if(macSize>blockCipher.getBlockSize()*8)
{
if (macSize > blockCipher.getBlockSize() * 8) {
this.error.setError("CM002", "The mac length must be less or equal than the algorithm block size.");
return "";
}
byte[] byteKey = Hex.decode(key);
byte[] byteKey = SecurityUtils.getHexa(key, "CM003", this.error);
if (this.hasError()) {
return "";
}
EncodingUtil eu = new EncodingUtil();
byte[] byteInput = eu.getBytes(plainText);

CipherParameters params = new KeyParameter(byteKey);

org.bouncycastle.crypto.macs.CMac mac = null;
if(macSize!=0)
{
mac= new CMac(blockCipher,macSize );
}else {
mac= new CMac(blockCipher);
if (macSize != 0) {
mac = new CMac(blockCipher, macSize);
} else {
mac = new CMac(blockCipher);
}
try {
mac.init(params);
} catch (Exception e) {
this.error.setError("CM004", e.getMessage());
return "";
}
mac.init(params);
byte[] resBytes = new byte[mac.getMacSize()];
mac.update(byteInput, 0, byteInput.length);
mac.doFinal(resBytes, 0);
Expand All @@ -64,7 +66,7 @@ public String calculate(String plainText, String key, String algorithm, int macS
return result;
}
return "";

}

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


/********EXTERNAL OBJECT PUBLIC METHODS - END ********/

/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/

/**
* @param digest
* byte array
* @param digest byte array
* @return String Hexa respresentation of the byte array digest
*/
private String toHexaString(byte[] digest) {
Expand All @@ -99,21 +99,20 @@ private String toHexaString(byte[] digest) {
return result.trim();

}

private boolean isValidAlgorithm(String algorithm) {
SymmetricBlockAlgorithm symmetricBlockAlgorithm = SymmetricBlockAlgorithm.getSymmetricBlockAlgorithm(algorithm,
this.error);
int blockSize = SymmetricBlockAlgorithm.getBlockSize(symmetricBlockAlgorithm, this.error);
if(this.hasError()) {
if (this.hasError()) {

return false;
}
if(blockSize != 64 && blockSize != 128)
{

if (blockSize != 64 && blockSize != 128) {

return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;

import com.genexus.cryptography.commons.HmacObject;
import com.genexus.cryptography.hash.Hashing;
Expand All @@ -17,10 +16,13 @@ public Hmac() {
super();
}

/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
/******** EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
@Override
public String calculate(String plainText, String password, String algorithm) {
byte[] pass = Hex.decode(password);
byte[] pass = SecurityUtils.getHexa(password, "HS002", this.error);
if (this.hasError()) {
return "";
}
EncodingUtil eu = new EncodingUtil();
byte[] inputBytes = eu.getBytes(plainText);
if (this.hasError()) {
Expand All @@ -33,7 +35,12 @@ public String calculate(String plainText, String password, String algorithm) {
}
Digest digest = hash.createHash(alg);
HMac engine = new HMac(digest);
engine.init(new KeyParameter(pass));
try {
engine.init(new KeyParameter(pass));
} catch (Exception e) {
this.error.setError("HS003", e.getMessage());
return "";
}
byte[] resBytes = new byte[engine.getMacSize()];
engine.update(inputBytes, 0, inputBytes.length);
engine.doFinal(resBytes, 0);
Expand All @@ -51,12 +58,11 @@ public boolean verify(String plainText, String password, String mac, String algo
String res = calculate(plainText, password, algorithm);
return SecurityUtils.compareStrings(res, mac);
}
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/

/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/

/**
* @param digest
* byte array
* @param digest byte array
* @return String Hexa respresentation of the byte array digest
*/
private String toHexaString(byte[] digest) {
Expand Down
Loading