Files
XACrypto/src/de/caydenno1/xacrypto/zekerrijndael/GCM/ciphers/AESGCM.java
T
Xiao X 413b038b8a Zeker rijndael (#10)
* Update README.md (#2)

* AES128.java + UnchangedData.java

* Several ciphers, alot of ZekerRijndael progress

* maak de j0-functie aan

* maak een inc32-functie aan

* progress!

* GCM.java complete!

* my dementia .. lol

* SM4 COMPLET!

* decryption bugfix

* easy

making these were so easy, just had to copy and paste AES128 and change like 5 numbers

* read desc

i feel sick. i made the rest of the functions dummy functions and will finish them in the future. i am unsure if i will continue working on this tomorrow, i may take a break or i may not, who knows.

* F func finished in Camellia. several bugfixes

feel much better btw, took a shower and slept :)

* FL don

would recommend deleting .idea when you clone. FL done

* Camellia

Camellia complete

* add factory

* bugfix,more consise

* Fixed very annoying seperate AES<bitcount>.java files into one unified AES.java file :D (#4)

* AES.java potential

* Update AES.java

bufxie

* remove old libs

removed old AES$.java files as they are deprecated now that AES.java is created. (AES.java should be 100% functional.)

* Update Factories.java

aaand rename the AES128 factory to use AES with bits:128 instead of AES128

* Update AES.java

Performance Enhancements

PKCS#7 OK!

Safety (missing bounds protection)

* Update README.md (#5)

* Update AES.java

ah yes i love things magically stop working after PR ! :P

* something idk

my head is hurting again! :D so fun. i might make AESGCM.java use GHASH.java's xor function if i can figure out why its not working. also AESGCM.java (in its current state) sucks. i need to work on it a bit but just not now.

* update gitignore

* uh

how did it break? i didnt even change either of those files since they were working

* very legit solution 4realz1

* useless

* fix (#7)

* GHASH compnonce + fixed AESGCM bugs

* Update AES.java

some fixes.

* Update AESGCM.java

add warning

* Camelia

performance enhancements + decryption (i forgot to add it before lol) + several bugfixes

* "It's not a disaster, It's innovation!"

* decryption + bufixes

* Update Project_Default.xml

* this isnt English class

* Update README.md

it duplicated itself lol

* desc

Aria.java progress, next commit will have GRK finished

* Aria.java complete!

* desc

added support for Camelia 192 + 256 bit operation, perf enhancements and bugfixes

* add ARIAGCM factory

my wrapper in ARIAGCM handles GCM :)

* Update README.md

add TODO

* added 128,192,256 support for AESGCM

* desc

soo... did you know sometimes you actually do want to reuse code without getting 68 warnings from intellij?

* note+camellia tab

* began Serpant.java

* Serpent COMPLET!

* desc

add dummy functions to twofish

* Update Twofish.java

progrses on twofish + HUMOR

* twofish done + readme upd

* HOLY

i actually finished Blowfish.java lol, almost done with ECB, just neeed 2 functions

* Bfish ECB done! :D

* desc

- fix typo

- add Aria.java support for Aria-ECB

- move Aria.java to globalpackaeg

* Create AriaECB.java

will finish this tmr. im so tired lol

* desc

ariaecb complete, also forgot to add factories for blowfish too

* Update AriaECB.java

* sm4ecb complete
2026-06-02 12:34:10 -04:00

94 lines
3.2 KiB
Java

package de.caydenno1.xacrypto.zekerrijndael.GCM.ciphers;
import de.caydenno1.xacrypto.misc.XACryptoException;
import de.caydenno1.xacrypto.zekerrijndael.GCM.GHASH;
import de.caydenno1.xacrypto.zekerrijndael.GCM.AES;
import de.caydenno1.xacrypto.zekerrijndael.GCM.Result;
import java.util.Objects;
import static de.caydenno1.xacrypto.misc.ToM.ToM;
interface AESCipher {
Result encryptBlock(byte[] pln, byte[] key, byte[] nonce, byte[] aad) throws XACryptoException;
byte[] decryptBlock(byte[] cip, byte[] key, byte[] nonce, byte[] aad, byte[] tag) throws XACryptoException;
}
public class AESGCM implements AESCipher {
public Result encryptBlock(byte[] pln, byte[] key, byte[] nonce, byte[] aad) throws XACryptoException {
System.out.println("WARNING! AESGCM may not be fully functional and partially broken. I am unsure if it fully works or not.");
@SuppressWarnings("DuplicatedCode")
byte[] zbyte = new byte[16];
byte[] J0 = new byte[16];
AES aes = new AES(key, getKeySize(key));
@SuppressWarnings("DuplicatedCode")
byte[] H = aes.encryptBlock(zbyte);
GHASH gh = new GHASH(H);
if (nonce.length == 12) {
System.arraycopy(nonce, 0, J0, 0, 12);
J0[15] = 1;
} else {
J0 = gh.compNonce(H, nonce);
}
byte[] cip = aes.encryptCTR(pln, J0);
byte[] S = gh.compute(aad, cip);
byte[] TB = aes.encryptBlock(J0);
byte[] Tag = gh.xor(TB, S);
return new Result(cip, Tag);
}
public byte[] decryptBlock(byte[] cip, byte[] key, byte[] nonce, byte[] aad, byte[] tag, String flag) throws XACryptoException {
return decBack(cip, key, nonce, aad, tag, Objects.equals(flag, "-override"));
}
public byte[] decryptBlock(byte[] cip, byte[] key, byte[] nonce, byte[] aad, byte[] tag) throws XACryptoException {
return decBack(cip, key, nonce, aad, tag, false);
}
private static int getKeySize(byte[] key) throws XACryptoException {
return switch (key.length) {
case 16 -> 128;
case 24 -> 192;
case 32 -> 256;
default -> throw new XACryptoException(
"Invalid AES key length. Expected 16, 24, or 32 bytes."
);
};
}
private byte[] decBack(byte[] cip, byte[] key, byte[] nonce, byte[] aad, byte[] tag, boolean flag) throws XACryptoException {
byte[] zbyte = new byte[16];
byte[] J0 = new byte[16];
@SuppressWarnings("DuplicatedCode")
AES aes = new AES(key, getKeySize(key));
byte[] H = aes.encryptBlock(zbyte);
GHASH gh = new GHASH(H);
if (nonce.length == 12) {
System.arraycopy(nonce, 0, J0, 0, 12);
J0[15] = 1;
} else {
J0 = gh.compNonce(H, nonce);
}
byte[] S = gh.compute(aad, cip);
byte[] TB = aes.encryptBlock(J0);
byte[] expectedTag = gh.xor(TB, S);
if (!ToM(tag, expectedTag) && !flag) {
throw new XACryptoException("Tag does not match. USE flag \"-override\" to ignore this.");
} else if (!ToM(tag,expectedTag) && flag) {
System.out.println("Continuing in insecure mode.");
}
return aes.encryptCTR(cip, J0);
}
}