feat: add sncrypto client side packages

This commit is contained in:
Karol Sójko
2022-07-06 12:21:21 +02:00
parent 9d1f7043e5
commit 6ec66795d2
71 changed files with 9786 additions and 34 deletions

View File

@@ -0,0 +1 @@
dist

View File

@@ -0,0 +1,6 @@
{
"extends": "../../.eslintrc",
"parserOptions": {
"project": "./linter.tsconfig.json"
}
}

View File

@@ -0,0 +1,80 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.9.1](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.9.0...@standardnotes/sncrypto-common@1.9.1) (2022-07-04)
### Bug Fixes
* add missing reflect-metadata package to all packages ([ce3a5bb](https://github.com/standardnotes/snjs/commit/ce3a5bbf3f1d2276ac4abc3eec3c6a44c8c3ba9b))
# [1.9.0](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.8.2...@standardnotes/sncrypto-common@1.9.0) (2022-05-20)
### Features
* authentication with PKCE mechanism ([#719](https://github.com/standardnotes/snjs/issues/719)) ([1bc19b7](https://github.com/standardnotes/snjs/commit/1bc19b79decf83a563d1cf095ee2e56f738152d1))
## [1.8.2](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.8.0...@standardnotes/sncrypto-common@1.8.2) (2022-05-04)
### Bug Fixes
* config package missing dependencies ([3dec12f](https://github.com/standardnotes/snjs/commit/3dec12fa4a83a8aed8419819eafb7c34795cb09f))
## [1.8.1](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.8.0...@standardnotes/sncrypto-common@1.8.1) (2022-05-04)
### Bug Fixes
* config package missing dependencies ([3dec12f](https://github.com/standardnotes/snjs/commit/3dec12fa4a83a8aed8419819eafb7c34795cb09f))
# [1.8.0](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.7.7...@standardnotes/sncrypto-common@1.8.0) (2022-04-28)
### Features
* refactor sncrypto to add unified sha256 and base64 usage ([#715](https://github.com/standardnotes/snjs/issues/715)) ([93aef4d](https://github.com/standardnotes/snjs/commit/93aef4d39228a63f01aa90a88e5d28c3375ed707))
## [1.7.7](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.7.6...@standardnotes/sncrypto-common@1.7.7) (2022-04-22)
**Note:** Version bump only for package @standardnotes/sncrypto-common
## [1.7.6](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.7.5...@standardnotes/sncrypto-common@1.7.6) (2022-04-15)
**Note:** Version bump only for package @standardnotes/sncrypto-common
## [1.7.5](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.7.4...@standardnotes/sncrypto-common@1.7.5) (2022-04-11)
**Note:** Version bump only for package @standardnotes/sncrypto-common
## [1.7.4](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.7.3...@standardnotes/sncrypto-common@1.7.4) (2022-03-31)
**Note:** Version bump only for package @standardnotes/sncrypto-common
## [1.7.3](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.7.1...@standardnotes/sncrypto-common@1.7.3) (2022-02-28)
### Bug Fixes
* add pseudo change to get lerna to trigger ([41e6817](https://github.com/standardnotes/snjs/commit/41e6817bbf726b0932cdf16f58622328b9e42803))
* add pseudo change to get lerna to trigger ([74e8af6](https://github.com/standardnotes/snjs/commit/74e8af640e3d0b8c2f0fc7cf792f4e2cdf33b50c))
## [1.7.2](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.7.2...@standardnotes/sncrypto-common@1.7.2) (2022-02-28)
### Bug Fixes
* add pseudo change to get lerna to trigger ([41e6817](https://github.com/standardnotes/snjs/commit/41e6817bbf726b0932cdf16f58622328b9e42803))
* add pseudo change to get lerna to trigger ([74e8af6](https://github.com/standardnotes/snjs/commit/74e8af640e3d0b8c2f0fc7cf792f4e2cdf33b50c))
## [1.7.1](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.7.0...@standardnotes/sncrypto-common@1.7.1) (2022-02-24)
**Note:** Version bump only for package @standardnotes/sncrypto-common
# [1.7.0](https://github.com/standardnotes/snjs/compare/@standardnotes/sncrypto-common@1.6.0...@standardnotes/sncrypto-common@1.7.0) (2022-02-16)
### Features
* syncronous crypto ([#600](https://github.com/standardnotes/snjs/issues/600)) ([66496f6](https://github.com/standardnotes/snjs/commit/66496f6487630689b76eae6cd15bcb0c31e6b9cc))
# 1.6.0 (2022-01-14)
### Features
* move sncrypto packages to snjs monorepo ([#554](https://github.com/standardnotes/snjs/issues/554)) ([db83991](https://github.com/standardnotes/snjs/commit/db8399190d9d10fdc31060568b836c62933fd525))

View File

@@ -0,0 +1,20 @@
# SNCrypto Common
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)
Cryptographic primitives used by [SNJS](https://github.com/standardnotes/snjs).
## Installing
```
yarn add @standardnotes/sncrypto-common
```
## Supported Algorithms
- Argon2id (Libsodium.js)
- XChaCha20+Poly1305 (Libsodium.js)
- PBDKF2 (WebCrypto)
- AES-CBC (WebCrypto)
- HMAC SHA-256
- SHA256

View File

@@ -0,0 +1,11 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const base = require('../../node_modules/@standardnotes/config/src/jest.json');
module.exports = {
...base,
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
},
}
};

View File

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["dist"]
}

View File

@@ -0,0 +1,34 @@
{
"name": "@standardnotes/sncrypto-common",
"version": "1.10.0",
"engines": {
"node": ">=16.0.0 <17.0.0"
},
"description": "SNCrypto common",
"main": "dist/index.js",
"author": "Standard Notes",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"license": "AGPL-3.0-or-later",
"publishConfig": {
"access": "public"
},
"scripts": {
"clean": "rm -fr dist",
"prebuild": "yarn clean",
"build": "tsc -p tsconfig.json",
"watch": "tsc -p tsconfig.json --watch",
"lint": "eslint . --ext .ts",
"test:unit": "yarn lint"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.30.0",
"@typescript-eslint/parser": "^5.12.1",
"eslint-plugin-prettier": "^4.2.1"
},
"dependencies": {
"reflect-metadata": "^0.1.13"
}
}

View File

@@ -0,0 +1,17 @@
import { Base64String } from '../Types/Base64String'
import { HexString } from '../Types/HexString'
/**
* @param iv initialization vector as a hex string
* @param tag authentication tag as a hex string
* @param ciphertext as a base64 string
* @param encoding that will be applied after decrypting
* @param aad additional authenticated data as a hex string
*/
export type Aes256GcmEncrypted<EncodingType> = {
iv: HexString
tag: HexString
ciphertext: Base64String
encoding: EncodingType
aad: HexString
}

View File

@@ -0,0 +1,15 @@
import { HexString } from '../Types/HexString'
import { Unencrypted } from '../Types/Unencrypted'
/**
* @param unencrypted -- UTF-8 string or a `string` with `encoding`
* @param iv initialization vector as a hex string
* @param key encryption key as a hex string
* @param aad additional authenticated data as a hex string
*/
export type Aes256GcmInput<EncodingType> = {
unencrypted: Unencrypted<EncodingType>
iv: HexString
key: HexString
aad?: HexString
}

View File

@@ -0,0 +1,20 @@
import { HexString } from '../Types/HexString'
import { Aes256GcmEncrypted } from './Aes256GcmEncrypted'
import { Aes256GcmInput } from './Aes256GcmInput'
export interface CryptoAes256GcmInterface<EncodingType> {
/**
* Encrypts a string using AES-GCM.
* @param input
* @returns An object which can be run through aes256GcmDecrypt to retrieve the input text.
*/
aes256GcmEncrypt(input: Aes256GcmInput<EncodingType>): Promise<Aes256GcmEncrypted<EncodingType>>
/**
* Decrypts a string using AES-GCM.
* @param encrypted
* @param key - encryption key as a hex string
* @returns A string encoded with encoding provided in the input
*/
aes256GcmDecrypt(encrypted: Aes256GcmEncrypted<EncodingType>, key: HexString): Promise<string>
}

View File

@@ -0,0 +1,3 @@
export * from './Aes256GcmEncrypted'
export * from './Aes256GcmInput'
export * from './CryptoAes256GcmInterface'

View File

@@ -0,0 +1,25 @@
import { Base64String } from '../Types/Base64String'
import { Utf8String } from '../Types/Utf8String'
export interface CryptoBase64Interface {
/**
* Converts a plain string into base64
* @param text - A plain string
* @returns A base64 encoded string
*/
base64Encode(text: Utf8String): Base64String
/**
* Converts a plain string into url-safe base64
* @param text - A plain string
* @returns A base64 encoded string
*/
base64URLEncode(text: Utf8String): Base64String
/**
* Converts a base64 string into a plain string
* @param base64String - A base64 encoded string
* @returns A plain string
*/
base64Decode(base64String: Base64String): Utf8String
}

View File

@@ -0,0 +1 @@
export * from './CryptoBase64Interface'

View File

@@ -0,0 +1,200 @@
import { Base64String } from '../Types/Base64String'
import { Base64URLSafeString } from '../Types/Base64URLSafeString'
import { HexString } from '../Types/HexString'
import { SodiumConstant } from '../Types/SodiumConstant'
import { StreamDecryptor } from '../Types/StreamDecryptor'
import { StreamEncryptor } from '../Types/StreamEncryptor'
import { Utf8String } from '../Types/Utf8String'
/**
* Interface that clients have to implement to use snjs
*/
export interface PureCryptoInterface {
initialize(): Promise<void>
/**
* Derives a key from a password and salt using PBKDF2 via WebCrypto.
* @param password - utf8 string
* @param salt - utf8 string
* @param iterations
* @param length - In bits
* @returns Hex string
*/
pbkdf2(password: Utf8String, salt: Utf8String, iterations: number, length: number): Promise<string | null>
/**
* Generates a random key in hex format
* @param bits - Length of key in bits
* @returns A string key in hex format
*/
generateRandomKey(bits: number): string
/**
* @legacy
* Encrypts a string using AES-CBC via WebCrypto.
* @param plaintext
* @param iv - In hex format
* @param key - In hex format
* @returns Ciphertext in Base64 format.
*/
aes256CbcEncrypt(plaintext: Utf8String, iv: HexString, key: HexString): Promise<Base64String>
/**
* @legacy
* Decrypts a string using AES-CBC via WebCrypto.
* @param ciphertext - Base64 format
* @param iv - In hex format
* @param key - In hex format
* @returns Plain utf8 string or null if decryption fails
*/
aes256CbcDecrypt(ciphertext: Base64String, iv: HexString, key: HexString): Promise<Utf8String | null>
/**
* Runs HMAC with SHA-256 on a message with key.
* @param message - Plain utf8 string
* @param key - In hex format
* @returns Hex string or null if computation fails
*/
hmac256(message: Utf8String, key: HexString): Promise<HexString | null>
/**
* @param text - Plain utf8 string
* @returns Hex string
*/
sha256(text: string): Promise<string>
/**
* Runs HMAC with SHA-1 on a message with key.
* @param message - Plain utf8 string
* @param key - In hex format
* @returns Hex string or null if computation fails
*/
hmac1(message: Utf8String, key: HexString): Promise<HexString | null>
/**
* Use only for legacy applications.
* @param text - Plain utf8 string
* @returns Hex string
*/
unsafeSha1(text: string): Promise<string>
/**
* Derives a key from a password and salt using
* argon2id (crypto_pwhash_ALG_DEFAULT).
* @param password - Plain text string
* @param salt - Salt in hex format
* @param iterations - The algorithm's opslimit (recommended min 2)
* @param bytes - The algorithm's memory limit (memlimit) (recommended min 67108864)
* @param length - The output key length
* @returns Derived key in hex format
*/
argon2(password: Utf8String, salt: HexString, iterations: number, bytes: number, length: number): HexString
/**
* Encrypt a message (and associated data) with XChaCha20-Poly1305.
* @param plaintext
* @param nonce - In hex format
* @param key - In hex format
* @param assocData
* @returns Base64 ciphertext string
*/
xchacha20Encrypt(plaintext: Utf8String, nonce: HexString, key: HexString, assocData: Utf8String): Base64String
/**
* Decrypt a message (and associated data) with XChaCha20-Poly1305
* @param ciphertext
* @param nonce - In hex format
* @param key - In hex format
* @param assocData
* @returns Plain utf8 string or null if decryption fails
*/
xchacha20Decrypt(
ciphertext: Base64String,
nonce: HexString,
key: HexString,
assocData: Utf8String | Uint8Array,
): Utf8String | null
xchacha20StreamInitEncryptor(key: HexString): StreamEncryptor
xchacha20StreamEncryptorPush(
encryptor: StreamEncryptor,
plainBuffer: Uint8Array,
assocData: Utf8String,
tag?: SodiumConstant,
): Uint8Array
xchacha20StreamInitDecryptor(header: Base64String, key: HexString): StreamDecryptor
xchacha20StreamDecryptorPush(
decryptor: StreamDecryptor,
encryptedBuffer: Uint8Array,
assocData: Utf8String,
): { message: Uint8Array; tag: SodiumConstant } | false
/**
* Converts a plain string into base64
* @param text - A plain string
* @returns A base64 encoded string
*/
base64Encode(text: Utf8String): Base64String
/**
* Converts a plain string into url safe base64
* @param text - A plain string
* @returns A base64 url safe encoded string
*/
base64URLEncode(text: Utf8String): Base64URLSafeString
/**
* Converts a base64 string into a plain string
* @param base64String - A base64 encoded string
* @returns A plain string
*/
base64Decode(base64String: Base64String): Utf8String
deinit(): void
/**
* Generates a UUID string syncronously.
*/
generateUUID(): string
/**
* Constant-time string comparison
* @param a
* @param b
*/
timingSafeEqual(a: string, b: string): boolean
/**
* Generates a random secret for TOTP authentication
*
* RFC4226 reccomends a length of at least 160 bits = 32 b32 chars
* https://datatracker.ietf.org/doc/html/rfc4226#section-4
*/
generateOtpSecret(): Promise<string>
/**
* Generates a HOTP code as per RFC4226 specification
* using HMAC-SHA1
* https://datatracker.ietf.org/doc/html/rfc4226
*
* @param secret OTP shared secret
* @param counter HOTP counter
* @returns HOTP auth code
*/
hotpToken(secret: string, counter: number, tokenLength: number): Promise<string>
/**
* Generates a TOTP code as per RFC6238 specification
* using HMAC-SHA1
* https://datatracker.ietf.org/doc/html/rfc6238
*
* @param secret OTP shared secret
* @param timestamp time specified in milliseconds since UNIX epoch
* @param step time step specified in seconds
* @returns TOTP auth code
*/
totpToken(secret: string, timestamp: number, tokenLength: number, step: number): Promise<string>
}

View File

@@ -0,0 +1,22 @@
/**
* Constant-time string comparison
* @param a
* @param b
*/
export function timingSafeEqual(a: string, b: string): boolean {
const strA = String(a)
let strB = String(b)
const lenA = strA.length
let result = 0
if (lenA !== strB.length) {
strB = strA
result = 1
}
for (let i = 0; i < lenA; i++) {
result |= strA.charCodeAt(i) ^ strB.charCodeAt(i)
}
return result === 0
}

View File

@@ -0,0 +1,2 @@
export * from './PureCryptoInterface'
export * from './Utils'

View File

@@ -0,0 +1,6 @@
import { HexString } from '../Types/HexString'
import { Utf8String } from '../Types/Utf8String'
export interface CryptoSha256Interface {
sha256(text: Utf8String): HexString
}

View File

@@ -0,0 +1 @@
export * from './CryptoSha256Interface'

View File

@@ -0,0 +1 @@
export type Base64String = string

View File

@@ -0,0 +1 @@
export type Base64URLSafeString = string

View File

@@ -0,0 +1 @@
export type HexString = string

View File

@@ -0,0 +1,11 @@
export enum SodiumConstant {
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_STATEBYTES = 52,
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17,
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24,
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32,
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0,
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1,
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2,
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3,
CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80,
}

View File

@@ -0,0 +1 @@
export type SodiumStateAddress = unknown

View File

@@ -0,0 +1,5 @@
import { SodiumStateAddress } from './SodiumStateAddress'
export type StreamDecryptor = {
state: SodiumStateAddress
}

View File

@@ -0,0 +1,6 @@
import { SodiumConstant } from './SodiumConstant'
export type StreamDecryptorResult = {
message: Uint8Array
tag: SodiumConstant
}

View File

@@ -0,0 +1,7 @@
import { Base64String } from './Base64String'
import { SodiumStateAddress } from './SodiumStateAddress'
export type StreamEncryptor = {
state: SodiumStateAddress
header: Base64String
}

View File

@@ -0,0 +1,6 @@
import { Utf8String } from './Utf8String'
/**
* Either a plaintext (UTF-8 string) or a `string` with an `encoding`.
*/
export type Unencrypted<EncodingType> = Utf8String | { string: string; encoding: EncodingType }

View File

@@ -0,0 +1 @@
export type Utf8String = string

View File

@@ -0,0 +1,10 @@
export * from './Base64String'
export * from './Base64URLSafeString'
export * from './HexString'
export * from './SodiumConstant'
export * from './SodiumStateAddress'
export * from './StreamDecryptor'
export * from './StreamDecryptorResult'
export * from './StreamEncryptor'
export * from './Unencrypted'
export * from './Utf8String'

View File

@@ -0,0 +1,5 @@
export * from './AES-GCM'
export * from './Base64'
export * from './Common'
export * from './SHA'
export * from './Types'

View File

@@ -0,0 +1,13 @@
{
"extends": "../../node_modules/@standardnotes/config/src/tsconfig.json",
"compilerOptions": {
"skipLibCheck": true,
"rootDir": "./src",
"outDir": "./dist",
},
"include": [
"src/**/*"
],
"references": [],
"exclude": ["**/*.spec.ts", "dist", "node_modules"]
}