2 import java.util.Vector;
3 import java.nio.ByteBuffer;
4 import javax.crypto.Mac;
5 import java.util.Arrays;
7 class Slot implements Liveness {
8 static final int SLOT_SIZE=2048;
9 static final int HMAC_SIZE=32;
12 private byte[] prevhmac;
14 private long machineid;
15 private Vector<Entry> entries;
16 private int livecount;
18 Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) {
23 entries=new Vector<Entry>();
27 Slot(long _seqnum, byte[] _bytes) {
35 byte[] getPrevHMAC() {
39 void addEntry(Entry e) {
44 Vector<Entry> getEntries() {
48 static Slot decode(byte[] array, Mac mac) {
49 mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
50 byte[] realmac=mac.doFinal();
52 ByteBuffer bb=ByteBuffer.wrap(array);
53 byte[] hmac=new byte[HMAC_SIZE];
54 byte[] prevhmac=new byte[HMAC_SIZE];
57 if (!Arrays.equals(realmac, hmac))
58 throw new Error("Server Error: Invalid HMAC! Potential Attack!");
60 long seqnum=bb.getLong();
61 long machineid=bb.getLong();
62 int numentries=bb.getInt();
63 Slot slot=new Slot(seqnum, machineid, prevhmac, hmac);
65 for(int i=0;i<numentries;i++) {
66 slot.addEntry(Entry.decode(slot, bb));
72 byte[] encode(Mac mac) {
73 byte[] array=new byte[SLOT_SIZE];
74 ByteBuffer bb=ByteBuffer.wrap(array);
75 bb.position(HMAC_SIZE); //Leave space for the HMACs
78 bb.putLong(machineid);
79 bb.putInt(entries.size());
80 for(Entry entry:entries) {
84 mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
85 byte[] realmac=mac.doFinal();
91 long getSequenceNumber() {
103 void decrementLiveCount() {
108 return livecount > 0;
111 public String toString() {
112 return "<"+getSequenceNumber()+", "+new String(getBytes())+">";