/// <summary>
        /// Returns the string to pass to the platform APIs for a given algorithm.
        /// </summary>
        /// <param name="algorithm">The algorithm desired.</param>
        /// <returns>The platform-specific string to pass to OpenAlgorithm.</returns>
        private static string GetAlgorithmName(MacAlgorithm algorithm)
        {
            switch (algorithm)
            {
            case MacAlgorithm.AesCmac:
                return(Platform.Core.MacAlgorithmNames.AesCmac);

            case MacAlgorithm.HmacMd5:
                return(Platform.Core.MacAlgorithmNames.HmacMd5);

            case MacAlgorithm.HmacSha1:
                return(Platform.Core.MacAlgorithmNames.HmacSha1);

            case MacAlgorithm.HmacSha256:
                return(Platform.Core.MacAlgorithmNames.HmacSha256);

            case MacAlgorithm.HmacSha384:
                return(Platform.Core.MacAlgorithmNames.HmacSha384);

            case MacAlgorithm.HmacSha512:
                return(Platform.Core.MacAlgorithmNames.HmacSha512);

            default:
                throw new NotSupportedException();
            }
        }
Exemple #2
0
        public static void InitializeWithDisposedKey(MacAlgorithm a)
        {
            var k = new Key(a);

            k.Dispose();
            Assert.Throws <ObjectDisposedException>(() => IncrementalMac.Initialize(k, out _));
        }
Exemple #3
0
        /// <summary>Returns the keyed hash algorithm from the platform.</summary>
        /// <param name="algorithm">The algorithm desired.</param>
        /// <returns>The platform-specific algorithm.</returns>
        internal static Platform.KeyedHashAlgorithm GetAlgorithm(MacAlgorithm algorithm)
        {
#if SILVERLIGHT
            switch (algorithm)
            {
            case MacAlgorithm.HmacSha1:
                return(new Platform.HMACSHA1());

            case MacAlgorithm.HmacSha256:
                return(new Platform.HMACSHA256());

            default:
                throw new NotSupportedException();
            }
#else
            string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm);
            var    result        = Platform.KeyedHashAlgorithm.Create(algorithmName);
            if (result == null)
            {
                throw new NotSupportedException();
            }

            return(result);
#endif
        }
Exemple #4
0
 public static void MacWithWrongKey(MacAlgorithm a)
 {
     using (var k = new Key(SignatureAlgorithm.Ed25519))
     {
         Assert.Throws <ArgumentException>("key", () => a.Mac(k, ReadOnlySpan <byte> .Empty));
     }
 }
Exemple #5
0
        public static void VerifyWithDisposedKey(MacAlgorithm a)
        {
            var k = new Key(a);

            k.Dispose();
            Assert.Throws <ObjectDisposedException>(() => a.Verify(k, ReadOnlySpan <byte> .Empty, new byte[a.MacSize]));
        }
        /// <summary>
        /// Returns the string to pass to the platform APIs for a given algorithm.
        /// </summary>
        /// <param name="algorithm">The algorithm desired.</param>
        /// <returns>The platform-specific string to pass to OpenAlgorithm.</returns>
        internal static string GetAlgorithmName(MacAlgorithm algorithm)
        {
            switch (algorithm)
            {
            case MacAlgorithm.AesCmac:
                return("AesCmac");

            case MacAlgorithm.HmacMd5:
                return("HmacMd5");

            case MacAlgorithm.HmacSha1:
                return("HmacSha1");

            case MacAlgorithm.HmacSha256:
                return("HmacSha256");

            case MacAlgorithm.HmacSha384:
                return("HmacSha384");

            case MacAlgorithm.HmacSha512:
                return("HmacSha512");

            default:
                throw new ArgumentException();
            }
        }
        /// <summary>
        /// Returns the secret key to use for initializing the Mac.
        /// </summary>
        /// <param name="algorithm">The algorithm.</param>
        /// <param name="keyMaterial">The key material.</param>
        /// <returns>The secret key.</returns>
        internal static SecretKeySpec GetSecretKey(MacAlgorithm algorithm, byte[] keyMaterial)
        {
            string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm);
            var    signingKey    = new SecretKeySpec(keyMaterial, algorithmName);

            return(signingKey);
        }
        private string MakeSignature(string basestring)
        {
            // Encrypt with either SHA1 or SHA256, creating the Signature
            byte[] keyBytes  = Encoding.UTF8.GetBytes(this.oauth_consumer_secret + "&");
            byte[] dataBytes = Encoding.UTF8.GetBytes(basestring);

            MacAlgorithm      a         = oauth_signature_method == "HMAC-SHA1" ? MacAlgorithm.HmacSha1 : MacAlgorithm.HmacSha256;
            var               algorithm = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(a);
            CryptographicHash hasher    = algorithm.CreateHash(keyBytes);

            hasher.Append(dataBytes);
            byte[] mac      = hasher.GetValueAndReset();
            string hmacsha1 = BitConverter.ToString(mac).Replace("-", "").ToLower();

            byte[] resultantArray = new byte[hmacsha1.Length / 2];

            for (int i = 0; i < resultantArray.Length; i++)
            {
                resultantArray[i] = Convert.ToByte(hmacsha1.Substring(i * 2, 2), 16);
            }

            string base64 = Convert.ToBase64String(resultantArray);
            string result = WebUtility.UrlEncode(base64);

            return(result);
        }
