Skip to content

Vendor and modularize the portions of the SJCL necessary for SHA-256 HMAC and CSPRNG #17

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

Merged
merged 3 commits into from
Jun 5, 2017
Merged

Vendor and modularize the portions of the SJCL necessary for SHA-256 HMAC and CSPRNG #17

merged 3 commits into from
Jun 5, 2017

Conversation

jeskew
Copy link
Contributor

@jeskew jeskew commented May 30, 2017

Spun off from #1

The Stanford JavaScript Crypto Library is trusted, reasonably performant, and does not make any assumptions about the environment on which it's running, but it binds all functions to a global variable, has no built-in typings, and is quite large when included in its entirety. This PR takes the files necessary for sjcl.misc.hmac and sjcl.random (as determined by the dependency tree provided here), converts them to CommonJS modules, and adds TypeScript declaration files.

@jeskew jeskew requested review from awood45 and chrisradek May 30, 2017 22:12
@jeskew jeskew mentioned this pull request May 30, 2017
7 tasks
@chrisradek chrisradek added wontfix and removed wontfix labels Jun 2, 2017
import {BitArray} from '@aws/crypto-sjcl-bitArray';

declare class aes {
constructor(key: BitArray);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the comments from the JSDoc here too?

//check to make sure the bitLength is divisible by 8, if it isn't
//we can't do anything since arraybuffers work with bytes, not bits
if ( sjcl.bitArray.bitLength(arr)%8 !== 0 ) {
throw new sjcl.exception.invalid("Invalid bit size, must be divisble by 8 to fit in an arraybuffer correctly");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have sjcl.exception defined in this file.

touchCollector: this._bind(this._touchCollector)
};

if (window.addEventListener) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work in environments where there isn't a window object? Thinking of react native and web workers specifically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular function will not work in those environments.

All CSPRNGs require some form of truly random seed, and startCollectors is only called if that seed is not supplied. Further down in the file, there's an IIFE that attempts to seed the algorithm by calling (in order of preference) require('crypto').randomBytes, window.crypto.getRandomValues, and window.msCrypto.getRandomValues -- random collection via event listeners is only attempted if all of those methods fail.

Packages that consume the SJCL random generator will need to be aware of this, but there really isn't anything we can do about it. Without a random seed, the Fortuna algorithm is useless.

ol = this._length,
nl = this._length = ol + sjcl.bitArray.bitLength(data);
if (nl > 9007199254740991){
throw new sjcl.exception.invalid("Cannot hash more than 2^53 - 1 bits");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to import exception in this file too.

Copy link
Contributor

@chrisradek chrisradek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it

@jeskew jeskew merged commit 848ae16 into aws:master Jun 5, 2017
@jeskew jeskew deleted the feature/vendor-portions-of-SJCL branch June 5, 2017 17:10
trivikr referenced this pull request in trivikr/aws-sdk-js-v3 Dec 10, 2018
…HMAC and CSPRNG (#17)

* Vendor and modularize the portions of the SJCL necessary for SHA-256 HMAC and CSPRNG

* Ensure comments and exceptions are present

* Ensure the SJCL random module does not throw reference errors
@lock
Copy link

lock bot commented Sep 26, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants