Top Description Fields Constructors Methods
org.bouncycastle.pqc.legacy.crypto.ntru

public Class NTRUEngine

extends Object
implements AsymmetricBlockCipher
Class Inheritance
All Implemented Interfaces
org.bouncycastle.crypto.AsymmetricBlockCipher
Imports
java.security.SecureRandom, org.bouncycastle.crypto.AsymmetricBlockCipher, .CipherParameters, .CryptoServicesRegistrar, .DataLengthException, .Digest, .InvalidCipherTextException, org.bouncycastle.crypto.params.ParametersWithRandom, org.bouncycastle.pqc.legacy.math.ntru.polynomial.DenseTernaryPolynomial, .IntegerPolynomial, .Polynomial, .ProductFormPolynomial, .SparseTernaryPolynomial, .TernaryPolynomial, org.bouncycastle.util.Arrays

Encrypts, decrypts data and generates key pairs.
The parameter p is hardcoded to 3.

Field Summary

Modifier and TypeField and Description
private boolean
private NTRUEncryptionParameters
private NTRUEncryptionPrivateKeyParameters
private NTRUEncryptionPublicKeyParameters
private SecureRandom

Constructor Summary

AccessConstructor and Description
public
NTRUEngine()

Constructs a new instance with a set of encryption parameters.

Method Summary

Modifier and TypeMethod and Description
private byte[]
buildSData(byte[] oid, byte[] m, int l, byte[] b, byte[] hTrunc)

private byte[]
calcHash(Digest hashAlg)

private byte[]
calcHash(Digest hashAlg, byte[] input)

private byte[]
copyOf(byte[] src, int len)

private byte[]

Returns:

the decrypted message
decrypt
(byte[]
The message to decrypt
data
,
NTRUEncryptionPrivateKeyParameters
the corresponding private key
privKey
)

Decrypts a message.

protected IntegerPolynomial

Returns:

an IntegerPolynomial representing the output.
decrypt
(IntegerPolynomial e, Polynomial
a polynomial such that if fastFp=true, f=1+3*priv_t; otherwise, f=priv_t
priv_t
,
IntegerPolynomial priv_fp)

private byte[]

Returns:

the encrypted message
encrypt
(byte[]
The message to encrypt
m
,
NTRUEncryptionPublicKeyParameters
the public key to encrypt the message with
pubKey
)

Encrypts a message.

protected IntegerPolynomial
private int[]

Returns:

an array containing numbers between -1 and 1
generateBlindingCoeffs
(IndexGenerator
an index generator
ig
,
int
number of ones / negative ones
dr
)

Generates an int array containing dr elements equal to 1 and dr elements equal to -1 using an index generator.

private Polynomial

Returns:

a blinding polynomial
generateBlindingPoly
(byte[] seed, byte[]
message representative
M
)

Deterministically generates a blinding polynomial from a seed and a message representative.

public int
getInputBlockSize()

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.getInputBlockSize.

returns the largest size an input block can be.
public int
getOutputBlockSize()

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.getOutputBlockSize.

returns the maximum size of the block produced by this cipher.
public void
init(boolean
if true the cipher is initialised for encryption, if false for decryption.
forEncryption
,
CipherParameters
the key and other data required by the cipher.
parameters
)

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.init.

initialise the cipher.
private int
log2(int value)

private IntegerPolynomial
MGF(byte[] seed, int N, int minCallsR, boolean
whether to hash the seed
hashSeed
)

An implementation of MGF-TP-1 from P1363.1 section 8.4.1.1.

public byte[]
processBlock(byte[]
the input data
in
,
int
offset into the in array where the data starts
inOff
,
int
the length of the block to be processed.
len
)

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.processBlock.

process the block of len bytes stored in in from offset inOff.
private void
putInt(Digest hashAlg, int counter)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

forEncryptionback to summary
private boolean forEncryption
paramsback to summary
private NTRUEncryptionParameters params
privKeyback to summary
private NTRUEncryptionPrivateKeyParameters privKey
pubKeyback to summary
private NTRUEncryptionPublicKeyParameters pubKey
randomback to summary
private SecureRandom random

Constructor Detail

NTRUEngineback to summary
public NTRUEngine()

Constructs a new instance with a set of encryption parameters.