Exemple #9
0
 public static void MacWithSpanTooLarge(MacAlgorithm a)
 {
     using (var k = new Key(a))
     {
         Assert.Throws <ArgumentException>("mac", () => a.Mac(k, ReadOnlySpan <byte> .Empty, new byte[a.MacSize + 1]));
     }
 }
Exemple #10
0
        public static void FinalizeSuccess(MacAlgorithm a)
        {
            using (var k = new Key(a))
            {
                var state = default(IncrementalMac);

                Assert.Null(state.Algorithm);

                IncrementalMac.Initialize(k, out state);

                Assert.Same(a, state.Algorithm);

                IncrementalMac.Update(ref state, Utilities.RandomBytes.Slice(0, 100));
                IncrementalMac.Update(ref state, Utilities.RandomBytes.Slice(100, 100));
                IncrementalMac.Update(ref state, Utilities.RandomBytes.Slice(200, 100));

                var actual = IncrementalMac.Finalize(ref state);

                Assert.Null(state.Algorithm);

                var expected = a.Mac(k, Utilities.RandomBytes.Slice(0, 300));

                Assert.Equal(expected, actual);
            }
        }
Exemple #11
0
 public static void VerifyWithSpanTooLarge(MacAlgorithm a)
 {
     using (var k = new Key(a))
     {
         Assert.False(a.Verify(k, ReadOnlySpan <byte> .Empty, new byte[a.MacSize + 1]));
     }
 }
Exemple #12
0
        public static void MacWithDisposedKey(MacAlgorithm a)
        {
            var k = new Key(a);

            k.Dispose();
            Assert.Throws <ObjectDisposedException>(() => a.Mac(k, ReadOnlySpan <byte> .Empty));
        }
Exemple #13
0
        public static void FinalizeWithSpanTooLarge(MacAlgorithm a)
        {
            using var k = new Key(a);

            IncrementalMac.Initialize(k, out var state);

            Assert.Throws <ArgumentException>("mac", () => IncrementalMac.Finalize(ref state, new byte[a.MacSize + 1]));
        }
Exemple #14
0
        /// <inheritdoc />
        public byte[] HashMac(byte[] data, byte[] keyMaterial, MacAlgorithm algorithm = MacAlgorithm.HmacSha256)
        {
            var algorithmProvider = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(algorithm);
            var hasher            = algorithmProvider.CreateHash(keyMaterial);

            hasher.Append(data);
            return(hasher.GetValueAndReset());
        }
Exemple #15
0
        public static void FinalizeAndVerifyFail(MacAlgorithm a)
        {
            using (var k = new Key(a))
            {
                IncrementalMac.Initialize(k, out var state);

                Assert.False(IncrementalMac.FinalizeAndVerify(ref state, new byte[a.MacSize]));
            }
        }
