summary refs log tree commit diff
path: root/crypto/src/security/DigestUtilities.cs
blob: 7ddf6c8e4aefaa9617de5acfb6ff197927708749 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Security
{
    /// <remarks>
    ///  Utility class for creating IDigest objects from their names/Oids
    /// </remarks>
    public sealed class DigestUtilities
    {
        private enum DigestAlgorithm {
            GOST3411,
            KECCAK_224, KECCAK_256, KECCAK_288, KECCAK_384, KECCAK_512,
            MD2, MD4, MD5,
            RIPEMD128, RIPEMD160, RIPEMD256, RIPEMD320,
            SHA_1, SHA_224, SHA_256, SHA_384, SHA_512,
            SHA_512_224, SHA_512_256,
            SHA3_224, SHA3_256, SHA3_384, SHA3_512,
            SHAKE128, SHAKE256,
            TIGER,
            WHIRLPOOL,
        };

        private DigestUtilities()
        {
        }

        private static readonly IDictionary algorithms = Platform.CreateHashtable();
        private static readonly IDictionary oids = Platform.CreateHashtable();

        static DigestUtilities()
        {
            // Signal to obfuscation tools not to change enum constants
            ((DigestAlgorithm)Enums.GetArbitraryValue(typeof(DigestAlgorithm))).ToString();

            algorithms[PkcsObjectIdentifiers.MD2.Id] = "MD2";
            algorithms[PkcsObjectIdentifiers.MD4.Id] = "MD4";
            algorithms[PkcsObjectIdentifiers.MD5.Id] = "MD5";

            algorithms["SHA1"] = "SHA-1";
            algorithms[OiwObjectIdentifiers.IdSha1.Id] = "SHA-1";
            algorithms["SHA224"] = "SHA-224";
            algorithms[NistObjectIdentifiers.IdSha224.Id] = "SHA-224";
            algorithms["SHA256"] = "SHA-256";
            algorithms[NistObjectIdentifiers.IdSha256.Id] = "SHA-256";
            algorithms["SHA384"] = "SHA-384";
            algorithms[NistObjectIdentifiers.IdSha384.Id] = "SHA-384";
            algorithms["SHA512"] = "SHA-512";
            algorithms[NistObjectIdentifiers.IdSha512.Id] = "SHA-512";
            algorithms["SHA512/224"] = "SHA-512/224";
            algorithms[NistObjectIdentifiers.IdSha512_224.Id] = "SHA-512/224";
            algorithms["SHA512/256"] = "SHA-512/256";
            algorithms[NistObjectIdentifiers.IdSha512_256.Id] = "SHA-512/256";

            algorithms["RIPEMD-128"] = "RIPEMD128";
            algorithms[TeleTrusTObjectIdentifiers.RipeMD128.Id] = "RIPEMD128";
            algorithms["RIPEMD-160"] = "RIPEMD160";
            algorithms[TeleTrusTObjectIdentifiers.RipeMD160.Id] = "RIPEMD160";
            algorithms["RIPEMD-256"] = "RIPEMD256";
            algorithms[TeleTrusTObjectIdentifiers.RipeMD256.Id] = "RIPEMD256";
            algorithms["RIPEMD-320"] = "RIPEMD320";
//			algorithms[TeleTrusTObjectIdentifiers.RipeMD320.Id] = "RIPEMD320";

            algorithms[CryptoProObjectIdentifiers.GostR3411.Id] = "GOST3411";

            algorithms[NistObjectIdentifiers.IdSha3_224.Id] = "SHA3-224";
            algorithms[NistObjectIdentifiers.IdSha3_256.Id] = "SHA3-256";
            algorithms[NistObjectIdentifiers.IdSha3_384.Id] = "SHA3-384";
            algorithms[NistObjectIdentifiers.IdSha3_512.Id] = "SHA3-512";
            algorithms[NistObjectIdentifiers.IdShake128.Id] = "SHAKE128";
            algorithms[NistObjectIdentifiers.IdShake256.Id] = "SHAKE256";

            oids["MD2"] = PkcsObjectIdentifiers.MD2;
            oids["MD4"] = PkcsObjectIdentifiers.MD4;
            oids["MD5"] = PkcsObjectIdentifiers.MD5;
            oids["SHA-1"] = OiwObjectIdentifiers.IdSha1;
            oids["SHA-224"] = NistObjectIdentifiers.IdSha224;
            oids["SHA-256"] = NistObjectIdentifiers.IdSha256;
            oids["SHA-384"] = NistObjectIdentifiers.IdSha384;
            oids["SHA-512"] = NistObjectIdentifiers.IdSha512;
            oids["SHA-512/224"] = NistObjectIdentifiers.IdSha512_224;
            oids["SHA-512/256"] = NistObjectIdentifiers.IdSha512_256;
            oids["SHA3-224"] = NistObjectIdentifiers.IdSha3_224;
            oids["SHA3-256"] = NistObjectIdentifiers.IdSha3_256;
            oids["SHA3-384"] = NistObjectIdentifiers.IdSha3_384;
            oids["SHA3-512"] = NistObjectIdentifiers.IdSha3_512;
            oids["SHAKE128"] = NistObjectIdentifiers.IdShake128;
            oids["SHAKE256"] = NistObjectIdentifiers.IdShake256;
            oids["RIPEMD128"] = TeleTrusTObjectIdentifiers.RipeMD128;
            oids["RIPEMD160"] = TeleTrusTObjectIdentifiers.RipeMD160;
            oids["RIPEMD256"] = TeleTrusTObjectIdentifiers.RipeMD256;
            oids["GOST3411"] = CryptoProObjectIdentifiers.GostR3411;
        }

        /// <summary>
        /// Returns a ObjectIdentifier for a given digest mechanism.
        /// </summary>
        /// <param name="mechanism">A string representation of the digest meanism.</param>
        /// <returns>A DerObjectIdentifier, null if the Oid is not available.</returns>

        public static DerObjectIdentifier GetObjectIdentifier(
            string mechanism)
        {
            if (mechanism == null)
                throw new System.ArgumentNullException("mechanism");

            mechanism = Platform.ToUpperInvariant(mechanism);
            string aliased = (string) algorithms[mechanism];

            if (aliased != null)
                mechanism = aliased;

            return (DerObjectIdentifier) oids[mechanism];
        }

        public static ICollection Algorithms
        {
            get { return oids.Keys; }
        }

        public static IDigest GetDigest(
            DerObjectIdentifier id)
        {
            return GetDigest(id.Id);
        }

        public static IDigest GetDigest(
            string algorithm)
        {
            string upper = Platform.ToUpperInvariant(algorithm);
            string mechanism = (string) algorithms[upper];

            if (mechanism == null)
            {
                mechanism = upper;
            }

            try
            {
                DigestAlgorithm digestAlgorithm = (DigestAlgorithm)Enums.GetEnumValue(
                    typeof(DigestAlgorithm), mechanism);

                switch (digestAlgorithm)
                {
                    case DigestAlgorithm.GOST3411:      return new Gost3411Digest();
                    case DigestAlgorithm.KECCAK_224:    return new KeccakDigest(224);
                    case DigestAlgorithm.KECCAK_256:    return new KeccakDigest(256);
                    case DigestAlgorithm.KECCAK_288:    return new KeccakDigest(288);
                    case DigestAlgorithm.KECCAK_384:    return new KeccakDigest(384);
                    case DigestAlgorithm.KECCAK_512:    return new KeccakDigest(512);
                    case DigestAlgorithm.MD2:           return new MD2Digest();
                    case DigestAlgorithm.MD4:		    return new MD4Digest();
                    case DigestAlgorithm.MD5:		    return new MD5Digest();
                    case DigestAlgorithm.RIPEMD128:	    return new RipeMD128Digest();
                    case DigestAlgorithm.RIPEMD160:	    return new RipeMD160Digest();
                    case DigestAlgorithm.RIPEMD256:	    return new RipeMD256Digest();
                    case DigestAlgorithm.RIPEMD320:	    return new RipeMD320Digest();
                    case DigestAlgorithm.SHA_1:		    return new Sha1Digest();
                    case DigestAlgorithm.SHA_224:	    return new Sha224Digest();
                    case DigestAlgorithm.SHA_256:	    return new Sha256Digest();
                    case DigestAlgorithm.SHA_384:	    return new Sha384Digest();
                    case DigestAlgorithm.SHA_512:	    return new Sha512Digest();
                    case DigestAlgorithm.SHA_512_224:   return new Sha512tDigest(224);
                    case DigestAlgorithm.SHA_512_256:   return new Sha512tDigest(256);
                    case DigestAlgorithm.SHA3_224:      return new Sha3Digest(224);
                    case DigestAlgorithm.SHA3_256:      return new Sha3Digest(256);
                    case DigestAlgorithm.SHA3_384:      return new Sha3Digest(384);
                    case DigestAlgorithm.SHA3_512:      return new Sha3Digest(512);
                    case DigestAlgorithm.SHAKE128:      return new ShakeDigest(128);
                    case DigestAlgorithm.SHAKE256:      return new ShakeDigest(256);
                    case DigestAlgorithm.TIGER:         return new TigerDigest();
                    case DigestAlgorithm.WHIRLPOOL:     return new WhirlpoolDigest();
                }
            }
            catch (ArgumentException)
            {
            }

            throw new SecurityUtilityException("Digest " + mechanism + " not recognised.");
        }

        public static string GetAlgorithmName(
            DerObjectIdentifier oid)
        {
            return (string) algorithms[oid.Id];
        }

        public static byte[] CalculateDigest(string algorithm, byte[] input)
        {
            IDigest digest = GetDigest(algorithm);
            digest.BlockUpdate(input, 0, input.Length);
            return DoFinal(digest);
        }

        public static byte[] DoFinal(
            IDigest digest)
        {
            byte[] b = new byte[digest.GetDigestSize()];
            digest.DoFinal(b, 0);
            return b;
        }

        public static byte[] DoFinal(
            IDigest	digest,
            byte[]	input)
        {
            digest.BlockUpdate(input, 0, input.Length);
            return DoFinal(digest);
        }
    }
}