Skip to content

Commit

Permalink
newhope
Browse files Browse the repository at this point in the history
  • Loading branch information
javisantos committed Aug 1, 2020
1 parent 341af00 commit af457ef
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
},
"main": "index.js",
"module": "main.js",

"scripts": {
"test": "tape -r esm test/v1.js | tap-spec",
"coverage": "rimraf node_modules/.cache && nyc --reporter=text-summary --reporter=html tape test/v1.js",
Expand All @@ -31,7 +30,8 @@
"esm": "^3.2.25",
"libsodium-wrappers": "^0.7.6",
"multihashes": "^1.0.1",
"sodium-universal": "^3.0.1"
"sodium-universal": "^3.0.1",
"thunky": "^1.1.0"
},
"devDependencies": {
"@babel/core": "^7.10.4",
Expand Down
33 changes: 29 additions & 4 deletions src/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import _libsodium from 'libsodium-wrappers' // pwhash, convert to curve25519
import sodium from 'sodium-universal'
import * as cbor from '@stablelib/cbor'
import { NewHope } from '@stablelib/newhope'
import { Buffer } from 'buffer'
import multibase from 'multibase'
import canonicalize from 'canonicalize'
import sha512 from 'sha512-wasm' // browser wait for wasm to load
import thunky from 'thunky/promise'

export const PROTOCOL = 'FAYTHE'
export const VERSION = '1'
Expand Down Expand Up @@ -80,10 +82,6 @@ export class Identity {

const keyPair = this.keyPairFor(this.idspace)
this.preRotatedKey = hash(this.keyPairFor(this.idspace, deriveFromKey(stretchedKey, rotation + 1, 'rotation')).publicKey)
// this.did = "A+base64url(publicKey)"
// console.log(encode(keyPair.publicKey), encode(keyPair.publicKey).length, encode(keyPair.publicKey).toString())
// this.inceptionData = [derivation,publicKey, config]
// this.inception = [this.inceptionData, signature]
this.namespace = hashBatch([this.idspace, keyPair.publicKey])
this.verPublicKey = Buffer.from(keyPair.publicKey)
this.verPrivateKey = Buffer.from(keyPair.privateKey) // 64 bytes
Expand All @@ -94,6 +92,29 @@ export class Identity {
else return null
}

offer (id) {
const nh = new NewHope()
const offer = nh.offer()
this.offers.set(id, nh.saveState())
return offer
}

accept (offerMsg, id) {
const nh = new NewHope()
const accept = nh.accept(offerMsg)
this.sharedKeys.set(id, nh.getSharedKey())
return accept
}

finish (acceptMsg, id) {
const nh = new NewHope()
nh.restoreState(this.offers.get(id))
nh.finish(acceptMsg)
this.sharedKeys.set(id, nh.getSharedKey())
this.offers.delete(id)
return nh.getSharedKey()
}

get publicKey () {
return this.verPublicKey
}
Expand Down Expand Up @@ -133,6 +154,10 @@ export class Identity {
}

export async function ready (cb) {
return thunky(await _ready(cb))
}

async function _ready (cb) {
await _libsodium.ready
libsodium = _libsodium
return new Promise(resolve => {
Expand Down

0 comments on commit af457ef

Please sign in to comment.