|
| 1 | +package org.bouncycastle.operator.bc; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import org.bouncycastle.asn1.ASN1Integer; |
| 8 | +import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers; |
| 9 | +import org.bouncycastle.asn1.gm.GMObjectIdentifiers; |
| 10 | +import org.bouncycastle.asn1.misc.MiscObjectIdentifiers; |
| 11 | +import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; |
| 12 | +import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers; |
| 13 | +import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
| 14 | +import org.bouncycastle.asn1.rosstandart.RosstandartObjectIdentifiers; |
| 15 | +import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers; |
| 16 | +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 17 | +import org.bouncycastle.crypto.ExtendedDigest; |
| 18 | +import org.bouncycastle.crypto.Xof; |
| 19 | +import org.bouncycastle.crypto.digests.GOST3411Digest; |
| 20 | +import org.bouncycastle.crypto.digests.GOST3411_2012_256Digest; |
| 21 | +import org.bouncycastle.crypto.digests.GOST3411_2012_512Digest; |
| 22 | +import org.bouncycastle.crypto.digests.MD2Digest; |
| 23 | +import org.bouncycastle.crypto.digests.MD4Digest; |
| 24 | +import org.bouncycastle.crypto.digests.MD5Digest; |
| 25 | +import org.bouncycastle.crypto.digests.RIPEMD128Digest; |
| 26 | +import org.bouncycastle.crypto.digests.RIPEMD160Digest; |
| 27 | +import org.bouncycastle.crypto.digests.RIPEMD256Digest; |
| 28 | +import org.bouncycastle.crypto.digests.SHA1Digest; |
| 29 | +import org.bouncycastle.crypto.digests.SHA224Digest; |
| 30 | +import org.bouncycastle.crypto.digests.SHA256Digest; |
| 31 | +import org.bouncycastle.crypto.digests.SHA384Digest; |
| 32 | +import org.bouncycastle.crypto.digests.SHA3Digest; |
| 33 | +import org.bouncycastle.crypto.digests.SHA512Digest; |
| 34 | +import org.bouncycastle.crypto.digests.SHAKEDigest; |
| 35 | +import org.bouncycastle.crypto.digests.SM3Digest; |
| 36 | +import org.bouncycastle.operator.OperatorCreationException; |
| 37 | + |
| 38 | +public class BcDefaultDigestProvider |
| 39 | + implements BcDigestProvider |
| 40 | +{ |
| 41 | + private static final Map lookup = createTable(); |
| 42 | + |
| 43 | + private static Map createTable() |
| 44 | + { |
| 45 | + Map table = new HashMap(); |
| 46 | + |
| 47 | + table.put(OIWObjectIdentifiers.idSHA1, new BcDigestProvider() |
| 48 | + { |
| 49 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 50 | + { |
| 51 | + return new SHA1Digest(); |
| 52 | + } |
| 53 | + }); |
| 54 | + table.put(NISTObjectIdentifiers.id_sha224, new BcDigestProvider() |
| 55 | + { |
| 56 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 57 | + { |
| 58 | + return new SHA224Digest(); |
| 59 | + } |
| 60 | + }); |
| 61 | + table.put(NISTObjectIdentifiers.id_sha256, new BcDigestProvider() |
| 62 | + { |
| 63 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 64 | + { |
| 65 | + return new SHA256Digest(); |
| 66 | + } |
| 67 | + }); |
| 68 | + table.put(NISTObjectIdentifiers.id_sha384, new BcDigestProvider() |
| 69 | + { |
| 70 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 71 | + { |
| 72 | + return new SHA384Digest(); |
| 73 | + } |
| 74 | + }); |
| 75 | + table.put(NISTObjectIdentifiers.id_sha512, new BcDigestProvider() |
| 76 | + { |
| 77 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 78 | + { |
| 79 | + return new SHA512Digest(); |
| 80 | + } |
| 81 | + }); |
| 82 | + table.put(NISTObjectIdentifiers.id_sha3_224, new BcDigestProvider() |
| 83 | + { |
| 84 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 85 | + { |
| 86 | + return new SHA3Digest(224); |
| 87 | + } |
| 88 | + }); |
| 89 | + table.put(NISTObjectIdentifiers.id_sha3_256, new BcDigestProvider() |
| 90 | + { |
| 91 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 92 | + { |
| 93 | + return new SHA3Digest(256); |
| 94 | + } |
| 95 | + }); |
| 96 | + table.put(NISTObjectIdentifiers.id_sha3_384, new BcDigestProvider() |
| 97 | + { |
| 98 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 99 | + { |
| 100 | + return new SHA3Digest(384); |
| 101 | + } |
| 102 | + }); |
| 103 | + table.put(NISTObjectIdentifiers.id_sha3_512, new BcDigestProvider() |
| 104 | + { |
| 105 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 106 | + { |
| 107 | + return new SHA3Digest(512); |
| 108 | + } |
| 109 | + }); |
| 110 | + table.put(NISTObjectIdentifiers.id_shake128, new BcDigestProvider() |
| 111 | + { |
| 112 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 113 | + { |
| 114 | + return new SHAKEDigest(128); |
| 115 | + } |
| 116 | + }); |
| 117 | + table.put(NISTObjectIdentifiers.id_shake256, new BcDigestProvider() |
| 118 | + { |
| 119 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 120 | + { |
| 121 | + return new SHAKEDigest(256); |
| 122 | + } |
| 123 | + }); |
| 124 | + table.put(NISTObjectIdentifiers.id_shake128_len, new BcDigestProvider() |
| 125 | + { |
| 126 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 127 | + { |
| 128 | + return new AdjustedXof(new SHAKEDigest(128), ASN1Integer.getInstance(digestAlgorithmIdentifier.getParameters()).intValueExact()); |
| 129 | + } |
| 130 | + }); |
| 131 | + table.put(NISTObjectIdentifiers.id_shake256_len, new BcDigestProvider() |
| 132 | + { |
| 133 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 134 | + { |
| 135 | + return new AdjustedXof(new SHAKEDigest(256), ASN1Integer.getInstance(digestAlgorithmIdentifier.getParameters()).intValueExact()); |
| 136 | + } |
| 137 | + }); |
| 138 | + table.put(PKCSObjectIdentifiers.md5, new BcDigestProvider() |
| 139 | + { |
| 140 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 141 | + { |
| 142 | + return new MD5Digest(); |
| 143 | + } |
| 144 | + }); |
| 145 | + table.put(PKCSObjectIdentifiers.md4, new BcDigestProvider() |
| 146 | + { |
| 147 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 148 | + { |
| 149 | + return new MD4Digest(); |
| 150 | + } |
| 151 | + }); |
| 152 | + table.put(PKCSObjectIdentifiers.md2, new BcDigestProvider() |
| 153 | + { |
| 154 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 155 | + { |
| 156 | + return new MD2Digest(); |
| 157 | + } |
| 158 | + }); |
| 159 | + table.put(CryptoProObjectIdentifiers.gostR3411, new BcDigestProvider() |
| 160 | + { |
| 161 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 162 | + { |
| 163 | + return new GOST3411Digest(); |
| 164 | + } |
| 165 | + }); |
| 166 | + table.put(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, new BcDigestProvider() |
| 167 | + { |
| 168 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 169 | + { |
| 170 | + return new GOST3411_2012_256Digest(); |
| 171 | + } |
| 172 | + }); |
| 173 | + table.put(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, new BcDigestProvider() |
| 174 | + { |
| 175 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 176 | + { |
| 177 | + return new GOST3411_2012_512Digest(); |
| 178 | + } |
| 179 | + }); |
| 180 | + table.put(TeleTrusTObjectIdentifiers.ripemd128, new BcDigestProvider() |
| 181 | + { |
| 182 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 183 | + { |
| 184 | + return new RIPEMD128Digest(); |
| 185 | + } |
| 186 | + }); |
| 187 | + table.put(TeleTrusTObjectIdentifiers.ripemd160, new BcDigestProvider() |
| 188 | + { |
| 189 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 190 | + { |
| 191 | + return new RIPEMD160Digest(); |
| 192 | + } |
| 193 | + }); |
| 194 | + table.put(TeleTrusTObjectIdentifiers.ripemd256, new BcDigestProvider() |
| 195 | + { |
| 196 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 197 | + { |
| 198 | + return new RIPEMD256Digest(); |
| 199 | + } |
| 200 | + }); |
| 201 | + table.put(GMObjectIdentifiers.sm3, new BcDigestProvider() |
| 202 | + { |
| 203 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 204 | + { |
| 205 | + return new SM3Digest(); |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + return Collections.unmodifiableMap(table); |
| 210 | + } |
| 211 | + |
| 212 | + public static final BcDigestProvider INSTANCE = new BcDefaultDigestProvider(); |
| 213 | + |
| 214 | + private BcDefaultDigestProvider() |
| 215 | + { |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) |
| 220 | + throws OperatorCreationException |
| 221 | + { |
| 222 | + BcDigestProvider extProv = (BcDigestProvider)lookup.get(digestAlgorithmIdentifier.getAlgorithm()); |
| 223 | + |
| 224 | + if (extProv == null) |
| 225 | + { |
| 226 | + throw new OperatorCreationException("cannot recognise digest"); |
| 227 | + } |
| 228 | + |
| 229 | + return extProv.get(digestAlgorithmIdentifier); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * -len OIDs for SHAKE include an integer representing the bitlength in of the output. |
| 234 | + */ |
| 235 | + private static class AdjustedXof |
| 236 | + implements Xof |
| 237 | + { |
| 238 | + private final Xof xof; |
| 239 | + private final int length; |
| 240 | + |
| 241 | + AdjustedXof(Xof xof, int length) |
| 242 | + { |
| 243 | + this.xof = xof; |
| 244 | + this.length = length; |
| 245 | + } |
| 246 | + |
| 247 | + public String getAlgorithmName() |
| 248 | + { |
| 249 | + return xof.getAlgorithmName() + "-" + length; |
| 250 | + } |
| 251 | + |
| 252 | + public int getDigestSize() |
| 253 | + { |
| 254 | + return (length + 7) / 8; |
| 255 | + } |
| 256 | + |
| 257 | + public void update(byte in) |
| 258 | + { |
| 259 | + xof.update(in); |
| 260 | + } |
| 261 | + |
| 262 | + public void update(byte[] in, int inOff, int len) |
| 263 | + { |
| 264 | + xof.update(in, inOff, len); |
| 265 | + } |
| 266 | + |
| 267 | + public int doFinal(byte[] out, int outOff) |
| 268 | + { |
| 269 | + return doFinal(out, outOff, getDigestSize()); |
| 270 | + } |
| 271 | + |
| 272 | + public void reset() |
| 273 | + { |
| 274 | + xof.reset(); |
| 275 | + } |
| 276 | + |
| 277 | + public int getByteLength() |
| 278 | + { |
| 279 | + return xof.getByteLength(); |
| 280 | + } |
| 281 | + |
| 282 | + public int doFinal(byte[] out, int outOff, int outLen) |
| 283 | + { |
| 284 | + return xof.doFinal(out, outOff, outLen); |
| 285 | + } |
| 286 | + |
| 287 | + public int doOutput(byte[] out, int outOff, int outLen) |
| 288 | + { |
| 289 | + return xof.doOutput(out, outOff, outLen); |
| 290 | + } |
| 291 | + } |
| 292 | +} |
0 commit comments