Skip to content

Commit

Permalink
Added import/export for private key. Added random key generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
desudesutalk committed Dec 4, 2016
1 parent ef49668 commit 302683c
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 76 deletions.
2 changes: 1 addition & 1 deletion ddt.meta.js
@@ -1,7 +1,7 @@
// ==UserScript==
// @name DesuDesuTalk
// @namespace udp://desushelter/*
// @version 0.4.83
// @version 0.4.84
// @description Write something useful!
// @include *://dobrochan.com/*/*
// @include *://dobrochan.ru/*/*
Expand Down
122 changes: 85 additions & 37 deletions ddt.user.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/libs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/libs/FileSaver.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/metablock.js
@@ -1,7 +1,7 @@
// ==UserScript==
// @name DesuDesuTalk
// @namespace udp://desushelter/*
// @version 0.4.83
// @version 0.4.84
// @description Write something useful!
// @include *://dobrochan.com/*/*
// @include *://dobrochan.ru/*/*
Expand Down
34 changes: 21 additions & 13 deletions src/misc/codec.js
Expand Up @@ -2,35 +2,43 @@ var rsaProfile = {},
rsa = null,
rsa_hash, rsa_hashB64, broadProfile = {}, broad_hashB64;

var do_login = function(e, key) {
var do_login = function(e, fromCfg, key, rnd) {
"use strict";
var lf = document.loginform;
if(!key){
rsaProfile = cryptCore.login(lf.passwd.value, lf.magik_num.value, false);
var lf = document.loginform,
pwd = lf.passwd.value,
slt = lf.magik_num.value;

lf.magik_num.value = lf.passwd.value = '';

if(key){
rsaProfile = cryptCore.login(null, null, false, key);
}else if(rnd){
rsaProfile = cryptCore.login();
}else if(!fromCfg){
rsaProfile = cryptCore.login(pwd, slt, false);
}else{
rsaProfile = cryptCore.login(null, null, true);
}

if(!rsaProfile) {
rsaProfile = {};
return false;
}
lf.magik_num.value = lf.passwd.value = '';

rsa_hash = rsaProfile.publicKeyPairPrintableHash;
rsa_hashB64 = rsaProfile.publicKeyPairPrintable;

rsa_hashB64 = rsaProfile.publicKeyPairPrintable;

$('#identi').html(rsa_hashB64).identicon5({
rotate: true,
size: 64
});
$('#identi').append('<br/><br/><i style="color: #009;">'+rsa_hashB64+'</i>');
$('#identi').append('<br/><br/><i style="color: #009;">'+rsa_hashB64+'</i>');
};

var do_loginBroadcast = function(e, key) {
"use strict";
var lf = document.broadcastform;
if(!key){
if(!key){
broadProfile = cryptCore.loginBroadcast(lf.passwd.value, lf.magik_num.value, false);
}else{
broadProfile = cryptCore.loginBroadcast(null, null, true);
Expand All @@ -43,7 +51,7 @@ var do_loginBroadcast = function(e, key) {
rotate: true,
size: 64
});
$('#identi_broad').append('<br/><br/><i style="color: #009;">'+broad_hashB64+'</i>');
$('#identi_broad').append('<br/><br/><i style="color: #009;">'+broad_hashB64+'</i>');
};


Expand Down Expand Up @@ -71,7 +79,7 @@ var do_encode = function() {

if(!("publicKeyPairPrintable" in rsaProfile)){
alert('Please log in.');
return false;
return false;
}

payLoad.text = $('#hidbord_reply_text').val();
Expand All @@ -94,7 +102,7 @@ var do_encode = function() {
keys[c] = contacts[c];
continue;
}

if('hide' in contacts[c] && contacts[c].hide == 1){
continue;
}
Expand Down Expand Up @@ -130,7 +138,7 @@ var do_encode = function() {
if(!final_container) return false;

//var out_file = appendBuffer(final_container, lastRand);

//var compressedB64 = arrayBufferDataUri(out_file);

sendBoardForm(final_container);
Expand Down
58 changes: 36 additions & 22 deletions src/misc/cryptcore.js
Expand Up @@ -10,17 +10,21 @@ var cryptCore = (function(){
return sjcl.codec.bytes.fromBits(sjcl.hash.sha256.hash(sjcl.codec.bytes.toBits(sharedSecret)));
};

cryptCore.login = function login(password, salt, key) {
cryptCore.login = function login(password, salt, fromCfg, key) {
var privateKey = null, encKey = null;

if(key){
privateKey = bs58.dec(key);
}else if(fromCfg){
if (ssGet(boardHostName + profileStoreName)) {
privateKey = bs58.dec(ssGet(boardHostName + profileStoreName).privateKeyPair);
}else{
return false;
}
}else{
}else if(password && salt){
privateKey = sjcl.codec.bytes.fromBits(sjcl.misc.pbkdf2(password, salt, 500017, 256));
}else{
privateKey = ECcrypt.genKeyPair().getPrivate().toArray();
}

encKey = ECcrypt.keyPair(privateKey);
Expand Down Expand Up @@ -120,7 +124,7 @@ var cryptCore = (function(){
ephemeral_byte = hexToBytes(ephemeral.getPublic(true, "hex"));

if(hideSender && numContacts < 3) hideRecievers = true;

ephemeral_byte[0] ^= (Math.random() * 0x100 | 0) & 0xfe;

sessionKey[31] = 0xAA;
Expand All @@ -137,7 +141,7 @@ var cryptCore = (function(){

msgHash.update(sjcl.codec.bytes.toBits(ephemeral_byte));
msgHash.update(iv);

for (i = 0; i < slots.length; i++) {
msgHash.update(sjcl.codec.bytes.toBits(slots[i]));
}
Expand All @@ -146,7 +150,7 @@ var cryptCore = (function(){
msgLength += 80; // Length of signature here!
}

if(!hideRecievers){
if(!hideRecievers){
msgLength += 32 + 33 * numContacts;
}else if(!hideSender){
msgLength += 33; // add sender address
Expand All @@ -158,7 +162,7 @@ var cryptCore = (function(){

if(!hideSender){
container2sig = new Uint8Array(containerAB, 0, msgLength - 88);
}
}

var addByte = function(byte){
container[contPos++] = byte;
Expand All @@ -174,7 +178,7 @@ var cryptCore = (function(){
1, // container version;
(hideSender ? 1 : 0) + (hideRecievers ? 2 : 0), //flags
0, // reserved

codedAt & 255, //coding unix_timestamp
(codedAt >> 8) & 255,
(codedAt >> 16) & 255,
Expand All @@ -195,7 +199,7 @@ var cryptCore = (function(){

for (i in contacts) {
if(contacts[i].publicKeyPairPrintable != keyPair.publicKeyPairPrintable)
msgContacts.push(contacts[i].publicKeyPair);
msgContacts.push(contacts[i].publicKeyPair);
}
msgContacts = shuffleArray(msgContacts);
}
Expand All @@ -217,11 +221,11 @@ var cryptCore = (function(){

if(!hideSender){
var sig = keyPair.privateEnc.sign(sjcl.codec.bytes.fromBits(msgHash.update(sjcl.codec.bytes.toBits(container2sig)).finalize())).toDER();

if(sig.length > 80){
throw 'SIGNATURE TO LOONG!!!';
}

if(sig.length < 80){
var add = 80 - sig.length;
for (i = 0; i < add; i++) {
Expand All @@ -230,7 +234,7 @@ var cryptCore = (function(){
}

addBytes(sig);
}
}

var aes_cypher = new sjcl.cipher.aes(sessionKeyBits),
crypted_msg = sjcl.codec.bytes.fromBits(sjcl.mode.ccm.encrypt(aes_cypher, sjcl.codec.bytes.toBits(container), iv, [], 64)),
Expand Down Expand Up @@ -262,7 +266,7 @@ var cryptCore = (function(){
secrets = new Uint8Array(msg, 81),
msgHash = new sjcl.hash.sha256(),
shift = 0, secret, sessionKey = [], ephemeral = [], i, j, aesDecryptor, message = {};

msgHash.update(sjcl.codec.bytes.toBits(ephemAB));
msgHash.update(sjcl.codec.bytes.toBits(iv));

Expand All @@ -273,7 +277,7 @@ var cryptCore = (function(){
ephemeral[0] &= 1;
ephemeral[0] |= 2;

var firstByte = 0xAA;
var firstByte = 0xAA;

try {
secret = getSharedSecret(forBroadcast ? keyPairBroadcast.privateEnc : keyPair.privateEnc, ephemeral);
Expand All @@ -284,7 +288,7 @@ var cryptCore = (function(){
}

while(shift < secrets.byteLength){

if(firstByte != secrets[shift + 31]){
shift += 32;
continue;
Expand Down Expand Up @@ -312,18 +316,18 @@ var cryptCore = (function(){
var crypted_msg = appendBuffer(contHead, new Uint8Array(msg, 81 + 32 * message.contactsNum, message.msgLength - 32));

aes_decypher = new sjcl.cipher.aes(sjcl.codec.bytes.toBits(sessionKey));
try{
try{
res = sjcl.mode.ccm.decrypt(aes_decypher, sjcl.codec.bytes.toBits(crypted_msg), sjcl.codec.bytes.toBits(iv), [], 64);
} catch(e){
return undefined;
return undefined;
}

msgHash.update(sjcl.codec.bytes.toBits(new Uint8Array(msg, 81, 32 * message.contactsNum)));
msgHash.update(sjcl.codec.bytes.toBits(new Uint8Array(msg, 81, 32 * message.contactsNum)));

res = sjcl.codec.bytes.fromBits(res);

if(message.senderHidden){
message.text = utf8ArrToStr(pako.inflateRaw(res.slice(16 + (message.contactsHidden?0:(32 + 33*message.contactsNum))))); //
message.text = utf8ArrToStr(pako.inflateRaw(res.slice(16 + (message.contactsHidden?0:(32 + 33*message.contactsNum))))); //
msgHash.update(sjcl.codec.bytes.toBits(res));
}else{
message.text = utf8ArrToStr(pako.inflateRaw(res.slice(16 + (message.contactsHidden?33:(32 + 33*message.contactsNum)), -80)));
Expand All @@ -341,18 +345,18 @@ var cryptCore = (function(){

for (i = 0; i < message.contactsNum; i++) {
otherSecrets[i] = [];
for (j = 0; j < 32; j++) {
for (j = 0; j < 32; j++) {
otherSecrets[i][j] = secrets[j + i*32] ^ message.sessionKey[j];
}
otherSecrets[i] = arrayBufferDataUri(otherSecrets[i]);
}
}

for (i = 0; i < message.contactsNum; i++) {
pubEncKey = res.slice( 48 + i*33, 33 + 48 + i*33);
tmpSecret = arrayBufferDataUri(getSharedSecret(message.ephemeralPriv, pubEncKey));

if(otherSecrets.indexOf(tmpSecret) == -1){
return undefined;
return undefined;
}

message.msgContacts.push(bs58.enc(res.slice( 48 + i*33, 33 + 48 + i*33)));
Expand All @@ -364,7 +368,7 @@ var cryptCore = (function(){
message.sender = bs58.enc(res.slice(16 + (message.contactsHidden?0:32), 33 + 16 + (message.contactsHidden?0:32)));

if(!pubSigKey.verify(message.msgHash, res.slice(-80))){
return undefined;
return undefined;
}
message.signatureOk = true;
}
Expand All @@ -380,5 +384,15 @@ var cryptCore = (function(){
return null;
};

cryptCore.savePKey = function savePKey(){
if(!keyPair || !keyPair.publicKeyPairPrintable || !keyPair.privateKeyPair){
alert('Nothing to export. Log In first.');
return false;
}

var blob =
saveAs(new Blob([keyPair.privateKeyPair], {type: "text/plain;charset=utf-8"}), keyPair.publicKeyPairPrintable+".privateKey", true);
};

return cryptCore;
})();
26 changes: 26 additions & 0 deletions src/misc/ui.js
Expand Up @@ -23,6 +23,8 @@ var inject_ui = function() {
' <div class="hidbord_contacts hidbord_maincontent" style="display: none"></div>'+
' <div class="hidbord_config hidbord_maincontent" style="display: none">'+
' <div class="hidbord_msg"><h3 style="text-align: center;">Your key:</h3><p id="identi" style="text-align: center;"></p>'+
'<p style="text-align: center; font-size: x-small;"><a href="javascript:;" id="hidboard_key_random">generate random</a> | <a href="javascript:;" id="hidboard_key_export">export private key</a> | <a href="javascript:;" id="hidboard_key_import">import private key</a></p>'+
'<input type="file" style="display:none" id="hidboard_key_import_file">'+
' <form name="loginform" style="margin: 0;">'+
' <table style="margin-left:auto; margin-right:auto; text-align: right;"><tr><td>Password: </td><td><input name="passwd" type="text" value="" style="width: 300px; color: rgb(221, 221, 221); max-width: none;"></td></tr><tr><td>Salt: </td>'+
' <td><input name="magik_num" type="text" value="" style="width: 300px; color: rgb(221, 221, 221); max-width: none;"></td></tr>'+
Expand Down Expand Up @@ -263,9 +265,33 @@ var inject_ui = function() {
steg_iv = [];
});

$('#hidboard_key_random').on('click', function(){do_login(false, false, false, true);});
$('#hidboard_key_export').on('click', function(){cryptCore.savePKey();});
$('#hidboard_key_import').on('click', function(){$('#hidboard_key_import_file').click();});

$('#hidboard_key_import_file').on('change', handleKeySelect);
};

function handleKeySelect(evt) {
"use strict";

var files = evt.target.files,
reader = new FileReader();

reader.onload = function(e) {
var pKey = e.target.result;

if(pKey.length > 100) {
alert('Inkorrect key file!');
return;
}
do_login(false, false, pKey);

};

reader.readAsText(files[0]);
}

var popup_del_timer;

var do_popup = function(e) {
Expand Down

0 comments on commit 302683c

Please sign in to comment.