Tests/Private/Digest.Tests.ps1

BeforeDiscovery {
    Import-Module (Join-Path $PSScriptRoot '../../Posh-SecretSharing.psd1') -Force
}

Describe 'Private/Digest' -Tag Unit {
    InModuleScope 'Posh-SecretSharing' {
        Describe 'Get-SecretSharingDigestConstant' {
            It 'returns the SLIP-0039 digest/secret index and digest length constants' {
                $constant = Get-SecretSharingDigestConstant
                $constant.DigestIndex | Should -Be 254
                $constant.SecretIndex | Should -Be 255
                $constant.DigestLength | Should -Be 4
            }
        }

        Describe 'Invoke-SecretSharingHmacSha256' {
            It 'returns a 32-byte digest' {
                $hash = Invoke-SecretSharingHmacSha256 -Key ([byte[]](1, 2, 3)) -Message ([byte[]](4, 5, 6))
                $hash.Count | Should -Be 32
            }

            It 'matches the RFC 4231 HMAC-SHA256 test case 1 vector' {
                $key = [byte[]](, 0x0b * 20)
                $message = [System.Text.Encoding]::ASCII.GetBytes('Hi There')
                $expected = [byte[]](
                    0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
                    0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
                )

                $actual = Invoke-SecretSharingHmacSha256 -Key $key -Message $message
                $actual | Should -Be $expected
            }
        }

        Describe 'New-SecretSharingDigestShare' {
            It 'returns a share the same length as the secret' {
                $secret = [byte[]](1, 2, 3, 4, 5, 6, 7, 8)
                (New-SecretSharingDigestShare -Secret $secret).Count | Should -Be $secret.Count
            }

            It 'throws when the secret is not longer than the digest length' {
                { New-SecretSharingDigestShare -Secret ([byte[]](1, 2, 3, 4)) } | Should -Throw
            }

            It 'produces a digest share that Test-SecretSharingDigestShare accepts' {
                $secret = Get-SecretSharingRandomByte -Length 16
                $digestShare = New-SecretSharingDigestShare -Secret $secret
                Test-SecretSharingDigestShare -Secret $secret -DigestShare $digestShare | Should -BeTrue
            }

            It 'produces different digest shares for the same secret on repeated calls' {
                $secret = [byte[]](1, 1, 1, 1, 1, 1, 1, 1)
                $shareA = New-SecretSharingDigestShare -Secret $secret
                $shareB = New-SecretSharingDigestShare -Secret $secret
                ($shareA -join ',') | Should -Not -Be ($shareB -join ',')
            }
        }

        Describe 'Test-SecretSharingDigestShare' {
            It 'returns $false when the digest byte has been corrupted' {
                $secret = Get-SecretSharingRandomByte -Length 16
                $digestShare = New-SecretSharingDigestShare -Secret $secret
                $corrupted = [byte[]]$digestShare.Clone()
                $corrupted[0] = $corrupted[0] -bxor 0xFF

                Test-SecretSharingDigestShare -Secret $secret -DigestShare $corrupted | Should -BeFalse
            }

            It 'returns $false when the secret does not match the digest share' {
                $secret = Get-SecretSharingRandomByte -Length 16
                $digestShare = New-SecretSharingDigestShare -Secret $secret
                $otherSecret = Get-SecretSharingRandomByte -Length 16

                Test-SecretSharingDigestShare -Secret $otherSecret -DigestShare $digestShare | Should -BeFalse
            }

            It 'throws when DigestShare and Secret lengths differ' {
                $secret = [byte[]](1, 2, 3, 4, 5, 6, 7, 8)
                $digestShare = [byte[]](1, 2, 3, 4, 5, 6)
                { Test-SecretSharingDigestShare -Secret $secret -DigestShare $digestShare } | Should -Throw
            }

            It 'throws when DigestShare is not longer than the digest length' {
                $secret = [byte[]](1, 2, 3, 4)
                $digestShare = [byte[]](1, 2, 3, 4)
                { Test-SecretSharingDigestShare -Secret $secret -DigestShare $digestShare } | Should -Throw
            }
        }
    }
}