Skip to content

Commit 7af111c

Browse files
committed
Merge branch '5.8.x'
2 parents a453a71 + c50441b commit 7af111c

File tree

8 files changed

+261
-61
lines changed

8 files changed

+261
-61
lines changed

Diff for: crypto/src/main/java/org/springframework/security/crypto/argon2/Argon2PasswordEncoder.java

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,9 +52,9 @@ public class Argon2PasswordEncoder implements PasswordEncoder {
5252

5353
private static final int DEFAULT_PARALLELISM = 1;
5454

55-
private static final int DEFAULT_MEMORY = 1 << 12;
55+
private static final int DEFAULT_MEMORY = 1 << 14;
5656

57-
private static final int DEFAULT_ITERATIONS = 3;
57+
private static final int DEFAULT_ITERATIONS = 2;
5858

5959
private final Log logger = LogFactory.getLog(getClass());
6060

@@ -68,10 +68,24 @@ public class Argon2PasswordEncoder implements PasswordEncoder {
6868

6969
private final BytesKeyGenerator saltGenerator;
7070

71+
/**
72+
* Constructs an Argon2 password encoder with a salt length of 16 bytes, a hash length
73+
* of 32 bytes, parallelism of 1, memory cost of 1 << 12 and 3 iterations.
74+
* @deprecated Use {@link #defaultsForSpringSecurity_v5_2()} instead
75+
*/
76+
@Deprecated
7177
public Argon2PasswordEncoder() {
72-
this(DEFAULT_SALT_LENGTH, DEFAULT_HASH_LENGTH, DEFAULT_PARALLELISM, DEFAULT_MEMORY, DEFAULT_ITERATIONS);
78+
this(16, 32, 1, 1 << 12, 3);
7379
}
7480

81+
/**
82+
* Constructs an Argon2 password encoder with the provided parameters.
83+
* @param saltLength the salt length (in bytes)
84+
* @param hashLength the hash length (in bytes)
85+
* @param parallelism the parallelism
86+
* @param memory the memory cost
87+
* @param iterations the number of iterations
88+
*/
7589
public Argon2PasswordEncoder(int saltLength, int hashLength, int parallelism, int memory, int iterations) {
7690
this.hashLength = hashLength;
7791
this.parallelism = parallelism;
@@ -80,6 +94,29 @@ public Argon2PasswordEncoder(int saltLength, int hashLength, int parallelism, in
8094
this.saltGenerator = KeyGenerators.secureRandom(saltLength);
8195
}
8296

97+
/**
98+
* Constructs an Argon2 password encoder with a salt length of 16 bytes, a hash length
99+
* of 32 bytes, parallelism of 1, memory cost of 1 << 12 and 3 iterations.
100+
* @return the {@link Argon2PasswordEncoder}
101+
* @since 5.8
102+
* @deprecated Use {@link #defaultsForSpringSecurity_v5_8()} instead
103+
*/
104+
@Deprecated
105+
public static Argon2PasswordEncoder defaultsForSpringSecurity_v5_2() {
106+
return new Argon2PasswordEncoder(16, 32, 1, 1 << 12, 3);
107+
}
108+
109+
/**
110+
* Constructs an Argon2 password encoder with a salt length of 16 bytes, a hash length
111+
* of 32 bytes, parallelism of 1, memory cost of 1 << 14 and 2 iterations.
112+
* @return the {@link Argon2PasswordEncoder}
113+
* @since 5.8
114+
*/
115+
public static Argon2PasswordEncoder defaultsForSpringSecurity_v5_8() {
116+
return new Argon2PasswordEncoder(DEFAULT_SALT_LENGTH, DEFAULT_HASH_LENGTH, DEFAULT_PARALLELISM, DEFAULT_MEMORY,
117+
DEFAULT_ITERATIONS);
118+
}
119+
83120
@Override
84121
public String encode(CharSequence rawPassword) {
85122
byte[] salt = this.saltGenerator.generateKey();

Diff for: crypto/src/main/java/org/springframework/security/crypto/factory/PasswordEncoderFactories.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,13 +52,19 @@ private PasswordEncoderFactories() {
5252
* <li>MD5 - {@code new MessageDigestPasswordEncoder("MD5")}</li>
5353
* <li>noop -
5454
* {@link org.springframework.security.crypto.password.NoOpPasswordEncoder}</li>
55-
* <li>pbkdf2 - {@link Pbkdf2PasswordEncoder}</li>
56-
* <li>scrypt - {@link SCryptPasswordEncoder}</li>
55+
* <li>pbkdf2 - {@link Pbkdf2PasswordEncoder#defaultsForSpringSecurity_v5_5()}</li>
56+
* <li>pbkdf2@SpringSecurity_v5_8 -
57+
* {@link Pbkdf2PasswordEncoder#defaultsForSpringSecurity_v5_8()}</li>
58+
* <li>scrypt - {@link SCryptPasswordEncoder#defaultsForSpringSecurity_v4_1()}</li>
59+
* <li>scrypt@SpringSecurity_v5_8 -
60+
* {@link SCryptPasswordEncoder#defaultsForSpringSecurity_v5_8()}</li>
5761
* <li>SHA-1 - {@code new MessageDigestPasswordEncoder("SHA-1")}</li>
5862
* <li>SHA-256 - {@code new MessageDigestPasswordEncoder("SHA-256")}</li>
5963
* <li>sha256 -
6064
* {@link org.springframework.security.crypto.password.StandardPasswordEncoder}</li>
61-
* <li>argon2 - {@link Argon2PasswordEncoder}</li>
65+
* <li>argon2 - {@link Argon2PasswordEncoder#defaultsForSpringSecurity_v5_2()}</li>
66+
* <li>argon2@SpringSecurity_v5_8 -
67+
* {@link Argon2PasswordEncoder#defaultsForSpringSecurity_v5_8()}</li>
6268
* </ul>
6369
* @return the {@link PasswordEncoder} to use
6470
*/
@@ -71,13 +77,16 @@ public static PasswordEncoder createDelegatingPasswordEncoder() {
7177
encoders.put("MD4", new org.springframework.security.crypto.password.Md4PasswordEncoder());
7278
encoders.put("MD5", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("MD5"));
7379
encoders.put("noop", org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance());
74-
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
75-
encoders.put("scrypt", new SCryptPasswordEncoder());
80+
encoders.put("pbkdf2", Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_5());
81+
encoders.put("pbkdf2@SpringSecurity_v5_8", Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8());
82+
encoders.put("scrypt", SCryptPasswordEncoder.defaultsForSpringSecurity_v4_1());
83+
encoders.put("scrypt@SpringSecurity_v5_8", SCryptPasswordEncoder.defaultsForSpringSecurity_v5_8());
7684
encoders.put("SHA-1", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-1"));
7785
encoders.put("SHA-256",
7886
new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256"));
7987
encoders.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder());
80-
encoders.put("argon2", new Argon2PasswordEncoder());
88+
encoders.put("argon2", Argon2PasswordEncoder.defaultsForSpringSecurity_v5_2());
89+
encoders.put("argon2@SpringSecurity_v5_8", Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8());
8190
return new DelegatingPasswordEncoder(encodingId, encoders);
8291
}
8392

Diff for: crypto/src/main/java/org/springframework/security/crypto/password/Pbkdf2PasswordEncoder.java

+106-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,8 +36,6 @@
3636
* <li>a configurable random salt value length (default is {@value #DEFAULT_SALT_LENGTH}
3737
* bytes)</li>
3838
* <li>a configurable number of iterations (default is {@value #DEFAULT_ITERATIONS})</li>
39-
* <li>a configurable output hash width (default is {@value #DEFAULT_HASH_WIDTH}
40-
* bits)</li>
4139
* <li>a configurable key derivation function (see {@link SecretKeyFactoryAlgorithm})</li>
4240
* <li>a configurable secret appended to the random salt (default is empty)</li>
4341
* </ul>
@@ -50,85 +48,163 @@
5048
*/
5149
public class Pbkdf2PasswordEncoder implements PasswordEncoder {
5250

53-
private static final int DEFAULT_SALT_LENGTH = 8;
51+
private static final int DEFAULT_SALT_LENGTH = 16;
5452

55-
private static final int DEFAULT_HASH_WIDTH = 256;
53+
private static final SecretKeyFactoryAlgorithm DEFAULT_ALGORITHM = SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA256;
5654

57-
private static final int DEFAULT_ITERATIONS = 185000;
55+
private static final int DEFAULT_HASH_WIDTH = 256; // SHA-256
56+
57+
private static final int DEFAULT_ITERATIONS = 310000;
5858

5959
private final BytesKeyGenerator saltGenerator;
6060

6161
private final byte[] secret;
6262

63-
private final int hashWidth;
64-
6563
private final int iterations;
6664

67-
private String algorithm = SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA1.name();
65+
private String algorithm = DEFAULT_ALGORITHM.name();
66+
67+
private int hashWidth = DEFAULT_HASH_WIDTH;
68+
69+
// @formatter:off
70+
/*
71+
The length of the hash should be derived from the hashing algorithm.
72+
73+
For example:
74+
SHA-1 - 160 bits (20 bytes)
75+
SHA-256 - 256 bits (32 bytes)
76+
SHA-512 - 512 bits (64 bytes)
77+
78+
However, the original configuration for PBKDF2 was hashWidth=256 and algorithm=SHA-1, which is incorrect.
79+
The default configuration has been updated to hashWidth=256 and algorithm=SHA-256 (see gh-10506).
80+
In order to preserve backwards compatibility, the variable 'overrideHashWidth' has been introduced
81+
to indicate usage of the deprecated constructor that honors the hashWidth parameter.
82+
*/
83+
// @formatter:on
84+
private boolean overrideHashWidth = true;
6885

6986
private boolean encodeHashAsBase64;
7087

7188
/**
7289
* Constructs a PBKDF2 password encoder with no additional secret value. There will be
73-
* a salt length of {@value #DEFAULT_SALT_LENGTH} bytes, {@value #DEFAULT_ITERATIONS}
74-
* iterations and a hash width of {@value #DEFAULT_HASH_WIDTH} bits. The default is
75-
* based upon aiming for .5 seconds to validate the password when this class was
76-
* added. Users should tune password verification to their own systems.
90+
* a salt length of 8 bytes, 185,000 iterations, SHA-1 algorithm and a hash length of
91+
* 256 bits. The default is based upon aiming for .5 seconds to validate the password
92+
* when this class was added. Users should tune password verification to their own
93+
* systems.
94+
* @deprecated Use {@link #defaultsForSpringSecurity_v5_5()} instead
7795
*/
96+
@Deprecated
7897
public Pbkdf2PasswordEncoder() {
7998
this("");
8099
}
81100

82101
/**
83-
* Constructs a standard password encoder with a secret value which is also included
84-
* in the password hash. There will be a salt length of {@value #DEFAULT_SALT_LENGTH}
85-
* bytes, {@value #DEFAULT_ITERATIONS} iterations and a hash width of
86-
* {@value #DEFAULT_HASH_WIDTH} bits.
102+
* Constructs a PBKDF2 password encoder with a secret value which is also included in
103+
* the password hash. There will be a salt length of 8 bytes, 185,000 iterations,
104+
* SHA-1 algorithm and a hash length of 256 bits.
87105
* @param secret the secret key used in the encoding process (should not be shared)
106+
* @deprecated Use {@link #Pbkdf2PasswordEncoder(CharSequence, int, int, int)} instead
88107
*/
108+
@Deprecated
89109
public Pbkdf2PasswordEncoder(CharSequence secret) {
90-
this(secret, DEFAULT_SALT_LENGTH, DEFAULT_ITERATIONS, DEFAULT_HASH_WIDTH);
110+
this(secret, 8);
91111
}
92112

93113
/**
94-
* Constructs a standard password encoder with a secret value as well as salt length.
95-
* There will be {@value #DEFAULT_ITERATIONS} iterations and a hash width of
96-
* {@value #DEFAULT_HASH_WIDTH} bits.
114+
* Constructs a PBKDF2 password encoder with a secret value as well as salt length.
115+
* There will be 185,000 iterations, SHA-1 algorithm and a hash length of 256 bits.
97116
* @param secret the secret
98117
* @param saltLength the salt length (in bytes)
99118
* @since 5.5
119+
* @deprecated Use {@link #Pbkdf2PasswordEncoder(CharSequence, int, int, int)} instead
100120
*/
121+
@Deprecated
101122
public Pbkdf2PasswordEncoder(CharSequence secret, int saltLength) {
102-
this(secret, saltLength, DEFAULT_ITERATIONS, DEFAULT_HASH_WIDTH);
123+
this(secret, saltLength, 185000, 256);
103124
}
104125

105126
/**
106-
* Constructs a standard password encoder with a secret value as well as iterations
107-
* and hash width. The salt length will be of {@value #DEFAULT_SALT_LENGTH} bytes.
127+
* Constructs a PBKDF2 password encoder with a secret value as well as iterations and
128+
* hash width. The salt length will be 8 bytes.
108129
* @param secret the secret
109130
* @param iterations the number of iterations. Users should aim for taking about .5
110131
* seconds on their own system.
111132
* @param hashWidth the size of the hash (in bits)
133+
* @deprecated Use {@link #Pbkdf2PasswordEncoder(CharSequence, int, int, int)} instead
112134
*/
135+
@Deprecated
113136
public Pbkdf2PasswordEncoder(CharSequence secret, int iterations, int hashWidth) {
114-
this(secret, DEFAULT_SALT_LENGTH, iterations, hashWidth);
137+
this(secret, 8, iterations, hashWidth);
115138
}
116139

117140
/**
118-
* Constructs a standard password encoder with a secret value as well as salt length,
141+
* Constructs a PBKDF2 password encoder with a secret value as well as salt length,
119142
* iterations and hash width.
120143
* @param secret the secret
121144
* @param saltLength the salt length (in bytes)
122145
* @param iterations the number of iterations. Users should aim for taking about .5
123146
* seconds on their own system.
124147
* @param hashWidth the size of the hash (in bits)
125148
* @since 5.5
149+
* @deprecated Use
150+
* {@link #Pbkdf2PasswordEncoder(CharSequence, int, int, SecretKeyFactoryAlgorithm)}
151+
* instead
126152
*/
153+
@Deprecated
127154
public Pbkdf2PasswordEncoder(CharSequence secret, int saltLength, int iterations, int hashWidth) {
128155
this.secret = Utf8.encode(secret);
129156
this.saltGenerator = KeyGenerators.secureRandom(saltLength);
130157
this.iterations = iterations;
131158
this.hashWidth = hashWidth;
159+
this.algorithm = SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA1.name();
160+
this.overrideHashWidth = false; // Honor 'hashWidth' to preserve backwards
161+
// compatibility
162+
}
163+
164+
/**
165+
* Constructs a PBKDF2 password encoder with a secret value as well as salt length,
166+
* iterations and algorithm.
167+
* @param secret the secret
168+
* @param saltLength the salt length (in bytes)
169+
* @param iterations the number of iterations. Users should aim for taking about .5
170+
* seconds on their own system.
171+
* @param secretKeyFactoryAlgorithm the algorithm to use
172+
* @since 5.8
173+
*/
174+
public Pbkdf2PasswordEncoder(CharSequence secret, int saltLength, int iterations,
175+
SecretKeyFactoryAlgorithm secretKeyFactoryAlgorithm) {
176+
this.secret = Utf8.encode(secret);
177+
this.saltGenerator = KeyGenerators.secureRandom(saltLength);
178+
this.iterations = iterations;
179+
setAlgorithm(secretKeyFactoryAlgorithm);
180+
}
181+
182+
/**
183+
* Constructs a PBKDF2 password encoder with no additional secret value. There will be
184+
* a salt length of 8 bytes, 185,000 iterations, SHA-1 algorithm and a hash length of
185+
* 256 bits. The default is based upon aiming for .5 seconds to validate the password
186+
* when this class was added. Users should tune password verification to their own
187+
* systems.
188+
* @return the {@link Pbkdf2PasswordEncoder}
189+
* @since 5.8
190+
* @deprecated Use {@link #defaultsForSpringSecurity_v5_8()} instead
191+
*/
192+
@Deprecated
193+
public static Pbkdf2PasswordEncoder defaultsForSpringSecurity_v5_5() {
194+
return new Pbkdf2PasswordEncoder("", 8, 185000, 256);
195+
}
196+
197+
/**
198+
* Constructs a PBKDF2 password encoder with no additional secret value. There will be
199+
* a salt length of 16 bytes, 310,000 iterations, SHA-256 algorithm and a hash length
200+
* of 256 bits. The default is based upon aiming for .5 seconds to validate the
201+
* password when this class was added. Users should tune password verification to
202+
* their own systems.
203+
* @return the {@link Pbkdf2PasswordEncoder}
204+
* @since 5.8
205+
*/
206+
public static Pbkdf2PasswordEncoder defaultsForSpringSecurity_v5_8() {
207+
return new Pbkdf2PasswordEncoder("", DEFAULT_SALT_LENGTH, DEFAULT_ITERATIONS, DEFAULT_ALGORITHM);
132208
}
133209

134210
/**
@@ -153,6 +229,10 @@ public void setAlgorithm(SecretKeyFactoryAlgorithm secretKeyFactoryAlgorithm) {
153229
catch (NoSuchAlgorithmException ex) {
154230
throw new IllegalArgumentException("Invalid algorithm '" + algorithmName + "'.", ex);
155231
}
232+
if (this.overrideHashWidth) {
233+
this.hashWidth = SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA1.equals(secretKeyFactoryAlgorithm) ? 160
234+
: SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA256.equals(secretKeyFactoryAlgorithm) ? 256 : 512;
235+
}
156236
}
157237

158238
/**

0 commit comments

Comments
 (0)