2018-09-03 14:39:42 +00:00
|
|
|
// -------------- Global vars --------------------
|
|
|
|
var filesHandleSK = document.getElementById('files_sk_upload');
|
|
|
|
var CSRF = $( "input[name='csrfmiddlewaretoken']" ).val();
|
|
|
|
|
|
|
|
// -------------- Helper fns --------------------
|
2018-07-13 16:19:35 +00:00
|
|
|
//SK checking algorithm - If PK and SK matches, it returns True; otherwise, it returns false.
|
|
|
|
// Written by Bingsheng Zhang
|
|
|
|
function skCheck(ctx, params, SK, PK) {
|
|
|
|
var D = ctx.PAIR.G1mul(params.g1, SK);
|
|
|
|
return D.equals(PK)
|
|
|
|
}
|
|
|
|
|
2018-09-03 14:39:42 +00:00
|
|
|
function csrfSafeMethod(method) {
|
|
|
|
// these HTTP methods do not require CSRF protection
|
|
|
|
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
|
|
|
|
}
|
|
|
|
|
|
|
|
function showDialog(titleTxt, bodyTxt) {
|
|
|
|
var modalDialog = $('#modalDialog');
|
|
|
|
var title = modalDialog.find('.modal-title');
|
|
|
|
var body = modalDialog.find('.modal-body');
|
|
|
|
|
|
|
|
title.text(titleTxt);
|
|
|
|
var bodyText = bodyTxt;
|
|
|
|
|
|
|
|
var p = document.createElement("p");
|
|
|
|
p.innerHTML = bodyText;
|
|
|
|
body.empty();
|
|
|
|
body.append( p );
|
|
|
|
|
|
|
|
modalDialog.modal('show');
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------
|
|
|
|
|
2018-07-13 16:19:35 +00:00
|
|
|
function validateSKFromString(SKStr) {
|
|
|
|
// Re-create the SK from the string byte definition
|
|
|
|
let ctx = new CTX("BN254CX");
|
|
|
|
|
|
|
|
let skBytes = SKStr.split(",");
|
|
|
|
let sk = new ctx.BIG.fromBytes(skBytes);
|
|
|
|
|
|
|
|
// Re-create the params
|
|
|
|
let n = new ctx.BIG();
|
|
|
|
let g1 = new ctx.ECP();
|
|
|
|
let g2 = new ctx.ECP2();
|
|
|
|
|
|
|
|
n.copy(tempParams.n);
|
|
|
|
g1.copy(tempParams.g1);
|
|
|
|
g2.copy(tempParams.g2);
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
n:n,
|
|
|
|
g1:g1,
|
|
|
|
g2:g2
|
|
|
|
};
|
|
|
|
|
|
|
|
// Re-create the trustee PK from the string byte definition
|
|
|
|
let pkBytes = trustee_pk.split(',').map(function(byteStr) {
|
|
|
|
return parseInt(byteStr)
|
|
|
|
});
|
|
|
|
|
|
|
|
let pk = new ctx.ECP.fromBytes(pkBytes);
|
|
|
|
|
|
|
|
// Check that the SK supplies generates the PK we know about
|
|
|
|
return skCheck(ctx, params, sk, pk);
|
|
|
|
}
|
|
|
|
|
2018-09-03 14:39:42 +00:00
|
|
|
function decryptSubmitCiphers() {
|
|
|
|
var skString = $('#secret-key').val();
|
2018-07-13 16:19:35 +00:00
|
|
|
|
2018-09-03 14:39:42 +00:00
|
|
|
if (!skString) {
|
|
|
|
showDialog('Error', 'You haven\'t supplied your secret key. Please go back and upload this from file.');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Rebuild the trustee's secret key
|
|
|
|
var ctx = new CTX("BN254CX");
|
|
|
|
var skBytes = skString.split(",");
|
|
|
|
var sk = new ctx.BIG.fromBytes(skBytes);
|
2018-07-13 16:19:35 +00:00
|
|
|
|
2018-09-03 14:39:42 +00:00
|
|
|
var inputs = $("form input[type=text]");
|
2018-07-13 16:19:35 +00:00
|
|
|
|
2018-09-03 14:39:42 +00:00
|
|
|
inputs.each(function() { //for each ciphertext to decrypt
|
|
|
|
let input = $(this);
|
|
|
|
console.log(input.attr('name'));
|
|
|
|
|
|
|
|
var ciphertext = {
|
|
|
|
C1: null,
|
|
|
|
C2: null
|
|
|
|
};
|
|
|
|
|
|
|
|
var temp = JSON.parse(input.val());
|
|
|
|
var c1Bytes = getBytes(temp.C1.split(','));
|
|
|
|
ciphertext.C1 = new ctx.ECP.fromBytes(c1Bytes);
|
|
|
|
|
|
|
|
var c2Bytes = getBytes(temp.C2.split(','));
|
|
|
|
ciphertext.C2 = new ctx.ECP.fromBytes(c2Bytes);
|
|
|
|
|
|
|
|
// Perform partial decryption where the method returns an object containing an ECP()
|
|
|
|
var partial = partDec(sk, ciphertext);
|
|
|
|
|
|
|
|
var bytes = [];
|
|
|
|
partial.D.toBytes(bytes);
|
|
|
|
input.val(bytes.toString());
|
|
|
|
});
|
|
|
|
}
|
2018-07-13 16:19:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
function processFileSKChange(event) {
|
|
|
|
var files = event.target.files;
|
|
|
|
|
|
|
|
if(files !== undefined
|
|
|
|
&& files[0] !== undefined) {
|
|
|
|
var reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = function(e) {
|
2018-07-13 16:19:35 +00:00
|
|
|
var SKStr = reader.result;
|
|
|
|
|
|
|
|
// Check that the SK string is not blank
|
|
|
|
if(SKStr === '') {
|
|
|
|
// Show a dialog informing the user that they've uploaded a blank file
|
|
|
|
showDialog('Error', 'The file you have uploaded is blank.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const valid = validateSKFromString(SKStr);
|
|
|
|
|
|
|
|
if(valid) {
|
|
|
|
$('input#secret-key').val(SKStr);
|
|
|
|
} else {
|
|
|
|
// Show a dialog informing the user that they've supplied an invalid SK
|
|
|
|
showDialog('Error',
|
|
|
|
'The secret key you have supplied is invalid and doesn\'t match with the recorded public key.');
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsText(files[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(filesHandleSK) {
|
|
|
|
filesHandleSK.addEventListener('change', processFileSKChange, false);
|
|
|
|
}
|