Skip to content

Commit 9ccd3bb

Browse files
committed
Give CryptoServicePurpose an explicit code, equal to its current ordinal, and read and write a digest's encoded-state purpose byte through getCode/forCode rather than ordinal()/values()[index], so reordering the enum no longer changes the encoded-state format.
1 parent 1fdfb53 commit 9ccd3bb

11 files changed

Lines changed: 76 additions & 25 deletions

File tree

‎core/src/main/java/org/bouncycastle/crypto/CryptoServicePurpose.java‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,49 @@
22

33
public enum CryptoServicePurpose
44
{
5-
AGREEMENT,
6-
ENCRYPTION,
7-
DECRYPTION,
8-
KEYGEN,
9-
SIGNING, // for signatures (and digests)
10-
VERIFYING,
11-
AUTHENTICATION, // for MACs (and digests)
12-
VERIFICATION,
13-
PRF,
14-
ANY
5+
AGREEMENT(0),
6+
ENCRYPTION(1),
7+
DECRYPTION(2),
8+
KEYGEN(3),
9+
SIGNING(4), // for signatures (and digests)
10+
VERIFYING(5),
11+
AUTHENTICATION(6), // for MACs (and digests)
12+
VERIFICATION(7),
13+
PRF(8),
14+
ANY(9);
15+
16+
private final int code;
17+
18+
CryptoServicePurpose(int code)
19+
{
20+
this.code = code;
21+
}
22+
23+
/**
24+
* The stable numeric code for this purpose. It is written as the trailing byte of a digest's
25+
* encoded state, so - unlike {@link #ordinal()} - it must not shift if the constants are ever
26+
* reordered; each constant carries its own code for that reason.
27+
*/
28+
public int getCode()
29+
{
30+
return code;
31+
}
32+
33+
/**
34+
* Return the purpose with the given {@link #getCode() code}.
35+
*
36+
* @throws IllegalArgumentException if no purpose has that code.
37+
*/
38+
public static CryptoServicePurpose forCode(int code)
39+
{
40+
for (CryptoServicePurpose purpose : values())
41+
{
42+
if (purpose.code == code)
43+
{
44+
return purpose;
45+
}
46+
}
47+
48+
throw new IllegalArgumentException("unknown CryptoServicePurpose code: " + code);
49+
}
1550
}

‎core/src/main/java/org/bouncycastle/crypto/digests/GeneralDigest.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ protected GeneralDigest(GeneralDigest t)
5151

5252
protected GeneralDigest(byte[] encodedState)
5353
{
54-
CryptoServicePurpose[] values = CryptoServicePurpose.values();
55-
this.purpose = values[encodedState[encodedState.length - 1]];
54+
this.purpose = CryptoServicePurpose.forCode(encodedState[encodedState.length - 1]);
5655

5756
System.arraycopy(encodedState, 0, xBuf, 0, xBuf.length);
5857
xBufOff = Pack.bigEndianToInt(encodedState, 4);

‎core/src/main/java/org/bouncycastle/crypto/digests/KeccakDigest.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ protected KeccakDigest(byte[] encodedState, CryptoServicePurpose purpose)
105105

106106
private static CryptoServicePurpose getCryptoServicePurpose(byte b)
107107
{
108-
CryptoServicePurpose[] values = CryptoServicePurpose.values();
109-
return values[b];
108+
return CryptoServicePurpose.forCode(b);
110109
}
111110

112111
protected void copyIn(KeccakDigest source)
@@ -533,7 +532,7 @@ protected byte[] getEncodedState(byte[] encState)
533532
ensureQueuePacked(rate >>> 3);
534533
}
535534

536-
encState[0] = (byte)purpose.ordinal();
535+
encState[0] = (byte)purpose.getCode();
537536

538537
int sOff = 1;
539538
for (int i = 0; i != state.length; i++)

‎core/src/main/java/org/bouncycastle/crypto/digests/MD5Digest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public byte[] getEncodedState()
351351
Pack.intToBigEndian(X[i], state, 36 + (i * 4));
352352
}
353353

