All files storage-encryption.ts

94% Statements 47/50
85.71% Branches 12/14
100% Functions 8/8
94% Lines 47/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                        2x 2x 2x 2x 2x 2x 2x   2x 2x             221x 221x       221x       98x 98x 98x   98x 98x   98x 98x   98x       104x   104x 1x     103x 103x       103x 103x 103x       103x   103x       103x 103x   103x 103x       78x 78x 78x             42x       2x 201x 1x           200x 1x             2x 201x 201x 201x    
/*
 * This file is part of midnight-js.
 * Copyright (C) 2025 Midnight Foundation
 * SPDX-License-Identifier: Apache-2.0
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { Buffer } from 'buffer';
import { createCipheriv, createDecipheriv, pbkdf2Sync, randomBytes } from 'crypto';
 
export type PrivateStoragePasswordProvider = () => string | Promise<string>;
 
const ALGORITHM = 'aes-256-gcm';
const KEY_LENGTH = 32;
const IV_LENGTH = 12;
const AUTH_TAG_LENGTH = 16;
const SALT_LENGTH = 32;
const PBKDF2_ITERATIONS = 100000;
const ENCRYPTION_VERSION = 1;
 
const VERSION_PREFIX_LENGTH = 1;
const HEADER_LENGTH = VERSION_PREFIX_LENGTH + SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH;
 
export class StorageEncryption {
  private readonly encryptionKey: Buffer;
  private readonly salt: Buffer;
 
  constructor(password: string, existingSalt?: Buffer) {
    this.salt = existingSalt ?? randomBytes(SALT_LENGTH);
    this.encryptionKey = this.deriveKey(password, this.salt);
  }
 
  private deriveKey(password: string, salt: Buffer): Buffer {
    return pbkdf2Sync(password, salt, PBKDF2_ITERATIONS, KEY_LENGTH, 'sha256');
  }
 
  encrypt(data: string): string {
    const plaintext = Buffer.from(data, 'utf-8');
    const iv = randomBytes(IV_LENGTH);
    const cipher = createCipheriv(ALGORITHM, this.encryptionKey, iv);
 
    const encrypted = Buffer.concat([cipher.update(plaintext), cipher.final()]);
    const authTag = cipher.getAuthTag();
 
    const version = Buffer.from([ENCRYPTION_VERSION]);
    const result = Buffer.concat([version, this.salt, iv, authTag, encrypted]);
 
    return result.toString('base64');
  }
 
  decrypt(encryptedData: string): string {
    const data = Buffer.from(encryptedData, 'base64');
 
    if (data.length < HEADER_LENGTH) {
      throw new Error('Invalid encrypted data: too short');
    }
 
    const version = data[0];
    Iif (version !== ENCRYPTION_VERSION) {
      throw new Error(`Unsupported encryption version: ${version}`);
    }
 
    const salt = data.subarray(VERSION_PREFIX_LENGTH, VERSION_PREFIX_LENGTH + SALT_LENGTH);
    const iv = data.subarray(VERSION_PREFIX_LENGTH + SALT_LENGTH, VERSION_PREFIX_LENGTH + SALT_LENGTH + IV_LENGTH);
    const authTag = data.subarray(
      VERSION_PREFIX_LENGTH + SALT_LENGTH + IV_LENGTH,
      VERSION_PREFIX_LENGTH + SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH
    );
    const encrypted = data.subarray(HEADER_LENGTH);
 
    Iif (!this.salt.equals(salt)) {
      throw new Error('Salt mismatch: data was encrypted with a different password');
    }
 
    const decipher = createDecipheriv(ALGORITHM, this.encryptionKey, iv);
    decipher.setAuthTag(authTag);
 
    const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
    return decrypted.toString('utf-8');
  }
 
  static isEncrypted(data: string): boolean {
    try {
      const buffer = Buffer.from(data, 'base64');
      return buffer.length >= HEADER_LENGTH && buffer[0] === ENCRYPTION_VERSION;
    } catch {
      return false;
    }
  }
 
  getSalt(): Buffer {
    return this.salt;
  }
}
 
const validatePassword = (password: string): void => {
  if (!password) {
    throw new Error(
      'Password is required for private state encryption.\n' +
      'Please provide a password via privateStoragePasswordProvider in the configuration.'
    );
  }
 
  if (password.length < 16) {
    throw new Error(
      'Password must be at least 16 characters long.\n' +
      'Use a strong, randomly generated password for production.'
    );
  }
};
 
export const getPasswordFromProvider = async (provider: PrivateStoragePasswordProvider): Promise<string> => {
  const password = await provider();
  validatePassword(password);
  return password;
};