Exemple #16
0
        public static void VerifyWithSpanSuccess(MacAlgorithm a)
        {
            using var k = new Key(a);
            var d = ReadOnlySpan <byte> .Empty;

            var mac = a.Mac(k, d);

            Assert.True(a.Verify(k, d, mac));
        }
Exemple #17
0
        public static void FinalizeAndVerifySuccess(MacAlgorithm a)
        {
            using (var k = new Key(a))
            {
                IncrementalMac.Initialize(k, out var state);

                Assert.True(IncrementalMac.FinalizeAndVerify(ref state, a.Mac(k, ReadOnlySpan <byte> .Empty)));
            }
        }
Exemple #18
0
    public void Mac(MacAlgorithm algorithm)
    {
        var provider = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(algorithm);
        var key      = provider.CreateKey(WinRTCrypto.CryptographicBuffer.GenerateRandom(32));

        byte[] data = new byte[5];
        byte[] mac  = WinRTCrypto.CryptographicEngine.Sign(key, data);
        Assert.Equal(provider.MacLength, mac.Length);
        Assert.True(WinRTCrypto.CryptographicEngine.VerifySignature(key, data, mac));
    }
 /// <summary>
 /// Returns the keyed hash algorithm from the platform.
 /// </summary>
 /// <param name="algorithm">The algorithm desired.</param>
 /// <returns>The platform-specific algorithm.</returns>
 internal static Mac GetAlgorithm(MacAlgorithm algorithm)
 {
     string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm);
     try
     {
         return Mac.GetInstance(algorithmName);
     }
     catch (Java.Security.NoSuchAlgorithmException ex)
     {
         throw new NotSupportedException(ex.Message, ex);
     }
 }
Exemple #20
0
        public static void ExportImportRaw(MacAlgorithm a)
        {
            var b = Utilities.RandomBytes.Slice(0, a.KeySize);

            using var k = Key.Import(a, b, KeyBlobFormat.RawSymmetricKey, new KeyCreationParameters { ExportPolicy = KeyExportPolicies.AllowPlaintextArchiving });
            Assert.Equal(KeyExportPolicies.AllowPlaintextArchiving, k.ExportPolicy);

            var expected = b.ToArray();
            var actual   = k.Export(KeyBlobFormat.RawSymmetricKey);

            Assert.Equal(expected, actual);
        }
        /// <summary>
        /// Returns the keyed hash algorithm from the platform.
        /// </summary>
        /// <param name="algorithm">The algorithm desired.</param>
        /// <returns>The platform-specific algorithm.</returns>
        internal static Platform.KeyedHashAlgorithm GetAlgorithm(MacAlgorithm algorithm)
        {
            string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm);
            var    result        = Platform.KeyedHashAlgorithm.Create(algorithmName);

            if (result == null)
            {
                throw new NotSupportedException();
            }

            return(result);
        }
Exemple #22
0
        public static void MacWithSpanSuccess(MacAlgorithm a)
        {
            using var k = new Key(a);
            var data = Utilities.RandomBytes.Slice(0, 100);

            var expected = new byte[a.MacSize];
            var actual   = new byte[a.MacSize];

            a.Mac(k, data, expected);
            a.Mac(k, data, actual);
            Assert.Equal(expected, actual);
        }
Exemple #23
0
        public static void MacSuccess(MacAlgorithm a)
        {
            using var k = new Key(a);
            var data = Utilities.RandomBytes.Slice(0, 100);

            var expected = a.Mac(k, data);
            var actual   = a.Mac(k, data);

            Assert.NotNull(actual);
            Assert.Equal(a.MacSize, actual.Length);
            Assert.Equal(expected, actual);
        }
        /// <summary>
        /// Returns the keyed hash algorithm from the platform.
        /// </summary>
        /// <param name="algorithm">The algorithm desired.</param>
        /// <returns>The platform-specific algorithm.</returns>
        internal static Mac GetAlgorithm(MacAlgorithm algorithm)
        {
            string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm);

            try
            {
                return(Mac.GetInstance(algorithmName));
            }
            catch (Java.Security.NoSuchAlgorithmException ex)
            {
                throw new NotSupportedException(ex.Message, ex);
            }
        }