Method Detail

buildSDataback to summary
private byte[] buildSData(byte[] oid, byte[] m, int l, byte[] b, byte[] hTrunc)
calcHashback to summary
private byte[] calcHash(Digest hashAlg)
calcHashback to summary
private byte[] calcHash(Digest hashAlg, byte[] input)
copyOfback to summary
private byte[] copyOf(byte[] src, int len)
decryptback to summary
private byte[] decrypt(byte[] data, NTRUEncryptionPrivateKeyParameters privKey) throws InvalidCipherTextException

Decrypts a message.
See P1363.1 section 9.2.3.

Parameters
data:byte[]

The message to decrypt

privKey:NTRUEncryptionPrivateKeyParameters

the corresponding private key

Returns:byte[]

the decrypted message

Exceptions
InvalidCipherTextException:
if the encrypted data is invalid, or maxLenBytes is greater than 255
decryptback to summary
protected IntegerPolynomial decrypt(IntegerPolynomial e, Polynomial priv_t, IntegerPolynomial priv_fp)
Parameters
priv_t:Polynomial

a polynomial such that if fastFp=true, f=1+3*priv_t; otherwise, f=priv_t

Returns:IntegerPolynomial

an IntegerPolynomial representing the output.

encryptback to summary
private byte[] encrypt(byte[] m, NTRUEncryptionPublicKeyParameters pubKey)

Encrypts a message.
See P1363.1 section 9.2.2.

Parameters
m:byte[]

The message to encrypt

pubKey:NTRUEncryptionPublicKeyParameters

the public key to encrypt the message with

Returns:byte[]

the encrypted message

encryptback to summary
protected IntegerPolynomial encrypt(IntegerPolynomial m, TernaryPolynomial r, IntegerPolynomial pubKey)
generateBlindingCoeffsback to summary
private int[] generateBlindingCoeffs(IndexGenerator ig, int dr)

Generates an int array containing dr elements equal to 1 and dr elements equal to -1 using an index generator.

Parameters
ig:IndexGenerator

an index generator

dr:int

number of ones / negative ones

Returns:int[]

an array containing numbers between -1 and 1

generateBlindingPolyback to summary
private Polynomial generateBlindingPoly(byte[] seed, byte[] M)

Deterministically generates a blinding polynomial from a seed and a message representative.

Parameters
M:byte[]

message representative

Returns:Polynomial

a blinding polynomial

getInputBlockSizeback to summary
public int getInputBlockSize()

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.getInputBlockSize.

Doc from org.bouncycastle.crypto.AsymmetricBlockCipher.getInputBlockSize.

returns the largest size an input block can be.

Returns:int

maximum size for an input block.

getOutputBlockSizeback to summary
public int getOutputBlockSize()

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.getOutputBlockSize.

Doc from org.bouncycastle.crypto.AsymmetricBlockCipher.getOutputBlockSize.

returns the maximum size of the block produced by this cipher.

Returns:int

maximum size of the output block produced by the cipher.

initback to summary
public void init(boolean forEncryption, CipherParameters parameters)

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.init.

Doc from org.bouncycastle.crypto.AsymmetricBlockCipher.init.

initialise the cipher.

Parameters
forEncryption:boolean

if true the cipher is initialised for encryption, if false for decryption.

parameters:CipherParameters

the key and other data required by the cipher.

log2back to summary
private int log2(int value)
MGFback to summary
private IntegerPolynomial MGF(byte[] seed, int N, int minCallsR, boolean hashSeed)

An implementation of MGF-TP-1 from P1363.1 section 8.4.1.1.

Parameters
hashSeed:boolean

whether to hash the seed

processBlockback to summary
public byte[] processBlock(byte[] in, int inOff, int len) throws InvalidCipherTextException

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.processBlock.

Doc from org.bouncycastle.crypto.AsymmetricBlockCipher.processBlock.

process the block of len bytes stored in in from offset inOff.

Parameters
in:byte[]

the input data

inOff:int

offset into the in array where the data starts

len:int

the length of the block to be processed.

Returns:byte[]

the resulting byte array of the encryption/decryption process.

Exceptions
InvalidCipherTextException:
data decrypts improperly.
putIntback to summary
private void putInt(Digest hashAlg, int counter)