Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Simolation committed Feb 21, 2023
2 parents b6077a9 + 0e41227 commit f9e513d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ascon-js",
"private": false,
"version": "1.0.2",
"version": "1.0.3",
"author": "Simon Osterlehner <[email protected]>",
"license": "MIT",
"sideEffects": false,
Expand Down
56 changes: 56 additions & 0 deletions src/ascon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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);
});
}
);
});
8 changes: 5 additions & 3 deletions src/ascon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Ascon, fromHex, randomBytes, toHex } from ".";
import { arrayEquals } from "./helper";

const main = () => {
const key = fromHex("e4ea93530575bd6f5dc68cb241e32d1c");
Expand All @@ -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",
Expand All @@ -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();

0 comments on commit f9e513d

Please sign in to comment.