Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
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
7 changes: 6 additions & 1 deletion 7 core/src/main/java/org/bouncycastle/crypto/macs/CMac.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.ISO7816d4Padding;
import org.bouncycastle.crypto.params.KeyParameter;

/**
* CMAC - as specified at www.nuee.nagoya-u.ac.jp/labs/tiwata/omac/omac.html
Expand Down Expand Up @@ -121,7 +122,7 @@ private static byte[] doubleLu(byte[] in)

public void init(CipherParameters params)
{
if (params != null)
if (params instanceof KeyParameter)
{
cipher.init(true, params);

Expand All @@ -130,6 +131,10 @@ public void init(CipherParameters params)
cipher.processBlock(ZEROES, 0, L, 0);
Lu = doubleLu(L);
Lu2 = doubleLu(Lu);
} else if (params != null)
{
// CMAC mode does not permit IV to underlying CBC mode
throw new IllegalArgumentException("CMac mode only permits key to be set.");
}

reset();
Expand Down
17 changes: 17 additions & 0 deletions 17 core/src/test/java/org/bouncycastle/crypto/test/CMacTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.macs.CMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;

Expand Down Expand Up @@ -248,6 +250,21 @@ public void performTest()
fail("Failed - expected " + new String(Hex.encode(output_k256_m64))
+ " got " + new String(Hex.encode(out)));
}

testExceptions();
}

private void testExceptions()
{
try
{
CMac mac = new CMac(new AESEngine());
mac.init(new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16]));
fail("CMac does not accept IV");
} catch(IllegalArgumentException e)
{
// Expected
}
}

public String getName()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.