Skip to content

Commit b5bff1b

Browse files
committed
Merge branch 'pgpainless-hashUtil'
2 parents 0053f46 + 56b4565 commit b5bff1b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.bouncycastle.bcpg;
2+
3+
public class HashUtils
4+
{
5+
6+
/**
7+
* Return the length of the salt per hash algorithm, used in OpenPGP v6 signatures.
8+
*
9+
* @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#hash-algorithms-registry">
10+
* Salt Size declarations</a>
11+
* @param hashAlgorithm hash algorithm tag
12+
* @return size of the salt for the given hash algorithm in bytes
13+
*/
14+
public static int getV6SignatureSaltSizeInBytes(int hashAlgorithm)
15+
{
16+
switch (hashAlgorithm)
17+
{
18+
case HashAlgorithmTags.SHA256:
19+
case HashAlgorithmTags.SHA224:
20+
case HashAlgorithmTags.SHA3_256:
21+
case HashAlgorithmTags.SHA3_256_OLD:
22+
return 16;
23+
case HashAlgorithmTags.SHA384:
24+
return 24;
25+
case HashAlgorithmTags.SHA512:
26+
case HashAlgorithmTags.SHA3_512:
27+
case HashAlgorithmTags.SHA3_512_OLD:
28+
return 32;
29+
default:
30+
throw new IllegalArgumentException("Salt size not specified for Hash Algorithm with ID " + hashAlgorithm);
31+
}
32+
}
33+
34+
/**
35+
* Return true, if the encountered saltLength matches the value the specification gives for the hashAlgorithm.
36+
*
37+
* @param hashAlgorithm hash algorithm tag
38+
* @param saltSize encountered salt size
39+
* @return true if the encountered size matches the spec
40+
* @implNote LibrePGP allows for zero-length signature salt values, so this method only works for IETF OpenPGP v6.
41+
*/
42+
public boolean saltSizeMatchesSpec(int hashAlgorithm, int saltSize)
43+
{
44+
try
45+
{
46+
return saltSize == getV6SignatureSaltSizeInBytes(hashAlgorithm);
47+
}
48+
catch (IllegalArgumentException e) // Unknown algorithm or salt size is not specified for the hash algo
49+
{
50+
return false;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)