Top Description Fields Constructors Methods
org.bouncycastle.crypto.macs

public Class Poly1305

extends Object
implements Mac
Class Inheritance
All Implemented Interfaces
org.bouncycastle.crypto.Mac
Imports
org.bouncycastle.crypto.BlockCipher, .CipherParameters, .DataLengthException, .Mac, .OutputLengthException, org.bouncycastle.crypto.generators.Poly1305KeyGenerator, org.bouncycastle.crypto.params.KeyParameter, .ParametersWithIV, org.bouncycastle.util.Pack

Poly1305 message authentication code, designed by D. J. Bernstein.

Poly1305 computes a 128-bit (16 bytes) authenticator, using a 128 bit nonce and a 256 bit key consisting of a 128 bit key applied to an underlying cipher, and a 128 bit key (with 106 effective key bits) used in the authenticator.

The polynomial calculation in this implementation is adapted from the public domain poly1305-donna-unrolled C implementation by Andrew M (@floodyberry).

See Also
Poly1305KeyGenerator

Field Summary

Modifier and TypeField and Description
private static final int
private final BlockCipher
private final byte[]
currentBlock

Current block of buffered input

private int
currentBlockOffset

Current offset in input buffer

private int
h0

Polynomial accumulator

private int
h1

Polynomial accumulator

private int
h2

Polynomial accumulator

private int
h3

Polynomial accumulator

private int
h4

Polynomial accumulator

private int
k0

Encrypted nonce

private int
k1

Encrypted nonce

private int
k2

Encrypted nonce

private int
k3

Encrypted nonce

private int
r0

Polynomial key

private int
r1

Polynomial key

private int
r2

Polynomial key

private int
r3

Polynomial key

private int
r4

Polynomial key

private int
s1

Precomputed 5 * r[1..4]

private int
s2

Precomputed 5 * r[1..4]

private int
s3

Precomputed 5 * r[1..4]

private int
s4

Precomputed 5 * r[1..4]

private final byte[]

Constructor Summary

AccessConstructor and Description
public
Poly1305()

Constructs a Poly1305 MAC, where the key passed to init() will be used directly.

public
Poly1305(final BlockCipher cipher)

Constructs a Poly1305 MAC, using a 128 bit block cipher.

Method Summary

Modifier and TypeMethod and Description
public int
doFinal(final byte[]
the array the MAC is to be output to.
out
,
final int
the offset into the out buffer the output is to start at.
outOff
)

Implements org.bouncycastle.crypto.Mac.doFinal.

Compute the final stage of the MAC writing the output to the out parameter.
public String
getAlgorithmName()

Implements org.bouncycastle.crypto.Mac.getAlgorithmName.

Return the name of the algorithm the MAC implements.
public int
getMacSize()

Implements org.bouncycastle.crypto.Mac.getMacSize.

Return the block size for this MAC (in bytes).
public void
init(CipherParameters
if used with a block cipher, then a ParametersWithIV containing a 128 bit nonce and a KeyParameter with a 256 bit key complying to the Poly1305 key format, otherwise just the KeyParameter.
params
)

Implements org.bouncycastle.crypto.Mac.init.

Initialises the Poly1305 MAC.
private static final long
mul32x32_64(int i1, int i2)

private void
public void
reset()

Implements org.bouncycastle.crypto.Mac.reset.

Reset the MAC.
private void
setKey(final byte[] key, final byte[] nonce)

public void
update(final byte
the byte to be processed.
in
)

Implements org.bouncycastle.crypto.Mac.update.

add a single byte to the mac for processing.
public void
update(final byte[]
the array containing the input.
in
,
final int
the index in the array the data begins at.
inOff
,
final int
the length of the input starting at inOff.
len
)

Implements org.bouncycastle.crypto.Mac.update.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BLOCK_SIZEback to summary
private static final int BLOCK_SIZE
cipherback to summary
private final BlockCipher cipher
currentBlockback to summary
private final byte[] currentBlock

Current block of buffered input

currentBlockOffsetback to summary
private int currentBlockOffset

Current offset in input buffer

h0back to summary
private int h0

Polynomial accumulator

h1back to summary
private int h1

Polynomial accumulator

h2back to summary
private int h2

Polynomial accumulator

h3back to summary
private int h3

Polynomial accumulator

h4back to summary
private int h4

Polynomial accumulator

