This repository has been archived by the owner on Feb 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from anvilresearch/ibudea-aes-gcm-alg
JWE with AES-GCM
- Loading branch information
Showing
24 changed files
with
1,333 additions
and
1,585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const crypto = require('@trust/webcrypto') | ||
const { JWT } = require('../src') | ||
const base64url = require('base64url') | ||
|
||
|
||
let key | ||
let plaintext = "The true sign of intelligence is not knowledge but imagination." | ||
let protectedHeader = {alg: "dir", enc: "A256GCM"} | ||
|
||
crypto.subtle | ||
|
||
// use webcrypto to generate a keypair | ||
.generateKey( | ||
{ | ||
name: 'AES-GCM', | ||
length: 256 | ||
}, | ||
false, | ||
['encrypt', 'decrypt'] | ||
) | ||
|
||
// use key with JWA to encrypt | ||
.then(result => { | ||
key = result | ||
console.log(key) | ||
// this should give me back a JWE | ||
return JWT.encrypt({ | ||
protected: protectedHeader, | ||
key, | ||
plaintext, | ||
serialization: 'compact' | ||
}) | ||
}) | ||
|
||
// print the ciphertext | ||
.then(jwe => { | ||
console.log(jwe) | ||
// JWT.decrypt('A128GCM', key, ciphertext, iv), | ||
return JWT.decrypt({key, serialized: jwe }) | ||
}) | ||
|
||
.then(console.log) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
'use strict' | ||
/** | ||
* Dependencies | ||
*/ | ||
const crypto = require('@trust/webcrypto') | ||
const NotSupportedError = require('./errors/NotSupportedError') | ||
const { JWA } = require('@trust/jwa') | ||
|
||
/** | ||
* KeyManagement | ||
*/ | ||
class KeyManagement { | ||
|
||
/** | ||
* constructor | ||
*/ | ||
constructor () { | ||
// Entries for key algorithms used to decide on | ||
// cek and compute the encrypted key | ||
this.keyAlgorithms = new Map([ | ||
['dir', { encrypt: this.direct, decrypt: this.direct }] | ||
]) | ||
} | ||
|
||
direct (alg, key) { | ||
return { | ||
cek: key, | ||
encrypted_key: new Uint8Array() | ||
} | ||
} | ||
|
||
keyWrapOrEncrypt (alg, key) { | ||
let cek, encrypted_key | ||
cek = new Uint8Array(this.keyAlgorithms.get(alg).cekLength) | ||
cek = crypto.getRandomValues(cek) | ||
JWA.encryptKey(alg, cek, key) | ||
.then(result => { | ||
encrypted_key = result | ||
|
||
return { | ||
cek, | ||
encrypted_key | ||
} | ||
}) | ||
} | ||
|
||
keyAgreeAndWrap (alg, key) { | ||
let cek = new Uint8Array(this.keyAlgorithms.get(alg).cekLength) | ||
cek = crypto.getRandomValues(cek) | ||
// use alg to agree on the key | ||
JWA.generateKey(alg) | ||
.then(agreedKey => { | ||
JWA.encryptKey(alg, cek, agreedKey) | ||
.then(result => { | ||
encrypted_key = result | ||
|
||
return { | ||
cek, | ||
encrypted_key | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
directAgree (alg, key) { | ||
JWA.generateKey(alg) | ||
.then(agreedKey => { | ||
return { | ||
cek: agreedKey, | ||
encrypted_key: new Uint8Array() | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* determineCek | ||
* | ||
* @description | ||
* Call the corresponding method for the algorithm type | ||
* based on JWA alg name | ||
* | ||
* @param {Boolean} verify | ||
* @param {Object} alg | ||
* @param {Object} key | ||
* | ||
* @returns {Promise} | ||
*/ | ||
determineCek (verify, alg, key) { | ||
if (!this.keyAlgorithms.get(alg)) { | ||
throw new NotSupportedError("Key Algorithm is not supported") | ||
} | ||
if (!verify) { | ||
return (this.keyAlgorithms.get(alg).encrypt)(alg, key) | ||
} else { | ||
return (this.keyAlgorithms.get(alg).decrypt)(alg, key) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Export | ||
*/ | ||
module.exports = KeyManagement |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.