Socket
Socket
Sign inDemoInstall

asmcrypto.js

Package Overview
Dependencies
Maintainers
3
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

asmcrypto.js - npm Package Compare versions

Comparing version 0.22.0 to 2.0.0

.esmrc

59

package.json
{
"name": "asmcrypto.js",
"version": "0.22.0",
"version": "2.0.0",
"description": "Asm.js implementation of WebCrypto API",
"homepage": "https://github.com/vibornoff/asmcrypto.js",
"main": "asmcrypto.js",
"module": "asmcrypto.mjs",
"homepage": "https://github.com/asmcrypto/asmcrypto.js",
"main": "asmcrypto.all.js",
"module": "asmcrypto.all.es8.js",
"license": "MIT",
"author": {
"name": "Artem S Vybornov",
"email": "vybornov@gmail.com",
"web": "https://github.com/vibornoff"
"name": "Ádám Lippai",
"email": "adam@rigo.sk",
"web": "https://github.com/alippai"
},
"contributors": [
{
"name": "Ádám Lippai",
"email": "adam@rigo.sk",
"web": "https://github.com/alippai"
"name": "Artem S Vybornov",
"email": "vybornov@gmail.com",
"web": "https://github.com/vibornoff"
},

@@ -28,25 +28,28 @@ {

"type": "git",
"url": "https://github.com/vibornoff/asmcrypto.js.git"
"url": "https://github.com/asmcrypto/asmcrypto.js.git"
},
"devDependencies": {
"grunt": "~1.0",
"grunt-cli": "~1.2",
"grunt-contrib-clean": "~1.1",
"grunt-contrib-concat": "~1.0",
"grunt-contrib-connect": "~1.0",
"grunt-contrib-uglify-es": "git+https://github.com/gruntjs/grunt-contrib-uglify.git#harmony",
"grunt-contrib-watch": "~1.0",
"grunt-jsdoc": "~2.2",
"grunt-rollup": "^9.0.0",
"grunt-shell": "^2.1.0",
"prettier": "^1.10.2",
"qunit-puppeteer": "^1.0.1",
"uglify-es": "^3.3.4"
"@types/chai": "^4.1.3",
"@types/mocha": "^5.2.0",
"@types/node": "^10.1.2",
"chai": "^4.1.2",
"esm": "3.0.36",
"fs-extra": "^6.0.1",
"mocha": "^5.2.0",
"prettier": "^1.13.4",
"rollup": "^0.59.1",
"ts-node": "^6.0.3",
"tslint": "^5.10.0",
"tslint-plugin-prettier": "^1.3.0",
"typescript": "^2.8.3",
"uglify-es": "^3.3.9",
"uglify-js": "^3.3.25"
},
"scripts": {
"prepublish": "grunt && grunt esm && grunt all",
"test": "grunt test",
"prettier": "prettier --single-quote --trailing-comma all --write \"src/**/*.js\" --print-width 120"
"prepublish": "node -r esm build.js",
"test": "node -r esm build.js && mocha -r esm test/*.js",
"prettier": "prettier --single-quote --trailing-comma all --write \"src/**/*.js\" \"src/**/*.ts\" --print-width 120"
},
"typings": "./asmcrypto.js.d.ts"
"typings": "./dist_es8/entry-export_all.d.ts",
"dependencies": {}
}

@@ -6,54 +6,5 @@ asmCrypto [![Build Status](https://travis-ci.org/asmcrypto/asmcrypto.js.svg?branch=master)](https://travis-ci.org/asmcrypto/asmcrypto.js) [![Join the chat at https://gitter.im/asmcrypto/asmcrypto.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/asmcrypto/asmcrypto.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Synopsis
--------
Add `<script src="path/to/asmcrypto.js"></script>` into your page.
// Hash whole string at once
digest = asmCrypto.SHA256.hex("The quick brown fox jumps over the lazy dog");
Index
-----
* [Build & Test](#build--test)
* [Performance](#performance)
* [API Reference](#api-reference)
* [Message Digest](#sha256)
* [SHA1](#sha1)
* [SHA256](#sha256)
* [SHA512](#sha512)
* [Hash-based Message Authentication](#hmac)
* [HMAC-SHA1](#hmac_sha1)
* [HMAC-SHA256](#hmac_sha256)
* [HMAC-SHA512](#hmac_sha512)
* [Password-based Key Derivation](#pbkdf2)
* [PBKDF2-HMAC-SHA1](#pbkdf2_hmac_sha1)
* [PBKDF2-HMAC-SHA256](#pbkdf2_hmac_sha256)
* [PBKDF2-HMAC-SHA512](#pbkdf2_hmac_sha512)
* [Block Cipher](#aes)
* [AES-EBC](#aes_ecb)
* [AES-CBC](#aes_cbc)
* [AES-CFB](#aes_cfb)
* [AES-OFB](#aes_ofb)
* [AES-CTR](#aes_ctr)
* [AES-CCM](#aes_ccm)
* [AES-GCM](#aes_gcm)
* [Asymmetric encryption](#rsa)
* [RSA](#rsa)
* [RSA-OAEP-SHA1](#rsa_oaep_sha1)
* [RSA-OAEP-SHA256](#rsa_oaep_sha256)
* [RSA-OAEP-SHA512](#rsa_oaep_sha512)
* [RSA-PSS-SHA1](#rsa_pss_sha1)
* [RSA-PSS-SHA256](#rsa_pss_sha256)
* [RSA-PSS-SHA512](#rsa_pss_sha512)
* [Cryptographically secure pseudorandom number generator](#cryptographically-secure-pseudorandom-number-generator)
Build & Test
------------
Before you start check that [npm](http://npmjs.org/) is installed:
npm --version
Then download and build the stuff:

@@ -71,13 +22,7 @@

Performance
-----------
In the development of this project, special attention was paid to the performance issues.
In the result of all the optimizations made this stuff is pretty fast under Firefox and Chrome.
Support
-----------
* NodeJS 8 and NodeJS 9
* IE11 (temporarily disabled, use previous versions: 0.16.x)
* NodeJS 10
* IE11
* last two Chrome versions

@@ -88,476 +33,6 @@ * last two Firefox versions and the latest Firefox ESR

API Reference
-------------
AsmCrypto 2.0
-----------
### Message Digest
#### SHA1
[Secure Hash Algorithm](http://en.wikipedia.org/wiki/SHA-1) — a cryptographic hash function with 160-bit output.
A cryptographic hash fuction with 256-bit output.
##### SHA1.BLOCK_SIZE = 64
##### SHA1.HASH_SIZE = 20
##### SHA1.bytes( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns raw message digest as an `Uint8Array` object.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### SHA1.hex( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns a string containing hex-encoded message digest.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### SHA1.base64( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns a string containing hex-encoded message digest.
Throws
* `TypeError` when something ridiculous is supplied as input data.
#### SHA256
[Secure Hash Algorithm](http://en.wikipedia.org/wiki/SHA-2) — a cryptographic hash functions family.
A cryptographic hash fuction with 256-bit output.
##### SHA256.BLOCK_SIZE = 64
##### SHA256.HASH_SIZE = 32
##### SHA256.bytes( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns raw message digest as an `Uint8Array` object.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### SHA256.hex( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns a string containing hex-encoded message digest.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### SHA256.base64( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns a string containing hex-encoded message digest.
Throws
* `TypeError` when something ridiculous is supplied as input data.
#### SHA512
A cryptographic hash function with 512-bit output.
##### SHA512.BLOCK_SIZE = 128
##### SHA512.HASH_SIZE = 64
##### SHA512.bytes( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns raw message digest as an `Uint8Array` object.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### SHA512.hex( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns a string containing hex-encoded message digest.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### SHA512.base64( data )
Calculates message digest of the supplied input `data` (can be a binary string or `ArrayBuffer`/`Uint8Array` object).
Returns a string containing hex-encoded message digest.
Throws
* `TypeError` when something ridiculous is supplied as input data.
### HMAC
[Hash-based Message Authentication Code](http://en.wikipedia.org/wiki/HMAC)
Used to calculate message authentication code with a cryptographic hash function
in combination with a secret cryptographic key.
#### HMAC_SHA1
##### HMAC_SHA1.BLOCK_SIZE = 64
##### HMAC_SHA1.HMAC_SIZE = 20
##### HMAC_SHA1.bytes( data, password )
Calculates HMAC-SHA1 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns araw message authentication code as an `Uint8Array` object.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### HMAC_SHA1.hex( data, password )
Calculates HMAC-SHA1 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns a string containing hex-encoded message authentication code.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### HMAC_SHA1.base64( data, password )
Calculates HMAC-SHA1 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns a string containing base64-encoded message authentication code.
Throws
* `TypeError` when something ridiculous is supplied as input data.
#### HMAC_SHA256
##### HMAC_SHA256.BLOCK_SIZE = 64
##### HMAC_SHA256.HMAC_SIZE = 32
##### HMAC_SHA256.bytes( data, password )
Calculates HMAC-SHA256 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns araw message authentication code as an `Uint8Array` object.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### HMAC_SHA256.hex( data, password )
Calculates HMAC-SHA256 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns a string containing hex-encoded message authentication code.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### HMAC_SHA256.base64( data, password )
Calculates HMAC-SHA256 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns a string containing base64-encoded message authentication code.
Throws
* `TypeError` when something ridiculous is supplied as input data.
#### HMAC_SHA512
##### HMAC_SHA512.BLOCK_SIZE = 128
##### HMAC_SHA512.HMAC_SIZE = 64
##### HMAC_SHA512.bytes( data, password )
Calculates HMAC-SHA512 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns araw message authentication code as an `Uint8Array` object.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### HMAC_SHA512.hex( data, password )
Calculates HMAC-SHA512 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns a string containing hex-encoded message authentication code.
Throws
* `TypeError` when something ridiculous is supplied as input data.
##### HMAC_SHA512.base64( data, password )
Calculates HMAC-SHA512 of `data` with `password`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Returns a string containing base64-encoded message authentication code.
Throws
* `TypeError` when something ridiculous is supplied as input data.
### PBKDF2
[Password-Based Key Derivation Function 2](http://en.wikipedia.org/wiki/PBKDF2)
Applies a cryptographic hash function to the input password or passphrase along with a salt value and repeats the process many times to produce a derived key,
which can then be used as a cryptographic key in subsequent operations. The added computational work makes password cracking much more difficult.
#### PBKDF2_HMAC_SHA1
##### PBKDF2_HMAC_SHA1.bytes( password, salt, iterations, dklen )
Derive key from the `password` with `salt`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Optional `iterations` (number of key derivatoin rounds) and `dklen` (desired key length) may be supplied.
Throws
* `TypeError`.
##### PBKDF2_HMAC_SHA1.hex( password, salt, iterations, dklen )
The same as above except returning value type.
##### PBKDF2_HMAC_SHA1.base64( password, salt, iterations, dklen )
The same as above except returning value type.
#### PBKDF2_HMAC_SHA256
##### PBKDF2_HMAC_SHA256.bytes( password, salt, iterations, dklen )
Derive key from the `password` with `salt`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Optional `iterations` (number of key derivatoin rounds) and `dklen` (desired key length) may be supplied.
Throws
* `TypeError`.
##### PBKDF2_HMAC_SHA256.hex( password, salt, iterations, dklen )
The same as above except returning value type.
##### PBKDF2_HMAC_SHA256.base64( password, salt, iterations, dklen )
The same as above except returning value type.
#### PBKDF2_HMAC_SHA512
##### PBKDF2_HMAC_SHA512.bytes( password, salt, iterations, dklen )
Derive key from the `password` with `salt`. Both can be either binary strings or `Uint8Array`/`ArrayBuffer` objects.
Optional `iterations` (number of key derivatoin rounds) and `dklen` (desired key length) may be supplied.
Throws
* `TypeError`.
##### PBKDF2_HMAC_SHA512.hex( password, salt, iterations, dklen )
The same as above except returning value type.
##### PBKDF2_HMAC_SHA512.base64( password, salt, iterations, dklen )
The same as above except returning value type.
### AES
Advanced Encryption Standard
#### AES_ECB
TODO
#### AES_CBC
Cipher Block Chaining Mode.
##### AES_CBC.encrypt( data, key, padding, iv )
Encrypts supplied `data` with `key` in CBC mode. Both can be either binary strings or `Uint8Array` objects or `ArrayBuffer` objects.
Optional `padding` and `iv` may be passed to override default settings (PKCS#7 padding is on and iv is zero-vector).
Returns encrypted data as `Uint8Array`.
##### AES_CBC.decrypt( data, key, padding, iv )
Decrypts supplied `data` with `key` in CBC mode. Both can be either binary strings or `Uint8Array` objects or `ArrayBuffer` objects.
Optional `padding` and `iv` may be passed to override default settings (PKCS#7 padding is on and iv is zero-vector).
Returns encrypted data as `Uint8Array`.
#### AES_CFB
Cipher Feedback Mode.
##### AES_CFB.encrypt( data, key, iv )
Encrypts supplied `data` with `key` in CFB mode. Both can be either binary strings or `Uint8Array` objects or `ArrayBuffer` objects.
Optional `iv` may be passed to override default settings (zero-vector `iv`).
Returns encrypted data as `Uint8Array`.
##### AES_CFB.decrypt( data, key, iv )
Decrypts supplied `data` with `key` in CFB mode. Both can be either binary strings or `Uint8Array` objects or `ArrayBuffer` objects.
Optional `iv` may be passed to override default settings (zero-vector `iv`).
Returns encrypted data as `Uint8Array`.
#### AES_OFB
Output Feedback Mode.
##### AES_OFB.encrypt( data, key, iv )
Encrypts supplied `data` with `key` in OFB mode. Both can be either binary strings or `Uint8Array` objects or `ArrayBuffer` objects.
Optional `iv` may be passed to override default settings (zero-vector `iv`).
Returns encrypted data as `Uint8Array`.
##### AES_OFB.decrypt( data, key, iv )
Decrypts supplied `data` with `key` in OFB mode. Both can be either binary strings or `Uint8Array` objects or `ArrayBuffer` objects.
Optional `iv` may be passed to override default settings (zero-vector `iv`).
Returns encrypted data as `Uint8Array`.
#### AES_CTR
TODO
#### AES_CCM
Counter with CBC-MAC mode.
Due to JS limitations (counter is 32-bit unsigned) maximum encrypted message length is limited to near 64 GiB ( 2^36 - 16 ) per `nonce`-`key` pair.
Additional authenticated data `adata` maximum length is limited to 65279 bytes ( 2^16 - 2^8 ),
wich is considered enough for the most of use-cases.
Optional `tagSize`, the size of the authentication tag, may be 4, 6, 8, 12, 16 (default).
Keep in mind that **same nonce must not be used more than once with the same key**.
##### AES_CCM.encrypt( data, key, nonce, adata, tagsize )
Encrypts supplied `data` with `key`-`nonce` in CCM mode.
Returns encrypted data as `Uint8Array`.
##### AES_CCM.decrypt( data, key, nonce, adata, tagsize )
Decrypts supplied `data` with `key`-`nonce` in CCM mode.
Returns encrypted data as `Uint8Array`.
#### AES_GCM
TODO
### RSA
#### RSA.generateKey( bitlen, pubexp )
Generate RSA private key of `bitlen` length along with the public exponent `pubexp`.
#### RSA_OAEP_SHA1
##### RSA_OAEP_SHA1.encrypt( data, key, label )
TODO
##### RSA_OAEP_SHA1.decrypt( data, key, label )
TODO
#### RSA_OAEP_SHA256
##### RSA_OAEP_SHA256.encrypt( data, key, label )
TODO
##### RSA_OAEP_SHA256.decrypt( data, key, label )
TODO
#### RSA_OAEP_SHA512
##### RSA_OAEP_SHA512.encrypt( data, key, label )
TODO
##### RSA_OAEP_SHA512.decrypt( data, key, label )
TODO
#### RSA_PSS_SHA1
##### RSA_PSS_SHA1.sign( data, key, slen )
TODO
##### RSA_PSS_SHA1.verify( signature, data, key, slen )
TODO
#### RSA_PSS_SHA256
##### RSA_PSS_SHA256.sign( data, key, slen )
TODO
##### RSA_PSS_SHA256.verify( signature, data, key, slen )
TODO
#### RSA_PSS_SHA512
##### RSA_PSS_SHA512.sign( data, key, slen )
TODO
##### RSA_PSS_SHA512.verify( signature, data, key, slen )
TODO
### Cryptographically secure pseudorandom number generator
ISAAC-based CSPRG
##### random.getValues( buffer )
Drop-in replacement for `window.crypto.getRandomValues`
##### random.getValues.seed( seed )
Perform PRNG seeding.
##### random.getValues.allowWeak = false
Allow implicitly-only seeded random output.
##### random.getValues.skipSystemRNGWarning = false
Disable implicit seeding warning when it's not desirable, e.g. at a unit test run.
Not yet implemented:
* scrypt,
* dsa, ecdsa,
* rsa-pkcs-v1.5
* Moved to TypeScript
* I have no confident knowledge on random generation, so I don't feel right maintaining it. As of 2.0 all custom random generation and seeding code is removed, the underlying browsers and environments have to provide secure random.

@@ -156,4 +156,4 @@ /**

* @class
* @param {Object} foreign - <i>ignored</i>
* @param {ArrayBuffer} buffer - heap buffer to link with
* @param foreign - <i>ignored</i>
* @param buffer - heap buffer to link with
*/

@@ -308,3 +308,3 @@ var wrapper = function (foreign, buffer) {

x2,
x3,
x3
);

@@ -334,3 +334,3 @@ }

x2,
x1,
x1
);

@@ -361,3 +361,3 @@

I2 ^ x2,
I3 ^ x3,
I3 ^ x3
);

@@ -392,3 +392,3 @@

x2,
x1,
x1
);

@@ -428,3 +428,3 @@

I2,
I3,
I3
);

@@ -458,3 +458,3 @@

I2,
I3,
I3
);

@@ -492,3 +492,3 @@

I2,
I3,
I3
);

@@ -526,3 +526,3 @@

N2,
N3,
N3
);

@@ -813,3 +813,3 @@

DATA[pos | 8] << 24 | DATA[pos | 9] << 16 | DATA[pos | 10] << 8 | DATA[pos | 11],
DATA[pos | 12] << 24 | DATA[pos | 13] << 16 | DATA[pos | 14] << 8 | DATA[pos | 15],
DATA[pos | 12] << 24 | DATA[pos | 13] << 16 | DATA[pos | 14] << 8 | DATA[pos | 15]
);

@@ -865,3 +865,3 @@

DATA[pos | 8] << 24 | DATA[pos | 9] << 16 | DATA[pos | 10] << 8 | DATA[pos | 11],
DATA[pos | 12] << 24 | DATA[pos | 13] << 16 | DATA[pos | 14] << 8 | DATA[pos | 15],
DATA[pos | 12] << 24 | DATA[pos | 13] << 16 | DATA[pos | 14] << 8 | DATA[pos | 15]
);

@@ -868,0 +868,0 @@

@@ -852,16 +852,16 @@ export function sha1_asm ( stdlib, foreign, buffer ) {

return {
// SHA1
reset: reset,
init: init,
process: process,
finish: finish,
// SHA1
reset: reset,
init: init,
process: process,
finish: finish,
// HMAC-SHA1
hmac_reset: hmac_reset,
hmac_init: hmac_init,
hmac_finish: hmac_finish,
// HMAC-SHA1
hmac_reset: hmac_reset,
hmac_init: hmac_init,
hmac_finish: hmac_finish,
// PBKDF2-HMAC-SHA1
pbkdf2_generate_block: pbkdf2_generate_block
// PBKDF2-HMAC-SHA1
pbkdf2_generate_block: pbkdf2_generate_block
}
}

@@ -811,16 +811,16 @@ export function sha256_asm ( stdlib, foreign, buffer ) {

return {
// SHA256
reset: reset,
init: init,
process: process,
finish: finish,
// SHA256
reset: reset,
init: init,
process: process,
finish: finish,
// HMAC-SHA256
hmac_reset: hmac_reset,
hmac_init: hmac_init,
hmac_finish: hmac_finish,
// HMAC-SHA256
hmac_reset: hmac_reset,
hmac_init: hmac_init,
hmac_finish: hmac_finish,
// PBKDF2-HMAC-SHA256
pbkdf2_generate_block: pbkdf2_generate_block
// PBKDF2-HMAC-SHA256
pbkdf2_generate_block: pbkdf2_generate_block
}
}

@@ -1,1008 +0,708 @@

module("AES");
import * as asmCrypto from '../asmcrypto.all.es8';
import chai from 'chai';
const expect = chai.expect;
Uint32Array.prototype.map = Array.prototype.map;
///////////////////////////////////////////////////////////////////////////////
//
// Asm.js module test
//
testIf( typeof AES_asm !== 'undefined', "Asm.js module", function () {
AES_asm = AES_asm.AES_asm;
var heap = new Uint8Array(65536);
var keys = new Uint32Array(heap.buffer);
var aes = AES_asm( null, heap.buffer );
// data block
heap.set( asmCrypto.hex_to_bytes('00112233445566778899aabbccddeeff'), AES_asm.HEAP_DATA );
//
// AES-128
//
aes.set_key( 4, 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f );
deepEqual(
keys.subarray( 0, 44 ).map( function (x) { return x } ),
[
0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
0xd6aa74fd, 0xd2af72fa, 0xdaa678f1, 0xd6ab76fe,
0xb692cf0b, 0x643dbdf1, 0xbe9bc500, 0x6830b3fe,
0xb6ff744e, 0xd2c2c9bf, 0x6c590cbf, 0x0469bf41,
0x47f7f7bc, 0x95353e03, 0xf96c32bc, 0xfd058dfd,
0x3caaa3e8, 0xa99f9deb, 0x50f3af57, 0xadf622aa,
0x5e390f7d, 0xf7a69296, 0xa7553dc1, 0x0aa31f6b,
0x14f9701a, 0xe35fe28c, 0x440adf4d, 0x4ea9c026,
0x47438735, 0xa41c65b9, 0xe016baf4, 0xaebf7ad2,
0x549932d1, 0xf0855768, 0x1093ed9c, 0xbe2c974e,
0x13111d7f, 0xe3944a17, 0xf307a78b, 0x4d2b30c5
],
"AES-128: encryption key schedule ok"
);
deepEqual(
keys.subarray( 0x100, 0x100+44 ).map( function (x) { return x } ),
[
0x13111d7f, 0x4d2b30c5, 0xf307a78b, 0xe3944a17,
0x13aa29be, 0x00f7bf03, 0xf770f580, 0x9c8faff6,
0x1362a463, 0xf7874a83, 0x6bff5a76, 0x8f258648,
0x8d82fc74, 0x9c7810f5, 0xe4dadc3e, 0x9c47222b,
0x72e3098d, 0x78a2cccb, 0x789dfe15, 0x11c5de5f,
0x2ec41027, 0x003f32de, 0x6958204a, 0x6326d7d2,
0xa8a2f504, 0x69671294, 0x0a7ef798, 0x4de2c7f5,
0xc7c6e391, 0x6319e50c, 0x479c306d, 0xe54032f1,
0xa0db0299, 0x2485d561, 0xa2dc029c, 0x2286d160,
0x8c56dff0, 0x8659d7fd, 0x805ad3fc, 0x825dd3f9,
0x00010203, 0x0c0d0e0f, 0x08090a0b, 0x04050607
],
"AES-128: decryption key schedule ok"
);
aes.cipher( AES_asm.ENC.ECB, AES_asm.HEAP_DATA, 16 );
equal(
asmCrypto.bytes_to_hex( heap.subarray( AES_asm.HEAP_DATA, AES_asm.HEAP_DATA+16 ) ),
'69c4e0d86a7b0430d8cdb78070b4c55a',
"AES-128: encrypt ok"
);
aes.cipher( AES_asm.DEC.ECB, AES_asm.HEAP_DATA, 16 );
equal(
asmCrypto.bytes_to_hex( heap.subarray( AES_asm.HEAP_DATA, AES_asm.HEAP_DATA+16 ) ),
'00112233445566778899aabbccddeeff',
"AES-128: decrypt ok"
);
//
// AES-192
//
aes.set_key( 6, 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f, 0x10111213, 0x14151617 );
deepEqual(
keys.subarray( 0, 52 ).map( function (x) { return x } ),
[
0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
0x10111213, 0x14151617, 0x5846f2f9, 0x5c43f4fe,
0x544afef5, 0x5847f0fa, 0x4856e2e9, 0x5c43f4fe,
0x40f949b3, 0x1cbabd4d, 0x48f043b8, 0x10b7b342,
0x58e151ab, 0x04a2a555, 0x7effb541, 0x6245080c,
0x2ab54bb4, 0x3a02f8f6, 0x62e3a95d, 0x66410c08,
0xf5018572, 0x97448d7e, 0xbdf1c6ca, 0x87f33e3c,
0xe5109761, 0x83519b69, 0x34157c9e, 0xa351f1e0,
0x1ea0372a, 0x99530916, 0x7c439e77, 0xff12051e,
0xdd7e0e88, 0x7e2fff68, 0x608fc842, 0xf9dcc154,
0x859f5f23, 0x7a8d5a3d, 0xc0c02952, 0xbeefd63a,
0xde601e78, 0x27bcdf2c, 0xa223800f, 0xd8aeda32,
0xa4970a33, 0x1a78dc09, 0xc418c271, 0xe3a41d5d
],
"AES-192: encryption key schedule ok"
);
deepEqual(
keys.subarray( 0x100, 0x100+52 ).map( function (x) { return x } ),
[
0xa4970a33, 0xe3a41d5d, 0xc418c271, 0x1a78dc09,
0xd6bebd0d, 0x3e021bb9, 0x4db07380, 0xc209ea49,
0x8fb999c9, 0x85c68c72, 0xc7f9d89d, 0x73b26839,
0xf77d6ec1, 0x14b75744, 0x5378317f, 0x423f54ef,
0x11476590, 0xfc0bf1f0, 0x9b0ece8d, 0x47cf663b,
0xdcc1a8b6, 0xb5423a2e, 0xcc5c194a, 0x67053f7d,
0xc6deb0ab, 0x568803ab, 0xa4055fbe, 0x791e2364,
0xdd1b7cda, 0xbbc497cb, 0x8a49ab1d, 0xf28d5c15,
0x78c4f708, 0xbfc093cf, 0x9655b701, 0x318d3cd6,
0x60dcef10, 0x2f9620cf, 0x62dbef15, 0x299524ce,
0x4b4ecbdb, 0x4949cbde, 0x5752d7c7, 0x4d4dcfda,
0x1a1f181d, 0x4949cbde, 0x4742c7d7, 0x1e1b1c19,
0x00010203, 0x0c0d0e0f, 0x08090a0b, 0x04050607
],
"AES-192: decryption key schedule ok"
);
aes.cipher( AES_asm.ENC.ECB, AES_asm.HEAP_DATA, 16 );
equal(
asmCrypto.bytes_to_hex( heap.subarray( AES_asm.HEAP_DATA, AES_asm.HEAP_DATA+16 ) ),
'dda97ca4864cdfe06eaf70a0ec0d7191',
"AES-192: encrypt ok"
);
aes.cipher( AES_asm.DEC.ECB, AES_asm.HEAP_DATA, 16 );
equal(
asmCrypto.bytes_to_hex( heap.subarray( AES_asm.HEAP_DATA, AES_asm.HEAP_DATA+16 ) ),
'00112233445566778899aabbccddeeff',
"AES-192: decrypt ok"
);
//
// AES-256
//
aes.set_key( 8, 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f, 0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f );
deepEqual(
keys.subarray( 0, 60 ).map( function (x) { return x } ),
[
0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f,
0xa573c29f, 0xa176c498, 0xa97fce93, 0xa572c09c,
0x1651a8cd, 0x0244beda, 0x1a5da4c1, 0x0640bade,
0xae87dff0, 0x0ff11b68, 0xa68ed5fb, 0x03fc1567,
0x6de1f148, 0x6fa54f92, 0x75f8eb53, 0x73b8518d,
0xc656827f, 0xc9a79917, 0x6f294cec, 0x6cd5598b,
0x3de23a75, 0x524775e7, 0x27bf9eb4, 0x5407cf39,
0x0bdc905f, 0xc27b0948, 0xad5245a4, 0xc1871c2f,
0x45f5a660, 0x17b2d387, 0x300d4d33, 0x640a820a,
0x7ccff71c, 0xbeb4fe54, 0x13e6bbf0, 0xd261a7df,
0xf01afafe, 0xe7a82979, 0xd7a5644a, 0xb3afe640,
0x2541fe71, 0x9bf50025, 0x8813bbd5, 0x5a721c0a,
0x4e5a6699, 0xa9f24fe0, 0x7e572baa, 0xcdf8cdea,
0x24fc79cc, 0xbf0979e9, 0x371ac23c, 0x6d68de36
],
"AES-256: encryption key schedule ok"
);
deepEqual(
keys.subarray( 0x100, 0x100+60 ).map( function (x) { return x } ),
[
0x24fc79cc, 0x6d68de36, 0x371ac23c, 0xbf0979e9,
0x34f1d1ff, 0x2558016e, 0xfce9e25f, 0xbfceaa2f,
0x5e1648eb, 0xdc80e684, 0x7571b746, 0x384c350a,
0xc8a30580, 0xd9b1e331, 0x43274870, 0x8b3f7bd0,
0xb5708e13, 0xa9f151c2, 0x4d3d824c, 0x665a7de1,
0x74da7ba3, 0x9a96ab41, 0xc81833a0, 0x439c7e50,
0x3ca69715, 0xe4ccd38e, 0x2b67ffad, 0xd32af3f2,
0xf85fc4f3, 0x528e98e1, 0x8b844df0, 0x374605f3,
0xde69409a, 0xcfab2c23, 0xf84d0c5f, 0xef8c64e7,
0xaed55816, 0xd90ad511, 0xbcc24803, 0xcf19c100,
0x15c668bd, 0x37e6207c, 0x17c168b8, 0x31e5247d,
0x7fd7850f, 0x65c89d12, 0x73db8903, 0x61cc9916,
0x2a2840c9, 0x202748c4, 0x26244cc5, 0x24234cc0,
0x1a1f181d, 0x16131411, 0x12171015, 0x1e1b1c19,
0x00010203, 0x0c0d0e0f, 0x08090a0b, 0x04050607
],
"AES-256: decryption key schedule ok"
);
aes.cipher( AES_asm.ENC.ECB, AES_asm.HEAP_DATA, 16 );
equal(
asmCrypto.bytes_to_hex( heap.subarray( AES_asm.HEAP_DATA, AES_asm.HEAP_DATA+16 ) ),
'8ea2b7ca516745bfeafc49904b496089',
"AES-256: encrypt ok"
);
aes.cipher( AES_asm.DEC.ECB, AES_asm.HEAP_DATA, 16 );
equal(
asmCrypto.bytes_to_hex( heap.subarray( AES_asm.HEAP_DATA, AES_asm.HEAP_DATA+16 ) ),
'00112233445566778899aabbccddeeff',
"AES-256: decrypt ok"
);
});
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.AES_ECB !== 'undefined' )
{
var ecb_aes_vectors = [
// AES-ECB-128
[
'2b7e151628aed2a6abf7158809cf4f3c', // key
'6bc1bee22e409f96e93d7e117393172a', // clear text
'3ad77bb40d7a3660a89ecaf32466ef97' // cipher text
],
[
'2b7e151628aed2a6abf7158809cf4f3c', // key
'ae2d8a571e03ac9c9eb76fac45af8e51', // clear text
'f5d3d58503b9699de785895a96fdbaaf' // cipher text
],
[
'2b7e151628aed2a6abf7158809cf4f3c', // key
'30c81c46a35ce411e5fbc1191a0a52ef', // clear text
'43b1cd7f598ece23881b00e3ed030688' // cipher text
],
[
'2b7e151628aed2a6abf7158809cf4f3c', // key
'f69f2445df4f9b17ad2b417be66c3710', // clear text
'7b0c785e27e8ad3f8223207104725dd4' // cipher text
],
[ // Two blocks
'2b7e151628aed2a6abf7158809cf4f3c', // key
'f69f2445df4f9b17ad2b417be66c3710f69f2445df4f9b17ad2b417be66c3710', // clear text
'7b0c785e27e8ad3f8223207104725dd47b0c785e27e8ad3f8223207104725dd4' // cipher text
],
// AES-ECB-256
[
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'6bc1bee22e409f96e93d7e117393172a', // clear text
'f3eed1bdb5d2a03c064b5a7e3db181f8' // cipher text
],
[
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'ae2d8a571e03ac9c9eb76fac45af8e51', // clear text
'591ccb10d410ed26dc5ba74a31362870' // cipher text
],
[
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'30c81c46a35ce411e5fbc1191a0a52ef', // clear text
'b6ed21b99ca6f4f9f153e7b1beafed1d' // cipher text
],
[ // Two blocks
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'30c81c46a35ce411e5fbc1191a0a52ef30c81c46a35ce411e5fbc1191a0a52ef', // clear text
'b6ed21b99ca6f4f9f153e7b1beafed1db6ed21b99ca6f4f9f153e7b1beafed1d' // cipher text
],
[
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'f69f2445df4f9b17ad2b417be66c3710', // clear text
'23304b7a39f9f3ff067d8d8f9e24ecc7' // cipher text
]
describe('AES', () => {
describe('ECB', () => {
const ecb_aes_vectors = [
// AES-ECB-128
[
'2b7e151628aed2a6abf7158809cf4f3c', // key
'6bc1bee22e409f96e93d7e117393172a', // clear text
'3ad77bb40d7a3660a89ecaf32466ef97', // cipher text
],
[
'2b7e151628aed2a6abf7158809cf4f3c', // key
'ae2d8a571e03ac9c9eb76fac45af8e51', // clear text
'f5d3d58503b9699de785895a96fdbaaf', // cipher text
],
[
'2b7e151628aed2a6abf7158809cf4f3c', // key
'30c81c46a35ce411e5fbc1191a0a52ef', // clear text
'43b1cd7f598ece23881b00e3ed030688', // cipher text
],
[
'2b7e151628aed2a6abf7158809cf4f3c', // key
'f69f2445df4f9b17ad2b417be66c3710', // clear text
'7b0c785e27e8ad3f8223207104725dd4', // cipher text
],
[
// Two blocks
'2b7e151628aed2a6abf7158809cf4f3c', // key
'f69f2445df4f9b17ad2b417be66c3710f69f2445df4f9b17ad2b417be66c3710', // clear text
'7b0c785e27e8ad3f8223207104725dd47b0c785e27e8ad3f8223207104725dd4', // cipher text
],
// AES-ECB-256
[
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'6bc1bee22e409f96e93d7e117393172a', // clear text
'f3eed1bdb5d2a03c064b5a7e3db181f8', // cipher text
],
[
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'ae2d8a571e03ac9c9eb76fac45af8e51', // clear text
'591ccb10d410ed26dc5ba74a31362870', // cipher text
],
[
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'30c81c46a35ce411e5fbc1191a0a52ef', // clear text
'b6ed21b99ca6f4f9f153e7b1beafed1d', // cipher text
],
[
// Two blocks
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'30c81c46a35ce411e5fbc1191a0a52ef30c81c46a35ce411e5fbc1191a0a52ef', // clear text
'b6ed21b99ca6f4f9f153e7b1beafed1db6ed21b99ca6f4f9f153e7b1beafed1d', // cipher text
],
[
'603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', // key
'f69f2445df4f9b17ad2b417be66c3710', // clear text
'23304b7a39f9f3ff067d8d8f9e24ecc7', // cipher text
],
];
test( "asmCrypto.AES_ECB.encrypt / asmCrypto.AES_ECB.decrypt", function () {
for ( var i = 0; i < ecb_aes_vectors.length; ++i ) {
var key = new Uint8Array( asmCrypto.hex_to_bytes(ecb_aes_vectors[i][0]) ),
clear = new Uint8Array( asmCrypto.hex_to_bytes(ecb_aes_vectors[i][1]) ),
cipher = new Uint8Array( asmCrypto.hex_to_bytes(ecb_aes_vectors[i][2]) );
it('asmCrypto.AES_ECB.encrypt / asmCrypto.AES_ECB.decrypt', function () {
for (let i = 0; i < ecb_aes_vectors.length; ++i) {
const key = new Uint8Array(asmCrypto.hex_to_bytes(ecb_aes_vectors[i][0]));
const clear = new Uint8Array(asmCrypto.hex_to_bytes(ecb_aes_vectors[i][1]));
const cipher = new Uint8Array(asmCrypto.hex_to_bytes(ecb_aes_vectors[i][2]));
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_ECB.encrypt( clear, key ) ),
asmCrypto.bytes_to_hex(cipher),
"encrypt vector " + i
);
expect(asmCrypto.AES_ECB.encrypt(clear, key), `encrypt vector ${i}`).to.deep.equal(cipher);
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_ECB.decrypt( cipher, key ) ),
asmCrypto.bytes_to_hex(clear),
"decrypt vector " + i
);
}
expect(asmCrypto.AES_ECB.decrypt(cipher, key), `decrypt vector ${i}`).to.deep.equal(clear);
}
});
}
else
{
skip( "asmCrypto.AES_ECB" );
}
});
if ( typeof asmCrypto.AES_CBC !== 'undefined' )
{
var cbc_aes_vectors = [
[ // key
[ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c ],
// iv
[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ],
// clear text
[ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ],
// cipher text
[ 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 ]
],
[ // key
[ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 ],
// iv
[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ],
// clear text
[ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ],
// cipher text
[ 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b ]
]
describe('CBC', () => {
const cbc_aes_vectors = [
[ // key
[0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c],
// iv
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f],
// clear text
[0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10],
// cipher text
[0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7]
],
[ // key
[0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4],
// iv
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f],
// clear text
[0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10],
// cipher text
[0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b]
]
];
test( "asmCrypto.AES_CBC.encrypt / asmCrypto.AES_CBC.decrypt", function () {
for ( var i = 0; i < cbc_aes_vectors.length; ++i ) {
var key = new Uint8Array( cbc_aes_vectors[i][0] ),
iv = new Uint8Array( cbc_aes_vectors[i][1] ),
clear = new Uint8Array( cbc_aes_vectors[i][2] ),
cipher = new Uint8Array( cbc_aes_vectors[i][3] );
it('asmCrypto.AES_CBC.encrypt / asmCrypto.AES_CBC.decrypt', function () {
for (let i = 0; i < cbc_aes_vectors.length; ++i) {
const key = new Uint8Array(cbc_aes_vectors[i][0]);
const iv = new Uint8Array(cbc_aes_vectors[i][1]);
const clear = new Uint8Array(cbc_aes_vectors[i][2]);
const cipher = new Uint8Array(cbc_aes_vectors[i][3]);
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CBC.encrypt( clear, key, false, iv ) ),
asmCrypto.bytes_to_hex(cipher),
"encrypt vector " + i
);
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CBC.encrypt(clear, key, false, iv)), `encrypt vector ${i}`).to.be.equal(asmCrypto.bytes_to_hex(cipher));
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CBC.decrypt( cipher, key, false, iv ) ),
asmCrypto.bytes_to_hex(clear),
"decrypt vector " + i
);
}
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CBC.decrypt(cipher, key, false, iv)), `decrypt vector ${i}`).to.be.equal(asmCrypto.bytes_to_hex(clear));
}
});
}
else
{
skip( "asmCrypto.AES_CBC" );
}
});
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.AES_CTR !== 'undefined' )
{
var ctr_aes_vectors = [
[
// key
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
// nonce
asmCrypto.hex_to_bytes('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
// input message
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'),
// encrypted message
asmCrypto.hex_to_bytes('874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee')
],
[
// key
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
// nonce
asmCrypto.hex_to_bytes('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
// input message
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c'),
// encrypted message
asmCrypto.hex_to_bytes('874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f300')
],
[
// key
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
// nonce
asmCrypto.hex_to_bytes('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
// input message
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e11739317'),
// encrypted message
asmCrypto.hex_to_bytes('874d6191b620e3261bef6864990db6')
],
[
// key
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
// nonce
asmCrypto.hex_to_bytes('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
// input message
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'),
// encrypted message
asmCrypto.hex_to_bytes('601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c52b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6')
]
describe('CTR', () => {
const ctr_aes_vectors = [
[
// key
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
// nonce
asmCrypto.hex_to_bytes('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
// input message
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'),
// encrypted message
asmCrypto.hex_to_bytes('874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee')
],
[
// key
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
// nonce
asmCrypto.hex_to_bytes('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
// input message
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c'),
// encrypted message
asmCrypto.hex_to_bytes('874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f300')
],
[
// key
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
// nonce
asmCrypto.hex_to_bytes('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
// input message
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e11739317'),
// encrypted message
asmCrypto.hex_to_bytes('874d6191b620e3261bef6864990db6')
],
[
// key
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
// nonce
asmCrypto.hex_to_bytes('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
// input message
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'),
// encrypted message
asmCrypto.hex_to_bytes('601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c52b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6')
]
];
test( "asmCrypto.AES_CTR.encrypt / asmCrypto.AES_CTR.decrypt", function () {
for ( var i = 0; i < ctr_aes_vectors.length; ++i ) {
var key = new Uint8Array( ctr_aes_vectors[i][0] ),
nonce = new Uint8Array( ctr_aes_vectors[i][1] ),
clear = new Uint8Array( ctr_aes_vectors[i][2] ),
cipher = new Uint8Array( ctr_aes_vectors[i][3] );
it('asmCrypto.AES_CTR.encrypt / asmCrypto.AES_CTR.decrypt', function () {
for (let i = 0; i < ctr_aes_vectors.length; ++i) {
const key = new Uint8Array(ctr_aes_vectors[i][0]);
const nonce = new Uint8Array(ctr_aes_vectors[i][1]);
const clear = new Uint8Array(ctr_aes_vectors[i][2]);
const cipher = new Uint8Array(ctr_aes_vectors[i][3]);
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CTR.encrypt( clear, key, nonce ) ),
asmCrypto.bytes_to_hex(cipher),
"encrypt vector " + i
);
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CTR.encrypt(clear, key, nonce)), `encrypt vector ${i}`).to.be.equal(asmCrypto.bytes_to_hex(cipher));
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CTR.decrypt( cipher, key, nonce ) ),
asmCrypto.bytes_to_hex(clear),
"decrypt vector " + i
);
}
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CTR.decrypt(cipher, key, nonce)), `decrypt vector ${i}`).to.be.equal(asmCrypto.bytes_to_hex(clear));
}
});
}
else
{
skip( "asmCrypto.AES_CTR" );
}
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.AES_CCM !== 'undefined' )
{
var ccm_aes_vectors = [
[
// key
new Uint8Array([ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf ]),
// nonce
new Uint8Array([ 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 ]),
// adata
new Uint8Array([ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ]),
// tagSize
8,
// input message
new Uint8Array([ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e ]),
// encrypted message
new Uint8Array([ 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2, 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0 ]),
],
[
// key
new Uint8Array([ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf ]),
// nonce
new Uint8Array([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]),
// adata
new Uint8Array([ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x61, 0x75, 0x74, 0x68, 0x69, 0x6e, 0x66, 0x6f, 0x6f ]),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('44696420796f75206b6e6f772e2e2e0d0a46726f6d2057696b6970656469612773206e657720616e6420726563656e746c7920696d70726f76656420636f6e74656e743a0d0a5361696e74204c656f6e61726420436174686f6c69632043687572636820284d616469736f6e2c204e65627261736b61290d0a2e2e2e207468617420746865204a61636f62204d2e204e616368746967616c6c2d64657369676e65642053742e204c656f6e61726420436174686f6c696320436875726368202870696374757265642920696e204d616469736f6e2c204e65627261736b612c20636f6e7461696e73206120626f6e652072656c6963206f6620697473206e616d6573616b653f0d0a2e2e2e2074686174207468652043616e61646196536f757468204b6f72656120467265652054726164652041677265656d656e742077696c6c20656c696d696e61746520393825206f6620616c6c20696d706f72742074617269666673206265747765656e207468652074776f20636f756e74726965733f0d0a2e2e2e207468617420526f7373204d634577616e2c2043454f206f66205242532047726f75702c206f6e63652074686520776f726c642773206c6172676573742062616e6b2c207477696365206661696c656420616e206163636f756e74616e6379206578616d3f0d0a2e2e2e2074686174205475726b69736820776f6d656e2773206e6174696f6e616c2069636520686f636b657920706c617965722047697a656d20d67a74617364656c656e2069732061206d656d626572206f662068657220666174686572277320636c7562204d696c656e79756d20506174656e20534b3f0d0a2e2e2e207468617420566572697a6f6e20762e204643432028323031342920776173207265706f7274656420746f20626520746865206465617468206f66206e6574776f726b206e65757472616c6974792c20686176696e6720766163617465642074776f206f662074686520464343204f70656e20496e7465726e6574204f726465722032303130277320746872656520726567756c6174696f6e733f0d0a2e2e2e207468617420746865206d6f737320737065636965732043686f7269736f646f6e7469756d206163697068796c6c756d2063616e207375727669766520666f72206d6f7265207468616e20312c3530302079656172732066726f7a656e3f'),
// encrypted message
asmCrypto.hex_to_bytes('09e3c1f3ba2f40eeca4dd7c27453085c71727d4b452a388dbdafc48a7f1406184b5516ea59d9ece55f347237b440792a4e71d26ee6df2dfcd39aea379080082a67be4d7c1af810181379d3f3a444512468e43494a41e9f968c6fe13f45027a297cc24ba3113a5e1b575fa3e1246004d75264e0960052d4e14b4e1a46b24f644428ef4ad4c50455e7029fa53b4eadbe5934c234043f23296b1c235bc8ffadd28deea7415b4bfd996071179cb361822894ab54078b5ad139a7dea6889a36d1417cbbbb1eb9afa0de88d736bf81e5140df06988f2901c275f63fed880fb6a00e7ebd0d5394360ca67b0680d64cc4ba5f7c69298a265916dc4ef03bb54b5e59c0cc48f83b20cf6ec1180b2423966e78ffd94ad1b74dc6b314802ddea17036d507f44c289effd820cb43d0daac09d3ee20ee41cff1e3f2858dc2643e13fcc481d4b1d36ada547e05f789f0d1067c73949c522fd54dc0240c942cc250af3304173dcbab38f1c8292ce0036c8f0c20ceb3d5cc70cc02e5b07329640dc971a410959e89e24edf15d96a6d2cf81abcb994355051371983533f788c9bd01a8e640b1b733c2b34b7ddf7229cf81d3664d85e0cf14dcfb73f0701939f6929e725de6ea590dc0a4caf5fa6fdacc96590e43b94c6f221a703c1c5073509e6b0700eeafde7ee99e149bdbf34a5acd948a513401ba78c4db7128e1f0aac26767f8a4754ae06a41287a12a7f3059c7a405aceb105b3748264c081240c3aa3f298a0ef5f2ea93151a25a3f746082d352eb3a52fb6f860cdf0f4d2186af5e4aa744893e8a59037daa6c23d8d31d2666c528a4ce4e249a27f7aab2bf14eeb7bf8c617380a34db5b7fade8eca02f1f030a62a2ffc7f2d2b14ec366b2a4269be4c763276195ce4c95b4c77c2cc001aac54dd6496099d7ecfe1f1e316d846ab41c4ef461ae0687588ea45532fe8bf9c91cf0840200a232adaa0b8036eaf3f29e4b2d898e8fb2315c22f4915b5746c7920a0bbc98548076e8f68a2fbf3b84df590d0a3154a66d17a80a115027c066f4d5f6c69769e52268f3cea1ee86150144bc05ba63d526e611a1ef723b0b573b37eb5949dd27875208219a77d5a8f170fcecf452ea1b4c78bc135a6345c853a2621154a664806d9fbb88a61ec7935c3511aa3ede4736ee37027e5f2ef2079447886ed5a30839eee442ff8feb17acfa832a8dedb28cbb52b07a950c5dcb853a32ed2f8c0ff83adea7b060aaf2466d148ad43d8e657')
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158269407)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
undefined,
// tagSize
16,
// plaintext
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// ciphertext
asmCrypto.base64_to_bytes('kMrwkAdqy9VuEdkUA75K2hxjjy4kyRfDXMGzg+l4CoHga1/Rh49R'),
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158269407)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
null,
// tagSize
16,
// plaintext
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// ciphertext
asmCrypto.base64_to_bytes('kMrwkAdqy9VuEdkUA75K2hxjjy4kyRfDXMGzg+l4CoHga1/Rh49R'),
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158269407)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// plaintext
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// ciphertext
asmCrypto.base64_to_bytes('kMrwkAdqy9VuEdkUA75K2hxjjy4kyRfDXMGzg+l4CoHga1/Rh49R'),
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158269407)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
new Uint8Array(0),
// tagSize
16,
// plaintext
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// ciphertext
asmCrypto.base64_to_bytes('kMrwkAdqy9VuEdkUA75K2hxjjy4kyRfDXMGzg+l4CoHga1/Rh49R'),
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158797782)
// key
asmCrypto.hex_to_bytes('0f0e0d0c0b0a09080706050403020100'),
// nonce
asmCrypto.hex_to_bytes('000102030405060708090a0b'),
// adata
undefined,
// tagSize
16,
// plaintext
asmCrypto.string_to_bytes('42'),
// ciphertext
asmCrypto.hex_to_bytes('28be1ac7b43d8868869b9a45d3de436cd0cc'),
],
});
describe('GCM', () => {
const gcm_aes_vectors = [
[
// key
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
undefined,
// tagSize
16,
// input message
asmCrypto.string_to_bytes(''),
// encrypted message
asmCrypto.hex_to_bytes('58e2fccefa7e3061367f1d57a4e7455a')
],
[
// key
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// encrypted message
asmCrypto.hex_to_bytes('0388dace60b6a392f328c2b971b2fe78ab6e47d42cec13bdf53a67b21257bddf')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('cafebabefacedbaddecaf888'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255'),
// encrypted message
asmCrypto.hex_to_bytes('42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f59854d5c2af327cd64a62cf35abd2ba6fab4')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('cafebabefacedbaddecaf888'),
// adata
asmCrypto.hex_to_bytes('feedfacedeadbeeffeedfacedeadbeefabaddad2'),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39'),
// encrypted message
asmCrypto.hex_to_bytes('42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e0915bc94fbc3221a5db94fae95ae7121a47')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('cafebabefacedbad'),
// adata
asmCrypto.hex_to_bytes('feedfacedeadbeeffeedfacedeadbeefabaddad2'),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39'),
// encrypted message
asmCrypto.hex_to_bytes('61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f45983612d2e79e3b0785561be14aaca2fccb')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b'),
// adata
asmCrypto.hex_to_bytes('feedfacedeadbeeffeedfacedeadbeefabaddad2'),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39'),
// encrypted message
asmCrypto.hex_to_bytes('8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5619cc5aefffe0bfa462af43c1699d050')
],
[
// key
asmCrypto.hex_to_bytes('0000000000000000000000000000000000000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// encrypted message
asmCrypto.hex_to_bytes('cea7403d4d606b6e074ec5d3baf39d18d0d1c8a799996bf0265b98b5d48ab919')
],
[
// key
asmCrypto.hex_to_bytes('0000000000000000000000000000000000000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes(''),
// encrypted message
asmCrypto.hex_to_bytes('530f8afbc74536b9a963b4f1c4cb738b')
],
[
// key
asmCrypto.hex_to_bytes('0000000000000000000000000000000000000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.string_to_bytes(''),
// encrypted message
asmCrypto.hex_to_bytes('530f8afbc74536b9a963b4f1c4cb738b')
],
[
// key
asmCrypto.hex_to_bytes('0000000000000000000000000000000000000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// encrypted message
asmCrypto.hex_to_bytes('cea7403d4d606b6e074ec5d3baf39d18d0d1c8a799996bf0265b98b5d48ab919')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b'),
// adata
asmCrypto.hex_to_bytes('feedfacedeadbeeffeedfacedeadbeefabaddad2'),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39'),
// encrypted message
asmCrypto.hex_to_bytes('5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3fa44a8266ee1c8eb0c8b5d4cf5ae9f19a')
],
[ // Test case for issue #70 (https://github.com/vibornoff/asmcrypto.js/issues/70)
// key
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('00'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('00'),
// encrypted message
asmCrypto.hex_to_bytes('e9d60634580263ebab909efa6623dafc61')
],
[ // Test case for issue #70 (https://github.com/vibornoff/asmcrypto.js/issues/92)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
undefined,
// tagSize
16,
// input message
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// encrypted message
asmCrypto.base64_to_bytes('L3zqVYAOsRk7zMg2KsNTVShcad8TjIQ7umfsvia21QO0XTj8vaeR')
],
];
test( "asmCrypto.AES_CCM.encrypt / asmCrypto.AES_CCM.decrypt", function () {
for ( var i = 0; i < ccm_aes_vectors.length; ++i ) {
var key = ccm_aes_vectors[i][0],
nonce = ccm_aes_vectors[i][1],
adata = ccm_aes_vectors[i][2],
tagsize = ccm_aes_vectors[i][3],
clear = ccm_aes_vectors[i][4],
cipher = ccm_aes_vectors[i][5];
it("asmCrypto.AES_GCM.encrypt", function () {
for (let i = 0; i < gcm_aes_vectors.length; ++i) {
const key = gcm_aes_vectors[i][0];
const nonce = gcm_aes_vectors[i][1];
const adata = gcm_aes_vectors[i][2];
const tagsize = gcm_aes_vectors[i][3];
const cleartext = gcm_aes_vectors[i][4];
const ciphertext = gcm_aes_vectors[i][5];
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CCM.encrypt( clear, key, nonce, adata, tagsize ) ),
asmCrypto.bytes_to_hex(cipher),
"encrypt vector " + i
);
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_GCM.encrypt(cleartext, key, nonce, adata, tagsize)), 'encrypt vector ' + i).to.be.equal(asmCrypto.bytes_to_hex(ciphertext));
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CCM.decrypt( cipher, key, nonce, adata, tagsize ) ),
asmCrypto.bytes_to_hex(clear),
"decrypt vector " + i
);
}
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_GCM.decrypt(ciphertext, key, nonce, adata, tagsize)), 'decrypt vector ' + i).to.be.equal(asmCrypto.bytes_to_hex(cleartext));
}
});
}
else
{
skip( "asmCrypto.AES_CCM" );
}
});
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.AES_GCM !== 'undefined' )
{
var gcm_aes_vectors = [
[
// key
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
null,
// tagSize
16,
// input message
asmCrypto.string_to_bytes(''),
// encrypted message
asmCrypto.hex_to_bytes('58e2fccefa7e3061367f1d57a4e7455a')
],
[
// key
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// encrypted message
asmCrypto.hex_to_bytes('0388dace60b6a392f328c2b971b2fe78ab6e47d42cec13bdf53a67b21257bddf')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('cafebabefacedbaddecaf888'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255'),
// encrypted message
asmCrypto.hex_to_bytes('42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f59854d5c2af327cd64a62cf35abd2ba6fab4')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('cafebabefacedbaddecaf888'),
// adata
asmCrypto.hex_to_bytes('feedfacedeadbeeffeedfacedeadbeefabaddad2'),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39'),
// encrypted message
asmCrypto.hex_to_bytes('42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e0915bc94fbc3221a5db94fae95ae7121a47')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('cafebabefacedbad'),
// adata
asmCrypto.hex_to_bytes('feedfacedeadbeeffeedfacedeadbeefabaddad2'),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39'),
// encrypted message
asmCrypto.hex_to_bytes('61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f45983612d2e79e3b0785561be14aaca2fccb')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b'),
// adata
asmCrypto.hex_to_bytes('feedfacedeadbeeffeedfacedeadbeefabaddad2'),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39'),
// encrypted message
asmCrypto.hex_to_bytes('8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5619cc5aefffe0bfa462af43c1699d050')
],
[
// key
asmCrypto.hex_to_bytes('0000000000000000000000000000000000000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// encrypted message
asmCrypto.hex_to_bytes('cea7403d4d606b6e074ec5d3baf39d18d0d1c8a799996bf0265b98b5d48ab919')
],
[
// key
asmCrypto.hex_to_bytes('0000000000000000000000000000000000000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes(''),
// encrypted message
asmCrypto.hex_to_bytes('530f8afbc74536b9a963b4f1c4cb738b')
],
[
// key
asmCrypto.hex_to_bytes('0000000000000000000000000000000000000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.string_to_bytes(''),
// encrypted message
asmCrypto.hex_to_bytes('530f8afbc74536b9a963b4f1c4cb738b')
],
[
// key
asmCrypto.hex_to_bytes('0000000000000000000000000000000000000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('000000000000000000000000'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// encrypted message
asmCrypto.hex_to_bytes('cea7403d4d606b6e074ec5d3baf39d18d0d1c8a799996bf0265b98b5d48ab919')
],
[
// key
asmCrypto.hex_to_bytes('feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308'),
// nonce
asmCrypto.hex_to_bytes('9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b'),
// adata
asmCrypto.hex_to_bytes('feedfacedeadbeeffeedfacedeadbeefabaddad2'),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39'),
// encrypted message
asmCrypto.hex_to_bytes('5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3fa44a8266ee1c8eb0c8b5d4cf5ae9f19a')
],
[ // Test case for issue #70 (https://github.com/vibornoff/asmcrypto.js/issues/70)
// key
asmCrypto.hex_to_bytes('00000000000000000000000000000000'),
// nonce
asmCrypto.hex_to_bytes('00'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('00'),
// encrypted message
asmCrypto.hex_to_bytes('e9d60634580263ebab909efa6623dafc61')
],
[ // Test case for issue #70 (https://github.com/vibornoff/asmcrypto.js/issues/92)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
undefined,
// tagSize
16,
// input message
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// encrypted message
asmCrypto.base64_to_bytes('L3zqVYAOsRk7zMg2KsNTVShcad8TjIQ7umfsvia21QO0XTj8vaeR')
],
describe('CFB', () => {
const cfb_aes_vectors = [
[ // key
[0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c],
// iv
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f],
// clear text
[0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10],
// cipher text
[0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a,
0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f, 0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b,
0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40, 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf,
0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e, 0xea, 0xc4, 0xc6, 0x6f, 0x9f, 0xf7, 0xf2, 0xe6]
],
[ // key
[0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c],
// iv
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f],
// clear text
[0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41],
// cipher text
[0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a,
0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f, 0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b,
0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40, 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf,
0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e, 0xea, 0xc4, 0xc6]
],
[ // key
[0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4],
// iv
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f],
// clear text
[0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10],
// cipher text
[0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60,
0x39, 0xff, 0xed, 0x14, 0x3b, 0x28, 0xb1, 0xc8, 0x32, 0x11, 0x3c, 0x63, 0x31, 0xe5, 0x40, 0x7b,
0xdf, 0x10, 0x13, 0x24, 0x15, 0xe5, 0x4b, 0x92, 0xa1, 0x3e, 0xd0, 0xa8, 0x26, 0x7a, 0xe2, 0xf9,
0x75, 0xa3, 0x85, 0x74, 0x1a, 0xb9, 0xce, 0xf8, 0x20, 0x31, 0x62, 0x3d, 0x55, 0xb1, 0xe4, 0x71]
],
[ // key
[0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4],
// iv
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f],
// clear text
[0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24],
// cipher text
[0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60,
0x39, 0xff, 0xed, 0x14, 0x3b, 0x28, 0xb1, 0xc8, 0x32, 0x11, 0x3c, 0x63, 0x31, 0xe5, 0x40, 0x7b,
0xdf, 0x10, 0x13, 0x24, 0x15, 0xe5, 0x4b, 0x92, 0xa1, 0x3e, 0xd0, 0xa8, 0x26, 0x7a, 0xe2, 0xf9,
0x75, 0xa3, 0x85]
]
];
test( "asmCrypto.AES_GCM.encrypt", function () {
for ( var i = 0; i < gcm_aes_vectors.length; ++i ) {
var key = gcm_aes_vectors[i][0],
nonce = gcm_aes_vectors[i][1],
adata = gcm_aes_vectors[i][2],
tagsize = gcm_aes_vectors[i][3],
cleartext = gcm_aes_vectors[i][4],
ciphertext = gcm_aes_vectors[i][5];
it('asmCrypto.AES_CFB.encrypt / asmCrypto.AES_CFB.decrypt', function () {
for (let i = 0; i < cfb_aes_vectors.length; ++i) {
const key = new Uint8Array(cfb_aes_vectors[i][0]);
const iv = new Uint8Array(cfb_aes_vectors[i][1]);
const clear = new Uint8Array(cfb_aes_vectors[i][2]);
const cipher = new Uint8Array(cfb_aes_vectors[i][3]);
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_GCM.encrypt( cleartext, key, nonce, adata, tagsize ) ),
asmCrypto.bytes_to_hex( ciphertext ),
"encrypt vector " + i
);
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CFB.encrypt(clear, key, iv)), `encrypt vector ${i}`).to.be.equal(asmCrypto.bytes_to_hex(cipher));
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_GCM.decrypt( ciphertext, key, nonce, adata, tagsize ) ),
asmCrypto.bytes_to_hex( cleartext ),
"decrypt vector " + i
);
}
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CFB.decrypt(cipher, key, iv)), `decrypt vector ${i}`).to.be.equal(asmCrypto.bytes_to_hex(clear));
}
});
}
else
{
skip( "asmCrypto.AES_GCM" );
}
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.AES_CFB !== 'undefined' )
{
var cfb_aes_vectors = [
[ // key
[ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c ],
// iv
[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ],
// clear text
[ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ],
// cipher text
[ 0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a,
0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f, 0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b,
0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40, 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf,
0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e, 0xea, 0xc4, 0xc6, 0x6f, 0x9f, 0xf7, 0xf2, 0xe6 ]
],
[ // key
[ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c ],
// iv
[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ],
// clear text
[ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41 ],
// cipher text
[ 0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a,
0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f, 0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b,
0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40, 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf,
0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e, 0xea, 0xc4, 0xc6 ]
],
[ // key
[ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 ],
// iv
[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ],
// clear text
[ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ],
// cipher text
[ 0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60,
0x39, 0xff, 0xed, 0x14, 0x3b, 0x28, 0xb1, 0xc8, 0x32, 0x11, 0x3c, 0x63, 0x31, 0xe5, 0x40, 0x7b,
0xdf, 0x10, 0x13, 0x24, 0x15, 0xe5, 0x4b, 0x92, 0xa1, 0x3e, 0xd0, 0xa8, 0x26, 0x7a, 0xe2, 0xf9,
0x75, 0xa3, 0x85, 0x74, 0x1a, 0xb9, 0xce, 0xf8, 0x20, 0x31, 0x62, 0x3d, 0x55, 0xb1, 0xe4, 0x71 ]
],
[ // key
[ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 ],
// iv
[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ],
// clear text
[ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24 ],
// cipher text
[ 0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60,
0x39, 0xff, 0xed, 0x14, 0x3b, 0x28, 0xb1, 0xc8, 0x32, 0x11, 0x3c, 0x63, 0x31, 0xe5, 0x40, 0x7b,
0xdf, 0x10, 0x13, 0x24, 0x15, 0xe5, 0x4b, 0x92, 0xa1, 0x3e, 0xd0, 0xa8, 0x26, 0x7a, 0xe2, 0xf9,
0x75, 0xa3, 0x85 ]
]
});
describe('OFB', () => {
// key, iv, cleartext, ciphertext
const ofb_vectors = [
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('000102030405060708090A0B0C0D0E0F'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172a'),
asmCrypto.hex_to_bytes('3b3fd92eb72dad20333449f8e83cfb4a'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('50FE67CC996D32B6DA0937E99BAFEC60'),
asmCrypto.hex_to_bytes('ae2d8a571e03ac9c9eb76fac45af8e51'),
asmCrypto.hex_to_bytes('7789508d16918f03f53c52dac54ed825'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('D9A4DADA0892239F6B8B3D7680E15674'),
asmCrypto.hex_to_bytes('30c81c46a35ce411e5fbc1191a0a52ef'),
asmCrypto.hex_to_bytes('9740051e9c5fecf64344f7a82260edcc'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('A78819583F0308E7A6BF36B1386ABF23'),
asmCrypto.hex_to_bytes('f69f2445df4f9b17ad2b417be66c3710'),
asmCrypto.hex_to_bytes('304c6528f659c77866a510d9c1d6ae5e'),
],
[
asmCrypto.hex_to_bytes('8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'),
asmCrypto.hex_to_bytes('000102030405060708090A0B0C0D0E0F'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172a'),
asmCrypto.hex_to_bytes('cdc80d6fddf18cab34c25909c99a4174'),
],
[
asmCrypto.hex_to_bytes('8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'),
asmCrypto.hex_to_bytes('A609B38DF3B1133DDDFF2718BA09565E'),
asmCrypto.hex_to_bytes('ae2d8a571e03ac9c9eb76fac45af8e51'),
asmCrypto.hex_to_bytes('fcc28b8d4c63837c09e81700c1100401'),
],
[
asmCrypto.hex_to_bytes('8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'),
asmCrypto.hex_to_bytes('52EF01DA52602FE0975F78AC84BF8A50'),
asmCrypto.hex_to_bytes('30c81c46a35ce411e5fbc1191a0a52ef'),
asmCrypto.hex_to_bytes('8d9a9aeac0f6596f559c6d4daf59a5f2'),
],
[
asmCrypto.hex_to_bytes('8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'),
asmCrypto.hex_to_bytes('BD5286AC63AABD7EB067AC54B553F71D'),
asmCrypto.hex_to_bytes('f69f2445df4f9b17ad2b417be66c3710'),
asmCrypto.hex_to_bytes('6d9f200857ca6c3e9cac524bd9acc92a'),
],
[
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
asmCrypto.hex_to_bytes('000102030405060708090A0B0C0D0E0F'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172a'),
asmCrypto.hex_to_bytes('dc7e84bfda79164b7ecd8486985d3860'),
],
[
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
asmCrypto.hex_to_bytes('B7BF3A5DF43989DD97F0FA97EBCE2F4A'),
asmCrypto.hex_to_bytes('ae2d8a571e03ac9c9eb76fac45af8e51'),
asmCrypto.hex_to_bytes('4febdc6740d20b3ac88f6ad82a4fb08d'),
],
[
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
asmCrypto.hex_to_bytes('E1C656305ED1A7A6563805746FE03EDC'),
asmCrypto.hex_to_bytes('30c81c46a35ce411e5fbc1191a0a52ef'),
asmCrypto.hex_to_bytes('71ab47a086e86eedf39d1c5bba97c408'),
],
[
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
asmCrypto.hex_to_bytes('41635BE625B48AFC1666DD42A09D96E7'),
asmCrypto.hex_to_bytes('f69f2445df4f9b17ad2b417be66c3710'),
asmCrypto.hex_to_bytes('0126141d67f37be8538f5a8be740e484'),
],
];
it('asmCrypto.AES_OFB.encrypt / asmCrypto.AES_OFB.decrypt', () => {
for (let i = 0; i < ofb_vectors.length; ++i) {
const key = new Uint8Array(ofb_vectors[i][0]);
const iv = new Uint8Array(ofb_vectors[i][1]);
const clear = new Uint8Array(ofb_vectors[i][2]);
const cipher = new Uint8Array(ofb_vectors[i][3]);
test( "asmCrypto.AES_CFB.encrypt / asmCrypto.AES_CFB.decrypt", function () {
for ( var i = 0; i < cfb_aes_vectors.length; ++i ) {
var key = new Uint8Array( cfb_aes_vectors[i][0] ),
iv = new Uint8Array( cfb_aes_vectors[i][1] ),
clear = new Uint8Array( cfb_aes_vectors[i][2] ),
cipher = new Uint8Array( cfb_aes_vectors[i][3] );
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_OFB.encrypt(clear, key, iv)), 'encrypt vector ' + i).to.be.equal(asmCrypto.bytes_to_hex(cipher));
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CFB.encrypt( clear, key, iv ) ),
asmCrypto.bytes_to_hex(cipher),
"encrypt vector " + i
);
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CFB.decrypt( cipher, key, iv ) ),
asmCrypto.bytes_to_hex(clear),
"decrypt vector " + i
);
}
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_OFB.decrypt(cipher, key, iv)), 'decrypt vector ' + i).to.be.equal(asmCrypto.bytes_to_hex(clear));
}
});
}
else
{
skip( "asmCrypto.AES_CFB" );
}
testIf( asmCrypto.AES_OFB !== undefined, "asmCrypto.AES_OFB", function () {
// key, iv, cleartext, ciphertext
var ofb_vectors = [
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('000102030405060708090A0B0C0D0E0F'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172a'),
asmCrypto.hex_to_bytes('3b3fd92eb72dad20333449f8e83cfb4a'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('50FE67CC996D32B6DA0937E99BAFEC60'),
asmCrypto.hex_to_bytes('ae2d8a571e03ac9c9eb76fac45af8e51'),
asmCrypto.hex_to_bytes('7789508d16918f03f53c52dac54ed825'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('D9A4DADA0892239F6B8B3D7680E15674'),
asmCrypto.hex_to_bytes('30c81c46a35ce411e5fbc1191a0a52ef'),
asmCrypto.hex_to_bytes('9740051e9c5fecf64344f7a82260edcc'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('A78819583F0308E7A6BF36B1386ABF23'),
asmCrypto.hex_to_bytes('f69f2445df4f9b17ad2b417be66c3710'),
asmCrypto.hex_to_bytes('304c6528f659c77866a510d9c1d6ae5e'),
],
[
asmCrypto.hex_to_bytes('8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'),
asmCrypto.hex_to_bytes('000102030405060708090A0B0C0D0E0F'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172a'),
asmCrypto.hex_to_bytes('cdc80d6fddf18cab34c25909c99a4174'),
],
[
asmCrypto.hex_to_bytes('8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'),
asmCrypto.hex_to_bytes('A609B38DF3B1133DDDFF2718BA09565E'),
asmCrypto.hex_to_bytes('ae2d8a571e03ac9c9eb76fac45af8e51'),
asmCrypto.hex_to_bytes('fcc28b8d4c63837c09e81700c1100401'),
],
[
asmCrypto.hex_to_bytes('8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'),
asmCrypto.hex_to_bytes('52EF01DA52602FE0975F78AC84BF8A50'),
asmCrypto.hex_to_bytes('30c81c46a35ce411e5fbc1191a0a52ef'),
asmCrypto.hex_to_bytes('8d9a9aeac0f6596f559c6d4daf59a5f2'),
],
[
asmCrypto.hex_to_bytes('8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'),
asmCrypto.hex_to_bytes('BD5286AC63AABD7EB067AC54B553F71D'),
asmCrypto.hex_to_bytes('f69f2445df4f9b17ad2b417be66c3710'),
asmCrypto.hex_to_bytes('6d9f200857ca6c3e9cac524bd9acc92a'),
],
[
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
asmCrypto.hex_to_bytes('000102030405060708090A0B0C0D0E0F'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172a'),
asmCrypto.hex_to_bytes('dc7e84bfda79164b7ecd8486985d3860'),
],
[
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
asmCrypto.hex_to_bytes('B7BF3A5DF43989DD97F0FA97EBCE2F4A'),
asmCrypto.hex_to_bytes('ae2d8a571e03ac9c9eb76fac45af8e51'),
asmCrypto.hex_to_bytes('4febdc6740d20b3ac88f6ad82a4fb08d'),
],
[
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
asmCrypto.hex_to_bytes('E1C656305ED1A7A6563805746FE03EDC'),
asmCrypto.hex_to_bytes('30c81c46a35ce411e5fbc1191a0a52ef'),
asmCrypto.hex_to_bytes('71ab47a086e86eedf39d1c5bba97c408'),
],
[
asmCrypto.hex_to_bytes('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'),
asmCrypto.hex_to_bytes('41635BE625B48AFC1666DD42A09D96E7'),
asmCrypto.hex_to_bytes('f69f2445df4f9b17ad2b417be66c3710'),
asmCrypto.hex_to_bytes('0126141d67f37be8538f5a8be740e484'),
],
});
describe('CMAC', () => {
// key, data, result
const cmac_vectors = [
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes(''),
asmCrypto.hex_to_bytes('bb1d6929e95937287fa37d129b756746'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172a'),
asmCrypto.hex_to_bytes('070a16b46b4d4144f79bdd9dd04a287c'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411'),
asmCrypto.hex_to_bytes('dfa66747de9ae63030ca32611497c827'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'),
asmCrypto.hex_to_bytes('51f0bebf7e3b9d92fc49741779363cfe'),
],
];
it('asmCrypto.AES_CMAC', function() {
for (let i = 0; i < cmac_vectors.length; ++i) {
const key = new Uint8Array(cmac_vectors[i][0]);
const data = new Uint8Array(cmac_vectors[i][1]);
const result = new Uint8Array(cmac_vectors[i][2]);
for ( var i = 0; i < ofb_vectors.length; ++i ) {
var key = new Uint8Array( ofb_vectors[i][0] ),
iv = new Uint8Array( ofb_vectors[i][1] ),
clear = new Uint8Array( ofb_vectors[i][2] ),
cipher = new Uint8Array( ofb_vectors[i][3] );
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_OFB.encrypt( clear, key, iv ) ),
asmCrypto.bytes_to_hex( cipher ),
"encrypt vector " + i
);
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_OFB.decrypt( cipher, key, iv ) ),
asmCrypto.bytes_to_hex( clear ),
"decrypt vector " + i
);
}
});
testIf( asmCrypto.AES_CMAC !== undefined, "asmCrypto.AES_CMAC", function () {
// key, data, result
var cmac_vectors = [
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes(''),
asmCrypto.hex_to_bytes('bb1d6929e95937287fa37d129b756746'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172a'),
asmCrypto.hex_to_bytes('070a16b46b4d4144f79bdd9dd04a287c'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411'),
asmCrypto.hex_to_bytes('dfa66747de9ae63030ca32611497c827'),
],
[
asmCrypto.hex_to_bytes('2b7e151628aed2a6abf7158809cf4f3c'),
asmCrypto.hex_to_bytes('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'),
asmCrypto.hex_to_bytes('51f0bebf7e3b9d92fc49741779363cfe'),
],
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CMAC.bytes(data, key)), 'cmac vector ' + i).to.be.equal(asmCrypto.bytes_to_hex(result));
}
});
});
describe('CCM', () => {
const ccm_aes_vectors = [
[
// key
new Uint8Array([ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf ]),
// nonce
new Uint8Array([ 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 ]),
// adata
new Uint8Array([ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ]),
// tagSize
8,
// input message
new Uint8Array([ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e ]),
// encrypted message
new Uint8Array([ 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2, 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0 ]),
],
[
// key
new Uint8Array([ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf ]),
// nonce
new Uint8Array([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]),
// adata
new Uint8Array([ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x61, 0x75, 0x74, 0x68, 0x69, 0x6e, 0x66, 0x6f, 0x6f ]),
// tagSize
16,
// input message
asmCrypto.hex_to_bytes('44696420796f75206b6e6f772e2e2e0d0a46726f6d2057696b6970656469612773206e657720616e6420726563656e746c7920696d70726f76656420636f6e74656e743a0d0a5361696e74204c656f6e61726420436174686f6c69632043687572636820284d616469736f6e2c204e65627261736b61290d0a2e2e2e207468617420746865204a61636f62204d2e204e616368746967616c6c2d64657369676e65642053742e204c656f6e61726420436174686f6c696320436875726368202870696374757265642920696e204d616469736f6e2c204e65627261736b612c20636f6e7461696e73206120626f6e652072656c6963206f6620697473206e616d6573616b653f0d0a2e2e2e2074686174207468652043616e61646196536f757468204b6f72656120467265652054726164652041677265656d656e742077696c6c20656c696d696e61746520393825206f6620616c6c20696d706f72742074617269666673206265747765656e207468652074776f20636f756e74726965733f0d0a2e2e2e207468617420526f7373204d634577616e2c2043454f206f66205242532047726f75702c206f6e63652074686520776f726c642773206c6172676573742062616e6b2c207477696365206661696c656420616e206163636f756e74616e6379206578616d3f0d0a2e2e2e2074686174205475726b69736820776f6d656e2773206e6174696f6e616c2069636520686f636b657920706c617965722047697a656d20d67a74617364656c656e2069732061206d656d626572206f662068657220666174686572277320636c7562204d696c656e79756d20506174656e20534b3f0d0a2e2e2e207468617420566572697a6f6e20762e204643432028323031342920776173207265706f7274656420746f20626520746865206465617468206f66206e6574776f726b206e65757472616c6974792c20686176696e6720766163617465642074776f206f662074686520464343204f70656e20496e7465726e6574204f726465722032303130277320746872656520726567756c6174696f6e733f0d0a2e2e2e207468617420746865206d6f737320737065636965732043686f7269736f646f6e7469756d206163697068796c6c756d2063616e207375727669766520666f72206d6f7265207468616e20312c3530302079656172732066726f7a656e3f'),
// encrypted message
asmCrypto.hex_to_bytes('09e3c1f3ba2f40eeca4dd7c27453085c71727d4b452a388dbdafc48a7f1406184b5516ea59d9ece55f347237b440792a4e71d26ee6df2dfcd39aea379080082a67be4d7c1af810181379d3f3a444512468e43494a41e9f968c6fe13f45027a297cc24ba3113a5e1b575fa3e1246004d75264e0960052d4e14b4e1a46b24f644428ef4ad4c50455e7029fa53b4eadbe5934c234043f23296b1c235bc8ffadd28deea7415b4bfd996071179cb361822894ab54078b5ad139a7dea6889a36d1417cbbbb1eb9afa0de88d736bf81e5140df06988f2901c275f63fed880fb6a00e7ebd0d5394360ca67b0680d64cc4ba5f7c69298a265916dc4ef03bb54b5e59c0cc48f83b20cf6ec1180b2423966e78ffd94ad1b74dc6b314802ddea17036d507f44c289effd820cb43d0daac09d3ee20ee41cff1e3f2858dc2643e13fcc481d4b1d36ada547e05f789f0d1067c73949c522fd54dc0240c942cc250af3304173dcbab38f1c8292ce0036c8f0c20ceb3d5cc70cc02e5b07329640dc971a410959e89e24edf15d96a6d2cf81abcb994355051371983533f788c9bd01a8e640b1b733c2b34b7ddf7229cf81d3664d85e0cf14dcfb73f0701939f6929e725de6ea590dc0a4caf5fa6fdacc96590e43b94c6f221a703c1c5073509e6b0700eeafde7ee99e149bdbf34a5acd948a513401ba78c4db7128e1f0aac26767f8a4754ae06a41287a12a7f3059c7a405aceb105b3748264c081240c3aa3f298a0ef5f2ea93151a25a3f746082d352eb3a52fb6f860cdf0f4d2186af5e4aa744893e8a59037daa6c23d8d31d2666c528a4ce4e249a27f7aab2bf14eeb7bf8c617380a34db5b7fade8eca02f1f030a62a2ffc7f2d2b14ec366b2a4269be4c763276195ce4c95b4c77c2cc001aac54dd6496099d7ecfe1f1e316d846ab41c4ef461ae0687588ea45532fe8bf9c91cf0840200a232adaa0b8036eaf3f29e4b2d898e8fb2315c22f4915b5746c7920a0bbc98548076e8f68a2fbf3b84df590d0a3154a66d17a80a115027c066f4d5f6c69769e52268f3cea1ee86150144bc05ba63d526e611a1ef723b0b573b37eb5949dd27875208219a77d5a8f170fcecf452ea1b4c78bc135a6345c853a2621154a664806d9fbb88a61ec7935c3511aa3ede4736ee37027e5f2ef2079447886ed5a30839eee442ff8feb17acfa832a8dedb28cbb52b07a950c5dcb853a32ed2f8c0ff83adea7b060aaf2466d148ad43d8e657')
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158269407)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
undefined,
// tagSize
16,
// plaintext
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// ciphertext
asmCrypto.base64_to_bytes('kMrwkAdqy9VuEdkUA75K2hxjjy4kyRfDXMGzg+l4CoHga1/Rh49R'),
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158269407)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
undefined,
// tagSize
16,
// plaintext
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// ciphertext
asmCrypto.base64_to_bytes('kMrwkAdqy9VuEdkUA75K2hxjjy4kyRfDXMGzg+l4CoHga1/Rh49R'),
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158269407)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
asmCrypto.string_to_bytes(''),
// tagSize
16,
// plaintext
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// ciphertext
asmCrypto.base64_to_bytes('kMrwkAdqy9VuEdkUA75K2hxjjy4kyRfDXMGzg+l4CoHga1/Rh49R'),
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158269407)
// key
asmCrypto.base64_to_bytes('dGQhii+B7+eLLHRiOA690w=='),
// nonce
asmCrypto.base64_to_bytes('R8q1njARXS7urWv3'),
// adata
new Uint8Array(0),
// tagSize
16,
// plaintext
asmCrypto.base64_to_bytes('dGQhwoovwoHDr8OnwossdGI4DsK9w5M='),
// ciphertext
asmCrypto.base64_to_bytes('kMrwkAdqy9VuEdkUA75K2hxjjy4kyRfDXMGzg+l4CoHga1/Rh49R'),
],
[ // Test case for issue #92 (https://github.com/vibornoff/asmcrypto.js/issues/92#issuecomment-158797782)
// key
asmCrypto.hex_to_bytes('0f0e0d0c0b0a09080706050403020100'),
// nonce
asmCrypto.hex_to_bytes('000102030405060708090a0b'),
// adata
undefined,
// tagSize
16,
// plaintext
asmCrypto.string_to_bytes('42'),
// ciphertext
asmCrypto.hex_to_bytes('28be1ac7b43d8868869b9a45d3de436cd0cc'),
],
];
for ( var i = 0; i < cmac_vectors.length; ++i ) {
var key = new Uint8Array( cmac_vectors[i][0] ),
data = new Uint8Array( cmac_vectors[i][1] ),
result = new Uint8Array( cmac_vectors[i][2] );
it('asmCrypto.AES_CCM.encrypt / asmCrypto.AES_CCM.decrypt', function() {
for (let i = 0; i < ccm_aes_vectors.length; ++i) {
const key = ccm_aes_vectors[i][0];
const nonce = ccm_aes_vectors[i][1];
const adata = ccm_aes_vectors[i][2];
const tagsize = ccm_aes_vectors[i][3];
const clear = ccm_aes_vectors[i][4];
const cipher = ccm_aes_vectors[i][5];
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CMAC.bytes( data, key ) ),
asmCrypto.bytes_to_hex(result),
"cmac vector " + i
);
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CCM.encrypt(clear, key, nonce, adata, tagsize)), 'encrypt vector ' + i).to.be.equal(asmCrypto.bytes_to_hex(cipher));
}
expect(asmCrypto.bytes_to_hex(asmCrypto.AES_CCM.decrypt(cipher, key, nonce, adata, tagsize)), 'decrypt vector ' + i).to.be.equal(asmCrypto.bytes_to_hex(clear));
}
});
});
});

@@ -1,287 +0,460 @@

module("BigNumber");
import * as asmCrypto from '../asmcrypto.all.es8';
import chai from 'chai';
const expect = chai.expect;
///////////////////////////////////////////////////////////////////////////////
describe('Bignum', () => {
it('new asmCrypto.BigNumber()', function() {
const zero = new asmCrypto.BigNumber();
expect(zero.limbs.length, 'zero.limbs.length').to.equal(0);
expect(zero.bitLength, 'zero.bitLength').to.equal(0);
expect(zero.valueOf(), 'zero.valueOf()').to.equal(0);
expect(zero.toString(16), 'zero.toString(16)').to.equal('0');
if ( typeof asmCrypto.BigNumber !== 'undefined' )
{
test( "new asmCrypto.BigNumber()", function () {
var zero = new asmCrypto.BigNumber();
ok( zero, "zero = new 0" );
equal( zero.limbs.length, 0, "zero.limbs.length" );
equal( zero.bitLength, 0, "zero.bitLength" );
equal( zero.valueOf(), 0, "zero.valueOf()" );
equal( zero.toString(16), '0', "zero.toString(16)" );
const one = asmCrypto.BigNumber.fromNumber(1);
expect(one.limbs.length, 'one.limbs.length').to.equal(1);
expect(one.limbs[0], 'one.limbs[0]').to.equal(1);
expect(one.bitLength, 'one.bitLength').to.equal(32);
expect(one.valueOf(), 'one.valueOf()').to.equal(1);
expect(one.toString(16), 'one.toString(16)').to.equal('1');
var one = asmCrypto.BigNumber.fromNumber(1);
ok( one, "one = new 1" );
equal( one.limbs.length, 1, "one.limbs.length" );
equal( one.limbs[0], 1, "one.limbs[0]" );
equal( one.bitLength, 32, "one.bitLength" );
equal( one.valueOf(), 1, "one.valueOf()" );
equal( one.toString(16), '1', "one.toString(16)" );
const ten = asmCrypto.BigNumber.fromNumber(10);
expect(ten.limbs.length, 'ten.limbs.length').to.equal(1);
expect(ten.limbs[0], 'ten.limbs[0]').to.equal(10);
expect(ten.bitLength, 'ten.bitLength').to.equal(32);
expect(ten.valueOf(), 'ten.valueOf()').to.equal(10);
expect(ten.toString(16), 'ten.toString(16)').to.equal('a');
var ten = asmCrypto.BigNumber.fromNumber(10);
ok( ten, "ten = new 10" );
equal( ten.limbs.length, 1, "ten.limbs.length" );
equal( ten.limbs[0], 10, "ten.limbs[0]" );
equal( ten.bitLength, 32, "ten.bitLength" );
equal( ten.valueOf(), 10, "ten.valueOf()" );
equal( ten.toString(16), 'a', "ten.toString(16)" );
const mten = asmCrypto.BigNumber.fromNumber(-10);
expect(mten.limbs.length, 'mten.limbs.length').to.equal(1);
expect(mten.limbs[0], 'mten.limbs[0]').to.equal(10);
expect(mten.bitLength, 'mten.bitLength').to.equal(32);
expect(mten.valueOf(), 'mten.valueOf()').to.equal(-10);
expect(mten.toString(16), 'mten.toString(16)').to.equal('-a');
var mten = asmCrypto.BigNumber.fromNumber(-10);
ok( mten, "mten = new -10" );
equal( mten.limbs.length, 1, "mten.limbs.length" );
equal( mten.limbs[0], 10, "mten.limbs[0]" );
equal( mten.bitLength, 32, "mten.bitLength" );
equal( mten.valueOf(), -10, "mten.valueOf()" );
equal( mten.toString(16), '-a', "mten.toString(16)" );
const ffffffff = asmCrypto.BigNumber.fromNumber(0xffffffff);
expect(ffffffff.limbs.length, 'ffffffff.limbs.length').to.equal(1);
expect(ffffffff.limbs[0], 'ffffffff.limbs[0]').to.equal(0xffffffff);
expect(ffffffff.bitLength, 'ffffffff.bitLength').to.equal(32);
expect(ffffffff.valueOf(), 'ffffffff.valueOf()').to.equal(0xffffffff);
expect(ffffffff.toString(16), 'ffffffff.toString(16)').to.equal('ffffffff');
var ffffffff = asmCrypto.BigNumber.fromNumber(0xffffffff);
ok( ffffffff, "ffffffff = new 0xfffffff" );
equal( ffffffff.limbs.length, 1, "ffffffff.limbs.length" );
equal( ffffffff.limbs[0], 0xffffffff, "ffffffff.limbs[0]" );
equal( ffffffff.bitLength, 32, "ffffffff.bitLength" );
equal( ffffffff.valueOf(), 0xffffffff, "ffffffff.valueOf()" );
equal( ffffffff.toString(16), 'ffffffff', "ffffffff.toString(16)" );
const deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe);
expect(deadbeefcafe.limbs.length, 'deadbeefcafe.limbs.length').to.equal(2);
expect(deadbeefcafe.limbs[0], 'deadbeefcafe.limbs[0]').to.equal(0xbeefcafe);
expect(deadbeefcafe.limbs[1], 'deadbeefcafe.limbs[1]').to.equal(0xdead);
expect(deadbeefcafe.bitLength, 'deadbeefcafe.bitLength').to.equal(52);
expect(deadbeefcafe.valueOf(), 'deadbeefcafe.valueOf()').to.equal(0xdeadbeefcafe);
expect(deadbeefcafe.toString(16), 'deadbeefcafe.toString(16)').to.equal('deadbeefcafe');
var deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe);
ok( deadbeefcafe, "deadbeefcafe = new 0xdeadbeefcafe" );
equal( deadbeefcafe.limbs.length, 2, "deadbeefcafe.limbs.length" );
equal( deadbeefcafe.limbs[0], 0xbeefcafe, "deadbeefcafe.limbs[0]" );
equal( deadbeefcafe.limbs[1], 0xdead, "deadbeefcafe.limbs[1]" );
equal( deadbeefcafe.bitLength, 52, "deadbeefcafe.bitLength" );
equal( deadbeefcafe.valueOf(), 0xdeadbeefcafe, "deadbeefcafe.valueOf()" );
equal( deadbeefcafe.toString(16), 'deadbeefcafe', "deadbeefcafe.toString(16)" );
const verylarge = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc',
),
);
expect(verylarge.limbs.length, 'verylarge.limbs.length').to.equal(64);
expect(verylarge.limbs[0], 'verylarge.limbs[0]').to.equal(0xf967b6cc);
expect(verylarge.limbs[63], 'verylarge.limbs[63]').to.equal(0x3f70f29d);
expect(verylarge.bitLength, 'verylarge.bitLength').to.equal(2048);
expect(verylarge.valueOf(), 'verylarge.valueOf()').to.equal(Infinity);
expect(verylarge.toString(16), 'verylarge.toString()').to.equal(
'3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc',
);
});
var verylarge = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc'));
ok( verylarge, "verylarge = new 3f70f29d…f967b6cc" );
equal( verylarge.limbs.length, 64, "verylarge.limbs.length" );
equal( verylarge.limbs[0], 0xf967b6cc, "verylarge.limbs[0]" );
equal( verylarge.limbs[63], 0x3f70f29d, "verylarge.limbs[63]" );
equal( verylarge.bitLength, 2048, "verylarge.bitLength" );
equal( verylarge.valueOf(), Infinity, "verylarge.valueOf()" );
equal( verylarge.toString(16), '3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc', "verylarge.toString()" );
});
it('asmCrypto.BigNumber.slice', function() {
const deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe);
expect(deadbeefcafe.slice(0).valueOf(), 'slice(0)').to.equal(0xdeadbeefcafe);
expect(deadbeefcafe.slice(52).valueOf(), 'slice(bitLength)').to.equal(0);
expect(deadbeefcafe.slice(24, 16).valueOf(), 'slice(middle)').to.equal(0xadbe);
});
test( "asmCrypto.BigNumber.slice", function () {
var deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe);
equal( deadbeefcafe.slice(0).valueOf(), 0xdeadbeefcafe, "slice(0)" );
equal( deadbeefcafe.slice(52).valueOf(), 0, "slice(bitLength)" );
equal( deadbeefcafe.slice(24,16).valueOf(), 0xadbe, "slice(middle)" );
});
it('asmCrypto.BigNumber.compare', function() {
const deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe);
const ffffffff = asmCrypto.BigNumber.fromNumber(0xffffffff);
let result = null;
test( "asmCrypto.BigNumber.compare", function () {
var deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe),
ffffffff = asmCrypto.BigNumber.fromNumber(0xffffffff),
result = null;
result = ffffffff.compare(asmCrypto.BigNumber.fromNumber(0xffffffff));
expect(result, 'ffffffff == 0xffffffff').to.equal(0);
result = ffffffff.compare(asmCrypto.BigNumber.fromNumber(0xffffffff));
equal( result, 0, "ffffffff == 0xffffffff" );
result = deadbeefcafe.compare(ffffffff);
expect(result, 'deadbeefcafe > ffffffff').to.equal(1);
result = deadbeefcafe.compare(ffffffff);
equal( result, 1, "deadbeefcafe > ffffffff" );
result = ffffffff.compare(deadbeefcafe);
expect(result, 'ffffffff > deadbeefcafe').to.equal(-1);
result = ffffffff.compare(deadbeefcafe);
equal( result, -1, "ffffffff > deadbeefcafe" );
result = ffffffff.compare(asmCrypto.BigNumber.fromNumber(-10));
expect(result, 'ffffffff > -10').to.equal(1);
});
result = ffffffff.compare(asmCrypto.BigNumber.fromNumber(-10));
equal( result, 1, "ffffffff > -10" );
});
it('asmCrypto.BigNumber.add', function() {
const deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe);
const ffffffff = asmCrypto.BigNumber.fromNumber(0xffffffff);
let result = null;
test( "asmCrypto.BigNumber.add", function () {
var deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe),
ffffffff = asmCrypto.BigNumber.fromNumber(0xffffffff),
result = null;
result = deadbeefcafe.add(ffffffff);
expect(result.toString(16), 'deadbeefcafe + ffffffff').to.equal('deaebeefcafd');
result = deadbeefcafe.add(ffffffff);
equal( result.toString(16), 'deaebeefcafd', "deadbeefcafe + ffffffff" );
result = ffffffff.add(deadbeefcafe);
expect(result.toString(16), 'ffffffff + deadbeefcafe').to.equal('deaebeefcafd');
result = ffffffff.add(deadbeefcafe);
equal( result.toString(16), 'deaebeefcafd', "ffffffff + deadbeefcafe" );
result = ffffffff.add(asmCrypto.BigNumber.fromNumber(-4294967295));
expect(result.valueOf(), 'ffffffff + (-ffffffff)').to.equal(0);
result = ffffffff.add(asmCrypto.BigNumber.fromNumber(-4294967295));
equal( result.valueOf(), 0, "ffffffff + (-ffffffff)" );
result = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'),
).add(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
),
),
);
expect(result.toString(16), 'large fff…fff').to.equal(
'10000000000000000000000000000000000000000000000000000000000000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe',
);
});
result = (new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'))).add(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')));
equal( result.toString(16), '10000000000000000000000000000000000000000000000000000000000000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe', "large fff…fff" );
});
it('asmCrypto.BigNumber.subtract', function() {
const deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe);
const ffffffff = asmCrypto.BigNumber.fromNumber(0xffffffff);
let result = null;
test( "asmCrypto.BigNumber.subtract", function () {
var deadbeefcafe = asmCrypto.BigNumber.fromNumber(0xdeadbeefcafe),
ffffffff = asmCrypto.BigNumber.fromNumber(0xffffffff),
result = null;
result = deadbeefcafe.subtract(ffffffff);
expect(result.toString(16), 'deadbeefcafe - ffffffff').to.equal('deacbeefcaff');
result = deadbeefcafe.subtract(ffffffff);
equal( result.toString(16), 'deacbeefcaff', "deadbeefcafe - ffffffff" );
result = ffffffff.subtract(deadbeefcafe);
expect(result.toString(16), 'ffffffff - deadbeefcafe').to.equal('-deacbeefcaff');
result = ffffffff.subtract(deadbeefcafe);
equal( result.toString(16), '-deacbeefcaff', "ffffffff - deadbeefcafe" );
result = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
),
).subtract(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'),
),
);
expect(result.toString(16), 'large fff…fff').to.equal(
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000',
);
});
result = (new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'))).subtract(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')));
equal( result.toString(16), 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000', "large fff…fff" );
});
it('asmCrypto.BigNumber.multiply', function() {
const small = asmCrypto.BigNumber.fromNumber(0xabcdabcd);
const large = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes('322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd'),
);
const verylarge = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc',
),
);
let result = null;
test( "asmCrypto.BigNumber.multiply", function () {
var small = asmCrypto.BigNumber.fromNumber(0xabcdabcd),
large = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd')),
verylarge = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc')),
result = null;
result = small.multiply(asmCrypto.BigNumber.fromNumber(0x1000));
expect(result.toString(16), 'small product is ok').to.equal('abcdabcd000');
result = small.multiply(asmCrypto.BigNumber.fromNumber(0x1000));
equal( result.toString(16), 'abcdabcd000', "small product is ok" );
result = large.multiply(large);
expect(result.toString(16), 'large product is ok').to.equal(
'9d616b569f3248a3e8b0bdcbed25f33122fd4e63f46cdacf664809417b3af1210cfd498deef48381295f067280f14d9ec85fe251e545f5013048853daab3b89',
);
result = large.multiply(large);
equal( result.toString(16), '9d616b569f3248a3e8b0bdcbed25f33122fd4e63f46cdacf664809417b3af1210cfd498deef48381295f067280f14d9ec85fe251e545f5013048853daab3b89', "large product is ok");
result = verylarge.multiply(verylarge);
expect(result.toString(16), 'verylarge product is ok').to.equal(
'fb8c93e94a3fb8c87d267c2550011118c118f0a8ed6b1f2a611a13d05c363e90514fd4e4b4f8485b9113846168ba5cca86bfb8faadd25a5b978da0e95432a4203ca0c58ad4c34a81acb7065dc182a58e5bbca29b1ab195209a48dd6429aaa29ea2109ba8ea28302108b7b1812dcbbf4221e72e7d1283264bf0a2e2cb180e8687892ba428b88b92bcfdc228b733a02dceec5e0ee501b81b4ee68d66e320e3aae26f63cbd2db9f01e43844b1c40c68dfd2f329925cd1334a5af0f33f8ea509c1bb9c810bed4a4e5d0b91504cf56178027af972130bc3eaaac52868b3b0c554204d55470e05ff5dd70d8b70b8c385277329d0d4d0a5aa7a1c555750eaee4f1e1581ab56e3b1210e14d46393539ccb793e3a6a6f15bcf61b1e8a9acdf36db03457a37a1ae522c0129c18d08345ccc2f44352ed159db24272d4ac2de9e5f6c361477826b9d62be54468a9c9949ba0c772548dd28eabb4e195bb87a01244c3d44462aaa0ab3f22b48693650da8a1ffddde979533709f4dfb2b1a7c6fa98646deeb4b97f29d8c79f74f3f537845b99f8564ff046d35fbe108e13cf17c3f1b9390512fc57cd2f66d6ff94a455ba646a3ebc7464376b63126c869e2b722510243ee579882540e3d02e796c997fe1d43e2364314ba3190bc8ff0ba09855df3ef9cd3277b4f4ffeba6aeafc9513d89c012507cc8a471ea2ab91b24898afd6575e572aeb290',
);
result = verylarge.multiply(verylarge);
equal( result.toString(16), 'fb8c93e94a3fb8c87d267c2550011118c118f0a8ed6b1f2a611a13d05c363e90514fd4e4b4f8485b9113846168ba5cca86bfb8faadd25a5b978da0e95432a4203ca0c58ad4c34a81acb7065dc182a58e5bbca29b1ab195209a48dd6429aaa29ea2109ba8ea28302108b7b1812dcbbf4221e72e7d1283264bf0a2e2cb180e8687892ba428b88b92bcfdc228b733a02dceec5e0ee501b81b4ee68d66e320e3aae26f63cbd2db9f01e43844b1c40c68dfd2f329925cd1334a5af0f33f8ea509c1bb9c810bed4a4e5d0b91504cf56178027af972130bc3eaaac52868b3b0c554204d55470e05ff5dd70d8b70b8c385277329d0d4d0a5aa7a1c555750eaee4f1e1581ab56e3b1210e14d46393539ccb793e3a6a6f15bcf61b1e8a9acdf36db03457a37a1ae522c0129c18d08345ccc2f44352ed159db24272d4ac2de9e5f6c361477826b9d62be54468a9c9949ba0c772548dd28eabb4e195bb87a01244c3d44462aaa0ab3f22b48693650da8a1ffddde979533709f4dfb2b1a7c6fa98646deeb4b97f29d8c79f74f3f537845b99f8564ff046d35fbe108e13cf17c3f1b9390512fc57cd2f66d6ff94a455ba646a3ebc7464376b63126c869e2b722510243ee579882540e3d02e796c997fe1d43e2364314ba3190bc8ff0ba09855df3ef9cd3277b4f4ffeba6aeafc9513d89c012507cc8a471ea2ab91b24898afd6575e572aeb290', "verylarge product is ok");
result = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc',
),
).multiply(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
),
),
);
expect(result.toString(16), 'verylarge2 product is ok').to.equal(
'3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
);
});
result = (new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc'))).multiply(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')));
equal( result.toString(16), '3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', "verylarge2 product is ok");
});
it('asmCrypto.BigNumber.square', function() {
const small = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffff'));
const medium = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffffffffffff'));
const medium2 = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'),
);
const large = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes('322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd'),
);
const verylarge = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc',
),
);
let result;
test( "asmCrypto.BigNumber.square", function () {
var small = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffff')),
medium = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffffffffffff')),
medium2 = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')),
large = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd')),
verylarge = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc')),
result;
result = small.square();
expect(result.toString(16), 'small square is ok').to.equal('fffffffe00000001');
result = small.square();
equal( result.toString(16), 'fffffffe00000001', "small square is ok" );
result = medium.square();
expect(result.toString(16), 'medium square is ok').to.equal('fffffffffffffffe0000000000000001');
result = medium.square();
equal( result.toString(16), 'fffffffffffffffe0000000000000001', "medium square is ok" );
result = medium2.square();
expect(result.toString(16), 'medium2 square is ok').to.equal(
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0000000000000000000000000000000000000000000000000000000000000001',
);
result = medium2.square();
equal( result.toString(16), 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0000000000000000000000000000000000000000000000000000000000000001', "medium2 square is ok" );
result = large.square();
expect(result.toString(16), 'large square is ok').to.equal(
'9d616b569f3248a3e8b0bdcbed25f33122fd4e63f46cdacf664809417b3af1210cfd498deef48381295f067280f14d9ec85fe251e545f5013048853daab3b89',
);
result = large.square();
equal( result.toString(16), '9d616b569f3248a3e8b0bdcbed25f33122fd4e63f46cdacf664809417b3af1210cfd498deef48381295f067280f14d9ec85fe251e545f5013048853daab3b89', "large square is ok");
result = verylarge.square();
expect(result.toString(16), 'verylarge square is ok').to.equal(
'fb8c93e94a3fb8c87d267c2550011118c118f0a8ed6b1f2a611a13d05c363e90514fd4e4b4f8485b9113846168ba5cca86bfb8faadd25a5b978da0e95432a4203ca0c58ad4c34a81acb7065dc182a58e5bbca29b1ab195209a48dd6429aaa29ea2109ba8ea28302108b7b1812dcbbf4221e72e7d1283264bf0a2e2cb180e8687892ba428b88b92bcfdc228b733a02dceec5e0ee501b81b4ee68d66e320e3aae26f63cbd2db9f01e43844b1c40c68dfd2f329925cd1334a5af0f33f8ea509c1bb9c810bed4a4e5d0b91504cf56178027af972130bc3eaaac52868b3b0c554204d55470e05ff5dd70d8b70b8c385277329d0d4d0a5aa7a1c555750eaee4f1e1581ab56e3b1210e14d46393539ccb793e3a6a6f15bcf61b1e8a9acdf36db03457a37a1ae522c0129c18d08345ccc2f44352ed159db24272d4ac2de9e5f6c361477826b9d62be54468a9c9949ba0c772548dd28eabb4e195bb87a01244c3d44462aaa0ab3f22b48693650da8a1ffddde979533709f4dfb2b1a7c6fa98646deeb4b97f29d8c79f74f3f537845b99f8564ff046d35fbe108e13cf17c3f1b9390512fc57cd2f66d6ff94a455ba646a3ebc7464376b63126c869e2b722510243ee579882540e3d02e796c997fe1d43e2364314ba3190bc8ff0ba09855df3ef9cd3277b4f4ffeba6aeafc9513d89c012507cc8a471ea2ab91b24898afd6575e572aeb290',
);
});
result = verylarge.square();
equal( result.toString(16), 'fb8c93e94a3fb8c87d267c2550011118c118f0a8ed6b1f2a611a13d05c363e90514fd4e4b4f8485b9113846168ba5cca86bfb8faadd25a5b978da0e95432a4203ca0c58ad4c34a81acb7065dc182a58e5bbca29b1ab195209a48dd6429aaa29ea2109ba8ea28302108b7b1812dcbbf4221e72e7d1283264bf0a2e2cb180e8687892ba428b88b92bcfdc228b733a02dceec5e0ee501b81b4ee68d66e320e3aae26f63cbd2db9f01e43844b1c40c68dfd2f329925cd1334a5af0f33f8ea509c1bb9c810bed4a4e5d0b91504cf56178027af972130bc3eaaac52868b3b0c554204d55470e05ff5dd70d8b70b8c385277329d0d4d0a5aa7a1c555750eaee4f1e1581ab56e3b1210e14d46393539ccb793e3a6a6f15bcf61b1e8a9acdf36db03457a37a1ae522c0129c18d08345ccc2f44352ed159db24272d4ac2de9e5f6c361477826b9d62be54468a9c9949ba0c772548dd28eabb4e195bb87a01244c3d44462aaa0ab3f22b48693650da8a1ffddde979533709f4dfb2b1a7c6fa98646deeb4b97f29d8c79f74f3f537845b99f8564ff046d35fbe108e13cf17c3f1b9390512fc57cd2f66d6ff94a455ba646a3ebc7464376b63126c869e2b722510243ee579882540e3d02e796c997fe1d43e2364314ba3190bc8ff0ba09855df3ef9cd3277b4f4ffeba6aeafc9513d89c012507cc8a471ea2ab91b24898afd6575e572aeb290', "verylarge square is ok");
});
it('asmCrypto.BigNumber.divide', function() {
const small = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('95705fac129de210'));
const small2 = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('fffffffe00000002'));
const small3 = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffff'));
const large = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'9d616b569f3248a3e8b0bdcbed25f33122fd4e63f46cdacf664809417b3af1210cfd498deef48381295f067280f14d9ec85fe251e545f5013048853daab3b89',
),
);
const large2 = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes('322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd'),
);
let result = null;
test( "asmCrypto.BigNumber.divide", function () {
var small = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('95705fac129de210')),
small2 = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('fffffffe00000002')),
small3 = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ffffffff')),
large = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('9d616b569f3248a3e8b0bdcbed25f33122fd4e63f46cdacf664809417b3af1210cfd498deef48381295f067280f14d9ec85fe251e545f5013048853daab3b89')),
large2 = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd')),
result = null;
result = small.divide(asmCrypto.BigNumber.fromNumber(0xabcd));
expect(result.remainder.toString(16), 'small % 0xabcd').to.equal('aaaa');
expect(result.quotient.toString(16), 'floor( small / 0xabcd )').to.equal('deadbeefcafe');
result = small.divide(asmCrypto.BigNumber.fromNumber(0xabcd));
equal( result.remainder.toString(16), 'aaaa', "small % 0xabcd" );
equal( result.quotient.toString(16), 'deadbeefcafe', "floor( small / 0xabcd )" );
result = small2.divide(small3);
expect(result.remainder.toString(16), 'small2 % small3').to.equal('1');
expect(result.quotient.toString(16), 'floor( small2 / small3 )').to.equal('ffffffff');
result = small2.divide(small3);
equal( result.remainder.toString(16), '1', "small2 % small3" );
equal( result.quotient.toString(16), 'ffffffff', "floor( small2 / small3 )" );
result = large.divide(large2);
expect(result.remainder, 'large % large2').to.deep.equal(asmCrypto.BigNumber.ZERO);
expect(result.quotient.toString(16), 'floor( large / large2 )').to.equal(
'322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd',
);
result = large.divide(large2);
equal( result.remainder, asmCrypto.BigNumber.ZERO, "large % large2" );
equal( result.quotient.toString(16), '322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd', "floor( large / large2 )" );
result = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
),
).divide(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'c7f1bc1dfb1be82d244aef01228c1409c198894eca9e21430f1669b4aa3864c9f37f3d51b2b4ba1ab9e80f59d267fda1521e88b05117993175e004543c6e3611242f24432ce8efa3b81f0ff660b4f91c5d52f2511a6f38181a7bf9abeef72db056508bbb4eeb5f65f161dd2d5b439655d2ae7081fcc62fdcb281520911d96700c85cdaf12e7d1f15b55ade867240722425198d4ce39019550c4c8a921fc231d3e94297688c2d77cd68ee8fdeda38b7f9a274701fef23b4eaa6c1a9c15b2d77f37634930386fc20ec291be95aed9956801e1c76601b09c413ad915ff03bfdc0b6b233686ae59e8caf11750b509ab4e57ee09202239baee3d6e392d1640185e1cd',
),
),
);
expect(result.quotient.toString(16), 'q is ok').to.equal('1');
expect(result.remainder.toString(16), 'r is ok').to.equal(
'380e43e204e417d2dbb510fedd73ebf63e6776b13561debcf0e9964b55c79b360c80c2ae4d4b45e54617f0a62d98025eade1774faee866ce8a1ffbabc391c9eedbd0dbbcd317105c47e0f0099f4b06e3a2ad0daee590c7e7e58406541108d24fa9af7444b114a09a0e9e22d2a4bc69aa2d518f7e0339d0234d7eadf6ee2698ff37a3250ed182e0ea4aa521798dbf8ddbdae672b31c6fe6aaf3b3756de03dce2c16bd689773d288329711702125c748065d8b8fe010dc4b15593e563ea4d2880c89cb6cfc7903df13d6e416a51266a97fe1e3899fe4f63bec526ea00fc4023f494dcc97951a617350ee8af4af654b1a811f6dfddc64511c291c6d2e9bfe7a1e33',
);
result = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'))
.divide(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('c7f1bc1dfb1be82d244aef01228c1409c198894eca9e21430f1669b4aa3864c9f37f3d51b2b4ba1ab9e80f59d267fda1521e88b05117993175e004543c6e3611242f24432ce8efa3b81f0ff660b4f91c5d52f2511a6f38181a7bf9abeef72db056508bbb4eeb5f65f161dd2d5b439655d2ae7081fcc62fdcb281520911d96700c85cdaf12e7d1f15b55ade867240722425198d4ce39019550c4c8a921fc231d3e94297688c2d77cd68ee8fdeda38b7f9a274701fef23b4eaa6c1a9c15b2d77f37634930386fc20ec291be95aed9956801e1c76601b09c413ad915ff03bfdc0b6b233686ae59e8caf11750b509ab4e57ee09202239baee3d6e392d1640185e1cd')));
equal( result.quotient.toString(16), '1', "q is ok" );
equal( result.remainder.toString(16), '380e43e204e417d2dbb510fedd73ebf63e6776b13561debcf0e9964b55c79b360c80c2ae4d4b45e54617f0a62d98025eade1774faee866ce8a1ffbabc391c9eedbd0dbbcd317105c47e0f0099f4b06e3a2ad0daee590c7e7e58406541108d24fa9af7444b114a09a0e9e22d2a4bc69aa2d518f7e0339d0234d7eadf6ee2698ff37a3250ed182e0ea4aa521798dbf8ddbdae672b31c6fe6aaf3b3756de03dce2c16bd689773d288329711702125c748065d8b8fe010dc4b15593e563ea4d2880c89cb6cfc7903df13d6e416a51266a97fe1e3899fe4f63bec526ea00fc4023f494dcc97951a617350ee8af4af654b1a811f6dfddc64511c291c6d2e9bfe7a1e33', "r is ok" );
result = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'ad399e5f74531e554ab7e7130b8ae864c7ea09621f5fff87e07160b080e89cca0bb74448c9e792b53806bce62a0cedfed2184ea47014988c92fdbafe60771d02d5b5dcf7d4f5ac1dc0a1dd010d7ae5672efdb92b38f56b78ac54797d18a6dd363fdac5e58b68321305983c81cf4d627ed2a59c150458999e23d1d2569beb083c67fab925ae495a97acb4465aa6960d1df08a73d3f5362a53c3db3813f006d7bb7a29028d0547e918f2bb407acf60f6391b7862a1db39f26727771c61747a7766619a42864faa21d8d23317e12abbb13e0ba2ad6f7f0e3d08',
),
).divide(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'a736146b621310f6cd645cb2fefeda223aa7ae33a53ac22e019b6ffb7167d9b29be1aebb3e1a7129ee3a5b4fb1a11660932b9be2b36a6dd3226451d7c4dd79619bdb9aa5596cef4e7b6d91f0e227bba2547b004ded1ed0e06182141dc55e183374fe1d93e23c38fcc81cd8eae82647528dde963cf1ef86f470e69436a2ac0d7fa7161d6fbfd32141217df992002320cb575e8de44c446d73bdf116719d61451c474701e153a01771cb8f070f8241d465d3d0124aed70ec459669bfc4927f941ddac97f4772f8d4d55165d1d06eec147749d0b9fee868ddf3',
),
),
);
expect(result.quotient.toString(16), 'q is ok').to.equal('1');
expect(result.remainder.toString(16), 'r is ok').to.equal(
'60389f412400d5e7d538a600c8c0e428d425b2e7a253d59ded5f0b50f80c3176fd5958d8bcd218b49cc6196786bd79e3eecb2c1bcaa2ab9709969269b99a3a139da42527b88bccf45344b102b5329c4da82b8dd4bd69a984ad2655f5348c502cadca851a92bf9163d7b6396e7271b2c44c705d8126912a9b2eb3e1ff93efabcc0e49bb5ee7639568b364cc8a672ec52992be5efa8f1bce005ea21a252a5929f32e200abb1a7d1a7272c396b4d1f21d347a85056edc90621910d5c9ce1fae34886d0c33edcb14d0380cd4610bbcf9cc6c1d1f37096a55f15',
);
result = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ad399e5f74531e554ab7e7130b8ae864c7ea09621f5fff87e07160b080e89cca0bb74448c9e792b53806bce62a0cedfed2184ea47014988c92fdbafe60771d02d5b5dcf7d4f5ac1dc0a1dd010d7ae5672efdb92b38f56b78ac54797d18a6dd363fdac5e58b68321305983c81cf4d627ed2a59c150458999e23d1d2569beb083c67fab925ae495a97acb4465aa6960d1df08a73d3f5362a53c3db3813f006d7bb7a29028d0547e918f2bb407acf60f6391b7862a1db39f26727771c61747a7766619a42864faa21d8d23317e12abbb13e0ba2ad6f7f0e3d08'))
.divide(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('a736146b621310f6cd645cb2fefeda223aa7ae33a53ac22e019b6ffb7167d9b29be1aebb3e1a7129ee3a5b4fb1a11660932b9be2b36a6dd3226451d7c4dd79619bdb9aa5596cef4e7b6d91f0e227bba2547b004ded1ed0e06182141dc55e183374fe1d93e23c38fcc81cd8eae82647528dde963cf1ef86f470e69436a2ac0d7fa7161d6fbfd32141217df992002320cb575e8de44c446d73bdf116719d61451c474701e153a01771cb8f070f8241d465d3d0124aed70ec459669bfc4927f941ddac97f4772f8d4d55165d1d06eec147749d0b9fee868ddf3')));
equal( result.quotient.toString(16), '1', "q is ok" );
equal( result.remainder.toString(16), '60389f412400d5e7d538a600c8c0e428d425b2e7a253d59ded5f0b50f80c3176fd5958d8bcd218b49cc6196786bd79e3eecb2c1bcaa2ab9709969269b99a3a139da42527b88bccf45344b102b5329c4da82b8dd4bd69a984ad2655f5348c502cadca851a92bf9163d7b6396e7271b2c44c705d8126912a9b2eb3e1ff93efabcc0e49bb5ee7639568b364cc8a672ec52992be5efa8f1bce005ea21a252a5929f32e200abb1a7d1a7272c396b4d1f21d347a85056edc90621910d5c9ce1fae34886d0c33edcb14d0380cd4610bbcf9cc6c1d1f37096a55f15', "r is ok" );
result = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'ad399e5f74531e554ab7e7130b8ae864c7ea09621f5fff87e07160b080e89cca0bb74448c9e792b53806bce62a0cedfed2184ea47014988c92fdbafe60771d02d5b5dcf7d4f5ac1dc0a1dd010d7ae5672efdb92b38f56b78ac54797d18a6dd363fdac5e58b68321305983c81cf4d627ed2a59c150458999e23d1d2569beb083c67fab925ae495a97acb4465aa6960d1df08a73d3f5362a53c3db3813f006d7bb7a29028d0547e918f2bb407acf60f6391b7862a1db39f26727771c61747a7766619a42864faa21d8d23317e12abbb13e0ba2ad6f7f0e3d08',
),
).divide(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'a736146b621310f6cd645cb2fefeda223aa7ae33a53ac22e019b6ffb7167d9b29be1aebb3e1a7129ee3a5b4fb1a11660932b9be2b36a6dd3226451d7c4dd79619bdb9aa5596cef4e7b6d91f0e227bba2547b004ded1ed0e06182141dc55e183374fe1d93e23c38fcc81cd8eae82647528dde963cf1ef86f470e69436a2ac0d7fa7161d6fbfd32141217df992002320cb575e8de44c446d73bdf116719d61451c474701e153a01771cb8f070f8241d465d3d0124aed70ec459669bfc4927f941ddac97f4772f8d4d55165d1d06eec147749d0b9fee868ddf3',
),
),
);
expect(result.quotient.toString(16), 'q is ok').to.equal('1');
expect(result.remainder.toString(16), 'r is ok').to.equal(
'60389f412400d5e7d538a600c8c0e428d425b2e7a253d59ded5f0b50f80c3176fd5958d8bcd218b49cc6196786bd79e3eecb2c1bcaa2ab9709969269b99a3a139da42527b88bccf45344b102b5329c4da82b8dd4bd69a984ad2655f5348c502cadca851a92bf9163d7b6396e7271b2c44c705d8126912a9b2eb3e1ff93efabcc0e49bb5ee7639568b364cc8a672ec52992be5efa8f1bce005ea21a252a5929f32e200abb1a7d1a7272c396b4d1f21d347a85056edc90621910d5c9ce1fae34886d0c33edcb14d0380cd4610bbcf9cc6c1d1f37096a55f15',
);
result = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('ad399e5f74531e554ab7e7130b8ae864c7ea09621f5fff87e07160b080e89cca0bb74448c9e792b53806bce62a0cedfed2184ea47014988c92fdbafe60771d02d5b5dcf7d4f5ac1dc0a1dd010d7ae5672efdb92b38f56b78ac54797d18a6dd363fdac5e58b68321305983c81cf4d627ed2a59c150458999e23d1d2569beb083c67fab925ae495a97acb4465aa6960d1df08a73d3f5362a53c3db3813f006d7bb7a29028d0547e918f2bb407acf60f6391b7862a1db39f26727771c61747a7766619a42864faa21d8d23317e12abbb13e0ba2ad6f7f0e3d08'))
.divide(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('a736146b621310f6cd645cb2fefeda223aa7ae33a53ac22e019b6ffb7167d9b29be1aebb3e1a7129ee3a5b4fb1a11660932b9be2b36a6dd3226451d7c4dd79619bdb9aa5596cef4e7b6d91f0e227bba2547b004ded1ed0e06182141dc55e183374fe1d93e23c38fcc81cd8eae82647528dde963cf1ef86f470e69436a2ac0d7fa7161d6fbfd32141217df992002320cb575e8de44c446d73bdf116719d61451c474701e153a01771cb8f070f8241d465d3d0124aed70ec459669bfc4927f941ddac97f4772f8d4d55165d1d06eec147749d0b9fee868ddf3')));
equal( result.quotient.toString(16), '1', "q is ok" );
equal( result.remainder.toString(16), '60389f412400d5e7d538a600c8c0e428d425b2e7a253d59ded5f0b50f80c3176fd5958d8bcd218b49cc6196786bd79e3eecb2c1bcaa2ab9709969269b99a3a139da42527b88bccf45344b102b5329c4da82b8dd4bd69a984ad2655f5348c502cadca851a92bf9163d7b6396e7271b2c44c705d8126912a9b2eb3e1ff93efabcc0e49bb5ee7639568b364cc8a672ec52992be5efa8f1bce005ea21a252a5929f32e200abb1a7d1a7272c396b4d1f21d347a85056edc90621910d5c9ce1fae34886d0c33edcb14d0380cd4610bbcf9cc6c1d1f37096a55f15', "r is ok" );
result = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'8251a22c009e3e37af0a516bbd73662a462531e4886005cec939265e99f177d7812fd7d5df184c5ea8ced1cace7e6724a4c31b1dbdedcd9636acff51936801df9bdf255850896b4da0aebfbc8944da8385a58f47e335ce94fd53ab1b299335c9e242fd89fc87126c11df2e65efa31fc37ce90e454b72afff5db16c271d476054022227c76c039c30feba1a1bd8d62c11b5c8a9e666b7726c4306c1e84f51c6fbe4485a2826cfacaea58d4a522a48a0164aa45df489944822953f860084d34b3ac537e27af0ed95613ffcb7f775832bf3acc7beff1469ed7a28abbff4dbf08859cafc8b8a7b3a2dc02cdff66095f53897ae74d2ce47e5644ea7ac59398a99b55',
),
).divide(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'4128d116004f1f1bd78528b5deb9b315231298f2443002e7649c932f4cf8bbebc097ebeaef8c262f546768e5673f339252618d8edef6e6cb1b567fa8c9b400efcdef92ac2844b5a6d0575fde44a26d41c2d2c7a3f19ae74a7ea9d58d94c99ae4f1217ec4fe43893608ef9732f7d18fe1be748722a5b957ffaed8b6138ea3b02a011113e3b601ce187f5d0d0dec6b1608dae454f3335bb936218360f427a8e37df2242d141367d65752c6a5291524500b25522efa44ca24114a9fc3004269a59d629bf13d7876cab09ffe5bfbbac195f9d663df7f8a34f6bd1455dffa6df8442ce57e45c53d9d16e0166ffb304afa9c4bd73a696723f2b22753d62c9cc54cdab',
),
),
);
expect(result.quotient.toString(16), 'q is ok').to.equal('1');
expect(result.remainder.toString(16), 'r is ok').to.equal(
'4128d116004f1f1bd78528b5deb9b315231298f2443002e7649c932f4cf8bbebc097ebeaef8c262f546768e5673f339252618d8edef6e6cb1b567fa8c9b400efcdef92ac2844b5a6d0575fde44a26d41c2d2c7a3f19ae74a7ea9d58d94c99ae4f1217ec4fe43893608ef9732f7d18fe1be748722a5b957ffaed8b6138ea3b02a011113e3b601ce187f5d0d0dec6b1608dae454f3335bb936218360f427a8e37df2242d141367d65752c6a5291524500b25522efa44ca24114a9fc3004269a59d629bf13d7876cab09ffe5bfbbac195f9d663df7f8a34f6bd1455dffa6df8442ce57e45c53d9d16e0166ffb304afa9c4bd73a696723f2b22753d62c9cc54cdaa',
);
result = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('8251a22c009e3e37af0a516bbd73662a462531e4886005cec939265e99f177d7812fd7d5df184c5ea8ced1cace7e6724a4c31b1dbdedcd9636acff51936801df9bdf255850896b4da0aebfbc8944da8385a58f47e335ce94fd53ab1b299335c9e242fd89fc87126c11df2e65efa31fc37ce90e454b72afff5db16c271d476054022227c76c039c30feba1a1bd8d62c11b5c8a9e666b7726c4306c1e84f51c6fbe4485a2826cfacaea58d4a522a48a0164aa45df489944822953f860084d34b3ac537e27af0ed95613ffcb7f775832bf3acc7beff1469ed7a28abbff4dbf08859cafc8b8a7b3a2dc02cdff66095f53897ae74d2ce47e5644ea7ac59398a99b55'))
.divide(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('4128d116004f1f1bd78528b5deb9b315231298f2443002e7649c932f4cf8bbebc097ebeaef8c262f546768e5673f339252618d8edef6e6cb1b567fa8c9b400efcdef92ac2844b5a6d0575fde44a26d41c2d2c7a3f19ae74a7ea9d58d94c99ae4f1217ec4fe43893608ef9732f7d18fe1be748722a5b957ffaed8b6138ea3b02a011113e3b601ce187f5d0d0dec6b1608dae454f3335bb936218360f427a8e37df2242d141367d65752c6a5291524500b25522efa44ca24114a9fc3004269a59d629bf13d7876cab09ffe5bfbbac195f9d663df7f8a34f6bd1455dffa6df8442ce57e45c53d9d16e0166ffb304afa9c4bd73a696723f2b22753d62c9cc54cdab')));
equal( result.quotient.toString(16), '1', "q is ok" );
equal( result.remainder.toString(16), '4128d116004f1f1bd78528b5deb9b315231298f2443002e7649c932f4cf8bbebc097ebeaef8c262f546768e5673f339252618d8edef6e6cb1b567fa8c9b400efcdef92ac2844b5a6d0575fde44a26d41c2d2c7a3f19ae74a7ea9d58d94c99ae4f1217ec4fe43893608ef9732f7d18fe1be748722a5b957ffaed8b6138ea3b02a011113e3b601ce187f5d0d0dec6b1608dae454f3335bb936218360f427a8e37df2242d141367d65752c6a5291524500b25522efa44ca24114a9fc3004269a59d629bf13d7876cab09ffe5bfbbac195f9d663df7f8a34f6bd1455dffa6df8442ce57e45c53d9d16e0166ffb304afa9c4bd73a696723f2b22753d62c9cc54cdaa', "r is ok" );
result = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('8000000000000000')).divide(
new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('80000000ffff')),
);
expect(result.quotient.toString(16), 'q is ok').to.equal('ffff');
expect(result.remainder.toString(16), 'r is ok').to.equal('7fff0001ffff');
});
result = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('8000000000000000'))
.divide(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('80000000ffff')));
equal( result.quotient.toString(16), 'ffff', "q is ok" );
equal( result.remainder.toString(16), '7fff0001ffff', "r is ok" );
});
it('asmCrypto.BigNumber.extGCD', function() {
let z;
test( "asmCrypto.BigNumber.extGCD", function () {
var z;
z = asmCrypto.BigNumber.extGCD(asmCrypto.BigNumber.fromNumber(3), asmCrypto.BigNumber.fromNumber(2));
expect(z.gcd.valueOf(), 'gcd ok').to.equal(1);
expect(z.x.valueOf(), 'x ok').to.equal(1);
expect(z.y.valueOf(), 'y ok').to.equal(-1);
z = asmCrypto.BigNumber.extGCD(asmCrypto.BigNumber.fromNumber(3), asmCrypto.BigNumber.fromNumber(2));
equal( z.gcd.valueOf(), 1, "gcd ok" );
equal( z.x.valueOf(), 1, "x ok" );
equal( z.y.valueOf(), -1, "y ok" );
z = asmCrypto.BigNumber.extGCD(asmCrypto.BigNumber.fromNumber(240), asmCrypto.BigNumber.fromNumber(46));
expect(z.gcd.valueOf(), 'gcd ok').to.equal(2);
expect(z.x.valueOf(), 'x ok').to.equal(-9);
expect(z.y.valueOf(), 'y ok').to.equal(47);
z = asmCrypto.BigNumber.extGCD(asmCrypto.BigNumber.fromNumber(240), asmCrypto.BigNumber.fromNumber(46));
equal( z.gcd.valueOf(), 2, "gcd ok" );
equal( z.x.valueOf(), -9, "x ok" );
equal( z.y.valueOf(), 47, "y ok" );
z = asmCrypto.BigNumber.extGCD(
new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('abcdabcdabcd')),
new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('20000000000000')),
);
expect(z.gcd.valueOf(), 'gcd ok').to.equal(1);
expect(z.x.toString(16), 'x ok').to.equal('9b51de3a73905');
expect(z.y.toString(16), 'y ok').to.equal('-341e3c1e3c1e');
z = asmCrypto.BigNumber.extGCD( new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('abcdabcdabcd')), new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('20000000000000')) );
equal( z.gcd.valueOf(), 1, "gcd ok" );
equal( z.x.toString(16), '9b51de3a73905', "x ok" );
equal( z.y.toString(16), '-341e3c1e3c1e', "y ok" );
z = asmCrypto.BigNumber.extGCD(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'00fcbd95956bea867fe1250b179fb2d4e0c59c4a2fe469a01cd90509d7d7c25cdf84f77eea1fbe509269819a828959b39b8f54a38a6f0290e48c0f3c9c45b78123',
),
),
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'00d5176443bebb2a31a44df711ff7c982395c5477365f3618398fd7d37fad4d2394f9458c39dec561dab0bc6c7ced1c76b29cfd2e14ec793d5d300c70d49ada9f1',
),
),
);
expect(z.gcd.valueOf(), 'gcd ok').to.equal(1);
z = asmCrypto.BigNumber.extGCD(
new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('00fcbd95956bea867fe1250b179fb2d4e0c59c4a2fe469a01cd90509d7d7c25cdf84f77eea1fbe509269819a828959b39b8f54a38a6f0290e48c0f3c9c45b78123')),
new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('00d5176443bebb2a31a44df711ff7c982395c5477365f3618398fd7d37fad4d2394f9458c39dec561dab0bc6c7ced1c76b29cfd2e14ec793d5d300c70d49ada9f1')),
);
equal( z.gcd.valueOf(), 1, "gcd ok" );
z = asmCrypto.BigNumber.extGCD(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
),
),
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'c7f1bc1dfb1be82d244aef01228c1409c198894eca9e21430f1669b4aa3864c9f37f3d51b2b4ba1ab9e80f59d267fda1521e88b05117993175e004543c6e3611242f24432ce8efa3b81f0ff660b4f91c5d52f2511a6f38181a7bf9abeef72db056508bbb4eeb5f65f161dd2d5b439655d2ae7081fcc62fdcb281520911d96700c85cdaf12e7d1f15b55ade867240722425198d4ce39019550c4c8a921fc231d3e94297688c2d77cd68ee8fdeda38b7f9a274701fef23b4eaa6c1a9c15b2d77f37634930386fc20ec291be95aed9956801e1c76601b09c413ad915ff03bfdc0b6b233686ae59e8caf11750b509ab4e57ee09202239baee3d6e392d1640185e1cd',
),
),
);
expect(z.gcd.valueOf(), 'gcd ok').to.equal(1);
expect(z.x.toString(16), 'x ok').to.equal(
'-210fc146c2f2919c3e9a4372a7221069fa359d7feba4ecaf765c47f29819a82ebb92c5944f921e090c8f0eee5218d243b35eb488fdef6a2f9712ba887625af9599b2a547595528054f079124831d94872243009cec4e3154199893d700c2a64c3fae1e259bd37b4f88e34a6ff0fcb7c221a9b222df4a74f4d381259c641cef4d05bbbc737ac29f06e050139aa823d00c2af2b484720a58eadc39ea10d53c8664289e5495fcb188abecb167c8b81a267a24fa304b447d484c37af38525f5c1c3c7bc9e614b779e21d582c0222fa8bc13bc37673cefbb60a84a70423dcec8850d6c3c80c244e09cee87e7f6dadaf24a2b9410bc31e1afc588f9d20d769a5c3df71',
);
expect(z.y.toString(16), 'y ok').to.equal(
'2a54a02a4b2182e2ea06578065a9608f53c45bd34ab2d3c47c18bca20e2bf9d93f6ac1aecc7a4bf18cfbc073db8cd0829b656bcb1f7a52b10bdc463ac246f11a30c0cc4ea00f093fcb0b4809a2b83bfb627789c6daac33d467a2b7bcda403018b344ca065fecccd2922afd53268ea599b17b96f29fe9fa4487cd0df93db31f3197a1973fafdd5f37a9f80f2554947ed63ffa4f12f0c5eefec24e9192ddcbc19ad179f76d95e361250300f18de3f7c9a067b84ccba3b31e1d1cf4379a492aa916882e09fa6836e3524b9bf750cf8f8dddbb48dd2ac0a9cfdfe6409330c0d62f08d13ec220436482bb39db9b1c595c5e0e0b743344620ac8eb0e18b0d3c641f305',
);
});
z = asmCrypto.BigNumber.extGCD(
new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')),
new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('c7f1bc1dfb1be82d244aef01228c1409c198894eca9e21430f1669b4aa3864c9f37f3d51b2b4ba1ab9e80f59d267fda1521e88b05117993175e004543c6e3611242f24432ce8efa3b81f0ff660b4f91c5d52f2511a6f38181a7bf9abeef72db056508bbb4eeb5f65f161dd2d5b439655d2ae7081fcc62fdcb281520911d96700c85cdaf12e7d1f15b55ade867240722425198d4ce39019550c4c8a921fc231d3e94297688c2d77cd68ee8fdeda38b7f9a274701fef23b4eaa6c1a9c15b2d77f37634930386fc20ec291be95aed9956801e1c76601b09c413ad915ff03bfdc0b6b233686ae59e8caf11750b509ab4e57ee09202239baee3d6e392d1640185e1cd'))
);
equal( z.gcd.valueOf(), 1, "gcd ok" );
equal( z.x.toString(16), '-210fc146c2f2919c3e9a4372a7221069fa359d7feba4ecaf765c47f29819a82ebb92c5944f921e090c8f0eee5218d243b35eb488fdef6a2f9712ba887625af9599b2a547595528054f079124831d94872243009cec4e3154199893d700c2a64c3fae1e259bd37b4f88e34a6ff0fcb7c221a9b222df4a74f4d381259c641cef4d05bbbc737ac29f06e050139aa823d00c2af2b484720a58eadc39ea10d53c8664289e5495fcb188abecb167c8b81a267a24fa304b447d484c37af38525f5c1c3c7bc9e614b779e21d582c0222fa8bc13bc37673cefbb60a84a70423dcec8850d6c3c80c244e09cee87e7f6dadaf24a2b9410bc31e1afc588f9d20d769a5c3df71', "x ok" );
equal( z.y.toString(16), '2a54a02a4b2182e2ea06578065a9608f53c45bd34ab2d3c47c18bca20e2bf9d93f6ac1aecc7a4bf18cfbc073db8cd0829b656bcb1f7a52b10bdc463ac246f11a30c0cc4ea00f093fcb0b4809a2b83bfb627789c6daac33d467a2b7bcda403018b344ca065fecccd2922afd53268ea599b17b96f29fe9fa4487cd0df93db31f3197a1973fafdd5f37a9f80f2554947ed63ffa4f12f0c5eefec24e9192ddcbc19ad179f76d95e361250300f18de3f7c9a067b84ccba3b31e1d1cf4379a492aa916882e09fa6836e3524b9bf750cf8f8dddbb48dd2ac0a9cfdfe6409330c0d62f08d13ec220436482bb39db9b1c595c5e0e0b743344620ac8eb0e18b0d3c641f305', "y ok" );
});
it('asmCrypto.Modulus', function() {
const M = new asmCrypto.Modulus(asmCrypto.BigNumber.fromNumber(123456789));
test( "asmCrypto.Modulus", function () {
var M = new asmCrypto.Modulus(asmCrypto.BigNumber.fromNumber(123456789));
expect(M.reduce(asmCrypto.BigNumber.fromNumber(987654321)).valueOf(), 'Modulus.reduce(small)').to.equal(9);
ok( M, "new Modulus" );
equal( M.reduce(asmCrypto.BigNumber.fromNumber(987654321)).valueOf(), 9, "Modulus.reduce(small)" );
const M2 = new asmCrypto.Modulus(asmCrypto.BigNumber.fromNumber(0xabcdabcdabcd));
expect(M2.comodulus.toString(16), 'M2 comodulus ok').to.equal('10000000000000000');
expect(M2.comodulusRemainder.toString(16), 'M2 comodulus remainder ok').to.equal('624f6250624f');
expect(M2.comodulusRemainderSquare.toString(16), 'M2 comodulus remainder square ok').to.equal('399f399e399e');
expect(M2.coefficient.toString(16), 'M2 coefficent ok').to.equal('1c58c6fb');
var M2 = new asmCrypto.Modulus(asmCrypto.BigNumber.fromNumber(0xabcdabcdabcd));
equal( M2.comodulus.toString(16), '10000000000000000', "M2 comodulus ok" );
equal( M2.comodulusRemainder.toString(16), '624f6250624f', "M2 comodulus remainder ok" );
equal( M2.comodulusRemainderSquare.toString(16), '399f399e399e', "M2 comodulus remainder square ok" );
equal( M2.coefficient.toString(16), '1c58c6fb', "M2 coefficent ok" );
const M3 = new asmCrypto.Modulus(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'c7f1bc1dfb1be82d244aef01228c1409c198894eca9e21430f1669b4aa3864c9f37f3d51b2b4ba1ab9e80f59d267fda1521e88b05117993175e004543c6e3611242f24432ce8efa3b81f0ff660b4f91c5d52f2511a6f38181a7bf9abeef72db056508bbb4eeb5f65f161dd2d5b439655d2ae7081fcc62fdcb281520911d96700c85cdaf12e7d1f15b55ade867240722425198d4ce39019550c4c8a921fc231d3e94297688c2d77cd68ee8fdeda38b7f9a274701fef23b4eaa6c1a9c15b2d77f37634930386fc20ec291be95aed9956801e1c76601b09c413ad915ff03bfdc0b6b233686ae59e8caf11750b509ab4e57ee09202239baee3d6e392d1640185e1cd',
),
),
);
expect(M3.comodulus.toString(16), 'M3 comodulus ok').to.be.equal(
'100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
);
expect(M3.comodulusRemainder.toString(16), 'M3 comodulus remainder ok').to.equal(
'380e43e204e417d2dbb510fedd73ebf63e6776b13561debcf0e9964b55c79b360c80c2ae4d4b45e54617f0a62d98025eade1774faee866ce8a1ffbabc391c9eedbd0dbbcd317105c47e0f0099f4b06e3a2ad0daee590c7e7e58406541108d24fa9af7444b114a09a0e9e22d2a4bc69aa2d518f7e0339d0234d7eadf6ee2698ff37a3250ed182e0ea4aa521798dbf8ddbdae672b31c6fe6aaf3b3756de03dce2c16bd689773d288329711702125c748065d8b8fe010dc4b15593e563ea4d2880c89cb6cfc7903df13d6e416a51266a97fe1e3899fe4f63bec526ea00fc4023f494dcc97951a617350ee8af4af654b1a811f6dfddc64511c291c6d2e9bfe7a1e33',
);
expect(M3.comodulusRemainderSquare.toString(16), 'M3 comodulus remainder square ok').to.equal(
'a8d0cc3c0069b1fe694294247f367071deb9b3fdc80824536f04fae0c3df7fccc9f856aeee2033803b371a3c455522fb288c60f326db2fdcaf7452b48b0f0a29cce2dabe844a63f8077be24d2a0db5051e8a1481c16f0b880819cf8d193adaa79c92f11f1e4a2e89f24bc0ef0e2285ff218a5c058908f6feef024b0c8bfe11d37cba38103339f19ba7466f3070588152f1a008dc454cebcc4f70879e94ac1eb26179833049da7b450fbe93d7d802edc5900b3a973d05ff76c6bbb7914c59b27265222501b14497fe0ef99b7fa67777bf9ab89a8b346aacb6dbf606e68da0ba2a5c4ce3b0f85225292cd1acafebae5f553c03e9c3857730c715017550e4e77a53',
);
expect(M3.coefficient.toString(16), 'M3 coefficent ok').to.equal('39be0cfb');
var M3 = new asmCrypto.Modulus(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('c7f1bc1dfb1be82d244aef01228c1409c198894eca9e21430f1669b4aa3864c9f37f3d51b2b4ba1ab9e80f59d267fda1521e88b05117993175e004543c6e3611242f24432ce8efa3b81f0ff660b4f91c5d52f2511a6f38181a7bf9abeef72db056508bbb4eeb5f65f161dd2d5b439655d2ae7081fcc62fdcb281520911d96700c85cdaf12e7d1f15b55ade867240722425198d4ce39019550c4c8a921fc231d3e94297688c2d77cd68ee8fdeda38b7f9a274701fef23b4eaa6c1a9c15b2d77f37634930386fc20ec291be95aed9956801e1c76601b09c413ad915ff03bfdc0b6b233686ae59e8caf11750b509ab4e57ee09202239baee3d6e392d1640185e1cd')));
equal( M3.comodulus.toString(16), '100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', "M3 comodulus ok" );
equal( M3.comodulusRemainder.toString(16), '380e43e204e417d2dbb510fedd73ebf63e6776b13561debcf0e9964b55c79b360c80c2ae4d4b45e54617f0a62d98025eade1774faee866ce8a1ffbabc391c9eedbd0dbbcd317105c47e0f0099f4b06e3a2ad0daee590c7e7e58406541108d24fa9af7444b114a09a0e9e22d2a4bc69aa2d518f7e0339d0234d7eadf6ee2698ff37a3250ed182e0ea4aa521798dbf8ddbdae672b31c6fe6aaf3b3756de03dce2c16bd689773d288329711702125c748065d8b8fe010dc4b15593e563ea4d2880c89cb6cfc7903df13d6e416a51266a97fe1e3899fe4f63bec526ea00fc4023f494dcc97951a617350ee8af4af654b1a811f6dfddc64511c291c6d2e9bfe7a1e33', "M3 comodulus remainder ok" );
equal( M3.comodulusRemainderSquare.toString(16), 'a8d0cc3c0069b1fe694294247f367071deb9b3fdc80824536f04fae0c3df7fccc9f856aeee2033803b371a3c455522fb288c60f326db2fdcaf7452b48b0f0a29cce2dabe844a63f8077be24d2a0db5051e8a1481c16f0b880819cf8d193adaa79c92f11f1e4a2e89f24bc0ef0e2285ff218a5c058908f6feef024b0c8bfe11d37cba38103339f19ba7466f3070588152f1a008dc454cebcc4f70879e94ac1eb26179833049da7b450fbe93d7d802edc5900b3a973d05ff76c6bbb7914c59b27265222501b14497fe0ef99b7fa67777bf9ab89a8b346aacb6dbf606e68da0ba2a5c4ce3b0f85225292cd1acafebae5f553c03e9c3857730c715017550e4e77a53', "M3 comodulus remainder square ok" );
equal( M3.coefficient.toString(16), "39be0cfb", "M3 coefficent ok" );
const M4 = new asmCrypto.Modulus(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'453b5e276054110d94fd7b413ca54e467543c28168730315dae65c6241c847aa7ca16aa99e84e89249addf73bdb326cb7787a667cce65537cd0be7a3564f40fecace8bd6eac901013d5cd5dc28ec9415defff41e30b495bf269472e66e163493403396b14e27c1c9697e90a6274ea8dfda5bcf94d014ed7fa9c64174c78deb2ca1222521f161eafa0752dddecb31c76968929a42669174c9839b97036a2371b5d1466fe5549e53bcc7f30f7ba676950bd7a751e9e916c6525f49bf3046903ff738c5b0514ffe375ba9fe41bb766daf5973ca1d7782d84628e59b3dfa14d7c86fb3d62a219176e84e17d6fae34faa461094ae0ffd9631dd49c9138f6691a1a854',
),
),
);
expect(
M4.inverse(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'145cc156e018b9b8b3599cb8d59a07f69af5cfcbb54f00e84f70edfec80dbab9ac2f79b96adbcbeeca6050c7b043c01db9be7c3ca5ad281f788b07e4bf08404af05addd5cc9578c4211b4df57572c2248ce1de633b806847479512bc3e7f00678b5a779d8f751ae0e2cadf3fed717cf68b846a3ad3c9eb7fe6a3b8e61c93270d2055563728e09067a7cd141459e176e2c4675a8c000ca9e0ea790e4c4c64c7175bab4e16461072fb49de139cd69b59037ba9aeae357f2b456751ecf014c103c12ed0bb6335a51f5731ff7cbeaa5c7ede12ff35d7db308d1b165ad5fe425d954e07b775cda34117260702fe7f176e50d7b34240f03b3bd7ac4a32edf0fda80455',
),
),
).toString(16),
'M4 inverse OK',
).to.equal('11');
var M4 = new asmCrypto.Modulus( new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('453b5e276054110d94fd7b413ca54e467543c28168730315dae65c6241c847aa7ca16aa99e84e89249addf73bdb326cb7787a667cce65537cd0be7a3564f40fecace8bd6eac901013d5cd5dc28ec9415defff41e30b495bf269472e66e163493403396b14e27c1c9697e90a6274ea8dfda5bcf94d014ed7fa9c64174c78deb2ca1222521f161eafa0752dddecb31c76968929a42669174c9839b97036a2371b5d1466fe5549e53bcc7f30f7ba676950bd7a751e9e916c6525f49bf3046903ff738c5b0514ffe375ba9fe41bb766daf5973ca1d7782d84628e59b3dfa14d7c86fb3d62a219176e84e17d6fae34faa461094ae0ffd9631dd49c9138f6691a1a854') ));
equal( M4.inverse( new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('145cc156e018b9b8b3599cb8d59a07f69af5cfcbb54f00e84f70edfec80dbab9ac2f79b96adbcbeeca6050c7b043c01db9be7c3ca5ad281f788b07e4bf08404af05addd5cc9578c4211b4df57572c2248ce1de633b806847479512bc3e7f00678b5a779d8f751ae0e2cadf3fed717cf68b846a3ad3c9eb7fe6a3b8e61c93270d2055563728e09067a7cd141459e176e2c4675a8c000ca9e0ea790e4c4c64c7175bab4e16461072fb49de139cd69b59037ba9aeae357f2b456751ecf014c103c12ed0bb6335a51f5731ff7cbeaa5c7ede12ff35d7db308d1b165ad5fe425d954e07b775cda34117260702fe7f176e50d7b34240f03b3bd7ac4a32edf0fda80455') )).toString(16), '11', "M4 inverse OK" );
const M5 = new asmCrypto.Modulus(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('0aabbccddaabbccdd')));
expect(
M5.reduce(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('1aabbccddaabbccdd'))).toString(16),
'M5 reduce ok',
).to.equal('5544332255443323');
});
var M5 = new asmCrypto.Modulus( new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('0aabbccddaabbccdd')) );
equal( M5.reduce( new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('1aabbccddaabbccdd')) ).toString(16), '5544332255443323', "M5 reduce ok" );
});
it('asmCrypto.Modulus.power', function() {
const base = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc',
),
);
const modulus = new asmCrypto.Modulus(
new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'c7f1bc1dfb1be82d244aef01228c1409c198894eca9e21430f1669b4aa3864c9f37f3d51b2b4ba1ab9e80f59d267fda1521e88b05117993175e004543c6e3611242f24432ce8efa3b81f0ff660b4f91c5d52f2511a6f38181a7bf9abeef72db056508bbb4eeb5f65f161dd2d5b439655d2ae7081fcc62fdcb281520911d96700c85cdaf12e7d1f15b55ade867240722425198d4ce39019550c4c8a921fc231d3e94297688c2d77cd68ee8fdeda38b7f9a274701fef23b4eaa6c1a9c15b2d77f37634930386fc20ec291be95aed9956801e1c76601b09c413ad915ff03bfdc0b6b233686ae59e8caf11750b509ab4e57ee09202239baee3d6e392d1640185e1cd',
),
),
),
exponent = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes('322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd'),
);
test( "asmCrypto.Modulus.power", function () {
var base = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('3f70f29d3f3ae354a6d2536ceafba83cfc787cd91e7acd2b6bde05e62beb8295ae18e3f786726f8d034bbc15bf8331df959f59d431736d5f306aaba63dacec279484e39d76db9b527738072af15730e8b9956a64e8e4dbe868f77d1414a8a8b8bf65380a1f008d39c5fabe1a9f8343929342ab7b4f635bdc52532d764701ff3d8072c475c012ff0c59373e8bc423928d99f58c3a6d9f6ab21ee20bc8e8818fc147db09f60c81906f2c6f73dc69725f075853a89f0cd02a30a8dd86b660ccdeffc292f398efb54088c822774445a6afde471f7dd327ef9996296898a5747726ccaeeceeb2e459df98b4128cb5ab8c7cd20c563f960a1aa770f3c81f13f967b6cc')),
modulus = new asmCrypto.Modulus(new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('c7f1bc1dfb1be82d244aef01228c1409c198894eca9e21430f1669b4aa3864c9f37f3d51b2b4ba1ab9e80f59d267fda1521e88b05117993175e004543c6e3611242f24432ce8efa3b81f0ff660b4f91c5d52f2511a6f38181a7bf9abeef72db056508bbb4eeb5f65f161dd2d5b439655d2ae7081fcc62fdcb281520911d96700c85cdaf12e7d1f15b55ade867240722425198d4ce39019550c4c8a921fc231d3e94297688c2d77cd68ee8fdeda38b7f9a274701fef23b4eaa6c1a9c15b2d77f37634930386fc20ec291be95aed9956801e1c76601b09c413ad915ff03bfdc0b6b233686ae59e8caf11750b509ab4e57ee09202239baee3d6e392d1640185e1cd'))),
exponent = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('322e393f76a1c22b147e7d193c00c023afb7c1500b006ff1bc1cc8d391fc38bd'));
expect(modulus.power(base, exponent).toString(16), 'Modulus.power ok').to.equal(
'5b3823974b3eda87286d3f38499de290bd575d8b02f06720acacf3d50950f9ca0ff6b749f3be03913ddca0b291e0b263bdab6c9cb97e4ab47ee9c235ff20931a8ca358726fab93614e2c549594f5c50b1c979b34f840b6d4fc51d6feb2dd072995421d17862cb405e040fc1ed662a3245a1f97bbafa6d1f7f76c7db6a802e3037acdf01ab5053f5da518d6753477193b9c25e1720519dcb9e2f6e70d5786656d356151845a49861dfc40187eff0e85cd18b1f3f3b97c476472edfa090b868b2388edfffecc521c20df8cebb8aacfb3669b020330dd6ea64b2a3067a972b8f249bccc19347eff43893e916f0949bd5789a5cce0f8b7cd87cece909d679345c0d4',
);
});
equal( modulus.power( base, exponent ).toString(16), '5b3823974b3eda87286d3f38499de290bd575d8b02f06720acacf3d50950f9ca0ff6b749f3be03913ddca0b291e0b263bdab6c9cb97e4ab47ee9c235ff20931a8ca358726fab93614e2c549594f5c50b1c979b34f840b6d4fc51d6feb2dd072995421d17862cb405e040fc1ed662a3245a1f97bbafa6d1f7f76c7db6a802e3037acdf01ab5053f5da518d6753477193b9c25e1720519dcb9e2f6e70d5786656d356151845a49861dfc40187eff0e85cd18b1f3f3b97c476472edfa090b868b2388edfffecc521c20df8cebb8aacfb3669b020330dd6ea64b2a3067a972b8f249bccc19347eff43893e916f0949bd5789a5cce0f8b7cd87cece909d679345c0d4', "Modulus.power ok" );
});
test( "asmCrypto.isProbablePrime", function () {
var p = new asmCrypto.BigNumber(asmCrypto.hex_to_bytes('8844ae66464b4b7db53644be87617124f314a1d8243d347867c8cfd6afb595bdb88ce63538fbd6c3f8461133d77ed4f5ef403f48c65b7340c683839c00bc7874bff3e9ffe7916a3ca085c7096f31871f2d628198f9c1deaeaefa581ebaef834a89afdf663b9570287a257bd6e4f507cede3b31eda6bd7fd4f8ae3c5b8791d89f'));
ok( p.isProbablePrime(), "p is probable prime" );
});
}
else
{
skip( "asmCrypto.BigNumber" );
}
it('asmCrypto.isProbablePrime', function() {
const p = new asmCrypto.BigNumber(
asmCrypto.hex_to_bytes(
'8844ae66464b4b7db53644be87617124f314a1d8243d347867c8cfd6afb595bdb88ce63538fbd6c3f8461133d77ed4f5ef403f48c65b7340c683839c00bc7874bff3e9ffe7916a3ca085c7096f31871f2d628198f9c1deaeaefa581ebaef834a89afdf663b9570287a257bd6e4f507cede3b31eda6bd7fd4f8ae3c5b8791d89f',
),
);
expect(p.isProbablePrime(), 'p is probable prime').to.be.true;
});
});

