-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
117 additions
and
28 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
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
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,3 @@ | ||
import * as integrated from './integrated-encryption' | ||
|
||
export { integrated } |
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,41 @@ | ||
import * as jose from "jose"; | ||
|
||
import { prepareSenderContext } from "../prepareSenderContext"; | ||
import { prepareRecipientHeader } from "../prepareRecipientHeader"; | ||
|
||
import * as aead from '../jwe/aead' | ||
|
||
import { prepareRecipientContext } from '../prepareRecipientContext' | ||
|
||
export const encrypt = async (plaintext: Uint8Array, publicKeyJwk: any, options?: any): Promise<any> => { | ||
const sender = await prepareSenderContext(publicKeyJwk, options) | ||
const header = await prepareRecipientHeader(publicKeyJwk, options) | ||
const encrypted_key = jose.base64url.encode(new Uint8Array(sender.enc)) | ||
const protectedHeader = jose.base64url.encode(JSON.stringify(header)) | ||
const encodedAad = options.additionalAuthenticatedData ? jose.base64url.encode(options.additionalAuthenticatedData) : undefined | ||
const aad = aead.prepareJweAad(protectedHeader, encodedAad) | ||
const ciphertext = jose.base64url.encode(new Uint8Array(await sender.seal(plaintext, aad))); | ||
const encrypted = { | ||
protected: protectedHeader, | ||
encrypted_key, | ||
ciphertext, | ||
} as any | ||
if (options.additionalAuthenticatedData){ | ||
encrypted.aad = encodedAad | ||
} | ||
return encrypted | ||
} | ||
|
||
|
||
export const decrypt = async (encrypted: any, privateKeyJwk: any, options?: any): Promise<any> => { | ||
const header = JSON.parse(new TextDecoder().decode(jose.base64url.decode(encrypted.protected))) | ||
const ek = jose.base64url.decode(encrypted.encrypted_key) | ||
const context = await prepareRecipientContext(privateKeyJwk, ek, options) | ||
const aad = aead.prepareJweAad(encrypted.protected, encrypted.aad) | ||
const plaintext = await context.open(jose.base64url.decode(encrypted.ciphertext), aad) | ||
return { | ||
protectedHeader: header, | ||
plaintext: new Uint8Array(plaintext), | ||
additionalAuthenticatedData: jose.base64url.decode(encrypted.aad) | ||
} | ||
} |
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,49 @@ | ||
import { jose as hpke } from '../src' | ||
// import * as jose from 'jose' | ||
|
||
it('HPKE Integrated Encryption, Auth Mode, PSK and AAD', async () => { | ||
const privateKey = { | ||
"kid": "urn:ietf:params:oauth:jwk-thumbprint:sha-256:S6AXfdU_6Yfzvu0KDDJb0sFuwnIWPk6LMTErYhPb32s", | ||
"alg": "HPKE-P256-SHA256-A128GCM", | ||
"kty": "EC", | ||
"crv": "P-256", | ||
"x": "wt36K06T4T4APWfGtioqDBXCvRN9evqkZjNydib9MaM", | ||
"y": "eupgedeE_HAmVJ62kpSt2_EOoXb6e0y2YF1JPlfr1-I", | ||
"d": "O3KznUTAxw-ov-9ZokwNaJ289RgP9VxQc7GJthaXzWY" | ||
} | ||
const publicKey = await hpke.jwk.publicFromPrivate(privateKey) | ||
const pskid = new TextEncoder().encode("our-pre-shared-key-id") | ||
const psk = new TextEncoder().encode("jugemujugemugokounosurikirekaija") | ||
const plaintext = new TextEncoder().encode(`🖤 this plaintext!`) | ||
const additionalAuthenticatedData = new TextEncoder().encode('🏴☠️ beware the aad!') | ||
const commonOptions = { | ||
keyManagementParameters: { | ||
psk: { | ||
id: pskid, | ||
key: psk, | ||
} | ||
} | ||
} | ||
const encryptOptions = { | ||
additionalAuthenticatedData, | ||
senderPrivateKey: privateKey, | ||
recipientPublicKey: publicKey, | ||
...commonOptions | ||
} | ||
const encrypted = await hpke.modes.integrated.encrypt(plaintext, publicKey, encryptOptions) | ||
const decryptOptions = { | ||
senderPublicKey: publicKey, | ||
recipientPrivateKey: privateKey, | ||
...commonOptions | ||
} | ||
const decrypted = await hpke.modes.integrated.decrypt(encrypted, privateKey, decryptOptions) | ||
expect(new TextDecoder().decode(decrypted.plaintext)).toBe('🖤 this plaintext!') | ||
expect(new TextDecoder().decode(decrypted.additionalAuthenticatedData)).toBe('🏴☠️ beware the aad!') | ||
expect(decrypted.protectedHeader).toBeDefined() | ||
// console.log(JSON.stringify({ | ||
// "kty":"oct", | ||
// "kid": "our-pre-shared-key-id", | ||
// k: jose.base64url.encode(psk) | ||
// }, null, 2)) | ||
// console.log(JSON.stringify(encrypted, null, 2)) | ||
}) |
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