Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation and type clarification #68

Merged
merged 21 commits into from
Mar 5, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ts derivefromrid, removed generaterandnum function
  • Loading branch information
lucyq committed Feb 28, 2018
commit 2f42d5b8f7c740effb17ac9a40c47ab30aef270b
72 changes: 36 additions & 36 deletions client/app/services/crypto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,7 @@ function symmetricEncrypt(key, msg) {
return encrypted;
}

function generateRandNum() {
return Math.floor(Math.random() * 10);
}

function deriveFromRid(hexRid) {

const ridLen = hexRid.length;

const slope = bigInt(hexRid.substr(0, ridLen / 2), HEX);

// hashing it to make it conform to key size: 32 bytes
const kId = sodium.crypto_generichash(sodium.crypto_generichash_BYTES, hexRid.substr(ridLen / 2, ridLen));

return {slope, kId};
}

// Y is a bigInt number
function encryptSecretValue(y) {
Expand Down Expand Up @@ -128,22 +114,6 @@ function symmetricDecrypt(key, cipherText) {
}


function decryptRecords(data, rid) {

const decryptedRecords = [];
const derived = deriveFromRid(rid.toString(HEX));

for (let i = 0; i < data.length; i++) {
const encryptedRecord = data[i].encryptedRecord;

const decryptedRecordKey = symmetricDecrypt(data[i].kId, data[i].encryptedRecordKey);
const decryptedRecord = symmetricDecrypt(decryptedRecordKey, encryptedRecord);
const dStr = new encoding.TextDecoder("utf-8").decode(decryptedRecord);
decryptedRecords.push(JSON.parse(dStr));
}
return decryptedRecords;
}


// decrypt Y values
function decryptSecretValues(data) {
Expand Down Expand Up @@ -204,11 +174,26 @@ export class CryptoService {
* ENCRYPTION
*/

/* Used in both encryption and decryption to derive a slope and key for a unique RID
hexRid - hex string
*/
private deriveFromRid(hexRid: string) {

const ridLen = hexRid.length;

const slope = bigInt(hexRid.substr(0, ridLen / 2), HEX);

// hashing it to make it conform to key size: 32 bytes
const kId = sodium.crypto_generichash(sodium.crypto_generichash_BYTES, hexRid.substr(ridLen / 2, ridLen));

return {slope, kId};
}

// rid: base64 string
private generateDataValues(rid: string, userId: string, record: object) {
const prgRid = sodium.to_hex(sodium.crypto_hash(sodium.from_base64(rid)));

const derived = deriveFromRid(prgRid);
const derived = this.deriveFromRid(prgRid);
const hashedUserId = bigInt(sodium.to_hex(sodium.crypto_hash(userId)), HEX);
const intRid = bigInt(prgRid, HEX);

Expand Down Expand Up @@ -280,10 +265,6 @@ export class CryptoService {







/*
* DECRYPTION
*/
Expand Down Expand Up @@ -323,9 +304,28 @@ export class CryptoService {
const strRid = getIntercept(coordA, slope);

return {
decryptedRecords: decryptRecords(data, strRid.toString(HEX)),
decryptedRecords: this.decryptRecords(data, strRid.toString(HEX)),
slope,
strRid,
};
}

/* */
private decryptRecords(data: Array<EncryptedData>, rid) {

const decryptedRecords = [];
const derived = this.deriveFromRid(rid.toString(HEX));

for (let i = 0; i < data.length; i++) {
const encryptedRecord = data[i].encryptedRecord;

const decryptedRecordKey = symmetricDecrypt(data[i].kId, data[i].encryptedRecordKey);
const decryptedRecord = symmetricDecrypt(decryptedRecordKey, encryptedRecord);
const dStr = new encoding.TextDecoder("utf-8").decode(decryptedRecord);
decryptedRecords.push(JSON.parse(dStr));
}
return decryptedRecords;
}


}