From da74180884eaa8c694002b26f72e0ad8d1429c08 Mon Sep 17 00:00:00 2001 From: vince0656 Date: Tue, 4 Sep 2018 15:21:31 +0100 Subject: [PATCH] Public Keys and Secret Keys for Trustees are now Base64 encoded. I've tested this against 1 trustee and multiple trustees. --- Node/index.js | 30 ++++++++++++++----- Node/package.json | 1 + allauthdemo/polls/models.py | 2 +- .../templates/bases/bootstrap-jquery.html | 16 ++++++++-- static/js/decrypt_event.js | 26 ++++++++++++---- 5 files changed, 57 insertions(+), 18 deletions(-) diff --git a/Node/index.js b/Node/index.js index f8f6945..9df424b 100755 --- a/Node/index.js +++ b/Node/index.js @@ -10,6 +10,7 @@ Dependencies can be found in 'package.json' and installed using 'npm install' var port = 8080; var Buffer = require('buffer').Buffer; +var atob = require("atob"); var CTX = require('milagro-crypto-js'); var express = require('express'); @@ -65,6 +66,13 @@ app.get('/combpk', function(request, response){ }); +function getKeyBytes(key, byteArray) { + for(let i = 0; i < key.length; i += 4) { + let B64EncodedByte = key.substring(i, i + 4); + + byteArray.push(atob(B64EncodedByte)); + } +} //byte array version app.post('/cmpkstring', function(request, response){ @@ -77,19 +85,25 @@ app.post('/cmpkstring', function(request, response){ if(partials.length > 1)//if we're submitting more than one key { console.log('Combining ' + partials.length + " public keys into one..."); - for (var i = partials.length - 1; i >= 0; i--) { - console.log('PK' + i + ': ' + partials[i]); - var bytes = Buffer.from(partials[i].split(','), 'hex'); - var pk = new ctx.ECP.fromBytes(bytes); - parsed.push(pk); + for (let i = partials.length - 1; i >= 0; i--) { + console.log('PK' + i + ': ' + partials[i]); + + let rawBytes = []; + getKeyBytes(partials[i], rawBytes); + + parsed.push(new ctx.ECP.fromBytes(Buffer.from(rawBytes, 'hex'))); } } else if(partials.length === 1) { console.log("Combining just one public key..."); - var bytes = Buffer.from(partials[0].split(','), 'hex'); - var pk = new ctx.ECP.fromBytes(bytes); - parsed.push(pk); + let PKStr = partials[0]; + console.log("PK: " + PKStr); + + let rawBytes = []; + getKeyBytes(PKStr, rawBytes); + + parsed.push(new ctx.ECP.fromBytes(Buffer.from(rawBytes, 'hex'))); } response.json(combine_pks(parsed)); diff --git a/Node/package.json b/Node/package.json index 3133702..9e11f20 100644 --- a/Node/package.json +++ b/Node/package.json @@ -17,6 +17,7 @@ "author": "Bingsheng Zang, Thomas Smith, Vincent de Almeida", "license": "ISC", "dependencies": { + "atob": "^2.1.2", "body-parser": "^1.18.3", "express": "^4.16.3", "milagro-crypto-js": "git+https://github.com/milagro-crypto/milagro-crypto-js.git" diff --git a/allauthdemo/polls/models.py b/allauthdemo/polls/models.py index 8239ccb..9a51427 100755 --- a/allauthdemo/polls/models.py +++ b/allauthdemo/polls/models.py @@ -145,7 +145,7 @@ class Event(models.Model): class TrusteeKey(models.Model): event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name="trustee_keys") user = models.ForeignKey(EmailUser, on_delete=models.CASCADE, related_name="trustee_keys") - key = models.CharField(max_length=255, unique=True) + key = models.CharField(max_length=260) class AccessKey(models.Model): diff --git a/allauthdemo/templates/bases/bootstrap-jquery.html b/allauthdemo/templates/bases/bootstrap-jquery.html index 16b6754..59279ac 100755 --- a/allauthdemo/templates/bases/bootstrap-jquery.html +++ b/allauthdemo/templates/bases/bootstrap-jquery.html @@ -108,11 +108,21 @@ keypair.PK.toBytes(PKbytes); keypair.SK.toBytes(SKbytes); - $('input#public-key').val(PKbytes.toString()); - $('input#secret-key').val(SKbytes.toString()); + var PKB64Encoded = ""; + for(let i = 0; i < PKbytes.length; i++) { + PKB64Encoded += btoa(PKbytes[i]); + } + + var SKB64Encoded = ""; + for(let j = 0; j < SKbytes.length; j++) { + SKB64Encoded += btoa(SKbytes[j]); + } + + $('input#public-key').val(PKB64Encoded); + $('input#secret-key').val(SKB64Encoded); //mostly code from before here - var blob = new Blob([SKbytes.toString()], {type : 'text/plain'}); + var blob = new Blob([SKB64Encoded], {type : 'text/plain'}); var dlBtn = $('a#download-btn'); var url = URL.createObjectURL(blob); diff --git a/static/js/decrypt_event.js b/static/js/decrypt_event.js index 400a7e2..5255fa7 100644 --- a/static/js/decrypt_event.js +++ b/static/js/decrypt_event.js @@ -15,6 +15,14 @@ function csrfSafeMethod(method) { return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } +function getKeyBytes(key, byteArray) { + for(let i = 0; i < key.length; i += 4) { + let B64EncodedByte = key.substring(i, i + 4); + + byteArray.push(atob(B64EncodedByte)); + } +} + function showDialog(titleTxt, bodyTxt) { var modalDialog = $('#modalDialog'); var title = modalDialog.find('.modal-title'); @@ -37,7 +45,14 @@ function validateSKFromString(SKStr) { // Re-create the SK from the string byte definition let ctx = new CTX("BN254CX"); - let skBytes = SKStr.split(","); + // Check that the length is valid, otherwise display an error + if(!(SKStr.length % 4 === 0)) { + showDialog('Error', + 'The length of the supplied secret key appears to be invalid. Check and try again.'); + } + + let skBytes = []; + getKeyBytes(SKStr, skBytes); let sk = new ctx.BIG.fromBytes(skBytes); // Re-create the params @@ -56,10 +71,8 @@ function validateSKFromString(SKStr) { }; // Re-create the trustee PK from the string byte definition - let pkBytes = trustee_pk.split(',').map(function(byteStr) { - return parseInt(byteStr) - }); - + let pkBytes = []; + getKeyBytes(trustee_pk, pkBytes); let pk = new ctx.ECP.fromBytes(pkBytes); // Check that the SK supplies generates the PK we know about @@ -75,7 +88,8 @@ function decryptSubmitCiphers() { else { // Rebuild the trustee's secret key var ctx = new CTX("BN254CX"); - var skBytes = skString.split(","); + var skBytes = []; + getKeyBytes(skString, skBytes); var sk = new ctx.BIG.fromBytes(skBytes); var inputs = $("form input[type=text]");