Skip to content

Commit e4e7240

Browse files
authored
Merge pull request #237 from subrahmanyaman/rsa_oaep_wychproof_fix
Fixed the issue with one of the wychproof test vectors for RSA OAEP
2 parents c69a1da + d028409 commit e4e7240

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Applet/AndroidSEProviderLib/src/com/android/javacard/seprovider/KMRsaOAEPEncoding.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,8 @@ public short doFinal(byte[] inBuff, short inOffset, short inLength,
125125
if (len != 256 || outBuff[0] != 0) {
126126
CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
127127
}
128-
inBuff = outBuff;
129-
inOffset = (short) (outOffset + 1);
130-
return rsaOAEPDecode(inBuff, inOffset, (short) (len - 1), outBuff,
131-
outOffset);
128+
Util.arrayCopyNonAtomic(outBuff, (short) (outOffset + 1), outBuff, (short) 0, (short) (len -1));
129+
return rsaOAEPDecode(outBuff, (short) 0, (short) (len - 1));
132130

133131
}
134132

@@ -177,7 +175,7 @@ private void I2OS(short i, byte[] out, short offset) {
177175
}
178176

179177
private short rsaOAEPDecode(byte[] encodedMsg, short encodedMsgOff,
180-
short encodedMsgLen, byte[] msg, short offset) {
178+
short encodedMsgLen) {
181179
MessageDigest.OneShot md = null;
182180
byte[] tmpArray = KMAndroidSEProvider.getInstance().tmpArray;
183181

@@ -232,22 +230,26 @@ private short rsaOAEPDecode(byte[] encodedMsg, short encodedMsgOff,
232230
// encoding parameters is calculated and then copied from the
233231
// starting of the block and a variable length of 0's are
234232
// appended to the end of the hash till the 0x01 byte.
235-
short start = 0;
233+
short start = (short) (encodedMsgOff + encodedMsgLen);
236234
for (short i = (short) (encodedMsgOff + 2 * hLen);
237235
i < (short) (encodedMsgOff + encodedMsgLen); i++) {
238-
if (i == (short) ((encodedMsgOff + encodedMsgLen) - 1)) {
239-
// Bad Padding.
240-
CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
241-
}
242-
if (encodedMsg[i] != 0) {
236+
if ((encodedMsg[i] != 0)) {
243237
start = i;
244238
break;
245239
}
246240
}
247-
// Copy the message
248-
Util.arrayCopyNonAtomic(encodedMsg, (short) (start + 1), msg, offset,
249-
(short) (encodedMsgLen - ((start - encodedMsgOff) + 1)));
250-
return (short) (encodedMsgLen - ((start - encodedMsgOff) + 1));
241+
if ((start >= (short)(encodedMsgOff + encodedMsgLen)) ||
242+
(encodedMsg[start] != 0x01)) {
243+
// Bad Padding.
244+
CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
245+
}
246+
start++; // Message starting pos.
247+
if (start < (short)(encodedMsgOff + encodedMsgLen)) {
248+
// Copy the message
249+
Util.arrayCopyNonAtomic(encodedMsg, start, encodedMsg, encodedMsgOff,
250+
(short) (encodedMsgLen - (start - encodedMsgOff)));
251+
}
252+
return (short) (encodedMsgLen - (start - encodedMsgOff));
251253

252254
} finally {
253255
if (md != null) {

0 commit comments

Comments
 (0)