-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented InvCipher function for AES
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
aes/src/commonMain/kotlin/com/mooncloak/kodetools/aes/InvCipher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.mooncloak.kodetools.aes | ||
|
||
/** | ||
* Represents the general decryption function for executing the AES (Advanced Encryption Standard). | ||
* | ||
* @param [input] The data input, which is a block represented as a linear array of 16 bytes. | ||
* | ||
* @param [numberOfRounds] The number of rounds, "Nr", for the instance. This value is dependent on the key bit size. | ||
* The following values are supported: AES-128: `10`, for AES-192: `12`, AES-256: `14`. | ||
* | ||
* @param [roundKeys] The keys for each round. This value must be of size [numberOfRounds]. A round key is a block that | ||
* is usually represented as a sequence of four words (16 bytes). The "KEYEXPANSION()" function, defined by the AES | ||
* Specification, takes the block cipher key as input and generates the round keys as output. | ||
* | ||
* @see [AES Specification](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197-upd1.pdf) | ||
*/ | ||
internal fun InvCipher( | ||
input: ByteArray, | ||
numberOfRounds: Int, | ||
roundKeys: Array<ByteArray> | ||
): ByteArray { | ||
val state = State(input) | ||
|
||
state.addRoundKey(roundKeys.copyOfRange(fromIndex = 4 * numberOfRounds, toIndex = 4 * numberOfRounds + 3)) | ||
|
||
for (round in (numberOfRounds - 1) downTo 1) { | ||
state.invShiftRows() | ||
state.invSubBytes() | ||
state.addRoundKey(roundKeys.copyOfRange(fromIndex = 4 * round, toIndex = 4 * round + 3)) | ||
state.invMixColumns() | ||
} | ||
|
||
state.invShiftRows() | ||
state.invSubBytes() | ||
state.addRoundKey(roundKeys.copyOfRange(fromIndex = 0, toIndex = 4)) | ||
|
||
return state.output() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters