Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replaced built-in crypto library with @web5/crypto #816

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: return comments to encryption file
  • Loading branch information
Toheeb-Ojuolape committed Oct 21, 2024
commit 7220425ffa392960c5755a445b3a37b1d855b995
28 changes: 22 additions & 6 deletions src/utils/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ import { Readable } from 'readable-stream';
// Compress publicKey for message encryption
eciesjs.ECIES_CONFIG.isEphemeralKeyCompressed = true;

export interface EciesEncryptionOutput {
export type EciesEncryptionOutput = {
ciphertext: Uint8Array;
ephemeralPublicKey: Uint8Array;
initializationVector: Uint8Array;
messageAuthenticationCode: Uint8Array;
}
};

export interface EciesEncryptionInput {
export type EciesEncryptionInput = {
privateKey: Uint8Array;
ephemeralPublicKey: Uint8Array;
initializationVector: Uint8Array;
messageAuthenticationCode: Uint8Array;
ciphertext: Uint8Array;
}
};

/**
* Utility class for performing common, non-DWN specific encryption operations.
*/
export class Encryption {

/**
* Converts a key to base64url encoding
* @param key - Uint8Array to convert
* Encrypts the given plaintext stream using AES-256-CTR algorithm.
*/

public static isEphemeralKeyCompressed: boolean = true; // Set default value

private static toBase64Url(buffer: Buffer): string {
Expand Down Expand Up @@ -89,6 +90,11 @@ export class Encryption {
return cipherStream; // Return the cipher stream
}


/**
* Decrypts the given cipher stream using AES-256-CTR algorithm.
*/

public static async aes256CtrDecrypt(
key: Uint8Array,
initializationVector: Uint8Array,
Expand Down Expand Up @@ -139,6 +145,7 @@ export class Encryption {
const plaintextBuffer = Buffer.from(plaintext);

const cryptogram = eciesjs.encrypt(publicKey, plaintextBuffer);
// split cryptogram returned into constituent parts

let start = 0;
let end = Encryption.isEphemeralKeyCompressed ? 33 : 65;
Expand All @@ -162,6 +169,12 @@ export class Encryption {
};
}

/**
* Decrypt the given plaintext using ECIES (Elliptic Curve Integrated Encryption Scheme)
* with SECP256K1 for the asymmetric calculations, HKDF as the key-derivation function,
* and AES-GCM for the symmetric encryption and MAC algorithms.
*/

public static async eciesSecp256k1Decrypt(
input: EciesEncryptionInput
): Promise<Uint8Array> {
Expand All @@ -173,6 +186,9 @@ export class Encryption {
input.ciphertext,
]);

/**
* Expose eciesjs library configuration
*/
return eciesjs.decrypt(privateKeyBuffer, eciesEncryptionOutput);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/hd-key.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { PrivateJwk, PublicJwk } from '../types/jose-types.js';
import { Encoder } from './encoder.js';
import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
import { Secp256k1 } from './secp256k1.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import type { PrivateJwk, PublicJwk } from '../types/jose-types.js';

export enum KeyDerivationScheme {
/**
Expand Down