|
16 | 16 | package dev.paseto.jpaseto.crypto.bouncycastle; |
17 | 17 |
|
18 | 18 | import com.google.auto.service.AutoService; |
19 | | -import dev.paseto.jpaseto.InvalidMacException; |
20 | | -import dev.paseto.jpaseto.impl.crypto.Hmacs; |
21 | | -import dev.paseto.jpaseto.impl.crypto.PreAuthEncoder; |
| 19 | +import dev.paseto.jpaseto.impl.crypto.BaseV1LocalCryptoProvider; |
22 | 20 | import dev.paseto.jpaseto.impl.crypto.V1LocalCryptoProvider; |
23 | | -import dev.paseto.jpaseto.impl.lang.Bytes; |
24 | 21 | import org.bouncycastle.crypto.generators.HKDFBytesGenerator; |
25 | 22 | import org.bouncycastle.crypto.params.HKDFParameters; |
26 | 23 | import org.bouncycastle.crypto.util.DigestFactory; |
27 | 24 |
|
28 | | -import javax.crypto.Cipher; |
29 | 25 | import javax.crypto.SecretKey; |
30 | | -import java.security.MessageDigest; |
31 | | -import java.util.Arrays; |
32 | | - |
33 | | -import static java.nio.charset.StandardCharsets.UTF_8; |
34 | 26 |
|
35 | 27 | @AutoService(V1LocalCryptoProvider.class) |
36 | | -public class BouncyCastleV1LocalCryptoProvider implements V1LocalCryptoProvider { |
37 | | - |
38 | | - private static final byte[] HEADER_BYTES = "v1.local.".getBytes(UTF_8); |
| 28 | +public class BouncyCastleV1LocalCryptoProvider extends BaseV1LocalCryptoProvider { |
39 | 29 |
|
40 | 30 | @Override |
41 | | - public byte[] encrypt(byte[] payload, byte[] footer, byte[] nonce, SecretKey sharedSecret) { |
42 | | - |
43 | | - byte[] salt = Arrays.copyOf(nonce, 16); |
44 | | - byte[] rightNonce = Arrays.copyOfRange(nonce, 16, nonce.length); |
45 | | - |
46 | | - // 4 |
47 | | - byte[] encryptionKey = encryptionKey(sharedSecret, salt); |
48 | | - byte[] authenticationKey = authenticationKey(sharedSecret, salt); |
49 | | - |
50 | | - // 5 |
51 | | - byte[] cipherText = V1LocalCryptoProvider.doCipher(Cipher.ENCRYPT_MODE, encryptionKey, rightNonce, payload); |
52 | | - |
53 | | - //6 |
54 | | - byte[] preAuth = PreAuthEncoder.encode(HEADER_BYTES, nonce, cipherText, footer); |
55 | | - |
56 | | - //7 |
57 | | - byte[] calculatedMac = Hmacs.hmacSha384(authenticationKey, preAuth); |
58 | | - |
59 | | - // 8 |
60 | | - return Bytes.concat(nonce, cipherText, calculatedMac); |
61 | | - } |
62 | | - |
63 | | - @Override |
64 | | - public byte[] decrypt(byte[] encryptedBytes, byte[] footer, byte[] nonce, SecretKey sharedSecret) { |
65 | | - |
66 | | - // 3 |
67 | | - byte[] salt = Arrays.copyOf(nonce, 16); |
68 | | - byte[] rightNonce = Arrays.copyOfRange(nonce, 16, nonce.length); |
69 | | - |
70 | | - byte[] cipherText = Arrays.copyOfRange(encryptedBytes, 32, encryptedBytes.length - 48); |
71 | | - byte[] mac = Arrays.copyOfRange(encryptedBytes, encryptedBytes.length - 48, encryptedBytes.length); |
72 | | - |
73 | | - // 4 |
74 | | - byte[] encryptionKey = encryptionKey(sharedSecret, salt); |
75 | | - byte[] authenticationKey = authenticationKey(sharedSecret, salt); |
76 | | - |
77 | | - // 5 |
78 | | - byte[] preAuth = PreAuthEncoder.encode(HEADER_BYTES, nonce, cipherText, footer); |
79 | | - |
80 | | - // 6 |
81 | | - byte[] calculatedMac = Hmacs.hmacSha384(authenticationKey, preAuth); |
82 | | - |
83 | | - // 7 |
84 | | - if (!MessageDigest.isEqual(calculatedMac, mac)) { |
85 | | - throw new InvalidMacException("Failed to validate mac in token"); |
86 | | - } |
87 | | - |
88 | | - // 8 |
89 | | - return V1LocalCryptoProvider.doCipher(Cipher.DECRYPT_MODE, encryptionKey, rightNonce, cipherText); |
90 | | - } |
91 | | - |
92 | | - private byte[] hkdfSha384(SecretKey sharedSecret, byte[] salt, String info) { |
93 | | - |
| 31 | + protected byte[] hkdfSha384(SecretKey sharedSecret, byte[] salt, byte[] info) { |
94 | 32 | HKDFBytesGenerator hkdfBytesGenerator = new HKDFBytesGenerator(DigestFactory.createSHA384()); |
95 | | - hkdfBytesGenerator.init(new HKDFParameters(sharedSecret.getEncoded(), salt, info.getBytes(UTF_8))); |
| 33 | + hkdfBytesGenerator.init(new HKDFParameters(sharedSecret.getEncoded(), salt, info)); |
96 | 34 | byte[] result = new byte[32]; |
97 | 35 | hkdfBytesGenerator.generateBytes(result, 0, result.length); |
98 | 36 | return result; |
99 | 37 | } |
100 | | - |
101 | | - private byte[] encryptionKey(SecretKey sharedSecret, byte[] salt) { |
102 | | - return hkdfSha384(sharedSecret, salt, "paseto-encryption-key"); |
103 | | - } |
104 | | - |
105 | | - private byte[] authenticationKey(SecretKey sharedSecret, byte[] salt) { |
106 | | - return hkdfSha384(sharedSecret, salt, "paseto-auth-key-for-aead"); |
107 | | - } |
108 | 38 | } |
0 commit comments