mirror of
https://github.com/emn178/js-sha3.git
synced 2026-11-16 23:08:50 +08:00
### Added
- KMACXOF128 and KMACXOF256 (NIST SP 800-185)
This commit is contained in:
+43
-53
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
||||
*
|
||||
* @version 0.11.0
|
||||
* @version 0.12.0
|
||||
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
||||
* @copyright Chen, Yi-Cyuan 2015-2023
|
||||
* @copyright Chen, Yi-Cyuan 2015-2026
|
||||
* @license MIT
|
||||
*/
|
||||
/*jslint bitwise: true */
|
||||
@@ -111,9 +111,15 @@
|
||||
};
|
||||
};
|
||||
|
||||
var createKmacOutputMethod = function (bits, padding, outputType) {
|
||||
var createKmacOutputMethod = function (bits, padding, xof, outputType) {
|
||||
return function (key, message, outputBits, s) {
|
||||
return methods['kmac' + bits].update(key, message, outputBits, s)[outputType]();
|
||||
return methods[(xof ? 'kmacxof' : 'kmac') + bits].update(key, message, outputBits, s)[outputType]();
|
||||
};
|
||||
};
|
||||
|
||||
var createTupleHashOutputMethod = function (bits, padding, xof, outputType) {
|
||||
return function (inputs, outputBits, s) {
|
||||
return methods[(xof ? 'tuplehashxof' : 'tuplehash') + bits].update(inputs, outputBits, s)[outputType]();
|
||||
};
|
||||
};
|
||||
|
||||
@@ -163,22 +169,18 @@
|
||||
return createOutputMethods(method, createCshakeOutputMethod, bits, padding);
|
||||
};
|
||||
|
||||
var createKmacMethod = function (bits, padding) {
|
||||
var createKmacMethod = function (bits, padding, xof) {
|
||||
var w = CSHAKE_BYTEPAD[bits];
|
||||
var method = createKmacOutputMethod(bits, padding, 'hex');
|
||||
var method = createKmacOutputMethod(bits, padding, xof, 'hex');
|
||||
method.create = function (key, outputBits, s) {
|
||||
return new Kmac(bits, padding, outputBits).bytepad(['KMAC', s], w).bytepad([key], w);
|
||||
return new Kmac(bits, padding, outputBits, xof).bytepad(['KMAC', s], w).bytepad([key], w);
|
||||
};
|
||||
method.update = function (key, message, outputBits, s) {
|
||||
return method.create(key, outputBits, s).update(message);
|
||||
};
|
||||
return createOutputMethods(method, createKmacOutputMethod, bits, padding);
|
||||
};
|
||||
|
||||
var createTupleHashOutputMethod = function (bits, padding, xof, outputType) {
|
||||
return function (inputs, outputBits, s) {
|
||||
return methods[(xof ? 'tuplehashxof' : 'tuplehash') + bits].update(inputs, outputBits, s)[outputType]();
|
||||
};
|
||||
return createOutputMethods(method, function (b, p, outputType) {
|
||||
return createKmacOutputMethod(b, p, xof, outputType);
|
||||
}, bits, padding);
|
||||
};
|
||||
|
||||
var createTupleHashMethod = function (bits, padding, xof) {
|
||||
@@ -207,7 +209,12 @@
|
||||
{ name: 'sha3', padding: PADDING, bits: BITS, createMethod: createMethod },
|
||||
{ name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod },
|
||||
{ name: 'cshake', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createCshakeMethod },
|
||||
{ name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod },
|
||||
{ name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: function (bits, padding) {
|
||||
return createKmacMethod(bits, padding, false);
|
||||
}},
|
||||
{ name: 'kmacxof', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: function (bits, padding) {
|
||||
return createKmacMethod(bits, padding, true);
|
||||
}},
|
||||
{ name: 'tuplehash', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: function (bits, padding) {
|
||||
return createTupleHashMethod(bits, padding, false);
|
||||
}},
|
||||
@@ -488,27 +495,27 @@
|
||||
return array;
|
||||
};
|
||||
|
||||
function Kmac(bits, padding, outputBits) {
|
||||
function Kmac(bits, padding, outputBits, xof) {
|
||||
Keccak.call(this, bits, padding, outputBits);
|
||||
this.xof = xof;
|
||||
}
|
||||
|
||||
Kmac.prototype = new Keccak();
|
||||
|
||||
Kmac.prototype.finalize = function () {
|
||||
if (!this.finalized) {
|
||||
this.encode(this.outputBits, true);
|
||||
this.encode(this.xof ? 0 : this.outputBits, true);
|
||||
}
|
||||
return Keccak.prototype.finalize.call(this);
|
||||
};
|
||||
|
||||
function TupleHash(bits, padding, outputBits, xof) {
|
||||
Keccak.call(this, bits, padding, outputBits);
|
||||
this.xof = !!xof;
|
||||
Kmac.call(this, bits, padding, outputBits, xof);
|
||||
this.inputActive = false;
|
||||
this.inputBytesRemaining = 0;
|
||||
}
|
||||
|
||||
TupleHash.prototype = new Keccak();
|
||||
TupleHash.prototype = new Kmac();
|
||||
|
||||
TupleHash.prototype.getMessageByteLength = function (message) {
|
||||
var result = formatMessage(message);
|
||||
@@ -534,9 +541,6 @@
|
||||
};
|
||||
|
||||
TupleHash.prototype.beginInput = function (byteLength) {
|
||||
if (this.finalized) {
|
||||
throw new Error(FINALIZE_ERROR);
|
||||
}
|
||||
if (this.inputActive) {
|
||||
throw new Error(TUPLE_INCOMPLETE_ERROR);
|
||||
}
|
||||
@@ -545,28 +549,15 @@
|
||||
throw new Error(TUPLE_BYTE_LENGTH_ERROR);
|
||||
}
|
||||
this.encode(byteLength * 8, false);
|
||||
if (byteLength === 0) {
|
||||
this.inputActive = false;
|
||||
this.inputBytesRemaining = 0;
|
||||
} else {
|
||||
if (byteLength !== 0) {
|
||||
this.inputActive = true;
|
||||
this.inputBytesRemaining = byteLength;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
TupleHash.prototype.updateChunk = function (message) {
|
||||
if (this.finalized) {
|
||||
throw new Error(FINALIZE_ERROR);
|
||||
}
|
||||
if (!this.inputActive) {
|
||||
throw new Error(TUPLE_ACTIVE_ERROR);
|
||||
}
|
||||
var byteLength = this.getMessageByteLength(message);
|
||||
if (byteLength > this.inputBytesRemaining) {
|
||||
throw new Error(TUPLE_LENGTH_ERROR);
|
||||
}
|
||||
Keccak.prototype.update.call(this, message);
|
||||
TupleHash.prototype._updateChunk = function (message, byteLength) {
|
||||
Kmac.prototype.update.call(this, message);
|
||||
this.inputBytesRemaining -= byteLength;
|
||||
if (this.inputBytesRemaining === 0) {
|
||||
this.inputActive = false;
|
||||
@@ -574,29 +565,28 @@
|
||||
return this;
|
||||
};
|
||||
|
||||
TupleHash.prototype.update = function (message) {
|
||||
if (this.finalized) {
|
||||
throw new Error(FINALIZE_ERROR);
|
||||
}
|
||||
if (this.inputActive) {
|
||||
throw new Error(TUPLE_INCOMPLETE_ERROR);
|
||||
TupleHash.prototype.updateChunk = function (message) {
|
||||
if (!this.inputActive) {
|
||||
throw new Error(TUPLE_ACTIVE_ERROR);
|
||||
}
|
||||
var byteLength = this.getMessageByteLength(message);
|
||||
this.beginInput(byteLength);
|
||||
if (byteLength === 0) {
|
||||
return this;
|
||||
if (byteLength > this.inputBytesRemaining) {
|
||||
throw new Error(TUPLE_LENGTH_ERROR);
|
||||
}
|
||||
return this.updateChunk(message);
|
||||
return this._updateChunk(message, byteLength);
|
||||
};
|
||||
|
||||
TupleHash.prototype.update = function (message) {
|
||||
var byteLength = this.getMessageByteLength(message);
|
||||
this.beginInput(byteLength);
|
||||
return this._updateChunk(message, byteLength);
|
||||
};
|
||||
|
||||
TupleHash.prototype.finalize = function () {
|
||||
if (this.inputActive) {
|
||||
throw new Error(TUPLE_INCOMPLETE_ERROR);
|
||||
}
|
||||
if (!this.finalized) {
|
||||
this.encode(this.xof ? 0 : this.outputBits, true);
|
||||
}
|
||||
return Keccak.prototype.finalize.call(this);
|
||||
return Kmac.prototype.finalize.call(this);
|
||||
};
|
||||
|
||||
var f = function (s) {
|
||||
|
||||
@@ -29,6 +29,10 @@ export const {
|
||||
kmac_256,
|
||||
kmac128,
|
||||
kmac256,
|
||||
kmacxof_128,
|
||||
kmacxof_256,
|
||||
kmacxof128,
|
||||
kmacxof256,
|
||||
|
||||
tuplehash_128,
|
||||
tuplehash_256,
|
||||
|
||||
Reference in New Issue
Block a user