k0back to summary
private int k0

Encrypted nonce

k1back to summary
private int k1

Encrypted nonce

k2back to summary
private int k2

Encrypted nonce

k3back to summary
private int k3

Encrypted nonce

r0back to summary
private int r0

Polynomial key

r1back to summary
private int r1

Polynomial key

r2back to summary
private int r2

Polynomial key

r3back to summary
private int r3

Polynomial key

r4back to summary
private int r4

Polynomial key

s1back to summary
private int s1

Precomputed 5 * r[1..4]

s2back to summary
private int s2

Precomputed 5 * r[1..4]

s3back to summary
private int s3

Precomputed 5 * r[1..4]

s4back to summary
private int s4

Precomputed 5 * r[1..4]

singleByteback to summary
private final byte[] singleByte

Constructor Detail

Poly1305back to summary
public Poly1305()

Constructs a Poly1305 MAC, where the key passed to init() will be used directly.

Poly1305back to summary
public Poly1305(final BlockCipher cipher)

Constructs a Poly1305 MAC, using a 128 bit block cipher.

Method Detail

doFinalback to summary
public int doFinal(final byte[] out, final int outOff) throws DataLengthException, IllegalStateException

Implements org.bouncycastle.crypto.Mac.doFinal.

Doc from org.bouncycastle.crypto.Mac.doFinal.

Compute the final stage of the MAC writing the output to the out parameter.

doFinal leaves the MAC in the same state it was after the last init.

Parameters
out:byte[]

the array the MAC is to be output to.

outOff:int

the offset into the out buffer the output is to start at.

Exceptions
DataLengthException:
if there isn't enough space in out.
IllegalStateException:
if the MAC is not initialised.
getAlgorithmNameback to summary
public String getAlgorithmName()

Implements org.bouncycastle.crypto.Mac.getAlgorithmName.

Doc from org.bouncycastle.crypto.Mac.getAlgorithmName.

Return the name of the algorithm the MAC implements.

Returns:String

the name of the algorithm the MAC implements.

getMacSizeback to summary
public int getMacSize()

Implements org.bouncycastle.crypto.Mac.getMacSize.

Doc from org.bouncycastle.crypto.Mac.getMacSize.

Return the block size for this MAC (in bytes).

Returns:int

the block size for this MAC in bytes.

initback to summary
public void init(CipherParameters params) throws IllegalArgumentException

Implements org.bouncycastle.crypto.Mac.init.

Initialises the Poly1305 MAC.

Parameters
params:CipherParameters

if used with a block cipher, then a ParametersWithIV containing a 128 bit nonce and a KeyParameter with a 256 bit key complying to the Poly1305 key format, otherwise just the KeyParameter.

Exceptions
IllegalArgumentException:

Doc from org.bouncycastle.crypto.Mac.init.

if the params argument is inappropriate.

mul32x32_64back to summary
private static final long mul32x32_64(int i1, int i2)
processBlockback to summary
private void processBlock()
resetback to summary
public void reset()

Implements org.bouncycastle.crypto.Mac.reset.

Doc from org.bouncycastle.crypto.Mac.reset.

Reset the MAC. At the end of resetting the MAC should be in the in the same state it was after the last init (if there was one).

setKeyback to summary
private void setKey(final byte[] key, final byte[] nonce)
updateback to summary
public void update(final byte in) throws IllegalStateException

Implements org.bouncycastle.crypto.Mac.update.

Doc from org.bouncycastle.crypto.Mac.update.

add a single byte to the mac for processing.

Parameters
in:byte

the byte to be processed.

Exceptions
IllegalStateException:
if the MAC is not initialised.
updateback to summary
public void update(final byte[] in, final int inOff, final int len) throws DataLengthException, IllegalStateException

Implements org.bouncycastle.crypto.Mac.update.

Parameters
in:byte[]

Doc from org.bouncycastle.crypto.Mac.update.

the array containing the input.

inOff:int

Doc from org.bouncycastle.crypto.Mac.update.

the index in the array the data begins at.

len:int

Doc from org.bouncycastle.crypto.Mac.update.

the length of the input starting at inOff.

Exceptions
DataLengthException:

Doc from org.bouncycastle.crypto.Mac.update.

if there isn't enough data in in.

IllegalStateException:

Doc from org.bouncycastle.crypto.Mac.update.

if the MAC is not initialised.