You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit adds the ability for a user to specify the algorithm/mode of
operation/padding directly in their `application.yml`. This is pretty
flexible and allows the user easy access to many JCA "transformations"
without them needing to write any code. A new, incompatible format for
the encrypted binary blob is introduced to achieve this. The versioned
format allows us to make continuous improvements to it without rendering
all previous outputs undecryptable. Provisions were made for version-1
outputs: these can still be decrypted. When migrating from version 1 to
version 2, legacy key versions should be marked as such in the config.
These key versions are then only allowed to decrypt: no new encryptions
can be performed with them.
# Cryptvault: versioned, secure, generic encryption/decryption in Java
5
5
6
-
Allows for a versioned, secure generic crypt/decrypt in java.
7
-
8
-
Originally developed for [spring-data-mongodb-encrypt](https://github.com/bolcom/spring-data-mongodb-encrypt), it is now offered as a general use library.
6
+
> When in doubt, encrypt. When not in doubt, be in doubt.
9
7
10
8
## Features
11
9
12
10
- key versioning (to help migrating to new key without need to convert data)
13
11
- uses 256-bit AES by default
14
-
- supports any encryption available in Java (via JCE)
12
+
- supports any encryption available in Java (via Java Cryptography Architecture
You can also configure `CryptVault` yourself. Look at [how spring autoconfig configures CryptVault](src/main/java/com/bol/config/CryptVaultAutoConfiguration.java) for details.
58
-
59
54
## Keys
60
55
61
-
This library supports AES 256 bit keys out of the box. It's possible to extend this, check the source code (`CryptVault` specifically) on how to do so.
56
+
This library uses the encryption keys specified in the configuration directly.
57
+
Notably, it does not use any key-derivation. That means that you are responsible
58
+
for providing a key from a high-entropy source.
62
59
63
-
To generate a key, you can use the following command line:
60
+
The length of the key depends on the algorithm specified. When using AES-256,
61
+
you need to provide a key that is 256 bits/32 bytes long. (For comparison, the
62
+
weak DES uses 64-bit keys.)
64
63
65
-
```
66
-
dd if=/dev/urandom bs=1 count=32 | base64
64
+
To generate a key suitable for AES-256 bit, you can use the following command:
65
+
66
+
```console
67
+
$ dd if=/dev/urandom bs=1 count=32 | base64
67
68
```
68
69
69
-
## Exchange keys
70
+
## Rotating keys
70
71
71
-
It is advisable to rotate your keys every now and then. To do so, define a new key version in `application.yml`:
72
+
It is advisable to rotate your keys every now and then. To do so, define a new
73
+
key version in `application.yml`:
72
74
73
75
```yaml
74
76
cryptvault:
@@ -79,7 +81,13 @@ cryptvault:
79
81
key: ge2L+MA9jLA8UiUJ4z5fUoK+Lgj2yddlL6EzYIBqb1Q=
80
82
```
81
83
82
-
`spring-data-mongodb-encrypt` would automatically use the highest versioned key for encryption by default, but supports decryption using any of the keys. This allows you to deploy a new key, and either let old data slowly get phased out, or run a nightly load+save batch job to force key migration. Once all old keys are phased out, you may remove the old key from the configuration.
84
+
CryptVault automatically uses the highest versioned key for encryption by
85
+
default, but supports decryption using any of the keys. This allows you to
86
+
deploy a new key, and either let old data slowly get phased out, or run a
87
+
nightly load+save batch job to force key migration. Once all old keys are phased
88
+
out, you may remove the old key from the configuration.
89
+
90
+
## Specify default key version
83
91
84
92
You can use
85
93
@@ -88,9 +96,102 @@ cryptvault:
88
96
default-key: 1
89
97
```
90
98
91
-
to override which version of the defined keys is considered 'default'.
99
+
to override which version of the defined keys is considered default.
100
+
101
+
## Specify encryption algorithm
102
+
103
+
Instead of using the default AES-256 in CBC mode, you can specify the algorithm,
104
+
mode of operation and padding scheme directly in the configuration:
105
+
106
+
```yaml
107
+
cryptvault:
108
+
keys:
109
+
version: 1
110
+
key: Ifw/+pLuWBjn7a1mjuToQ8hpIh8DV0WLf9b4z7iinGs=
111
+
transformation: AES/GCM/NoPadding
112
+
```
113
+
114
+
You can use all the algorithms specified by JCA. Other valid transformations
115
+
are, for example, "DES/CTR/NoPadding" and "ChaCha20-Poly1305". For a
116
+
comprehensive list, see [Java Security Standard Algorithm Names][Java Security
117
+
Standard Algorithm Names].
118
+
119
+
The YAML key is called "transformation" because it signifies more than just an
120
+
algorithm, but rather a set of operations performed on an input to produce some
121
+
output. Naming it this way is consistent with JCA parlance.
0 commit comments