Skip to content

Commit 64ef5f2

Browse files
authored
adding an option to use hex data
1 parent 1f6bc71 commit 64ef5f2

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

ascon.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ function non_eng(){
1919
}
2020
}
2121

22+
// data format: raw or hex
23+
var format = "raw";
24+
function data_format(){
25+
if(format == "raw"){
26+
format = "hex";
27+
} else {
28+
format = "raw";
29+
}
30+
}
31+
2232
// one function for authenticated encryption & decryption
2333
function ascon_aead(key, nonce, associateddata, data, operation, variant){
2434
// make sure parameters are within the correct ranges
@@ -44,15 +54,20 @@ function ascon_aead(key, nonce, associateddata, data, operation, variant){
4454
b = 8;
4555
rate = 16;
4656
}
57+
58+
// data format: raw or hex
59+
if(format == "hex"){associateddata = to_unicode(associateddata);}
4760

4861
S = ascon_initialize(S, rate, a, b, key, nonce);
4962
ascon_process_associated_data(S, b, rate, associateddata);
5063

5164
if(operation == "encrypt"){
65+
if(format == "hex"){data = to_unicode(data);} // data format: raw or hex
5266
ciphertext = ascon_process_plaintext(S, b, rate, data);
5367
tag = ascon_finalize(S, rate, a, key);
5468
// output = ciphertext (same size as plaintext) + tag (128-bits)
5569
return ciphertext + tag;
70+
5671
} else {
5772
plaintext = ascon_process_ciphertext(S, b, rate, data.slice(0, -32)); // exclude the tag
5873
tag = ascon_finalize(S, rate, a, key);
@@ -73,6 +88,10 @@ function ascon_xof(message, hashlength, variant){
7388
var a = 12; // intial & final rounds
7489
var b = 12; // intermediate rounds
7590

91+
// data format: raw or hex
92+
if(format == "hex"){
93+
message = to_unicode(message);
94+
}
7695
if(variant=="Ascon-XOFa"){
7796
b = 8;
7897
}
@@ -139,7 +158,7 @@ function ascon_process_associated_data(S, b, rate, associateddata) {
139158
S[0] ^= str_to_long(ad_padded.slice(block, block+8));
140159

141160
if(rate == 16){
142-
S[1] ^= str_to_long(ad_padded.slice(block+8, block+16))
161+
S[1] ^= str_to_long(ad_padded.slice(block+8, block+16));
143162
}
144163
ascon_permutation(S, b);
145164
}
@@ -273,7 +292,7 @@ function ascon_permutation(S, rounds) {
273292
S[2] ^= S[1];
274293

275294
// NOR & ANDing operations
276-
T = []
295+
T = [];
277296
for(var i = 0; i < 5; i++){
278297
T.push((S[i] ^ BigInt('0xFFFFFFFFFFFFFFFF')) & S[(i+1)%5]);
279298
}
@@ -449,6 +468,10 @@ function decrypt(key, nonce, ad, ct, variant){
449468

450469
if(pt != null){
451470
verification = "succeeded!";
471+
// data format: raw or hex
472+
if(format == "hex"){
473+
pt = JSON.stringify(bytes_to_hex_(to_ascii(pt))).replaceAll(/[",\][]/g,'');
474+
}
452475
return "plaintext: " + pt + "\nverification: " + verification;
453476
} else {
454477
return "verification failed!";
@@ -484,7 +507,7 @@ function hof_toggle(){
484507
// ctf
485508
var ctf_flag = false;
486509
var ctf = "solve a challenge, reach out to me with the flag, your name get listed in the solvers section<br>you'll find my email in ascon.js file<br>____________<br>";
487-
var challenge_1 = "<br><b>#1</b> <b>challenge name:</b> epic fail<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difficulty:</b> can't be easier<br><b>description:</b> \"I am lazy to generate more than one random number. I don't think you can decrypt my message though!\"<br><br><b>givens:</b><br>variant = Ascon-128<br>associated_data = playascon_ctf<br>nonce = ed7299db65af5fb3a683c17127a6050c<br>encrypted_message = 4d47c9affe000c392114494d7d9a4b874c455111a258cfa61c075dbcb36515eda093accf3c636ba1061510edbe58b87349cf975518536ed68c5a84c82c<br>tag = e9533dd90ef6abd06fa665496fed5054<br>key = well, at least I know this must kept secret!"
510+
var challenge_1 = "<br><b>#1</b> <b>challenge name:</b> epic fail<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difficulty:</b> can't be easier<br><b>description:</b> \"I am lazy to generate more than one random number. I don't think you can decrypt my message though!\"<br><br><b>givens:</b><br>variant = Ascon-128<br>associated_data = playascon_ctf<br>nonce = ed7299db65af5fb3a683c17127a6050c<br>encrypted_message = 4d47c9affe000c392114494d7d9a4b874c455111a258cfa61c075dbcb36515eda093accf3c636ba1061510edbe58b87349cf975518536ed68c5a84c82c<br>tag = e9533dd90ef6abd06fa665496fed5054<br>key = well, at least I know this must kept secret!";
488511

489512
function ctf_toggle(){
490513
if(!ctf_flag){

0 commit comments

Comments
 (0)