Private/Digest/Test-SecretSharingDigestShare.ps1
|
function Test-SecretSharingDigestShare { <# .SYNOPSIS Verifies a SLIP-0039 digest share against a reconstructed secret. .DESCRIPTION Splits DigestShare into its stored digest (first 4 bytes) and random part (remaining bytes), recomputes HMAC-SHA256(key = random part, message = Secret), and returns whether the recomputed digest matches the stored one. A mismatch means the shares combined to produce Secret were not a valid, unmodified set for this threshold - the caller (a future Join-SecretSharingSecret) is expected to treat a $false result as a hard reconstruction failure, not a warning. #> [CmdletBinding()] [OutputType([bool])] param( [Parameter(Mandatory)] [byte[]]$Secret, [Parameter(Mandatory)] [byte[]]$DigestShare ) $constant = Get-SecretSharingDigestConstant if ($DigestShare.Length -ne $Secret.Length) { throw 'DigestShare must be the same length as Secret.' } if ($DigestShare.Length -le $constant.DigestLength) { throw "DigestShare must be longer than $($constant.DigestLength) bytes." } $storedDigest = [byte[]]$DigestShare[0..($constant.DigestLength - 1)] $randomPart = [byte[]]$DigestShare[$constant.DigestLength..($DigestShare.Length - 1)] $fullHash = Invoke-SecretSharingHmacSha256 -Key $randomPart -Message $Secret $computedDigest = [byte[]]$fullHash[0..($constant.DigestLength - 1)] for ($i = 0; $i -lt $constant.DigestLength; $i++) { if ($storedDigest[$i] -ne $computedDigest[$i]) { return $false } } return $true } |