1 19 20 package com.maverick.ssl; 21 22 import com.maverick.crypto.digests.MD5Digest; 23 24 29 public abstract class SSLCipherSuiteWithMD5MAC implements SSLCipherSuite { 30 31 MD5Digest generateDigest = new MD5Digest(); 32 MD5Digest verifyDigest = new MD5Digest(); 33 34 byte[] encryptMAC; 35 byte[] decryptMAC; 36 37 byte[] padding1 = new byte[] { 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 38 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 39 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36 }; 40 41 byte[] padding2 = new byte[] { 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 42 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 43 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c }; 44 45 public SSLCipherSuiteWithMD5MAC() { 46 47 } 48 49 public abstract int getKeyLength(); 50 51 public abstract int getIVLength(); 52 53 public abstract void encrypt(byte[] b, int offset, int len); 54 55 public abstract void decrypt(byte[] b, int offset, int len); 56 57 public final void init(byte[] encryptKey, byte[] encryptIV, byte[] encryptMAC, byte[] decryptKey, byte[] decryptIV, 58 byte[] decryptMAC) { 59 this.encryptMAC = encryptMAC; 60 this.decryptMAC = decryptMAC; 61 62 init(encryptKey, encryptIV, decryptKey, decryptIV); 63 } 64 65 protected abstract void init(byte[] encryptKey, byte[] encryptIV, byte[] decryptKey, byte[] decryptIV); 66 67 public final int getMACLength() { 68 return generateDigest.getDigestSize(); 69 } 70 71 public byte[] generateMAC(byte[] b, int offset, int len, int type, long sequenceNo) { 72 return calculateMAC(generateDigest, b, offset, len, type, sequenceNo, encryptMAC); 73 } 74 75 public boolean verifyMAC(byte[] b, int offset, int len, int type, long sequenceNo, byte[] mac, int macoff, int maclen) { 76 byte[] gen = calculateMAC(verifyDigest, b, offset, len, type, sequenceNo, decryptMAC); 77 for (int i = 0; i < gen.length; i++) { 78 if (gen[i] != mac[i + macoff]) { 79 return false; 80 } 81 } 82 return true; 83 } 84 85 private byte[] calculateMAC(MD5Digest digest, byte[] b, int off, int len, int type, long sequenceNo, byte[] key) { 86 digest.reset(); 87 88 digest.update(key, 0, key.length); 89 digest.update(padding1, 0, padding1.length); 90 digest.update((byte) ((sequenceNo >> 56) & 0xFF)); 91 digest.update((byte) ((sequenceNo >> 48) & 0xFF)); 92 digest.update((byte) ((sequenceNo >> 40) & 0xFF)); 93 digest.update((byte) ((sequenceNo >> 32) & 0xFF)); 94 digest.update((byte) ((sequenceNo >> 24) & 0xFF)); 95 digest.update((byte) ((sequenceNo >> 16) & 0xFF)); 96 digest.update((byte) ((sequenceNo >> 8) & 0xFF)); 97 digest.update((byte) ((sequenceNo >> 0) & 0xFF)); 98 99 digest.update((byte) type); 100 digest.update((byte) ((len >> 8) & 0xFF)); 101 digest.update((byte) (len & 0xFF)); 102 103 digest.update(b, off, len); 104 105 byte[] temp = new byte[digest.getDigestSize()]; 106 digest.doFinal(temp, 0); 107 108 digest.reset(); 109 110 digest.update(key, 0, key.length); 111 digest.update(padding2, 0, padding2.length); 112 digest.update(temp, 0, temp.length); 113 114 digest.doFinal(temp, 0); 115 116 return temp; 117 118 } 119 120 } | Popular Tags |