354-
state[state.length - 1] = (byte)purpose.ordinal();
354+
state[state.length - 1] = (byte)purpose.getCode();
355355

356356
return state;
357357
}

‎core/src/main/java/org/bouncycastle/crypto/digests/SHA1Digest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public byte[] getEncodedState()
353353
Pack.intToBigEndian(X[i], state, 40 + (i * 4));
354354
}
355355

356-
state[state.length - 1] = (byte)purpose.ordinal();
356+
state[state.length - 1] = (byte)purpose.getCode();
357357

358358
return state;
359359
}

‎core/src/main/java/org/bouncycastle/crypto/digests/SHA224Digest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public byte[] getEncodedState()
363363
Pack.intToBigEndian(X[i], state, 52 + (i * 4));
364364
}
365365

366-
state[state.length - 1] = (byte)purpose.ordinal();
366+
state[state.length - 1] = (byte)purpose.getCode();
367367

368368
return state;
369369
}

‎core/src/main/java/org/bouncycastle/crypto/digests/SHA256Digest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public byte[] getEncodedState()
384384
Pack.intToBigEndian(X[i], state, 52 + (i * 4));
385385
}
386386

387-
state[state.length - 1] = (byte)purpose.ordinal();
387+
state[state.length - 1] = (byte)purpose.getCode();
388388

389389
return state;
390390
}

‎core/src/main/java/org/bouncycastle/crypto/digests/SHA384Digest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public SHA384Digest(SHA384Digest t)
6161
*/
6262
public SHA384Digest(byte[] encodedState)
6363
{
64-
super(CryptoServicePurpose.values()[encodedState[encodedState.length - 1]]);
64+
super(CryptoServicePurpose.forCode(encodedState[encodedState.length - 1]));
6565

6666
restoreState(encodedState);
6767

@@ -134,7 +134,7 @@ public byte[] getEncodedState()
134134
byte[] encoded = new byte[getEncodedStateSize() + 1];
135135
super.populateState(encoded);
136136

137-
encoded[encoded.length - 1] = (byte)purpose.ordinal();
137+
encoded[encoded.length - 1] = (byte)purpose.getCode();
138138

139139
return encoded;
140140
}

‎core/src/main/java/org/bouncycastle/crypto/digests/SHA512Digest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public SHA512Digest(SHA512Digest t)
6161
*/
6262
public SHA512Digest(byte[] encodedState)
6363
{
64-
super(CryptoServicePurpose.values()[encodedState[encodedState.length - 1]]);
64+
super(CryptoServicePurpose.forCode(encodedState[encodedState.length - 1]));
6565

6666
restoreState(encodedState);
6767

@@ -136,7 +136,7 @@ public byte[] getEncodedState()
136136
byte[] encoded = new byte[getEncodedStateSize() + 1];
137137
super.populateState(encoded);
138138

139-
encoded[encoded.length - 1] = (byte)purpose.ordinal();
139+
encoded[encoded.length - 1] = (byte)purpose.getCode();
140140

141141
return encoded;
142142
}

‎core/src/main/java/org/bouncycastle/crypto/digests/SHA512tDigest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public SHA512tDigest(SHA512tDigest t)
6868

6969
public SHA512tDigest(byte[] encodedState)
7070
{
71-
this(readDigestLength(encodedState), CryptoServicePurpose.values()[encodedState[encodedState.length - 1]]);
71+
this(readDigestLength(encodedState), CryptoServicePurpose.forCode(encodedState[encodedState.length - 1]));
7272

7373
CryptoServicesRegistrar.checkConstraints(cryptoServiceProperties());
7474

@@ -237,7 +237,7 @@ public byte[] getEncodedState()
237237
populateState(encoded);
238238
Pack.intToBigEndian(digestLength * 8, encoded, baseSize);
239239

240-
encoded[encoded.length - 1] = (byte)purpose.ordinal();
240+
encoded[encoded.length - 1] = (byte)purpose.getCode();
241241

242242
return encoded;
243243
}

0 commit comments

Comments
 (0)