Spaces:
Sleeping
Sleeping
File size: 14,867 Bytes
0865492 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | /**
* BLAKE3 Hasher - Incremental hashing with support for all modes
*
* Supports:
* - Regular hashing
* - Keyed hashing (MAC)
* - Key derivation (derive_key)
* - XOF (eXtendable Output Function) mode
*/
import { compress } from "./compress.js";
import {
IV,
CHUNK_START,
CHUNK_END,
PARENT,
ROOT,
KEYED_HASH,
DERIVE_KEY_CONTEXT,
DERIVE_KEY_MATERIAL,
BLOCK_LEN,
CHUNK_LEN,
OUT_LEN,
KEY_LEN,
MAX_DEPTH,
} from "./constants.js";
import {
IS_LITTLE_ENDIAN,
readLittleEndianWordsFull,
writeLittleEndianBytesPartial,
encodeUTF8,
} from "./utils.js";
/**
* Output state for XOF (eXtendable Output Function) mode.
* Allows reading arbitrary amounts of output.
*/
export class XofReader {
private inputCv: Uint32Array;
private blockWords: Uint32Array;
private counter: number;
private blockLen: number;
private flags: number;
private outputBlock: Uint32Array;
private outputBlockOffset: number;
constructor(
inputCv: Uint32Array,
blockWords: Uint32Array,
counter: number,
blockLen: number,
flags: number,
) {
this.inputCv = inputCv;
this.blockWords = blockWords;
this.counter = counter;
this.blockLen = blockLen;
this.flags = flags | ROOT;
this.outputBlock = new Uint32Array(16);
this.outputBlockOffset = 64; // Forces generation on first read
}
/**
* Read the next `length` bytes of output.
*/
read(length: number): Uint8Array {
const output = new Uint8Array(length);
let outputOffset = 0;
while (outputOffset < length) {
// Generate new output block if needed
if (this.outputBlockOffset >= 64) {
compress(
this.inputCv,
0,
this.blockWords,
0,
this.outputBlock,
0,
true, // full 64-byte output
this.counter++,
this.blockLen,
this.flags,
);
this.outputBlockOffset = 0;
}
// Copy bytes from output block
const available = 64 - this.outputBlockOffset;
const toCopy = Math.min(available, length - outputOffset);
// Optimized copy using writeLittleEndianBytesPartial
const wordOffset = this.outputBlockOffset >>> 2;
const byteWithinWord = this.outputBlockOffset & 3;
if (byteWithinWord === 0 && toCopy >= 4) {
// Aligned copy - can use word-at-a-time
const fullWords = toCopy >>> 2;
writeLittleEndianBytesPartial(
this.outputBlock,
wordOffset,
output,
outputOffset,
fullWords << 2,
);
const bytesCopied = fullWords << 2;
outputOffset += bytesCopied;
this.outputBlockOffset += bytesCopied;
} else {
// Byte-by-byte for unaligned access
for (let i = 0; i < toCopy; i++) {
const wordIdx = (this.outputBlockOffset + i) >>> 2;
const byteIdx = (this.outputBlockOffset + i) & 3;
output[outputOffset + i] = (this.outputBlock[wordIdx] >>> (byteIdx << 3)) & 0xff;
}
outputOffset += toCopy;
this.outputBlockOffset += toCopy;
}
}
return output;
}
}
/**
* Chunk state for processing input data.
* Each chunk is 1024 bytes and produces an 8-word chaining value.
*/
class ChunkState {
chainingValue: Uint32Array;
chunkCounter: number;
blockWords: Uint32Array;
blockLen: number;
blocksCompressed: number;
flags: number;
constructor(keyWords: Uint32Array, chunkCounter: number, flags: number) {
this.chainingValue = new Uint32Array(keyWords);
this.chunkCounter = chunkCounter;
this.blockWords = new Uint32Array(16);
this.blockLen = 0;
this.blocksCompressed = 0;
this.flags = flags;
}
resetTo(keyWords: Uint32Array, chunkCounter: number, flags: number): void {
this.chainingValue.set(keyWords);
this.chunkCounter = chunkCounter;
this.blockLen = 0;
this.blocksCompressed = 0;
this.flags = flags;
}
/**
* Get the flags for the current block.
*/
private startFlag(): number {
return this.blocksCompressed === 0 ? CHUNK_START : 0;
}
/**
* Update the chunk state with input data.
* Returns the number of bytes consumed.
*/
update(input: Uint8Array, inputOffset: number, inputLen: number): number {
let consumed = 0;
while (inputLen > 0) {
// If we have a full block, compress it
if (this.blockLen === BLOCK_LEN) {
compress(
this.chainingValue,
0,
this.blockWords,
0,
this.chainingValue,
0,
false,
this.chunkCounter,
BLOCK_LEN,
this.flags | this.startFlag(),
);
this.blocksCompressed++;
this.blockLen = 0;
}
// Fill the block buffer
const want = BLOCK_LEN - this.blockLen;
const take = Math.min(want, inputLen);
if (this.blockLen === 0 && take === BLOCK_LEN) {
readLittleEndianWordsFull(input, inputOffset, this.blockWords);
} else {
// Partial block - byte-by-byte into correct position
for (let i = 0; i < take; i++) {
const pos = this.blockLen + i;
const wordIdx = pos >>> 2;
const byteIdx = pos & 3;
if (byteIdx === 0) {
this.blockWords[wordIdx] = input[inputOffset + i];
} else {
this.blockWords[wordIdx] |= input[inputOffset + i] << (byteIdx << 3);
}
}
}
this.blockLen += take;
inputOffset += take;
inputLen -= take;
consumed += take;
}
return consumed;
}
/**
* Finalize this chunk and return its output.
* Returns 8 words (chaining value) or 16 words (if root).
*/
output(): {
inputCv: Uint32Array;
blockWords: Uint32Array;
blockLen: number;
counter: number;
flags: number;
} {
// Zero-pad unused words in blockWords to avoid stale data from previous blocks
// This is necessary when a partial block follows a full block within the same chunk
const usedWords = (this.blockLen + 3) >>> 2; // ceil(blockLen / 4)
for (let i = usedWords; i < 16; i++) {
this.blockWords[i] = 0;
}
return {
inputCv: this.chainingValue,
blockWords: this.blockWords,
blockLen: this.blockLen,
counter: this.chunkCounter,
flags: this.flags | this.startFlag() | CHUNK_END,
};
}
/**
* Get the number of bytes in this chunk.
*/
len(): number {
return this.blocksCompressed * BLOCK_LEN + this.blockLen;
}
}
/**
* Main BLAKE3 Hasher class.
*
* Usage:
* const hasher = new Hasher();
* hasher.update(data);
* const hash = hasher.finalize();
*
* Or with chaining:
* const hash = new Hasher().update(data).finalize();
*/
export class Hasher {
private chunkState: ChunkState;
private keyWords: Uint32Array;
private cvStack: Uint32Array;
private cvStackLen: number;
private flags: number;
private parentBlock: Uint32Array;
private parentCv: Uint32Array;
private chunkCv: Uint32Array;
private outWords: Uint32Array;
private finalizeCv: Uint32Array;
/**
* Create a new Hasher.
*
* @param keyWords - Initial key words (IV for regular hashing)
* @param flags - Domain separation flags
*/
constructor(keyWords?: Uint32Array, flags?: number) {
this.keyWords = keyWords ? new Uint32Array(keyWords) : new Uint32Array(IV);
this.flags = flags ?? 0;
this.chunkState = new ChunkState(this.keyWords, 0, this.flags);
this.cvStack = new Uint32Array(MAX_DEPTH * 8);
this.cvStackLen = 0;
this.parentBlock = new Uint32Array(16);
this.parentCv = new Uint32Array(8);
this.chunkCv = new Uint32Array(8);
this.outWords = new Uint32Array(16);
this.finalizeCv = new Uint32Array(8);
}
/**
* Reset the hasher to process a new message with the same key/flags.
* Reuses all internal buffers — zero allocations.
*/
reset(): this {
this.chunkState.resetTo(this.keyWords, 0, this.flags);
this.cvStackLen = 0;
return this;
}
/**
* Create a new keyed hasher (MAC).
*
* @param key - 32-byte key
*/
static newKeyed(key: Uint8Array): Hasher {
if (key.length !== KEY_LEN) {
throw new Error(`Key must be ${KEY_LEN} bytes, got ${key.length}`);
}
const keyWords = new Uint32Array(8);
if (IS_LITTLE_ENDIAN) {
const view = new Uint32Array(key.buffer, key.byteOffset, 8);
keyWords.set(view);
} else {
for (let i = 0; i < 8; i++) {
const off = i * 4;
keyWords[i] = key[off] | (key[off + 1] << 8) | (key[off + 2] << 16) | (key[off + 3] << 24);
}
}
return new Hasher(keyWords, KEYED_HASH);
}
/**
* Create a new key derivation hasher.
*
* @param context - Context string for domain separation
*/
static newDeriveKey(context: string): Hasher {
// First, hash the context string with DERIVE_KEY_CONTEXT flag
const contextBytes = encodeUTF8(context);
const contextHasher = new Hasher(new Uint32Array(IV), DERIVE_KEY_CONTEXT);
contextHasher.update(contextBytes);
// Get the context key
const contextKey = new Uint32Array(8);
const output = contextHasher.finalizeOutput();
compress(
output.inputCv,
0,
output.blockWords,
0,
contextKey,
0,
false,
output.counter,
output.blockLen,
output.flags | ROOT,
);
// Return a hasher initialized with the context key
return new Hasher(contextKey, DERIVE_KEY_MATERIAL);
}
/**
* Push a chaining value onto the stack.
*/
private pushCv(cv: Uint32Array, cvOffset: number): void {
this.cvStack.set(cv.subarray(cvOffset, cvOffset + 8), this.cvStackLen * 8);
this.cvStackLen++;
}
/**
* Pop a chaining value from the stack.
*/
private popCv(out: Uint32Array, outOffset: number): void {
this.cvStackLen--;
out.set(this.cvStack.subarray(this.cvStackLen * 8, (this.cvStackLen + 1) * 8), outOffset);
}
/**
* Add a chunk's chaining value and merge completed subtrees.
*/
private addChunkCv(newCv: Uint32Array, newCvOffset: number, totalChunks: number): void {
const parentBlock = this.parentBlock;
const parentCv = this.parentCv;
while ((totalChunks & 1) === 0) {
// Pop left child, new CV is right child
this.popCv(parentBlock, 0);
parentBlock.set(newCv.subarray(newCvOffset, newCvOffset + 8), 8);
compress(
this.keyWords,
0,
parentBlock,
0,
parentCv,
0,
false,
0,
BLOCK_LEN,
this.flags | PARENT,
);
newCv = parentCv;
newCvOffset = 0;
totalChunks >>>= 1;
}
this.pushCv(newCv, newCvOffset);
}
/**
* Update the hasher with input data.
*
* @param input - Data to hash
* @returns this (for chaining)
*/
update(input: Uint8Array): this {
let inputOffset = 0;
let inputLen = input.length;
// Fill the current chunk
while (inputLen > 0) {
// If current chunk is full, finalize it and start a new one
if (this.chunkState.len() === CHUNK_LEN) {
const output = this.chunkState.output();
const chunkCv = this.chunkCv;
compress(
output.inputCv,
0,
output.blockWords,
0,
chunkCv,
0,
false,
output.counter,
output.blockLen,
output.flags,
);
const totalChunks = this.chunkState.chunkCounter + 1;
this.addChunkCv(chunkCv, 0, totalChunks);
this.chunkState.resetTo(this.keyWords, totalChunks, this.flags);
}
// Fill the current chunk
const want = CHUNK_LEN - this.chunkState.len();
const take = Math.min(want, inputLen);
this.chunkState.update(input, inputOffset, take);
inputOffset += take;
inputLen -= take;
}
return this;
}
/**
* Get the output parameters (for XOF mode or finalization).
*/
private finalizeOutput(): {
inputCv: Uint32Array;
blockWords: Uint32Array;
blockLen: number;
counter: number;
flags: number;
} {
let output = this.chunkState.output();
let parentBlock = this.parentBlock;
let cv = this.finalizeCv;
// If there are chunks on the stack, merge them
if (this.cvStackLen > 0) {
// First compress the current chunk
compress(
output.inputCv,
0,
output.blockWords,
0,
cv,
0,
false,
output.counter,
output.blockLen,
output.flags,
);
// Merge with parent nodes from stack
while (this.cvStackLen > 0) {
this.cvStackLen--;
parentBlock.set(this.cvStack.subarray(this.cvStackLen * 8, (this.cvStackLen + 1) * 8), 0);
parentBlock.set(cv, 8);
if (this.cvStackLen > 0) {
compress(
this.keyWords,
0,
parentBlock,
0,
cv,
0,
false,
0,
BLOCK_LEN,
this.flags | PARENT,
);
} else {
// This is the root - return output params
return {
inputCv: this.keyWords,
blockWords: parentBlock,
blockLen: BLOCK_LEN,
counter: 0,
flags: this.flags | PARENT,
};
}
}
}
// Single chunk case
return output;
}
/**
* Finalize the hash and return the result.
*
* @param outputLength - Number of bytes to output (default: 32)
* @returns The hash output
*/
finalize(outputLength: number = OUT_LEN): Uint8Array {
const output = this.finalizeOutput();
const result = new Uint8Array(outputLength);
if (outputLength <= 64) {
const outWords = this.outWords;
compress(
output.inputCv,
0,
output.blockWords,
0,
outWords,
0,
outputLength > 32, // full output if > 32 bytes
output.counter,
output.blockLen,
output.flags | ROOT,
);
if (IS_LITTLE_ENDIAN) {
const outBytes = new Uint8Array(outWords.buffer);
result.set(outBytes.subarray(0, outputLength));
} else {
writeLittleEndianBytesPartial(outWords, 0, result, 0, outputLength);
}
} else {
// Multiple blocks - use XOF
const xof = this.finalizeXof();
const full = xof.read(outputLength);
result.set(full);
}
return result;
}
/**
* Finalize and return an XOF reader for arbitrary-length output.
*/
finalizeXof(): XofReader {
const output = this.finalizeOutput();
return new XofReader(
new Uint32Array(output.inputCv),
new Uint32Array(output.blockWords),
output.counter,
output.blockLen,
output.flags,
);
}
}
|