Git Product home page Git Product logo

Comments (4)

fisherstevenk avatar fisherstevenk commented on July 28, 2024

Apologies for not getting back to you sooner. If you haven't been able to resolve what's happening, I'll take a look this week.

from crystals-kyber-ts.

fisherstevenk avatar fisherstevenk commented on July 28, 2024

To revisit, each Handshake has two cipher text and secret key pairs that are generated by 2 different methods.

generateCipherTextAndSharedSecret -> Takes the remote public key and generates the "local" cipher text and secret key

When that user sends their cipher text back to the remote user, the remote user would run:

generateRemoteSharedSecret - > Take the remote user's generated cipher text and generates the "remote" cipher text and secret key.

Match ups are: Local cipher text to remote cipher text and local shared secret to remote shared secret. They will match.

Between 2 parties you could generate one pair and use one secret key, OR you could have both parties send their public keys to each other, and each party generates their local cipher and secret, then sends their cipher to the other party, who then generates the remote secret key for that cipher text.

In that case, you now have 2 separate cipher text/secret key pairs. You could always encrypt outgoing messages with your
"local" secret and decode the incoming messages with your "remote" secret. Your comms are still using a symmetric key, however you are using 2 separate keys for the traffic flow.

from crystals-kyber-ts.

fisherstevenk avatar fisherstevenk commented on July 28, 2024

I'll be posting up a repo with what I mean soon. In the meantime, here's an example using crypto-js:

import {Component} from '@angular/core';
import {Kyber1024Handshake} from "crystals-kyber-ts";
import * as CryptoJS from 'crypto-js';

@component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'kyber-ts-example';
public bobHandshake: Kyber1024Handshake;
public aliceHandshake: Kyber1024Handshake;
public alicePrivateKey: number[];
public alicePublicKey: number[];
public aliceCipherText: number[];
public bobPrivateKey: number[];
public bobPublicKey: number[];
public bobCipherText: number[] = [];
public bobRemoteCipherText: number[] = [];
bobRemoteSharedSecret: number[];
public aliceRemoteCipherText: number[] = [];
aliceRemoteSharedSecret: number[];
aliceSharedSecret: number[] = [];
bobSharedSecret: number[] = [];
bobClearText: string = "Testing encrypt and decrypt from Bob";
aliceClearText: string = "Testing encrypt and decrypt from Alice";
bobEncryptedText: string = "";
aliceEncryptedText: string = "";
bobDecryptedText: string = "";
aliceDecryptedText: string = "";
bobCipher: any;
aliceCipher: any;
bobDecipher: any;
aliceDecipher: any;

constructor() {

this.bobHandshake = new Kyber1024Handshake();
this.bobPublicKey = this.bobHandshake.publicKey;
this.bobPrivateKey = this.bobHandshake.privateKey;

this.aliceHandshake = new Kyber1024Handshake();
this.alicePublicKey = this.aliceHandshake.publicKey;
this.alicePrivateKey = this.aliceHandshake.privateKey;

// Bob sent Alice his public key and Alice generates a secret key and cipher text
this.aliceCipherText = this.aliceHandshake.generateCipherTextAndSharedSecret(this.bobPublicKey);
this.aliceSharedSecret = this.aliceHandshake.sharedSecret;
// Alice sends her generated Cipher text to Bob so that Bob can generate the same Secret Key that Alice now has
this.bobRemoteSharedSecret = this.bobHandshake.generateRemoteSharedSecret(this.aliceCipherText);
this.bobRemoteCipherText = this.bobHandshake.remoteCipherText;
// Bob wants to send Alice his message so he encrypts it with the shared secret he generated from Alice's cipher text (which was from his public key)
this.bobEncryptedText = CryptoJS.AES.encrypt(this.bobClearText, this.bobRemoteSharedSecret.toString()).toString();

// Use Alice's hex string secret to decrypt the message received from Bob
const base64String = CryptoJS.AES.decrypt(this.bobEncryptedText, this.aliceHandshake.sharedSecret.toString()).toString(CryptoJS.enc.Utf8);
this.aliceDecryptedText = Buffer.from(base64String).toString('utf-8');
// ==================================================================
// Can use the one shared secret for further comms going back and forth.. or:

// Alice sent Bob her public key and Bob generates a secret key and cipher text
this.bobCipherText = this.bobHandshake.generateCipherTextAndSharedSecret(this.alicePublicKey);
this.bobSharedSecret = this.bobHandshake.sharedSecret;
// Bob sends his generated Cipher text to Alice so that Alice can generate the same Secret Key that Bob now has
this.aliceRemoteSharedSecret = this.aliceHandshake.generateRemoteSharedSecret(this.bobCipherText);
this.aliceRemoteCipherText = this.aliceHandshake.remoteCipherText;
// Alice wants to send Bob her message so she encrypts it with the shared secret she generated from Bob's cipher text (which was from her public key)
this.aliceEncryptedText = CryptoJS.AES.encrypt(this.aliceClearText, this.aliceRemoteSharedSecret.toString()).toString();

// Use Bob's hex string secret to decrypt the message received from Alice
const base64String2 = CryptoJS.AES.decrypt(this.aliceEncryptedText, this.bobHandshake.sharedSecret.toString()).toString(CryptoJS.enc.Utf8);
this.bobDecryptedText = Buffer.from(base64String2).toString('utf-8');

}
}

image

from crystals-kyber-ts.

stanhebben avatar stanhebben commented on July 28, 2024

It is unclear what caused the problem last time, but I couldn't reproduce the issue anymore. Works fine now.

from crystals-kyber-ts.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.