Skip to content

Commit

Permalink
feat(Crypto): Implement asymetric encoding/decoding using tweennacl a…
Browse files Browse the repository at this point in the history
…nd Ed25519 keypair

Converts a 32-byte Ed25519 public key into a 32-byte Curve25519 public key.Converts a 64-byte
Ed25519 secret key (or just the first 32-byte part of it, which is the secret value) into a 32-byte
Curve25519 secret key

#465
  • Loading branch information
nduchak committed Jun 10, 2019
1 parent f81caf8 commit 65e3cc5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 58 deletions.
49 changes: 17 additions & 32 deletions es/utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import bs58check from 'bs58check'
import * as RLP from 'rlp'
import { blake2b } from 'blakejs'
import ed2curve from 'ed2curve'
import nacl from 'tweetnacl'
import aesjs from 'aes-js'
import { leftPad, rightPad, toBytes } from './bytes'
import shajs from 'sha.js'

import { leftPad, rightPad, toBytes } from './bytes'
import { decode as decodeNode } from '../tx/builder/helpers'

const Ecb = aesjs.ModeOfOperation.ecb
Expand Down Expand Up @@ -664,30 +666,22 @@ export function deserialize (binary, opts = { prettyTags: false }) {
* publicKey such that only the corresponding secretKey will
* be able to decrypt
* @rtype (msg: String, publicKey: String) => Object
* @param {String} msg - Data to encode
* @param {Buffer} msg - Data to encode
* @param {String} publicKey - Public key
* @return {Object}
*/
export function encryptData (msg, {publicKey, secretKey}) {
export function encryptData (msg, publicKey) {
const ephemeralKeyPair = nacl.box.keyPair()
const pubKeyUInt8Array = Buffer.from(decodeBase58Check(assertedType(publicKey, 'ak')))
const msgParamsUInt8Array = Buffer.from(new TextEncoder().encode(msg))
const nonce = Buffer.from(nacl.randomBytes(nacl.box.nonceLength))
const pubKeyUInt8Array = decodeBase58Check(assertedType(publicKey, 'ak'))
const nonce = nacl.randomBytes(nacl.box.nonceLength)

const encryptedMessage = nacl.box(
msgParamsUInt8Array,
Buffer.from(msg),
nonce,
pubKeyUInt8Array,
Buffer.from(ephemeralKeyPair.secretKey)
ed2curve.convertPublicKey(pubKeyUInt8Array),
ephemeralKeyPair.secretKey
)
console.log(ephemeralKeyPair.secretKey.length)
console.log()
console.log(nacl.box.open(
encryptedMessage,
nonce,
ephemeralKeyPair.publicKey,
Buffer.from(secretKey, 'hex')
))

return {
ciphertext: Buffer.from(encryptedMessage).toString('hex'),
ephemPubKey: Buffer.from(ephemeralKeyPair.publicKey).toString('hex'),
Expand All @@ -697,31 +691,22 @@ export function encryptData (msg, {publicKey, secretKey}) {
}

/**
* This function encrypts a message using base58check encoded and 'ak' prefixed
* publicKey such that only the corresponding secretKey will
* be able to decrypt
* @rtype (secretKey: String, publicKey: String) => Object
* This function decrypt a message using secret key
* @rtype (secretKey: String, encryptedData: Object) => Buffer|null
* @param {String} secretKey - Secret key
* @param {Object} encryptedData - Encrypted data
* @return {Object}
* @return {Buffer|null}
*/
export function decryptData (secretKey, encryptedData) {
const receiverSecretKeyUint8Array = Buffer.from(secretKey, 'hex').slice(0, 32)
const receiverSecretKeyUint8Array = ed2curve.convertSecretKey(Buffer.from(secretKey, 'hex'))
const nonce = Buffer.from(encryptedData.nonce, 'hex')
const ciphertext = Buffer.from(encryptedData.ciphertext, 'hex')
const ephemPubKey = Buffer.from(encryptedData.ephemPubKey, 'hex')

console.log(Buffer.from(secretKey, 'hex'))
console.log(receiverSecretKeyUint8Array.length)
console.log(nonce)
console.log(ciphertext)
console.log(ephemPubKey)

const decryptedMessage = nacl.box.open(
const decrypted = nacl.box.open(
ciphertext,
nonce,
ephemPubKey,
receiverSecretKeyUint8Array
)
return decryptedMessage
return decrypted ? Buffer.from(decrypted) : decrypted
}
85 changes: 65 additions & 20 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"blakejs": "^1.1.0",
"bs58check": "^2.1.1",
"commander": "^2.14.1",
"ed2curve": "^0.2.1",
"joi-browser": "^13.4.0",
"json-bigint": "github:davidyuk/json-bigint",
"libsodium-wrappers-sumo": "0.7.3",
Expand Down
Loading

0 comments on commit 65e3cc5

Please sign in to comment.