Exemple #25
0
        public static void MacWithSpanOverlapping(MacAlgorithm a)
        {
            using var k = new Key(a);
            var data = Utilities.RandomBytes.Slice(0, 100).ToArray();

            var expected = new byte[a.MacSize];
            var actual   = data.AsSpan(0, a.MacSize);

            a.Mac(k, data, expected);
            a.Mac(k, data, actual);

            Assert.Equal(expected, actual.ToArray());
        }
Exemple #26
0
        public PasswordBasedMacScheme(
            PasswordBasedKeyDerivationAlgorithm keyDerivationAlgorithm,
            MacAlgorithm macAlgorithm)
        {
            if (keyDerivationAlgorithm == null)
            {
                throw new ArgumentNullException(nameof(keyDerivationAlgorithm));
            }
            if (macAlgorithm == null)
            {
                throw new ArgumentNullException(nameof(macAlgorithm));
            }

            _keyDerivationAlgorithm = keyDerivationAlgorithm;
            _macAlgorithm           = macAlgorithm;
        }
Exemple #27
0
        public static void CreateKey(MacAlgorithm a)
        {
            using var k = new Key(a, new KeyCreationParameters { ExportPolicy = KeyExportPolicies.AllowPlaintextArchiving });
            Assert.Same(a, k.Algorithm);
            Assert.False(k.HasPublicKey);
            Assert.Throws <InvalidOperationException>(() => k.PublicKey);
            Assert.Equal(a.KeySize, k.Size);

            var actual = k.Export(KeyBlobFormat.RawSymmetricKey);

            var unexpected = new byte[actual.Length];

            Utilities.Fill(unexpected, actual[0]);

            Assert.NotEqual(unexpected, actual);
        }
 /// <summary>
 /// Returns the string to pass to the platform APIs for a given algorithm.
 /// </summary>
 /// <param name="algorithm">The algorithm desired.</param>
 /// <returns>The platform-specific string to pass to OpenAlgorithm.</returns>
 internal static string GetAlgorithmName(MacAlgorithm algorithm)
 {
     switch (algorithm)
     {
         case MacAlgorithm.AesCmac:
             return "AesCmac";
         case MacAlgorithm.HmacMd5:
             return "HmacMd5";
         case MacAlgorithm.HmacSha1:
             return "HmacSha1";
         case MacAlgorithm.HmacSha256:
             return "HmacSha256";
         case MacAlgorithm.HmacSha384:
             return "HmacSha384";
         case MacAlgorithm.HmacSha512:
             return "HmacSha512";
         default:
             throw new ArgumentException();
     }
 }
 /// <summary>
 /// Returns the string to pass to the platform APIs for a given algorithm.
 /// </summary>
 /// <param name="algorithm">The algorithm desired.</param>
 /// <returns>The platform-specific string to pass to OpenAlgorithm.</returns>
 private static string GetAlgorithmName(MacAlgorithm algorithm)
 {
     switch (algorithm)
     {
         case MacAlgorithm.AesCmac:
             return Platform.Core.MacAlgorithmNames.AesCmac;
         case MacAlgorithm.HmacMd5:
             return Platform.Core.MacAlgorithmNames.HmacMd5;
         case MacAlgorithm.HmacSha1:
             return Platform.Core.MacAlgorithmNames.HmacSha1;
         case MacAlgorithm.HmacSha256:
             return Platform.Core.MacAlgorithmNames.HmacSha256;
         case MacAlgorithm.HmacSha384:
             return Platform.Core.MacAlgorithmNames.HmacSha384;
         case MacAlgorithm.HmacSha512:
             return Platform.Core.MacAlgorithmNames.HmacSha512;
         default:
             throw new NotSupportedException();
     }
 }
