diff --git a/hwe/ts/util/LiteHashDRBG.ts b/hwe/ts/util/LiteHashDRBG.ts index 13df8e6a..251f66e2 100644 --- a/hwe/ts/util/LiteHashDRBG.ts +++ b/hwe/ts/util/LiteHashDRBG.ts @@ -85,7 +85,17 @@ export class LiteHashDRBG implements RNG { protected hq: DataView; protected hqIdxPos: number; - public constructor(protected seed: MaybeBytes, protected stateIdx = 0) { + public constructor(protected seed: MaybeBytes, protected stateIdx = 0, bufferIdx = 0) { + if(bufferIdx < 0){ + throw new Error(`bufferIdx ${bufferIdx} < 0`); + } + if(bufferIdx >= bufferByteSize){ + throw new Error(`bufferidx ${bufferIdx} >= ${bufferByteSize}`); + } + if(stateIdx < 0){ + throw new Error(`stateIdx ${stateIdx} < 0`); + } + const seedU8 = (() => { if (seed instanceof Uint8Array) { return seed; @@ -106,6 +116,7 @@ export class LiteHashDRBG implements RNG { this.hqIdxPos = seedU8.byteLength; this.genNextBlock(); + this.bufferIdx = bufferIdx; } protected genNextBlock(): void { diff --git a/src/sammo/LiteHashDRBG.php b/src/sammo/LiteHashDRBG.php index c46b8f87..35aaa3f5 100644 --- a/src/sammo/LiteHashDRBG.php +++ b/src/sammo/LiteHashDRBG.php @@ -21,9 +21,19 @@ class LiteHashDRBG implements RNG protected string $buffer; protected int $bufferIdx; - public function __construct(protected string $seed, protected int $stateIdx = 0) + public function __construct(protected string $seed, protected int $stateIdx = 0, int $bufferIdx = 0) { + if($bufferIdx < 0){ + throw new \InvalidArgumentException("bufferIdx {$bufferIdx} < 0"); + } + if($bufferIdx >= self::BUFFER_BYTE_SIZE){ + throw new \InvalidArgumentException("bufferIdx {$bufferIdx} >= ".self::BUFFER_BYTE_SIZE); + } + if($stateIdx < 0){ + throw new \InvalidArgumentException("stateIdx {$stateIdx} < 0"); + } $this->genNextBlock(); + $this->bufferIdx = $bufferIdx; } protected function genNextBlock(): void