44import org .bouncycastle .crypto .CipherParameters ;
55import org .bouncycastle .crypto .macs .CMac ;
66import org .bouncycastle .crypto .params .KeyParameter ;
7- import org .bouncycastle .util .encoders .Hex ;
87
98import com .genexus .cryptography .commons .CmacObject ;
109import com .genexus .cryptography .symmetric .SymmetricBlockCipher ;
1110import com .genexus .cryptography .symmetric .utils .SymmetricBlockAlgorithm ;
1211import com .genexus .securityapicommons .config .EncodingUtil ;
1312import 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}
0 commit comments