Skip to content

Commit d0cbafc

Browse files
committed
Minor code cleanup
1 parent c8b61d9 commit d0cbafc

File tree

2 files changed

+34
-67
lines changed

2 files changed

+34
-67
lines changed

src/main/java/us/q3q/fido2/FIDO2Applet.java

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,24 +1195,6 @@ private boolean makeGoodKeyPair(KeyPair keyPair, byte[] publicKeyBuffer, short p
11951195
return false;
11961196
}
11971197

1198-
/**
1199-
* Encrypts data from one buffer to another using the symmetric wrapping key.
1200-
* Before call, symmetric crypto must be initialized; after call, it still is.
1201-
*
1202-
* @param inBuf Buffer containing data to be encrypted
1203-
* @param inOffset Offset of data in input buffer
1204-
* @param inLen Length of data to encrypt
1205-
* @param outBuf Buffer into which to write output
1206-
* @param outOff Offset at which to write encrypted data
1207-
*/
1208-
private void symmetricWrap(byte[] inBuf, short inOffset, short inLen, byte[] outBuf, short outOff) {
1209-
short ret = symmetricWrapper.doFinal(inBuf, inOffset, inLen,
1210-
outBuf, outOff);
1211-
if (ret != inLen) {
1212-
throwException(ISO7816.SW_DATA_INVALID);
1213-
}
1214-
}
1215-
12161198
/**
12171199
* If, and only if, no PIN is set, directly initialize symmetric crypto
12181200
* from our flash-stored wrapping key (which should be unencrypted)
@@ -2975,8 +2957,11 @@ private void extractCredentialMixed(byte[] credentialBuffer, short credentialInd
29752957
(lowSecurity ? lowSecurityWrappingIV : externalCredentialIV);
29762958
AESKey key = (LOW_SECURITY_MAXIMUM_COMPLIANCE || lowSecurity) ? lowSecurityWrappingKey : highSecurityWrappingKey;
29772959
symmetricUnwrapper.init(key, Cipher.MODE_DECRYPT, iv, (short) 0, IV_LEN);
2978-
symmetricUnwrap(credentialBuffer, credentialIndex, CREDENTIAL_ID_LEN,
2960+
short ret = symmetricUnwrapper.doFinal(credentialBuffer, credentialIndex, CREDENTIAL_ID_LEN,
29792961
outputBuffer, outputOffset);
2962+
if (ret != CREDENTIAL_ID_LEN) {
2963+
throwException(ISO7816.SW_DATA_INVALID);
2964+
}
29802965
}
29812966

29822967
/**
@@ -5281,24 +5266,6 @@ private AESKey getAESKeyForExistingRK(short rkIndex) {
52815266
return getAESKeyForCreatingWithCredProtectLevel(residentKeys[rkIndex].getCredProtectLevel());
52825267
}
52835268

5284-
/**
5285-
* Uses the symmetric unwrapping key to decrypt stored data from one buffer to another.
5286-
* Before call, symmetric crypto must be initialized; after call, it still will be.
5287-
*
5288-
* @param inBuf Input buffer
5289-
* @param offset Offset of encrypted data in input buffer
5290-
* @param len Length of encrypted data
5291-
* @param outBuf Buffer into which to store output
5292-
* @param writeOffset Output at which to begin writing data
5293-
*/
5294-
private void symmetricUnwrap(byte[] inBuf, short offset, short len, byte[] outBuf, short writeOffset) {
5295-
short ret = symmetricUnwrapper.doFinal(inBuf, offset, len,
5296-
outBuf, writeOffset);
5297-
if (ret != len) {
5298-
throwException(ISO7816.SW_DATA_INVALID);
5299-
}
5300-
}
5301-
53025269
/**
53035270
* Processes the CTAP2.1 credential management getCredsMetaData command
53045271
*

src/main/java/us/q3q/fido2/ResidentKeyData.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,36 @@ public void setUser(AESKey key, Cipher wrapper, byte[] userIdBuffer, short userI
160160
this.userIdLength = userIdLength;
161161
}
162162

163+
public void unpackUserID(AESKey key, Cipher unwrapper, byte[] targetBuffer, short targetOffset) {
164+
unwrapper.init(key, Cipher.MODE_DECRYPT, userIV, (short) 0, (short) userIV.length);
165+
unwrapper.doFinal(userId, (short) 0, (short) userId.length,
166+
targetBuffer, targetOffset);
167+
}
168+
169+
public void unpackPublicKey(AESKey key, Cipher unwrapper, byte[] targetBuffer, short targetOffset) {
170+
unwrapper.init(key, Cipher.MODE_DECRYPT, pubKeyIV, (short) 0, (short) pubKeyIV.length);
171+
unwrapper.doFinal(publicKey, (short) 0, (short) publicKey.length,
172+
targetBuffer, targetOffset);
173+
}
174+
175+
public void unpackRpId(AESKey key, Cipher unwrapper, byte[] targetBuffer, short targetOffset) {
176+
unwrapper.init(key, Cipher.MODE_DECRYPT, RPIV, (short) 0, (short) RPIV.length);
177+
unwrapper.doFinal(rpId, (short) 0, (short) rpId.length,
178+
targetBuffer, targetOffset);
179+
}
180+
181+
public void unpackCredBlob(AESKey key, Cipher unwrapper, byte[] targetBuffer, short targetOffset) {
182+
unwrapper.init(key, Cipher.MODE_DECRYPT, credBlobIV, (short) 0, (short) credBlobIV.length);
183+
unwrapper.doFinal(credBlob, (short) 0, (short) credBlob.length,
184+
targetBuffer, targetOffset);
185+
}
186+
187+
public void emitLargeBlobKey(AESKey key, Cipher wrapper, byte[] targetBuffer, short targetOffset) {
188+
wrapper.init(key, Cipher.MODE_ENCRYPT, largeBlobIV, (short) 0, (short) largeBlobIV.length);
189+
wrapper.doFinal(publicKey, (short) 0, (short) 32,
190+
targetBuffer, targetOffset);
191+
}
192+
163193
public byte[] getCounter() {
164194
return counter;
165195
}
@@ -196,36 +226,6 @@ public byte getRpIdLength() {
196226
return rpIdLength;
197227
}
198228

199-
public void unpackUserID(AESKey key, Cipher unwrapper, byte[] targetBuffer, short targetOffset) {
200-
unwrapper.init(key, Cipher.MODE_DECRYPT, userIV, (short) 0, (short) userIV.length);
201-
unwrapper.doFinal(userId, (short) 0, (short) userId.length,
202-
targetBuffer, targetOffset);
203-
}
204-
205-
public void unpackPublicKey(AESKey key, Cipher unwrapper, byte[] targetBuffer, short targetOffset) {
206-
unwrapper.init(key, Cipher.MODE_DECRYPT, pubKeyIV, (short) 0, (short) pubKeyIV.length);
207-
unwrapper.doFinal(publicKey, (short) 0, (short) publicKey.length,
208-
targetBuffer, targetOffset);
209-
}
210-
211-
public void unpackRpId(AESKey key, Cipher unwrapper, byte[] targetBuffer, short targetOffset) {
212-
unwrapper.init(key, Cipher.MODE_DECRYPT, RPIV, (short) 0, (short) RPIV.length);
213-
unwrapper.doFinal(rpId, (short) 0, (short) rpId.length,
214-
targetBuffer, targetOffset);
215-
}
216-
217-
public void unpackCredBlob(AESKey key, Cipher unwrapper, byte[] targetBuffer, short targetOffset) {
218-
unwrapper.init(key, Cipher.MODE_DECRYPT, credBlobIV, (short) 0, (short) credBlobIV.length);
219-
unwrapper.doFinal(credBlob, (short) 0, (short) credBlob.length,
220-
targetBuffer, targetOffset);
221-
}
222-
223-
public void emitLargeBlobKey(AESKey key, Cipher wrapper, byte[] targetBuffer, short targetOffset) {
224-
wrapper.init(key, Cipher.MODE_ENCRYPT, largeBlobIV, (short) 0, (short) largeBlobIV.length);
225-
wrapper.doFinal(publicKey, (short) 0, (short) 32,
226-
targetBuffer, targetOffset);
227-
}
228-
229229
public byte getCredBlobLen() {
230230
return this.credBlobLen;
231231
}

0 commit comments

Comments
 (0)