// SPDX-License-Identifier: GPL-3.0-only // Copyright (c) 2022, Sylvain Huet, Ambermind // Minimacy (r) System use core.crypto.block;; fun aesCheckLength(msg) = !bitTest(strLength(msg), AES_BLOCK-1);; fun aesEncryptCbc(key, iv, msg)= if aesCheckLength(msg) then let aesCreate(key) -> aes in let bytesCreate(strLength(msg), 0) -> output in let cbcCreate(iv, output, 0, lambda(data, i, j) = aesWriteBytes(aesEncrypt(aes, data, i), output, j)) -> cbc in ( cbcEncryptIntoBytes(cbc, msg, 0, strLength(msg)); strFromBytes(output) );; fun aesDecryptCbc(key, iv, msg)= if aesCheckLength(msg) then let aesCreate(key) -> aes in let bytesCreate(strLength(msg), 0) -> output in let cbcCreate(iv, output, 0, lambda(data, i, j) = aesWriteBytes(aesDecrypt(aes, data, i), output, j)) -> cbc in ( cbcDecryptIntoBytes(cbc, msg); strFromBytes(output) );; fun aesEncryptEcb(key, msg)= if aesCheckLength(msg) then let aesCreate(key) -> aes in let bytesCreate(strLength(msg), 0) -> output in ( ecbEncryptIntoBytes(msg, AES_BLOCK, (lambda(data, i) = aesWriteBytes(aesEncrypt(aes, data, i), output, i))); strFromBytes(output) );; fun aesDecryptEcb(key, msg)= if aesCheckLength(msg) then let aesCreate(key) -> aes in let bytesCreate(strLength(msg), 0) -> output in ( ecbEncryptIntoBytes(msg, AES_BLOCK, (lambda(data, i) = aesWriteBytes(aesDecrypt(aes, data, i), output, i))); strFromBytes(output) );; //----- AES CTR fun aesEncryptCtr(key, count, msg)= let aesCreate(key) -> aes in let bigFromInt(1) -> one in strBuild(ctrEncrypt(msg, AES_BLOCK, count, 0, lambda(data)= aesOutput(aesEncrypt(aes, data, 0)), lambda(count)= bigAdd(one, count))) ;; fun aesEncryptCtrIncr(aes, count, msg)= strBuild(ctrEncryptCount(msg, AES_BLOCK, count, 0, lambda(data)= aesOutput(aesEncryptBytes(aes, data, 0)), lambda(count)= bytesMsbInc(count))) ;; //----- AES CMAC fun _aesCmacK1(aes)= let strSliceOfBytes(aesOutput(aesEncrypt(aes, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 0)), 0, nil) -> K1 in ( if 1==bytesLSL1(K1, 0) then bytesSet(K1, 15, 0x87^bytesGet(K1, 15)); K1 );; fun _aesCmacK2(aes)= let _aesCmacK1(aes) -> K2 in ( if 1==bytesLSL1(K2, 0) then bytesSet(K2, 15, 0x87^bytesGet(K2, 15)); K2 );; fun _aesPadding(msg)= strConcat(msg, strLeft("\$80\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16-strLength(msg)));; fun aesCmac(key, msg)= let strLength(msg) -> len in let len&15 -> r in let (len+15)>>4 -> n in let aesCreate(key) -> aes in let strSlice(msg, (n-1)<<4, nil) -> msg_end in let if (n>0)&&(r==0) then bytesXor(_aesCmacK1(aes), 0, msg_end, 0, nil) else bytesXor(_aesCmacK2(aes), 0, _aesPadding(msg_end), 0, nil) -> M_last in let "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ->iv in let bytesCreate(strLength(iv), 0) -> output in let cbcCreate(iv, output, 0, lambda(data, i, j) = aesWriteBytes(aesEncrypt(aes, data, i), output, 0)) -> cbc in ( cbcEncryptIntoBytes(cbc, msg, 0, (n-1)<<4); bytesXorBytes(M_last, 0, output, 0, nil); aesOutput(aesEncryptBytes(aes, M_last, 0)) );; //----- AES GCM fun _aesGcmGhash(mulH, A, C)= let bytesCreate(16, 0) -> Y in ( aesGcmGhash(Y, A, mulH); aesGcmGhash(Y, C, mulH); let 8*strLength(A) -> lenA in let 8*strLength(C) -> lenC in aesGcmGhash(Y, strFormat("\0\0\0\0*\0\0\0\0*", strInt32Msb(lenA), strInt32Msb(lenC)), mulH); );; fun _aesCtrEncrypt(msg, count, aes)= let strLength(msg) -> n in let bytesCreate(n, 0) -> buffer in ( for i=0;i count in let aesOutputBytes(aesEncryptBytes(aes, count, 0)) -> last in let bytesMsbInc(count) -> count in let _aesCtrEncrypt(data, count, aes) -> C in let if C==nil then "" else C -> C in let if encrypt then C else data -> src in let _aesGcmGhash(mulH, A, src) -> Y in let strFromBytes(bytesXorBytes(Y, 0, last, 0, nil)) -> T in [C, T];; fun aesGcmInitEncrypt(key)= let aesCreate(key) -> aes in let aesOutput(aesEncrypt(aes, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 0)) -> H in let aesGcmPrecompute(H) -> mulH in lambda(plainText, iv, A)= _aesGcm(true, plainText, iv, A, mulH, aes);; fun aesGcmInitDecrypt(key)= let aesCreate(key) -> aes in let aesOutput(aesEncrypt(aes, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 0)) -> H in let aesGcmPrecompute(H) -> mulH in lambda(cipherText, authBlock, iv, A)= let _aesGcm(false, cipherText, iv, A, mulH, aes) -> [msg, T2] in if authBlock==T2 then msg;; fun aesGcmEncrypt(key, plainText, iv, A)= let aesGcmInitEncrypt(key) -> fEncrypt in call fEncrypt(plainText, iv, A);; fun aesGcmDecrypt(key, cipherText, authBlock, iv, A) = let aesGcmInitDecrypt(key) -> fDecrypt in call fDecrypt(cipherText, authBlock, iv, A);;