Skip to content

Commit

Permalink
Merged PR 874
Browse files Browse the repository at this point in the history
Add LEAEngine

Found test more test vectors added those.

open-keychain#2
  • Loading branch information
mwcw committed Oct 5, 2021
1 parent c853df5 commit 2c23d5c
Show file tree
Hide file tree
Showing 3 changed files with 1,747 additions and 144 deletions.
64 changes: 39 additions & 25 deletions core/src/main/java/org/bouncycastle/crypto/engines/LEAEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* LEA Cipher engine.
*/
public class LEAEngine
implements BlockCipher
implements BlockCipher
{
/**
* Base number of rounds.
Expand Down Expand Up @@ -127,9 +127,9 @@ public class LEAEngine
* Delta values.
*/
private static final int[] DELTA =
{
{
0xc3efe9db, 0x44626b02, 0x79e27c8a, 0x78df30ec, 0x715ea49e, 0xc785da0a, 0xe04ef22a, 0xe5c40957
};
};

/**
* The work buffer.
Expand Down Expand Up @@ -166,11 +166,11 @@ public void init(final boolean pEncrypt,
if (!(pParams instanceof KeyParameter))
{
throw new IllegalArgumentException("Invalid parameter passed to LEA init - "
+ pParams.getClass().getName());
+ pParams.getClass().getName());
}

/* Validate keyLength */
final byte[] myKey = ((KeyParameter) pParams).getKey();
final byte[] myKey = ((KeyParameter)pParams).getKey();
final int myKeyLen = myKey.length;
if ((((myKeyLen << 1) % BLOCKSIZE) != 0)
|| myKeyLen < BLOCKSIZE
Expand Down Expand Up @@ -210,12 +210,13 @@ public int processBlock(final byte[] pInput,

/* Perform the encryption/decryption */
return forEncryption
? encryptBlock(pInput, pInOff, pOutput, pOutOff)
: decryptBlock(pInput, pInOff, pOutput, pOutOff);
? encryptBlock(pInput, pInOff, pOutput, pOutOff)
: decryptBlock(pInput, pInOff, pOutput, pOutOff);
}

/**
* Obtain buffer length (allowing for null).
*
* @param pBuffer the buffer
* @return the length
*/
Expand All @@ -226,6 +227,7 @@ private static int bufLength(final byte[] pBuffer)

/**
* Check buffer.
*
* @param pBuffer the buffer
* @param pOffset the offset
* @param pOutput is this an output buffer?
Expand All @@ -243,15 +245,16 @@ private static void checkBuffer(final byte[] pBuffer,
if (badLen || myLast > myBufLen)
{
throw pOutput
? new OutputLengthException("Output buffer too short.")
: new DataLengthException("Input buffer too short.");
? new OutputLengthException("Output buffer too short.")
: new DataLengthException("Input buffer too short.");
}
}

/**
* Encrypt a block.
* @param pInput the input buffer
* @param pInOff the input offset
*
* @param pInput the input buffer
* @param pInOff the input offset
* @param pOutput the output offset
* @param pOutOff the output offset
* @return the bytes processed
Expand Down Expand Up @@ -279,6 +282,7 @@ private int encryptBlock(final byte[] pInput,

/**
* Encrypt a round.
*
* @param pRound the round#
*/
private void encryptRound(final int pRound)
Expand All @@ -297,6 +301,7 @@ private void encryptRound(final int pRound)

/**
* Return the left of an index.
*
* @param pIndex the index
* @return the left of an index
*/
Expand All @@ -307,8 +312,9 @@ private static int leftIndex(final int pIndex)

/**
* Decrypt a block.
* @param pInput the input buffer
* @param pInOff the input offset
*
* @param pInput the input buffer
* @param pInOff the input offset
* @param pOutput the output offset
* @param pOutOff the output offset
* @return the bytes processed
Expand Down Expand Up @@ -336,6 +342,7 @@ private int decryptBlock(final byte[] pInput,

/**
* Decrypt a round.
*
* @param pRound the round#
*/
private void decryptRound(final int pRound)
Expand All @@ -354,6 +361,7 @@ private void decryptRound(final int pRound)

/**
* Return the left of an index.
*
* @param pIndex the index
* @return the left of an index
*/
Expand All @@ -364,6 +372,7 @@ private static int rightIndex(final int pIndex)

/**
* Generate the round keys.
*
* @param pKey the key
*/
private void generateRoundKeys(final byte[] pKey)
Expand All @@ -380,21 +389,22 @@ private void generateRoundKeys(final byte[] pKey)
/* Switch on number of words in the key */
switch (numWords)
{
case NUMWORDS128:
generate128RoundKeys(myT);
break;
case NUMWORDS192:
generate192RoundKeys(myT);
break;
case NUMWORDS256:
default:
generate256RoundKeys(myT);
break;
case NUMWORDS128:
generate128RoundKeys(myT);
break;
case NUMWORDS192:
generate192RoundKeys(myT);
break;
case NUMWORDS256:
default:
generate256RoundKeys(myT);
break;
}
}

/**
* Generate the round keys from 128-bit key.
*
* @param pWork the working keys
*/
private void generate128RoundKeys(final int[] pWork)
Expand All @@ -421,6 +431,7 @@ private void generate128RoundKeys(final int[] pWork)

/**
* Generate the round keys from 192-bit key.
*
* @param pWork the working keys
*/
private void generate192RoundKeys(final int[] pWork)
Expand All @@ -442,6 +453,7 @@ private void generate192RoundKeys(final int[] pWork)

/**
* Generate the round keys from 256-bit key.
*
* @param pWork the working keys
*/
private void generate256RoundKeys(final int[] pWork)
Expand Down Expand Up @@ -470,8 +482,9 @@ private void generate256RoundKeys(final int[] pWork)

/**
* rotate left.
*
* @param pValue the value to rotate
* @param pBits the # of bits to rotate
* @param pBits the # of bits to rotate
* @return the rotated value
*/
private static int rol32(final int pValue,
Expand All @@ -482,8 +495,9 @@ private static int rol32(final int pValue,

/**
* rotate right.
*
* @param pValue the value to rotate
* @param pBits the # of bits to rotate
* @param pBits the # of bits to rotate
* @return the rotated value
*/
private static int ror32(final int pValue,
Expand Down
Loading

0 comments on commit 2c23d5c

Please sign in to comment.