diff --git a/package.json b/package.json index ee72b7a..5bd8d28 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ascon-js", "private": false, - "version": "1.0.2", + "version": "1.0.3", "author": "Simon Osterlehner ", "license": "MIT", "sideEffects": false, diff --git a/src/ascon.spec.ts b/src/ascon.spec.ts index e2608ba..d716e27 100644 --- a/src/ascon.spec.ts +++ b/src/ascon.spec.ts @@ -9,6 +9,9 @@ const nonce = fromHex("6c27fff03b58975180cf12de2fd2d6e2"); const arrayToLong = fromHex("01020304050607080900010203040506070809"); const plainText = new TextEncoder().encode("ascon"); +const plainTextLonger = new TextEncoder().encode( + "This is a longer message used for testing purposes and is definitely longer than 32 bytes." +); const associatedData = new TextEncoder().encode("ASCON"); describe("Ascon.hash", () => { @@ -201,3 +204,56 @@ describe("Ascon.decrypt", () => { ).toThrow("Could not be decrypted. Tags don't match."); }); }); + +describe("Ascon de- and encryption", () => { + it.each([ + { + variant: "Ascon-128" as AsconEncryptionVariant, + associatedData, + }, + { + variant: "Ascon-128" as AsconEncryptionVariant, + associatedData: undefined, + }, + { + variant: "Ascon-128a" as AsconEncryptionVariant, + associatedData, + }, + { + variant: "Ascon-128a" as AsconEncryptionVariant, + associatedData: undefined, + }, + { + variant: "Ascon-80pq" as AsconEncryptionVariant, + associatedData, + }, + { + variant: "Ascon-80pq" as AsconEncryptionVariant, + associatedData: undefined, + }, + ])( + "Should correctly encrypt and decrypt different length plaintext with $variant", + ({ variant, associatedData }) => { + [Uint8Array.from([]), plainText, plainTextLonger].forEach((item) => { + const keyToUse = variant === "Ascon-80pq" ? key20 : key; + const encrypted = Ascon.encrypt(keyToUse, nonce, item, { + associatedData, + variant, + }); + + // Check the length of the encrypted text + 16 bytes for the tag + expect(encrypted).toHaveLength(item.length + 16); + expect(encrypted).not.toEqual(item); + + const decrypted = Ascon.decrypt(keyToUse, nonce, encrypted, { + associatedData, + variant, + }); + + // Check the length of the encrypted text + 16 bytes for the tag + expect(decrypted).toHaveLength(item.length); + expect(decrypted).toEqual(item); + }); + } + ); +}); diff --git a/src/ascon.ts b/src/ascon.ts index 865e9d7..afc3337 100644 --- a/src/ascon.ts +++ b/src/ascon.ts @@ -401,9 +401,11 @@ export class Ascon { plaintext = concatArrays( plaintext, - intToBytes(S[0] ^ c0, 8), - intToBytes(S[1] ^ c1, 8) - ).slice(0, cLastLen); + concatArrays(intToBytes(S[0] ^ c0, 8), intToBytes(S[1] ^ c1, 8)).slice( + 0, + cLastLen + ) + ); if (cLastLen < 8) { S[0] = c0 ^ (S[0] & cMask) ^ cPadding; diff --git a/src/main.ts b/src/main.ts index 1f8472b..eacc46c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,5 @@ import { Ascon, fromHex, randomBytes, toHex } from "."; +import { arrayEquals } from "./helper"; const main = () => { const key = fromHex("e4ea93530575bd6f5dc68cb241e32d1c"); @@ -7,7 +8,11 @@ const main = () => { const nonce = fromHex("6c27fff03b58975180cf12de2fd2d6e2"); const associated = new TextEncoder().encode("ASCON"); - const plaintext = new TextEncoder().encode("ascon"); + const plaintext = new TextEncoder().encode( + "Hallo mein Name ist Simon Osterlehner und ich bin in Muenchen geboren." + ); + + console.log("Plaintext", plaintext); const encrypted = Ascon.encrypt(key, nonce, plaintext, { variant: "Ascon-128a", @@ -25,7 +30,9 @@ const main = () => { const text = new TextDecoder().decode(decrypted); - console.log("Decrypted", text); + console.log("Decrypted", decrypted); + + console.log("Decrypted", arrayEquals(decrypted, plaintext), text); }; main();