@@ -1,110 +0,105 @@

module("RSA");
import * as asmCrypto from '../asmcrypto.all.es8';
import chai from 'chai';
const expect = chai.expect;
///////////////////////////////////////////////////////////////////////////////
const pubKey = [
asmCrypto.hex_to_bytes(
'c13f894819c136381a94c193e619851ddfcde5eca770003ec354f3142e0f61f0676d7d4215cc7a13b06e0744aa8316c9c3766cbefa30b2346fba8f1236d7e6548cf87d9578e6904fc4291e096a2737fcd96624f72e762793505f9dfc5fa17b44611add54f5c00bf54373d720cb6f4e5cabae36c4442b39dbf49158414547f453',
),
asmCrypto.hex_to_bytes('10001'),
];
if ( typeof asmCrypto.RSA !== 'undefined' )
{
var pubkey = [
asmCrypto.hex_to_bytes('c13f894819c136381a94c193e619851ddfcde5eca770003ec354f3142e0f61f0676d7d4215cc7a13b06e0744aa8316c9c3766cbefa30b2346fba8f1236d7e6548cf87d9578e6904fc4291e096a2737fcd96624f72e762793505f9dfc5fa17b44611add54f5c00bf54373d720cb6f4e5cabae36c4442b39dbf49158414547f453'),
asmCrypto.hex_to_bytes('10001')
];
const privkey = [
asmCrypto.hex_to_bytes(
'c13f894819c136381a94c193e619851ddfcde5eca770003ec354f3142e0f61f0676d7d4215cc7a13b06e0744aa8316c9c3766cbefa30b2346fba8f1236d7e6548cf87d9578e6904fc4291e096a2737fcd96624f72e762793505f9dfc5fa17b44611add54f5c00bf54373d720cb6f4e5cabae36c4442b39dbf49158414547f453',
),
asmCrypto.hex_to_bytes('10001'),
asmCrypto.hex_to_bytes(
'75497aa8a7f8fc4f50d2b82a6b9d518db027e7449adaff4b18829685c8eecd227ba3984263b896df1c55ab53a1a9ae4b06b6f9896f8fde98b4b725de882ac13fc11b614cb2cc81bcc69b9ad167dda093c5c6637754acd0ec9e9845b1b2244d597c9f63d7ea076bda19feadcdb3bd1ba9018915fec981657fb7a4301cb87a3e1',
),
asmCrypto.hex_to_bytes(
'ef2f8d91d7cd96710d6b3b5ea1b6762b4214efe329e7d0609ab8419744ef8620391e423d5890c864aebb36c0daf5035d27f3427e6a84fde36466a14b56ad1cfb',
),
asmCrypto.hex_to_bytes(
'ced5477e0acb9c836c3c54e33268e064ce8cdfd40452c8b87ab838b36b498ae22fdbdb331f59f61dd3ca1512143e77a68f8f2400dbe9e576a000084e6fcbb689',
),
asmCrypto.hex_to_bytes(
'227882f9a2d5513a27c9ed7b7ce8d3ecf61018666fb2a5f85633f9d7f82a60f521e6377ba9d8ebd87eca2260f6ed5ab7c13b30b91156eb542b331349cd4b13a3',
),
asmCrypto.hex_to_bytes(
'4dea2a3460fcb2c90f4ceaed6b5ff6a802e72eaa3fb6afc64ef476e79fd2e46eb078b1ea60351371c906a7495836effbdeb89d67757076f068f59a2b7211db81',
),
asmCrypto.hex_to_bytes(
'261a93613a93e438fa62858758d1db3b3db8366319517c039acfcc0ce04cd0d7349d7e8d8cb0e8a05ac966d04c18c81c49025de2b50bb87f78facccd19cd8602',
),
];
var privkey = [
asmCrypto.hex_to_bytes('c13f894819c136381a94c193e619851ddfcde5eca770003ec354f3142e0f61f0676d7d4215cc7a13b06e0744aa8316c9c3766cbefa30b2346fba8f1236d7e6548cf87d9578e6904fc4291e096a2737fcd96624f72e762793505f9dfc5fa17b44611add54f5c00bf54373d720cb6f4e5cabae36c4442b39dbf49158414547f453'),
asmCrypto.hex_to_bytes('10001'),
asmCrypto.hex_to_bytes('75497aa8a7f8fc4f50d2b82a6b9d518db027e7449adaff4b18829685c8eecd227ba3984263b896df1c55ab53a1a9ae4b06b6f9896f8fde98b4b725de882ac13fc11b614cb2cc81bcc69b9ad167dda093c5c6637754acd0ec9e9845b1b2244d597c9f63d7ea076bda19feadcdb3bd1ba9018915fec981657fb7a4301cb87a3e1'),
asmCrypto.hex_to_bytes('ef2f8d91d7cd96710d6b3b5ea1b6762b4214efe329e7d0609ab8419744ef8620391e423d5890c864aebb36c0daf5035d27f3427e6a84fde36466a14b56ad1cfb'),
asmCrypto.hex_to_bytes('ced5477e0acb9c836c3c54e33268e064ce8cdfd40452c8b87ab838b36b498ae22fdbdb331f59f61dd3ca1512143e77a68f8f2400dbe9e576a000084e6fcbb689'),
asmCrypto.hex_to_bytes('227882f9a2d5513a27c9ed7b7ce8d3ecf61018666fb2a5f85633f9d7f82a60f521e6377ba9d8ebd87eca2260f6ed5ab7c13b30b91156eb542b331349cd4b13a3'),
asmCrypto.hex_to_bytes('4dea2a3460fcb2c90f4ceaed6b5ff6a802e72eaa3fb6afc64ef476e79fd2e46eb078b1ea60351371c906a7495836effbdeb89d67757076f068f59a2b7211db81'),
asmCrypto.hex_to_bytes('261a93613a93e438fa62858758d1db3b3db8366319517c039acfcc0ce04cd0d7349d7e8d8cb0e8a05ac966d04c18c81c49025de2b50bb87f78facccd19cd8602')
];
describe('RSA', () => {
it('asmCrypto.RSA.privateKey', function() {
const m = new asmCrypto.Modulus(new asmCrypto.BigNumber(privkey[0]));
const e = new asmCrypto.BigNumber(privkey[1]);
const d = new asmCrypto.BigNumber(privkey[2]);
const p = new asmCrypto.BigNumber(privkey[3]);
const q = new asmCrypto.BigNumber(privkey[4]);
const dp = new asmCrypto.BigNumber(privkey[5]);
const dq = new asmCrypto.BigNumber(privkey[6]);
const qi = new asmCrypto.BigNumber(privkey[7]);
test( "asmCrypto.RSA", function () {
equal( typeof asmCrypto.RSA, 'object', "RSA exported" );
});
expect(p.multiply(q).toString(16), 'm == p*q').to.equal(m.toString(16));
expect(
e
.multiply(d)
.divide(p.subtract(asmCrypto.BigNumber.fromNumber(1)).multiply(q.subtract(asmCrypto.BigNumber.fromNumber(1))))
.remainder.toString(16),
'e*d == 1 mod (p-1)(q-1)',
).to.equal('1');
expect(
d.divide(p.subtract(asmCrypto.BigNumber.fromNumber(1))).remainder.toString(16),
'dp == d mod (p-1)',
).to.equal(dp.toString(16));
expect(
d.divide(q.subtract(asmCrypto.BigNumber.fromNumber(1))).remainder.toString(16),
'dq == d mod (q-1)',
).to.equal(dq.toString(16));
expect(
qi
.multiply(q)
.divide(p)
.remainder.toString(16),
'qi*q == 1 mod p',
).to.equal('1');
expect(m.slice(m.bitLength - 1).valueOf(), 'm highest bit is 1').to.equal(1);
});
});
test( "asmCrypto.RSA.generateKey", function () {
var key = asmCrypto.RSA.generateKey( 1024, 3 );
ok( key, "generateKey" );
describe('RSA-OAEP', () => {
it('asmCrypto.RSA_OAEP_SHA256 encrypt/decrypt', function() {
const cleartext = asmCrypto.string_to_bytes('HelloWorld!');
const rsaOaepEnc = new asmCrypto.RSA_OAEP(pubKey, new asmCrypto.Sha256(), asmCrypto.string_to_bytes('test'));
const rsaOaepDec = new asmCrypto.RSA_OAEP(privkey, new asmCrypto.Sha256(), asmCrypto.string_to_bytes('test'));
var m = new asmCrypto.Modulus( new asmCrypto.BigNumber(key[0]) ),
e = new asmCrypto.BigNumber( key[1] ),
d = new asmCrypto.BigNumber( key[2] ),
p = new asmCrypto.BigNumber( key[3] ),
q = new asmCrypto.BigNumber( key[4] ),
dp = new asmCrypto.BigNumber( key[5] ),
dq = new asmCrypto.BigNumber( key[6] ),
qi = new asmCrypto.BigNumber( key[7] );
const ciphertext = rsaOaepEnc.encrypt(cleartext);
equal( p.multiply(q).toString(16), m.toString(16), "m == p*q" );
equal( e.multiply(d).divide(p.subtract(asmCrypto.BigNumber.fromNumber(1)).multiply(q.subtract(asmCrypto.BigNumber.fromNumber(1)))).remainder.toString(16), '1', "e*d == 1 mod (p-1)(q-1)" );
equal( d.divide(p.subtract(asmCrypto.BigNumber.fromNumber(1))).remainder.toString(16), dp.toString(16), "dp == d mod (p-1)" );
equal( d.divide(q.subtract(asmCrypto.BigNumber.fromNumber(1))).remainder.toString(16), dq.toString(16), "dq == d mod (q-1)" );
equal( qi.multiply(q).divide(p).remainder.toString(16), '1', "qi*q == 1 mod p" );
equal( m.slice(m.bitLength-1).valueOf(), 1, "m highest bit is 1" );
});
}
else
{
skip( "asmCrypto.RSA" );
}
const result = rsaOaepDec.decrypt(ciphertext);
expect(asmCrypto.bytes_to_string(result), 'decrypt').to.equal('HelloWorld!');
});
});
///////////////////////////////////////////////////////////////////////////////
describe('RSA-PSS-SHA256', () => {
it('asmCrypto.RSA_PSS_SHA256 sign/verify', function() {
const text = 'HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!';
const rsaPssSign = new asmCrypto.RSA_PSS(privkey, new asmCrypto.Sha256());
const rsaPssVerify = new asmCrypto.RSA_PSS(pubKey, new asmCrypto.Sha256());
if ( typeof asmCrypto.RSA_RAW !== 'undefined' )
{
test( "asmCrypto.RSA_RAW.encrypt", function () {
var text = String.fromCharCode(1);
const signature = rsaPssSign.sign(asmCrypto.string_to_bytes(text));
rsaPssVerify.verify(signature, asmCrypto.string_to_bytes(text));
});
var ciphertext = asmCrypto.RSA_RAW.encrypt( text, pubkey );
equal( asmCrypto.bytes_to_hex(ciphertext).replace(/^0+/,''), '1', "ident encrypt" );
var result = asmCrypto.RSA_RAW.decrypt( ciphertext, privkey );
equal( asmCrypto.bytes_to_hex(result).replace(/^0+/,''), '1', "ident decrypt" );
});
}
else
{
skip( "asmCrypto.RSA_RAW" );
}
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.RSA_OAEP_SHA256 !== 'undefined' )
{
test( "asmCrypto.RSA_OAEP_SHA256 encrypt/decrypt", function () {
var cleartext = asmCrypto.string_to_bytes('HelloWorld!');
var ciphertext = asmCrypto.RSA_OAEP_SHA256.encrypt( cleartext, pubkey, 'test' );
ok( ciphertext, "encrypt" );
var result = asmCrypto.RSA_OAEP_SHA256.decrypt( ciphertext, privkey, 'test' );
equal( asmCrypto.bytes_to_string(result), 'HelloWorld!', "decrypt" );
});
}
else
{
skip( "asmCrypto.RSA_OAEP_SHA256" );
}
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.RSA_PSS_SHA256 !== 'undefined' )
{
test( "asmCrypto.RSA_PSS_SHA256 sign/verify", function () {
var text = 'HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!';
var signature = asmCrypto.RSA_PSS_SHA256.sign( text, privkey );
ok( signature, "sign" );
var result = asmCrypto.RSA_PSS_SHA256.verify( signature, text, pubkey );
ok( result, "verify" );
});
test( "asmCrypto.RSA_PSS_SHA256 verify OpenSSL-signed-data", function () {
var key = [
asmCrypto.hex_to_bytes('f30be5ce8941c8e6e764c78d12f3ce6e02a0dea03577bc0c16029de258321b74ceb43ea94f768aec900011c78eb247ab0e94b4477ea8f086ba7b5ce4b03c0ad7e0bf2f54ed509a536a0f179e27db539f729b38a279873f7b3a360690c8390e289dedca6da1ba232d8edc3c1eb229e1072716ddf3ef88caf4a824c152d6ad38f1'),
asmCrypto.hex_to_bytes('10001'),
/*
it('asmCrypto.RSA_PSS_SHA256 verify OpenSSL-signed-data', function() {
const key = [
asmCrypto.hex_to_bytes(
'f30be5ce8941c8e6e764c78d12f3ce6e02a0dea03577bc0c16029de258321b74ceb43ea94f768aec900011c78eb247ab0e94b4477ea8f086ba7b5ce4b03c0ad7e0bf2f54ed509a536a0f179e27db539f729b38a279873f7b3a360690c8390e289dedca6da1ba232d8edc3c1eb229e1072716ddf3ef88caf4a824c152d6ad38f1',
),
asmCrypto.hex_to_bytes('10001'),
/*
asmCrypto.hex_to_bytes('a2f4032c2ad2b4843bf851e2c0263eed7b4da875f9e3416d4904901ec5cb32a56a416711d5794143c278897326b5595fd2f2d8bc66ab96387ea75f6ce4cc1ce7ba0269a49ce03eb4aea16ca914938e88e5398b10b314276ba9f3f2e448a5f643515ee591cb4c4c5270edccacf7e5b88f86a0c08dc05311513a4ed01802de2511'),

@@ -117,17 +112,15 @@ asmCrypto.hex_to_bytes('fc592285e370d57900bfd2f8c66b15274b3381ca7ec485091d5aa0092ca8f2b97f8796e608a2fc6aa1df3647b10198c49801e3201fefa72ef9d7ccafcdae5d37'),

*/
];
];
var text = 'Hello There!';
const text = 'Hello There!';
var signature = asmCrypto.hex_to_bytes('A68BE713861409B4E536C12066B3D30650C7578F9B7AB61C1A302B42ECA14D58AE11899BC55FCB838F0AE06B99381DE26CE8D6318BD59BBFC4FFF56A995E9EFB0306FF105766F508297D1E74F22648B6BD66C18E06F4748BD258358ECB5BB722AC4AFFA146C04EE7BE84AD77ED2A84B5458D6CA4A7DA4D86DAB3F2B39FD647F4');
const signature = asmCrypto.hex_to_bytes(
'A68BE713861409B4E536C12066B3D30650C7578F9B7AB61C1A302B42ECA14D58AE11899BC55FCB838F0AE06B99381DE26CE8D6318BD59BBFC4FFF56A995E9EFB0306FF105766F508297D1E74F22648B6BD66C18E06F4748BD258358ECB5BB722AC4AFFA146C04EE7BE84AD77ED2A84B5458D6CA4A7DA4D86DAB3F2B39FD647F4',
);
var saltlen = 32;
const saltlen = 32;
var result = asmCrypto.RSA_PSS_SHA256.verify( signature, text, key, saltlen );
ok( result, "verify" );
});
}
else
{
skip( "asmCrypto.RSA_PSS_SHA256" );
}
const rsaPss = new asmCrypto.RSA_PSS(key, new asmCrypto.Sha256(), saltlen);
rsaPss.verify(signature, asmCrypto.string_to_bytes(text));
});
});

