removed NIO byte buffer usage and added internal implementation of bytebuffer

This commit is contained in:
Joakim Eriksson 2013-08-12 10:56:18 +02:00
parent 2136772677
commit 537fafe454
1 changed files with 64 additions and 9 deletions

View File

@ -32,8 +32,6 @@
*/
package se.sics.mspsim.core;
import java.nio.ByteBuffer;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@ -151,9 +149,66 @@ public class AES128 extends IOUnit {
/**
* Variable holders for the different registers needed by this peripheral
*/
private ByteBuffer key = ByteBuffer.allocate(16);
private ByteBuffer inData = ByteBuffer.allocate(16);
private ByteBuffer outData = ByteBuffer.allocate(16);
/* avoid using NIO resources */
private class ByteBuffer {
byte[] buffer;
int pos;
int limit; /* max data put in */
ByteBuffer(int size) {
buffer = new byte[size];
pos = 0;
}
public int position() {
return pos;
}
public void position(int p) {
pos = p;
}
public boolean hasRemaining() {
return pos < limit;
}
public void clear() {
limit = 0;
}
public void resetPos() {
pos = 0;
}
public byte[] array() {
return buffer;
}
public void put(byte[] bytes) {
for (int i = 0; i < bytes.length; i++) {
put(bytes[i]);
}
}
public void put(byte data) {
buffer[pos++] = data;
limit++;
}
/* assume that calling code is ok... */
public byte get() {
return buffer[pos++];
}
public int limit() {
return buffer.length;
}
}
private ByteBuffer key = new ByteBuffer(16);
private ByteBuffer inData = new ByteBuffer(16);
private ByteBuffer outData = new ByteBuffer(16);
/**
* Syntax sugar
@ -249,7 +304,7 @@ public class AES128 extends IOUnit {
byte[] bytes = cipher.doFinal(inData.array());
outData.clear();
outData.put(bytes);
outData.rewind();
outData.resetPos();
} catch (Exception e) {
log(e.getStackTrace().toString());
}
@ -271,7 +326,7 @@ public class AES128 extends IOUnit {
byte[] bytes = cipher.doFinal(inData.array());
outData.clear();
outData.put(bytes);
outData.rewind();
outData.resetPos();
} catch (Exception e) {
log(e.getStackTrace().toString());
}
@ -355,7 +410,7 @@ public class AES128 extends IOUnit {
if (!advancedCipherMode) {
value |= AESKEYWR;
} else {
key.rewind();
key.resetPos();
}
} else {
isBusy = true;
@ -382,7 +437,7 @@ public class AES128 extends IOUnit {
if (!advancedCipherMode) {
value |= AESKEYWR;
} else {
inData.rewind();
inData.resetPos();
}
} else {
inData.position(inData.limit());