< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Psha1DerivedKeyGenerator
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Psha1DerivedKeyGenerator.cs
Line coverage
88%
Covered lines: 37
Uncovered lines: 5
Coverable lines: 42
Total lines: 107
Line coverage: 88%
Branch coverage
62%
Covered branches: 10
Total branches: 16
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%22100%
GenerateDerivedKey(...)50%4466.66%
.ctor(...)100%11100%
GetDerivedKey(...)62.5%8872.72%
GetByte()100%22100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Psha1DerivedKeyGenerator.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Security.Cryptography;
 6using CoreWCF.Runtime;
 7
 8namespace CoreWCF.IdentityModel
 9{
 10    internal sealed class Psha1DerivedKeyGenerator
 11    {
 12        private readonly byte[] _key;
 13
 1014        public Psha1DerivedKeyGenerator(byte[] key)
 15        {
 1016            _key = key ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 1017        }
 18
 19        public byte[] GenerateDerivedKey(byte[] label, byte[] nonce, int derivedKeySize, int position)
 20        {
 1021            if (label == null)
 22            {
 023                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(label));
 24            }
 1025            if (nonce == null)
 26            {
 027                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(nonce));
 28            }
 1029            ManagedPsha1 dkcp = new ManagedPsha1(_key, label, nonce);
 1030            return dkcp.GetDerivedKey(derivedKeySize, position);
 31        }
 32
 33        // private class to do the real work
 34        // Note: Though named ManagedPsha1, this works for both fips and non-fips compliance
 35        private sealed class ManagedPsha1
 36        {
 37            private byte[] _aValue;
 38            private readonly byte[] _buffer;
 39            private byte[] _chunk;
 40            private readonly KeyedHashAlgorithm _hmac;
 41            private int _index;
 42            private int _position;
 43            private readonly byte[] _secret;
 44            private readonly byte[] _seed;
 45
 46            // assume arguments are already validated
 1047            public ManagedPsha1(byte[] secret, byte[] label, byte[] seed)
 48            {
 1049                _secret = secret;
 1050                _seed = Fx.AllocateByteArray(checked(label.Length + seed.Length));
 1051                label.CopyTo(_seed, 0);
 1052                seed.CopyTo(_seed, label.Length);
 53
 1054                _aValue = _seed;
 1055                _chunk = Array.Empty<byte>();
 1056                _index = 0;
 1057                _position = 0;
 1058                _hmac = CryptoHelper.NewHmacSha1KeyedHashAlgorithm(secret);
 59
 1060                _buffer = Fx.AllocateByteArray(checked(_hmac.HashSize / 8 + _seed.Length));
 1061            }
 62
 63            public byte[] GetDerivedKey(int derivedKeySize, int position)
 64            {
 1065                if (derivedKeySize < 0)
 66                {
 067                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(der
 68                }
 1069                if (_position > position)
 70                {
 071                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(pos
 72                }
 73
 74                // Seek to the desired position in the pseudo-random stream.
 1075                while (_position < position)
 76                {
 077                    GetByte();
 78                }
 1079                int sizeInBytes = derivedKeySize / 8;
 1080                byte[] derivedKey = new byte[sizeInBytes];
 66081                for (int i = 0; i < sizeInBytes; i++)
 82                {
 32083                    derivedKey[i] = GetByte();
 84                }
 1085                return derivedKey;
 86            }
 87
 88            private byte GetByte()
 89            {
 32090                if (_index >= _chunk.Length)
 91                {
 92                    // Calculate A(i) = HMAC_SHA1(secret, A(i-1)).
 2093                    _hmac.Initialize();
 2094                    _aValue = _hmac.ComputeHash(_aValue);
 95                    // Calculate P_SHA1(secret, seed)[j] = HMAC_SHA1(secret, A(j+1) || seed).
 2096                    _aValue.CopyTo(_buffer, 0);
 2097                    _seed.CopyTo(_buffer, _aValue.Length);
 2098                    _hmac.Initialize();
 2099                    _chunk = _hmac.ComputeHash(_buffer);
 20100                    _index = 0;
 101                }
 320102                _position++;
 320103                return _chunk[_index++];
 104            }
 105        }
 106    }
 107}