Skip to content

Commit b8ded89

Browse files
committed
Merge branch 'task/sec2_aes_iv_usage' into 'master'
Updated provisioning security2 scheme. See merge request idf/esp-idf-provisioning-android!72
2 parents 342ce0a + ad36729 commit b8ded89

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ To get this app please clone this repository using the below command:
4848
```
4949
And add a dependency code to your app module's `build.gradle` file.
5050
```
51-
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.2.2'
51+
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.2.3'
5252
```
5353

5454
## Using Provisioning Library

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ android {
1717
applicationId "com.espressif.wifi_provisioning"
1818
minSdkVersion 26
1919
targetSdkVersion 34
20-
versionCode 23
21-
versionName "2.2.2 - ${getGitHash()}"
20+
versionCode 24
21+
versionName "2.2.3 - ${getGitHash()}"
2222
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2323
consumerProguardFiles "consumer-proguard-rules.pro"
2424
}

provisioning/src/main/java/com/espressif/provisioning/ESPDevice.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class ESPDevice {
7979
private Session session;
8080
private Security security;
8181
private Transport transport;
82+
private int secPatchVersion;
8283

8384
private WiFiScanListener wifiScanListener;
8485
private ProvisionListener provisionListener;
@@ -338,6 +339,15 @@ public void setProofOfPossession(String pop) {
338339
this.proofOfPossession = pop;
339340
}
340341

342+
/**
343+
* This method used to set security patch version, received from device.
344+
*
345+
* @param patchVersion Security patch version to be set.
346+
*/
347+
public void setSecPatchVersion(int patchVersion) {
348+
this.secPatchVersion = patchVersion;
349+
}
350+
341351
/**
342352
* This method is used to get Proof Of Possession.
343353
*
@@ -662,6 +672,7 @@ public void initSession(final ResponseListener listener) {
662672
Log.d(TAG, "Device Version : " + deviceVersion);
663673
Log.d(TAG, "sec_ver value : " + provInfo.optInt("sec_ver"));
664674
Log.d(TAG, "Has sec_ver key : " + provInfo.has("sec_ver"));
675+
Log.d(TAG, "Has sec_patch_ver key : " + provInfo.has("sec_patch_ver"));
665676

666677
if (provInfo.has("sec_ver")) {
667678

@@ -678,6 +689,10 @@ public void initSession(final ResponseListener listener) {
678689
case 2:
679690
default:
680691
securityType = ESPConstants.SecurityType.SECURITY_2;
692+
if (provInfo.has("sec_patch_ver")) {
693+
secPatchVersion = provInfo.optInt("sec_patch_ver");
694+
Log.d(TAG, "Security Patch Version : " + secPatchVersion);
695+
}
681696
break;
682697
}
683698
} else {
@@ -702,7 +717,11 @@ public void initSession(final ResponseListener listener) {
702717
security = new Security1(proofOfPossession);
703718
break;
704719
case SECURITY_2:
705-
security = new Security2(userName, proofOfPossession);
720+
if (secPatchVersion == 1) {
721+
security = new Security2(userName, proofOfPossession, secPatchVersion);
722+
} else {
723+
security = new Security2(userName, proofOfPossession);
724+
}
706725
break;
707726
}
708727

provisioning/src/main/java/com/espressif/provisioning/security/Security2.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,28 @@ public class Security2 implements Security {
6666
private byte[] clientProof;
6767
private byte[] sharedKey;
6868
private byte[] key;
69+
private int counter;
70+
private int secPatchVersion = 0;
6971

7072
/***
7173
* Create Security 1 implementation
7274
*/
7375
public Security2(String username, String password) {
7476

7577
userName = username;
76-
Log.d(TAG, "User name : " + username + " password : " + password);
78+
try {
79+
this.cipher = Cipher.getInstance("AES/GCM/NoPadding");
80+
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
81+
e.printStackTrace();
82+
}
83+
client = new SRP6ClientSession();
84+
client.step1(username, password);
85+
}
7786

87+
public Security2(String username, String password, int patchVersion) {
88+
89+
userName = username;
90+
this.secPatchVersion = patchVersion;
7891
try {
7992
this.cipher = Cipher.getInstance("AES/GCM/NoPadding");
8093
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
@@ -210,6 +223,8 @@ private void processStep1Response(byte[] hexData) throws RuntimeException {
210223
sharedKey = BigIntegerUtils.bigIntegerToBytes(client.K);
211224
key = Arrays.copyOfRange(sharedKey, 0, 32);
212225

226+
counter = (deviceNonce[8] & 0xFF) << 24 | (deviceNonce[9] & 0xFF) << 16 | (deviceNonce[10] & 0xFF) << 8 | (deviceNonce[11] & 0xFF);
227+
213228
} catch (InvalidProtocolBufferException e) {
214229
Log.e(TAG, e.getMessage());
215230
}
@@ -220,6 +235,15 @@ public byte[] encrypt(byte[] data) {
220235
// Device nonce = IV
221236
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
222237
IvParameterSpec parameterSpec = new IvParameterSpec(deviceNonce);
238+
239+
if (secPatchVersion == 1) {
240+
byte[] nonce = new byte[12];
241+
System.arraycopy(deviceNonce, 0, nonce, 0, 8);
242+
System.arraycopy(intToBigEndian(counter), 0, nonce, 8, 4);
243+
counter++;
244+
parameterSpec = new IvParameterSpec(nonce);
245+
}
246+
223247
try {
224248
this.cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, parameterSpec);
225249
} catch (InvalidAlgorithmParameterException e) {
@@ -242,6 +266,15 @@ public byte[] decrypt(byte[] data) {
242266

243267
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
244268
IvParameterSpec parameterSpec = new IvParameterSpec(deviceNonce);
269+
270+
if (secPatchVersion == 1) {
271+
byte[] nonce = new byte[12];
272+
System.arraycopy(deviceNonce, 0, nonce, 0, 8);
273+
System.arraycopy(intToBigEndian(counter), 0, nonce, 8, 4);
274+
counter++;
275+
parameterSpec = new IvParameterSpec(nonce);
276+
}
277+
245278
try {
246279
this.cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, parameterSpec);
247280
} catch (InvalidAlgorithmParameterException e) {
@@ -259,4 +292,13 @@ public byte[] decrypt(byte[] data) {
259292
}
260293
return null;
261294
}
295+
296+
private byte[] intToBigEndian(int value) {
297+
return new byte[]{
298+
(byte) (value >> 24),
299+
(byte) (value >> 16),
300+
(byte) (value >> 8),
301+
(byte) value
302+
};
303+
}
262304
}

0 commit comments

Comments
 (0)