Exemple #30
0
        public static void FinalizeWithSpanSuccessNoUpdate(MacAlgorithm a)
        {
            using var k = new Key(a);
            var state = default(IncrementalMac);

            Assert.Null(state.Algorithm);

            IncrementalMac.Initialize(k, out state);

            Assert.Same(a, state.Algorithm);

            var actual = new byte[a.MacSize];

            IncrementalMac.Finalize(ref state, actual);

            Assert.Null(state.Algorithm);

            var expected = a.Mac(k, ReadOnlySpan <byte> .Empty);

            Assert.Equal(expected, actual);
        }
        /// <summary>
        /// Returns the keyed hash algorithm from the platform.
        /// </summary>
        /// <param name="algorithm">The algorithm desired.</param>
        /// <returns>The platform-specific algorithm.</returns>
        internal static Platform.KeyedHashAlgorithm GetAlgorithm(MacAlgorithm algorithm)
        {
#if SILVERLIGHT
            switch (algorithm)
            {
                case MacAlgorithm.HmacSha1:
                    return new Platform.HMACSHA1();
                case MacAlgorithm.HmacSha256:
                    return new Platform.HMACSHA256();
                default:
                    throw new NotSupportedException();
            }
#else
            string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm);
            var result = Platform.KeyedHashAlgorithm.Create(algorithmName);
            if (result == null)
            {
                throw new NotSupportedException();
            }

            return result;
#endif
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MacAlgorithmProvider"/> class.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 internal MacAlgorithmProvider(MacAlgorithm algorithm)
 {
     this.algorithm = algorithm;
     this.platform = Platform.Core.MacAlgorithmProvider.OpenAlgorithm(GetAlgorithmName(algorithm));
 }
 /// <summary>
 /// Returns the secret key to use for initializing the Mac.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="keyMaterial">The key material.</param>
 /// <returns>The secret key.</returns>
 internal static SecretKeySpec GetSecretKey(MacAlgorithm algorithm, byte[] keyMaterial)
 {
     string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm);
     var signingKey = new SecretKeySpec(keyMaterial, algorithmName);
     return signingKey;
 }
 /// <inheritdoc />
 public IMacAlgorithmProvider OpenAlgorithm(MacAlgorithm algorithm)
 {
     return new MacAlgorithmProvider(algorithm);
 }
 /// <inheritdoc />
 public IMacAlgorithmProvider OpenAlgorithm(MacAlgorithm algorithm)
 {
     return(new MacAlgorithmProvider(algorithm));
 }
 public void Mac(MacAlgorithm algorithm)
 {
     var provider = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(algorithm);
     var key = provider.CreateKey(WinRTCrypto.CryptographicBuffer.GenerateRandom(32));
     byte[] data = new byte[5];
     byte[] mac = WinRTCrypto.CryptographicEngine.Sign(key, data);
     Assert.Equal(provider.MacLength, mac.Length);
     Assert.True(WinRTCrypto.CryptographicEngine.VerifySignature(key, data, mac));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MacCryptographicKey" /> class.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="key">The key.</param>
 internal MacCryptographicKey(MacAlgorithm algorithm, byte[] key)
 {
     Requires.NotNull(key, "key");
     this.algorithm = algorithm;
     this.key       = key;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MacAlgorithmProvider"/> class.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 internal MacAlgorithmProvider(MacAlgorithm algorithm)
 {
     this.algorithm = algorithm;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MacCryptographicKey" /> class.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="key">The key.</param>
 internal MacCryptographicKey(MacAlgorithm algorithm, byte[] key)
 {
     Requires.NotNull(key, "key");
     this.algorithm = algorithm;
     this.key = key;
 }
Exemple #40
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MacAlgorithmProvider"/> class.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 internal MacAlgorithmProvider(MacAlgorithm algorithm)
 {
     this.algorithm = algorithm;
 }
 /// <summary>
 /// Returns the keyed hash algorithm from the platform.
 /// </summary>
 /// <param name="algorithm">The algorithm desired.</param>
 /// <returns>The platform-specific algorithm.</returns>
 internal static Mac GetAlgorithm(MacAlgorithm algorithm)
 {
     string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm);
     return Mac.GetInstance(algorithmName);
 }