Giter Site home page Giter Site logo

jas- / kruptein Goto Github PK

View Code? Open in Web Editor NEW
6.0 4.0 1.0 431 KB

crypto; from kruptein to hide or conceal

License: MIT License

JavaScript 100.00%
crypto symmetric-key-cryptography symmetric-cryptography symmetric-encryption scrypt key-derivation encryption decryption encrypt decrypt

kruptein's Introduction

kruptein 🐿️

crypto; from kruptein to hide or conceal.

npm Downloads Known Vulnerabilities Build Status

Sandbox

To test the module feel free to use the sandbox

Install

To install npm install kruptein

Methods

  • .set(secret, plaintext, [aad], callback)
  • .get(secret, ciphertext, [{at: auth_tag, aad: aad}], callback)

Options

Industry standards are used for the algorithm, hashing algorithm, key & IV sizes. The default key derivation is pbkdf2, however use of the scrypt derivation function can be enabled.

  • algorithm: (Optional) Cipher algorithm from crypto.getCiphers(). Default: aes-256-gcm.
  • hashing: (Optional) Hash algorithm from crypto.getHashes(). Default: sha512.
  • encodeas: (Optional) Output encoding. Currently supports binary, hex, & base64. Default: base64.
  • key_size: (Optional) Key size bytes (should match block size of algorithm). Default: 32
  • iv_size: (Optional) IV size bytes. Default: 16.
  • at_size: (Optional) Authentication tag size. Applicable to gcm & ocb cipher modes. Default: 128.
  • use_scrypt: (Optional) Use .scrypt() to derive a key. Requires node > v10. Default/Fallback: .pbkdf2().
  • use_asn1: (Optional) Disable the default ASN.1 encoding. Default: true

Usage

When selecting an algorithm from crypto.getCiphers() the iv and key_size values are calculated auto-magically to make implementation easy.

You can always define your own if the defaults per algorithm and mode aren't what you would like; see the options section above.

Create ciphertext from plaintext

To create a new ciphertext object. Run example to encrypt plaintext.

const kruptein = require("kruptein")(opts);
let secret = "squirrel";

kruptein.set(secret, "Operation mincemeat was an example of deception", (err, ct) => {
  if (err)
    throw err;

  console.log(ct);
});

Get plaintext from ciphertext

To retrieve plaintext from a ciphertext object. Run example to decrypt ciphertext.

const kruptein = require("kruptein")(opts);
let ciphertext, secret = "squirrel";

kruptein.get(secret, ciphertext, (err, pt) => {
  if (err)
    throw err;

  console.log(pt);
});

Output

The .set() method output depends on three factors; the encodeas, algorithm and use_asn1.

For any algorithm that supports authentication (AEAD), the object structure includes the Authentication Tag and the Additional Authentication Data attribute and value.

When the use_asn1 option is enabled (default is true), the result is an ASN.1 value using the encodeas value. While this is a more complex encoding option, it helps standardize & minimize the resulting ciphertext output.

Test harness

The included test harness, invoked with npm test, makes every attempt to trap and handle errors. Some of which come from side channel or possible malability of the resultant ciphertext.

This can be seen within the test/index.js CI test harness under the HMAC, AT & AAD validation test cases.

An online playgound for experimenting with the module can also be found here

Cryptography References

This module conforms to industry recommendations regarding algorithm type, mode, key size, iv size & implementation, digests, key derivation & management etc. References used provided here:

RFC:

  • RFC 2104: HMAC: Keyed-Hashing for Message Authentication
  • RFC 4086: Randomness Requirements for Security
  • RFC 5084: Using AES-CCM and AES-GCM Authenticated Encryption
  • RFC 7914: The scrypt Password-Based Key Derivation Function
  • RFC 8018: Password-Based Cryptography Specification
  • X.697: ASN.1 encoding rules: Specifications of JavaScript Object Notation Encoding Rules (JER)

NIST:

  • SP 800-38A: Block cipher modes of operation
  • SP 800-38B: Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
  • SP 800-57P1: Recommendations for key management
  • SP 800-107: Recommendation for Applications Using Approved Hash Algorithms
  • SP 800-108: Recommendation for Key Derivation Using Pseudorandom Functions
  • SP 800-131A: Transitioning the Use of Cryptographic Algorithms and Key Lengths
  • SP 800-132: Recommendation for Password-Based Key Derivation
  • SP 800-175B: Guideline for Using Cryptographic Standards in the Federal Government

FIPS:

  • FIPS 197: Advanced Encryption Standard (AES)
  • FIPS 198-1: The Keyed-Hash Message Authentication Code (HMAC)
  • FIPS 180-4: Secure Hash Standard (SHS)

Contributing

Contributions are welcome & appreciated!

Refer to the contributing document to help facilitate pull requests.

License

This software is licensed under the MIT License.

Copyright Jason Gerfen, 2019 to 2023.

kruptein's People

Contributors

dependabot[bot] avatar jas- avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

fossabot

kruptein's Issues

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type undefined

Stacktrace

(node:22642) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type undefined
    at validChunk (_stream_writable.js:263:10)
    at Hmac.Writable.write (_stream_writable.js:297:21)
    at kruptein._digest (${PROJECT_ROOT}/node_modules/kruptein/lib/kruptein.js:175:10)
    at kruptein.get (${PROJECT_ROOT}/node_modules/kruptein/lib/kruptein.js:69:17)
    at getSession (${PROJECT_ROOT}/src/router/session.js:64:31)
    at authMiddeware (${PROJECT_ROOT}/src/router/session.js:27:21)
    at Layer.handle [as handle_request] (${PROJECT_ROOT}/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (${PROJECT_ROOT}/node_modules/express/lib/router/index.js:317:13)
    at ${PROJECT_ROOT}/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (${PROJECT_ROOT}/node_modules/express/lib/router/index.js:335:12)
    at next (${PROJECT_ROOT}/node_modules/express/lib/router/index.js:275:10)
    at expressInit (${PROJECT_ROOT}/node_modules/express/lib/middleware/init.js:40:5)
    at Layer.handle [as handle_request] (${PROJECT_ROOT}/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (${PROJECT_ROOT}/node_modules/express/lib/router/index.js:317:13)
    at ${PROJECT_ROOT}/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (${PROJECT_ROOT}/node_modules/express/lib/router/index.js:335:12)
    at next (${PROJECT_ROOT}/node_modules/express/lib/router/index.js:275:10)
    at query (${PROJECT_ROOT}/node_modules/express/lib/middleware/query.js:45:5)
    at Layer.handle [as handle_request] (${PROJECT_ROOT}/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (${PROJECT_ROOT}/node_modules/express/lib/router/index.js:317:13)
    at ${PROJECT_ROOT}/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (${PROJECT_ROOT}/node_modules/express/lib/router/index.js:335:12)

Suggested fix

https://github.com/jas-/kruptein/blob/master/lib/kruptein.js#L69

From :

    hmac = this._digest(this.secret, ct.ct, this.hashing, this.encodeas)
    //                                 ^^^

To :

    hmac = this._digest(this.secret, ct, this.hashing, this.encodeas)

Support node 13

I'd like to use this module with Node 13.8.0 but the module specifically disable this

error [email protected]: The engine "node" is incompatible with this module. Expected version "^8 || ^10 || ^12". Got "13.8.0"
error Found incompatible module.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.