@@ -1,98 +0,139 @@

module("SHA1");
import * as asmCrypto from '../asmcrypto.all.es8';
import chai from 'chai';
const expect = chai.expect;
///////////////////////////////////////////////////////////////////////////////
describe('SHA1', () => {
const sha1_vectors = [
['a9993e364706816aba3e25717850c26c9cd0d89d', asmCrypto.string_to_bytes('abc')],
['84983e441c3bd26ebaae4aa1f95129e5e54670f1', asmCrypto.string_to_bytes('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')],
[
'a49b2446a02c645bf419f995b67091253a04a259',
asmCrypto.string_to_bytes('abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'),
],
['2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', asmCrypto.string_to_bytes('The quick brown fox jumps over the lazy dog')],
['de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3', asmCrypto.string_to_bytes('The quick brown fox jumps over the lazy cog')],
];
if ( typeof asmCrypto.SHA1 !== 'undefined' )
{
var sha1_vectors = [
[ 'a9993e364706816aba3e25717850c26c9cd0d89d', 'abc'],
[ '84983e441c3bd26ebaae4aa1f95129e5e54670f1', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'],
[ 'a49b2446a02c645bf419f995b67091253a04a259', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'],
[ '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', 'The quick brown fox jumps over the lazy dog' ],
[ 'de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3', 'The quick brown fox jumps over the lazy cog' ]
];
it('asmCrypto.SHA1.hex', function() {
for (let i = 0; i < sha1_vectors.length; ++i) {
const sha1 = new asmCrypto.Sha1();
expect(asmCrypto.bytes_to_hex(sha1.process(sha1_vectors[i][1]).finish().result), 'vector ' + i).to.equal(
sha1_vectors[i][0],
);
}
});
});
test( "asmCrypto.SHA1.hex", function () {
for ( var i = 0; i < sha1_vectors.length; ++i ) {
equal(
asmCrypto.SHA1.hex( sha1_vectors[i][1] ),
sha1_vectors[i][0],
"vector " + i
);
}
});
}
else
{
skip( "asmCrypto.SHA1" );
}
describe('HMAC-SHA1', () => {
const hmac_sha1_vectors = [
[
'5fd596ee78d5553c8ff4e72d266dfd192366da29',
asmCrypto.hex_to_bytes(
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f',
),
asmCrypto.hex_to_bytes('53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e'),
],
['fbdb1d1b18aa6c08324b7d64b71fb76370690e1d', asmCrypto.string_to_bytes(''), asmCrypto.string_to_bytes('')],
[
'de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9',
asmCrypto.string_to_bytes('key'),
asmCrypto.string_to_bytes('The quick brown fox jumps over the lazy dog'),
],
[
'b617318655057264e28bc0b6fb378c8ef146be00',
asmCrypto.hex_to_bytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'),
asmCrypto.string_to_bytes('Hi There'),
],
[
'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79',
asmCrypto.string_to_bytes('Jefe'),
asmCrypto.string_to_bytes('what do ya want for nothing?'),
],
];
///////////////////////////////////////////////////////////////////////////////
it('asmCrypto.HMAC_SHA1.hex', function() {
for (let i = 0; i < hmac_sha1_vectors.length; ++i) {
const hmacSha1 = new asmCrypto.HmacSha1(hmac_sha1_vectors[i][1]);
if ( typeof asmCrypto.HMAC_SHA1 !== 'undefined' )
{
var hmac_sha1_vectors = [
[ '5fd596ee78d5553c8ff4e72d266dfd192366da29', asmCrypto.hex_to_bytes('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f'), asmCrypto.hex_to_bytes('53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e') ],
[ 'fbdb1d1b18aa6c08324b7d64b71fb76370690e1d', '', '' ],
[ 'de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9', 'key', 'The quick brown fox jumps over the lazy dog' ],
[ 'b617318655057264e28bc0b6fb378c8ef146be00', asmCrypto.hex_to_bytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), 'Hi There' ],
[ 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', 'Jefe', 'what do ya want for nothing?']
];
expect(asmCrypto.bytes_to_hex(hmacSha1.process(hmac_sha1_vectors[i][2]).finish().result), 'vector ' + i).to.equal(
hmac_sha1_vectors[i][0],
);
}
});
});
test( "asmCrypto.HMAC_SHA1.hex", function () {
for ( var i = 0; i < hmac_sha1_vectors.length; ++i ) {
equal(
asmCrypto.HMAC_SHA1.hex( hmac_sha1_vectors[i][2], hmac_sha1_vectors[i][1] ),
hmac_sha1_vectors[i][0],
"vector " + i
);
}
});
}
else
{
skip( "asmCrypto.HMAC_SHA1" );
}
describe('PBKDF2-HMAC-SHA1', function () {
this.timeout(30000);
const pbkdf2_hmac_sha1_vectors = [
[
'0c60c80f961f0e71f3a9b524af6012062fe037a6',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
1,
20,
],
[
'ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
2,
20,
],
[
'4b007901b765489abead49d926f721d065a429c1',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
4096,
20,
],
[
'eefe3d61cd4da4e4e9945b3d6ba2158c2634e984',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
16777216,
20,
],
[
'3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038',
asmCrypto.string_to_bytes('passwordPASSWORDpassword'),
asmCrypto.string_to_bytes('saltSALTsaltSALTsaltSALTsaltSALTsalt'),
4096,
25,
],
[
'56fa6aa75548099dcc37d7f03425e0c3',
asmCrypto.string_to_bytes('pass\0word'),
asmCrypto.string_to_bytes('sa\0lt'),
4096,
16,
],
];
///////////////////////////////////////////////////////////////////////////////
it('asmCrypto.PBKDF2_HMAC_SHA1.hex', function() {
for (let i = 0; i < pbkdf2_hmac_sha1_vectors.length; ++i) {
expect(
// got
asmCrypto.bytes_to_hex(
asmCrypto.Pbkdf2HmacSha1(
pbkdf2_hmac_sha1_vectors[i][1], // password
pbkdf2_hmac_sha1_vectors[i][2], // salt
pbkdf2_hmac_sha1_vectors[i][3], // count
pbkdf2_hmac_sha1_vectors[i][4], // dklen
),
),
if ( typeof asmCrypto.PBKDF2_HMAC_SHA1 !== 'undefined' )
{
var pbkdf2_hmac_sha1_vectors = [
[ '0c60c80f961f0e71f3a9b524af6012062fe037a6', 'password', 'salt', 1, 20 ],
[ 'ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957', 'password', 'salt', 2, 20 ],
[ '4b007901b765489abead49d926f721d065a429c1', 'password', 'salt', 4096, 20 ],
//[ 'eefe3d61cd4da4e4e9945b3d6ba2158c2634e984', 'password', 'salt', 16777216, 20 ],
[ '3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038', 'passwordPASSWORDpassword', 'saltSALTsaltSALTsaltSALTsaltSALTsalt', 4096, 25 ],
[ '56fa6aa75548099dcc37d7f03425e0c3', "pass\0word", "sa\0lt", 4096, 16 ]
];
test( "asmCrypto.PBKDF2_HMAC_SHA1.hex", function () {
for ( var i = 0; i < pbkdf2_hmac_sha1_vectors.length; ++i ) {
equal(
// got
asmCrypto.PBKDF2_HMAC_SHA1.hex(
pbkdf2_hmac_sha1_vectors[i][1], // password
pbkdf2_hmac_sha1_vectors[i][2], // salt
pbkdf2_hmac_sha1_vectors[i][3], // count
pbkdf2_hmac_sha1_vectors[i][4] // dklen
),
// expect
pbkdf2_hmac_sha1_vectors[i][0],
// comment
"asmCrypto.PBKDF2_HMAC_SHA1.hex('"
+pbkdf2_hmac_sha1_vectors[i][1]+"', '"
+pbkdf2_hmac_sha1_vectors[i][2]+"', '"
+pbkdf2_hmac_sha1_vectors[i][3]+"', '"
+pbkdf2_hmac_sha1_vectors[i][4]
+"') is equal to '"+pbkdf2_hmac_sha1_vectors[i][0]+"'"
);
}
});
}
else
{
skip( "asmCrypto.PBKDF2_HMAC_SHA1" );
}
"asmCrypto.PBKDF2_HMAC_SHA1.hex('" +
pbkdf2_hmac_sha1_vectors[i][1] +
"', '" +
pbkdf2_hmac_sha1_vectors[i][2] +
"', '" +
pbkdf2_hmac_sha1_vectors[i][3] +
"', '" +
pbkdf2_hmac_sha1_vectors[i][4] +
"') is equal to '" +
pbkdf2_hmac_sha1_vectors[i][0] +
"'",
).to.equal(pbkdf2_hmac_sha1_vectors[i][0]);
}
});
});

@@ -1,99 +0,140 @@

module("SHA256");
import * as asmCrypto from '../asmcrypto.all.es8';
import chai from 'chai';
const expect = chai.expect;
///////////////////////////////////////////////////////////////////////////////
describe('SHA256', () => {
const sha256_vectors = [
['e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', asmCrypto.string_to_bytes('')],
['ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', asmCrypto.string_to_bytes('abc')],
['f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650', asmCrypto.string_to_bytes('message digest')],
[
'f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d',
asmCrypto.string_to_bytes('secure hash algorithm'),
],
[
'6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630',
asmCrypto.string_to_bytes('SHA256 is considered to be safe'),
],
[
'248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
asmCrypto.string_to_bytes('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'),
],
[
'f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342',
asmCrypto.string_to_bytes('For this sample, this 63-byte string will be used as input data'),
],
[
'ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8',
asmCrypto.string_to_bytes('This is exactly 64 bytes long, not counting the terminating byte'),
],
];
if ( typeof asmCrypto.SHA256 !== 'undefined' )
{
var sha256_vectors = [
[ 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', '' ],
[ 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', 'abc' ],
[ 'f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650', 'message digest' ],
[ 'f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d', 'secure hash algorithm' ],
[ '6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630', 'SHA256 is considered to be safe' ],
[ '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' ],
[ 'f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342', 'For this sample, this 63-byte string will be used as input data' ],
[ 'ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8', 'This is exactly 64 bytes long, not counting the terminating byte' ]
];
it('asmCrypto.SHA256.hex', function() {
for (let i = 0; i < sha256_vectors.length; ++i) {
const sha256 = new asmCrypto.Sha256();
expect(asmCrypto.bytes_to_hex(sha256.process(sha256_vectors[i][1]).finish().result), 'vector ' + i).to.equal(
sha256_vectors[i][0],
);
}
});
});
describe('HMAC-SHA256', () => {
const hmac_sha256_vectors = [
[
'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad',
asmCrypto.string_to_bytes(''),
asmCrypto.string_to_bytes(''),
],
[
'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8',
asmCrypto.string_to_bytes('key'),
asmCrypto.string_to_bytes('The quick brown fox jumps over the lazy dog'),
],
[
'b54d57e9b21940b6496b58d5ac120eda9f1637788b5df058928637f2eca40cd9',
asmCrypto.string_to_bytes('MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword'),
asmCrypto.string_to_bytes('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'),
],
];
test( "asmCrypto.SHA256.hex", function () {
for ( var i = 0; i < sha256_vectors.length; ++i ) {
equal(
asmCrypto.SHA256.hex( sha256_vectors[i][1] ),
sha256_vectors[i][0],
"vector " + i
);
}
});
}
else
{
skip( "asmCrypto.SHA256" );
}
it('asmCrypto.HMAC_SHA256.hex', function() {
for (let i = 0; i < hmac_sha256_vectors.length; ++i) {
const hmacSha256 = new asmCrypto.HmacSha256(hmac_sha256_vectors[i][1]);
expect(
asmCrypto.bytes_to_hex(hmacSha256.process(hmac_sha256_vectors[i][2]).finish().result),
'vector ' + i,
).to.equal(hmac_sha256_vectors[i][0]);
}
});
});
describe('PBKDF2-HMAC-SHA256', () => {
const pbkdf2_hmac_sha256_vectors = [
[
'120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
1,
32,
],
[
'ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
2,
32,
],
[
'c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
4096,
32,
],
[
'348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9',
asmCrypto.string_to_bytes('passwordPASSWORDpassword'),
asmCrypto.string_to_bytes('saltSALTsaltSALTsaltSALTsaltSALTsalt'),
4096,
40,
],
[
'89b69d0516f829893c696226650a8687',
asmCrypto.string_to_bytes('pass\0word'),
asmCrypto.string_to_bytes('sa\0lt'),
4096,
16,
],
[
'cdc8b1780ca68aba97f1f729c9d281719702eb4b308d7d87409817e60188be0d',
asmCrypto.string_to_bytes('MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword'),
asmCrypto.string_to_bytes('MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword'),
4096,
32,
],
];
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.HMAC_SHA256 !== 'undefined' )
{
var hmac_sha256_vectors = [
[ 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad', '', '' ],
[ 'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8', 'key', 'The quick brown fox jumps over the lazy dog' ],
[ 'b54d57e9b21940b6496b58d5ac120eda9f1637788b5df058928637f2eca40cd9', 'MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' ]
];
test( "asmCrypto.HMAC_SHA256.hex", function () {
for ( var i = 0; i < hmac_sha256_vectors.length; ++i ) {
equal(
asmCrypto.HMAC_SHA256.hex( hmac_sha256_vectors[i][2], hmac_sha256_vectors[i][1] ),
hmac_sha256_vectors[i][0],
"vector " + i
);
}
});
}
else
{
skip( "asmCrypto.HMAC_SHA256" );
}
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.PBKDF2_HMAC_SHA256 !== 'undefined' )
{
var pbkdf2_hmac_sha256_vectors = [
[ '120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b', 'password', 'salt', 1, 32 ],
[ 'ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43', 'password', 'salt', 2, 32 ],
[ 'c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a', 'password', 'salt', 4096, 32 ],
[ '348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9', 'passwordPASSWORDpassword', 'saltSALTsaltSALTsaltSALTsaltSALTsalt', 4096, 40 ],
[ '89b69d0516f829893c696226650a8687', "pass\0word", "sa\0lt", 4096, 16 ],
[ 'cdc8b1780ca68aba97f1f729c9d281719702eb4b308d7d87409817e60188be0d', 'MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword', 'MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword', 4096, 32 ]
];
test( "asmCrypto.PBKDF2_HMAC_SHA256.hex", function () {
for ( var i = 0; i < pbkdf2_hmac_sha256_vectors.length; ++i ) {
equal(
// got
asmCrypto.PBKDF2_HMAC_SHA256.hex(
pbkdf2_hmac_sha256_vectors[i][1], // password
pbkdf2_hmac_sha256_vectors[i][2], // salt
pbkdf2_hmac_sha256_vectors[i][3], // count
pbkdf2_hmac_sha256_vectors[i][4] // dklen
),
// expect
pbkdf2_hmac_sha256_vectors[i][0],
// comment
"asmCrypto.PBKDF2_HMAC_SHA256.hex('"
+pbkdf2_hmac_sha256_vectors[i][1]+"', '"
+pbkdf2_hmac_sha256_vectors[i][2]+"', '"
+pbkdf2_hmac_sha256_vectors[i][3]+"', '"
+pbkdf2_hmac_sha256_vectors[i][4]
+"') is equal to '"+pbkdf2_hmac_sha256_vectors[i][0]+"'"
);
}
});
}
else
{
skip( "asmCrypto.PBKDF2_HMAC_SHA256" );
}
it('asmCrypto.PBKDF2_HMAC_SHA256.hex', function() {
for (let i = 0; i < pbkdf2_hmac_sha256_vectors.length; ++i) {
expect(
// got
asmCrypto.bytes_to_hex(asmCrypto.Pbkdf2HmacSha256(
pbkdf2_hmac_sha256_vectors[i][1], // password
pbkdf2_hmac_sha256_vectors[i][2], // salt
pbkdf2_hmac_sha256_vectors[i][3], // count
pbkdf2_hmac_sha256_vectors[i][4], // dklen
)),
"asmCrypto.PBKDF2_HMAC_SHA256.hex('" +
pbkdf2_hmac_sha256_vectors[i][1] +
"', '" +
pbkdf2_hmac_sha256_vectors[i][2] +
"', '" +
pbkdf2_hmac_sha256_vectors[i][3] +
"', '" +
pbkdf2_hmac_sha256_vectors[i][4] +
"') is equal to '" +
pbkdf2_hmac_sha256_vectors[i][0] +
"'",
).to.equal(pbkdf2_hmac_sha256_vectors[i][0]);
}
});
});

@@ -1,95 +0,136 @@

module("SHA512");
import * as asmCrypto from '../asmcrypto.all.es8';
import chai from 'chai';
const expect = chai.expect;
///////////////////////////////////////////////////////////////////////////////
describe('SHA512', () => {
const sha512_vectors = [
[
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e',
asmCrypto.string_to_bytes(''),
],
[
'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f',
asmCrypto.string_to_bytes('abc'),
],
[
'8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909',
asmCrypto.string_to_bytes(
'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
),
],
[
'07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6',
asmCrypto.string_to_bytes('The quick brown fox jumps over the lazy dog'),
],
[
'91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed',
asmCrypto.string_to_bytes('The quick brown fox jumps over the lazy dog.'),
],
];
if ( typeof asmCrypto.SHA512 !== 'undefined' )
{
var sha512_vectors = [
[ 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', '' ],
[ 'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'abc' ],
[ '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu' ],
[ '07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6', 'The quick brown fox jumps over the lazy dog' ],
[ '91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed', 'The quick brown fox jumps over the lazy dog.' ]
];
it('asmCrypto.SHA512.hex', function() {
for (let i = 0; i < sha512_vectors.length; ++i) {
const sha512 = new asmCrypto.Sha512();
expect(asmCrypto.bytes_to_hex(sha512.process(sha512_vectors[i][1]).finish().result), 'vector ' + i).to.equal(
sha512_vectors[i][0],
);
}
});
});
describe('HMAC-SHA512', () => {
const hmac_sha512_vectors = [
[
'87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854',
asmCrypto.hex_to_bytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'),
asmCrypto.string_to_bytes('Hi There'),
],
[
'164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737',
asmCrypto.string_to_bytes('Jefe'),
asmCrypto.string_to_bytes('what do ya want for nothing?'),
],
[
'80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598',
asmCrypto.hex_to_bytes(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
),
asmCrypto.string_to_bytes('Test Using Larger Than Block-Size Key - Hash Key First'),
],
];
test( "asmCrypto.SHA512.hex", function () {
for ( var i = 0; i < sha512_vectors.length; ++i ) {
equal(
asmCrypto.SHA512.hex( sha512_vectors[i][1] ),
sha512_vectors[i][0],
"vector " + i
);
}
});
}
else
{
skip( "asmCrypto.SHA512" );
}
it('asmCrypto.HMAC_SHA512.hex', function() {
for (let i = 0; i < hmac_sha512_vectors.length; ++i) {
const hmacSha512 = new asmCrypto.HmacSha512(hmac_sha512_vectors[i][1]);
expect(
asmCrypto.bytes_to_hex(hmacSha512.process(hmac_sha512_vectors[i][2]).finish().result),
'vector ' + i,
).to.equal(hmac_sha512_vectors[i][0]);
}
});
});
describe('PBKDF2-HMAC-SHA512', () => {
const pbkdf2_hmac_sha512_vectors = [
[
'867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
1,
64,
],
[
'867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
1,
64,
],
[
'e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
2,
64,
],
[
'd197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5',
asmCrypto.string_to_bytes('password'),
asmCrypto.string_to_bytes('salt'),
4096,
64,
],
[
'8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b8',
asmCrypto.string_to_bytes('passwordPASSWORDpassword'),
asmCrypto.string_to_bytes('saltSALTsaltSALTsaltSALTsaltSALTsalt'),
4096,
64,
],
];
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.HMAC_SHA512 !== 'undefined' )
{
var hmac_sha512_vectors = [
[ '87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854', asmCrypto.hex_to_bytes('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), 'Hi There'],
[ '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737', 'Jefe', 'what do ya want for nothing?' ],
[ '80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598', asmCrypto.hex_to_bytes('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), 'Test Using Larger Than Block-Size Key - Hash Key First']
];
test( "asmCrypto.HMAC_SHA512.hex", function () {
for ( var i = 0; i < hmac_sha512_vectors.length; ++i ) {
equal(
asmCrypto.HMAC_SHA512.hex( hmac_sha512_vectors[i][2], hmac_sha512_vectors[i][1] ),
hmac_sha512_vectors[i][0],
"vector " + i
);
}
});
}
else
{
skip( "asmCrypto.HMAC_SHA512" );
}
///////////////////////////////////////////////////////////////////////////////
if ( typeof asmCrypto.PBKDF2_HMAC_SHA512 !== 'undefined' )
{
var pbkdf2_hmac_sha512_vectors = [
[ '867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce', 'password', 'salt', 1, 64 ],
[ '867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce', asmCrypto.string_to_bytes('password'), asmCrypto.string_to_bytes('salt'), 1, 64 ],
[ 'e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e', 'password', 'salt', 2, 64 ],
[ 'd197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5', 'password', 'salt', 4096, 64 ],
[ '8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b8', 'passwordPASSWORDpassword', 'saltSALTsaltSALTsaltSALTsaltSALTsalt', 4096, 64 ]
];
test( "asmCrypto.PBKDF2_HMAC_SHA512.hex", function () {
for ( var i = 0; i < pbkdf2_hmac_sha512_vectors.length; ++i ) {
equal(
// got
asmCrypto.PBKDF2_HMAC_SHA512.hex(
pbkdf2_hmac_sha512_vectors[i][1], // password
pbkdf2_hmac_sha512_vectors[i][2], // salt
pbkdf2_hmac_sha512_vectors[i][3], // count
pbkdf2_hmac_sha512_vectors[i][4] // dklen
),
// expect
pbkdf2_hmac_sha512_vectors[i][0],
// comment
"asmCrypto.PBKDF2_HMAC_SHA512.hex('"
+pbkdf2_hmac_sha512_vectors[i][1]+"', '"
+pbkdf2_hmac_sha512_vectors[i][2]+"', '"
+pbkdf2_hmac_sha512_vectors[i][3]+"', '"
+pbkdf2_hmac_sha512_vectors[i][4]
+"') is equal to '"+pbkdf2_hmac_sha512_vectors[i][0]+"'"
);
}
});
}
else
{
skip( "asmCrypto.PBKDF2_HMAC_SHA512" );
}
it('asmCrypto.PBKDF2_HMAC_SHA512.hex', function() {
for (let i = 0; i < pbkdf2_hmac_sha512_vectors.length; ++i) {
expect(
// got
asmCrypto.bytes_to_hex(
asmCrypto.Pbkdf2HmacSha512(
pbkdf2_hmac_sha512_vectors[i][1], // password
pbkdf2_hmac_sha512_vectors[i][2], // salt
pbkdf2_hmac_sha512_vectors[i][3], // count
pbkdf2_hmac_sha512_vectors[i][4], // dklen
),
),
"asmCrypto.PBKDF2_HMAC_SHA512.hex('" +
pbkdf2_hmac_sha512_vectors[i][1] +
"', '" +
pbkdf2_hmac_sha512_vectors[i][2] +
"', '" +
pbkdf2_hmac_sha512_vectors[i][3] +
"', '" +
pbkdf2_hmac_sha512_vectors[i][4] +
"') is expect to '" +
pbkdf2_hmac_sha512_vectors[i][0] +
"'",
).to.equal(pbkdf2_hmac_sha512_vectors[i][0]);
}
});
});
{
"compilerOptions": {
"module": "es6",
"module": "ES6",
"target": "es2017",
"noEmit": true,
"allowJs": true,
"checkJs": true,
"sourceMap": false
"lib": ["es5", "dom", "dom.iterable", "es6"],
"noEmit": false,
"strict": true,
"sourceMap": false,
"declaration": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist_es8"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
],
"include": [
"src"
"files": [
"src/entry-export_all.ts"
]
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc