Skip to content

Commit dce3551

Browse files
amoghvchiradeep
authored andcommitted
CLOUDSTACK-2312, CLOUDSTACK-2314 : SHA256 timing attack and brute force attack fix
Signed-off-by: Chiradeep Vittal <chiradeep@apache.org>
1 parent 5b0314f commit dce3551

2 files changed

Lines changed: 99 additions & 19 deletions

File tree

‎plugins/user-authenticators/sha256salted/src/com/cloud/server/auth/SHA256SaltedUserAuthenticator.java‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
@Local(value={UserAuthenticator.class})
3737
public class SHA256SaltedUserAuthenticator extends DefaultUserAuthenticator {
3838
public static final Logger s_logger = Logger.getLogger(SHA256SaltedUserAuthenticator.class);
39-
39+
private static final String s_defaultPassword = "000000000000000000000000000=";
40+
private static final String s_defaultSalt = "0000000000000000000000000000000=";
4041
@Inject
4142
private UserAccountDao _userAccountDao;
42-
private static int s_saltlen = 20;
43+
private static final int s_saltlen = 32;
4344

4445
@Override
4546
public boolean configure(String name, Map<String, Object> params)
@@ -60,21 +61,29 @@ public boolean authenticate(String username, String password,
6061
if (s_logger.isDebugEnabled()) {
6162
s_logger.debug("Retrieving user: " + username);
6263
}
64+
boolean realUser = true;
6365
UserAccount user = _userAccountDao.getUserAccount(username, domainId);
6466
if (user == null) {
6567
s_logger.debug("Unable to find user with " + username + " in domain " + domainId);
66-
return false;
68+
realUser = false;
6769
}
68-
69-
try {
70+
/* Fake Data */
71+
String realPassword = new String(s_defaultPassword);
72+
byte[] salt = new String(s_defaultSalt).getBytes();
73+
if (realUser) {
7074
String storedPassword[] = user.getPassword().split(":");
7175
if (storedPassword.length != 2) {
7276
s_logger.warn("The stored password for " + username + " isn't in the right format for this authenticator");
73-
return false;
77+
realUser = false;
78+
} else {
79+
realPassword = storedPassword[1];
80+
salt = Base64.decode(storedPassword[0]);
7481
}
75-
byte salt[] = Base64.decode(storedPassword[0]);
82+
}
83+
try {
7684
String hashedPassword = encode(password, salt);
77-
return storedPassword[1].equals(hashedPassword);
85+
/* constantTimeEquals comes first in boolean since we need to thwart timing attacks */
86+
return constantTimeEquals(realPassword, hashedPassword) && realUser;
7887
} catch (NoSuchAlgorithmException e) {
7988
throw new CloudRuntimeException("Unable to hash password", e);
8089
} catch (UnsupportedEncodingException e) {
@@ -109,9 +118,9 @@ public String encode(String password) {
109118

110119
public String encode(String password, byte[] salt) throws UnsupportedEncodingException, NoSuchAlgorithmException {
111120
byte[] passwordBytes = password.getBytes("UTF-8");
112-
byte[] hashSource = new byte[passwordBytes.length + s_saltlen];
121+
byte[] hashSource = new byte[passwordBytes.length + salt.length];
113122
System.arraycopy(passwordBytes, 0, hashSource, 0, passwordBytes.length);
114-
System.arraycopy(salt, 0, hashSource, passwordBytes.length, s_saltlen);
123+
System.arraycopy(salt, 0, hashSource, passwordBytes.length, salt.length);
115124

116125
// 2. Hash the password with the salt
117126
MessageDigest md = MessageDigest.getInstance("SHA-256");
@@ -120,4 +129,14 @@ public String encode(String password, byte[] salt) throws UnsupportedEncodingExc
120129

121130
return new String(Base64.encode(digest));
122131
}
132+
133+
private static boolean constantTimeEquals(String a, String b) {
134+
byte[] aBytes = a.getBytes();
135+
byte[] bBytes = b.getBytes();
136+
int result = aBytes.length ^ bBytes.length;
137+
for (int i = 0; i < aBytes.length && i < bBytes.length; i++) {
138+
result |= aBytes[i] ^ bBytes[i];
139+
}
140+
return result == 0;
141+
}
123142
}

‎plugins/user-authenticators/sha256salted/test/src/com/cloud/server/auth/test/AuthenticatorTest.java‎

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,66 @@
1717

1818
package src.com.cloud.server.auth.test;
1919

20-
import static org.junit.Assert.*;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.fail;
22+
import static org.mockito.Mockito.when;
2123

2224
import java.io.UnsupportedEncodingException;
2325
import java.security.NoSuchAlgorithmException;
2426
import java.util.Collections;
27+
import java.util.HashMap;
28+
import java.util.Map;
2529

2630
import javax.naming.ConfigurationException;
2731

2832
import org.bouncycastle.util.encoders.Base64;
33+
import org.junit.Assert;
2934
import org.junit.Before;
3035
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.mockito.InjectMocks;
38+
import org.mockito.Mock;
39+
import org.mockito.runners.MockitoJUnitRunner;
3140

3241
import com.cloud.server.auth.SHA256SaltedUserAuthenticator;
42+
import com.cloud.user.UserAccount;
43+
import com.cloud.user.dao.UserAccountDao;
3344

45+
@RunWith(MockitoJUnitRunner.class)
3446
public class AuthenticatorTest {
3547

48+
@Mock
49+
UserAccount adminAccount;
50+
51+
@Mock
52+
UserAccount adminAccount20Byte;
53+
54+
@Mock
55+
UserAccountDao _userAccountDao;
56+
57+
@InjectMocks
58+
SHA256SaltedUserAuthenticator authenticator;
59+
3660
@Before
3761
public void setUp() throws Exception {
62+
try {
63+
authenticator.configure("SHA256", Collections.<String, Object> emptyMap());
64+
} catch (ConfigurationException e) {
65+
fail(e.toString());
66+
}
67+
68+
when(_userAccountDao.getUserAccount("admin", 0L)).thenReturn(adminAccount);
69+
when(_userAccountDao.getUserAccount("admin20Byte", 0L)).thenReturn(adminAccount20Byte);
70+
when(_userAccountDao.getUserAccount("fake", 0L)).thenReturn(null);
71+
//32 byte salt, and password="password"
72+
when(adminAccount.getPassword()).thenReturn("WS3UHhBPKHZeV+G3jnn7G2N3luXgLSfL+2ORDieXa1U=:VhuFOrOU2IpsjKYH8cH1VDaDBh/VivjMcuADjeEbIig=");
73+
//20 byte salt, and password="password"
74+
when(adminAccount20Byte.getPassword()).thenReturn("QL2NsxVEmRuDaNRkvIyADny7C5w=:JoegiytiWnoBAxmSD/PwBZZYqkr746x2KzPrZNw4NgI=");
75+
3876
}
3977

4078
@Test
4179
public void testEncode() throws UnsupportedEncodingException, NoSuchAlgorithmException {
42-
SHA256SaltedUserAuthenticator authenticator =
43-
new SHA256SaltedUserAuthenticator();
44-
45-
try {
46-
authenticator.configure("SHA256", Collections.<String,Object>emptyMap());
47-
} catch (ConfigurationException e) {
48-
fail(e.toString());
49-
}
5080

5181
String encodedPassword = authenticator.encode("password");
5282

@@ -60,4 +90,35 @@ public void testEncode() throws UnsupportedEncodingException, NoSuchAlgorithmExc
6090

6191
}
6292

93+
@Test
94+
public void testAuthentication() throws UnsupportedEncodingException, NoSuchAlgorithmException {
95+
Map<String, Object[]> dummyMap = new HashMap<String, Object[]>();
96+
assertEquals("32 byte salt authenticated", true, authenticator.authenticate("admin", "password", 0L, dummyMap));
97+
assertEquals("20 byte salt authenticated", true, authenticator.authenticate("admin20Byte", "password", 0L, dummyMap));
98+
assertEquals("fake user not authenticated", false, authenticator.authenticate("fake", "fake", 0L, dummyMap));
99+
assertEquals("bad password not authenticated", false, authenticator.authenticate("admin", "fake", 0L, dummyMap));
100+
assertEquals("20 byte user bad password not authenticated", false, authenticator.authenticate("admin20Byte", "fake", 0L, dummyMap));
101+
}
102+
103+
@Test
104+
public void testTiming() throws UnsupportedEncodingException, NoSuchAlgorithmException {
105+
Map<String, Object[]> dummyMap = new HashMap<String, Object[]>();
106+
Double threshold = (double)500000; //half a millisecond
107+
108+
Long t1 = System.nanoTime();
109+
authenticator.authenticate("admin", "password", 0L, dummyMap);
110+
Long t2 = System.nanoTime();
111+
authenticator.authenticate("admin20Byte", "password", 0L, dummyMap);
112+
Long t3 = System.nanoTime();
113+
authenticator.authenticate("fake", "fake", 0L, dummyMap);
114+
Long t4 = System.nanoTime();
115+
authenticator.authenticate("admin", "fake", 0L, dummyMap);
116+
Long t5 = System.nanoTime();
117+
Long diff1 = t2 - t1;
118+
Long diff2 = t3 - t2;
119+
Long diff3 = t4 - t3;
120+
Long diff4 = t5 - t4;
121+
Assert.assertTrue("All computation times within " + threshold / 1000000 + " milisecond",
122+
(diff1 <= threshold) && (diff2 <= threshold) && (diff3 <= threshold) && (diff4 <= threshold));
123+
}
63124
}

0 commit comments

Comments
 (0)