MacWrapper { public static WrappedMac getMac(ProtocolVersion version, CipherSuite cipherSuite, byte[] key) throws NoSuchAlgorithmException { MacAlgorithm macAlg = AlgorithmResolver.getMacAlgorithm(version, cipherSuite); if (macAlg == MacAlgorithm.HMAC_GOSTR3411) { GOST3411Digest digest = new GOST3411Digest(); return new ContinuousMac(new HMac(digest), digest, new KeyParameter(key)); } else if (macAlg == MacAlgorithm.HMAC_GOSTR3411_2012_256) { GOST3411_2012_256Digest digest = new GOST3411_2012_256Digest(); return new ContinuousMac(new HMac(digest), digest, new KeyParameter(key)); } else if (macAlg == MacAlgorithm.IMIT_GOST28147) { ParametersWithSBox parameters = new ParametersWithSBox(new KeyParameter(key), GOSTUtils.getGostSBox(cipherSuite)); return new ContinuousMac(new GOST28147Mac(), parameters); } else if (macAlg.getJavaName() != null) { return new JavaMac(macAlg.getJavaName(), key); } else { throw new NoSuchAlgorithmException("Mac: " + macAlg + " is not supported!"); } }  static WrappedMac getMac(ProtocolVersion version, CipherSuite cipherSuite, byte[] key);  }
@Test public void testSha1() throws NoSuchAlgorithmException { WrappedMac mac = MacWrapper.getMac(ProtocolVersion.TLS10, CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA, new byte[20]); assertEquals(MacAlgorithm.HMAC_SHA1.getSize(), mac.getMacLength()); byte[] actual = mac.calculateMac("Test data".getBytes()); byte[] expected = ArrayConverter.hexStringToByteArray("2C667D86C1F63F00E86310B3A32F2D44DF34A316"); assertArrayEquals(expected, actual); }
@Test public void testGOST3411() throws NoSuchAlgorithmException { WrappedMac mac = MacWrapper.getMac(ProtocolVersion.TLS10, CipherSuite.TLS_GOSTR341094_WITH_NULL_GOSTR3411, new byte[32]); assertEquals(MacAlgorithm.HMAC_GOSTR3411.getSize(), mac.getMacLength()); byte[] actual = mac.calculateMac("Test data".getBytes()); byte[] expected = ArrayConverter .hexStringToByteArray("9E1A18525244994DF9D9006D057B4CA01093458D40DA788A91E6324278E575DF"); assertArrayEquals(expected, actual); actual = mac.calculateMac(" extended".getBytes()); expected = ArrayConverter .hexStringToByteArray("C0ECFFEA5D7021D84C74227D7A7E80C2C6BD5F3C5F298E904AF9A4BA5C5E5AEF"); assertArrayEquals(expected, actual); }
@Test public void testGOST28147IMIT() throws NoSuchAlgorithmException { WrappedMac mac = MacWrapper.getMac(ProtocolVersion.TLS12, CipherSuite.TLS_GOSTR341001_WITH_28147_CNT_IMIT, new byte[32]); assertEquals(MacAlgorithm.IMIT_GOST28147.getSize(), mac.getMacLength()); byte[] actual = mac.calculateMac("Test data".getBytes()); byte[] expected = ArrayConverter.hexStringToByteArray("2664CBA8"); assertArrayEquals(expected, actual); actual = mac.calculateMac(" extended".getBytes()); expected = ArrayConverter.hexStringToByteArray("9E80F481"); assertArrayEquals(expected, actual); }