Automatically calculate auth data length based on the auth method
Change-Id: I9d21834a160e6f4b2ed086f17cda144493e7c3aa
diff --git a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/LispMapServer.java b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/LispMapServer.java
index 69191d3..aca2c76 100644
--- a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/LispMapServer.java
+++ b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/LispMapServer.java
@@ -46,8 +46,6 @@
// TODO: need to be configurable
private static final String AUTH_KEY = "onos";
- private static final short AUTH_DATA_LENGTH = 20;
-
// TODO: need to be configurable
private static final short AUTH_METHOD = 1;
@@ -79,11 +77,11 @@
// build temp notify message
NotifyBuilder authNotifyBuilder = new DefaultNotifyBuilder();
authNotifyBuilder.withKeyId(AUTH_METHOD);
- authNotifyBuilder.withAuthDataLength(AUTH_DATA_LENGTH);
+ authNotifyBuilder.withAuthDataLength(valueOf(AUTH_METHOD).getHashLength());
authNotifyBuilder.withNonce(register.getNonce());
authNotifyBuilder.withMapRecords(register.getMapRecords());
- byte[] authData = new byte[AUTH_DATA_LENGTH];
+ byte[] authData = new byte[valueOf(AUTH_METHOD).getHashLength()];
Arrays.fill(authData, (byte) 0);
authNotifyBuilder.withAuthenticationData(authData);
@@ -97,13 +95,13 @@
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
- byte[] sha1AuthData =
- factory.createAuthenticationData(valueOf(register.getKeyId()), AUTH_KEY, bytes);
+ byte[] calcAuthData = factory.createAuthenticationData(
+ valueOf(register.getKeyId()), AUTH_KEY, bytes);
NotifyBuilder notifyBuilder = new DefaultNotifyBuilder();
notifyBuilder.withKeyId(AUTH_METHOD);
- notifyBuilder.withAuthDataLength((short) sha1AuthData.length);
- notifyBuilder.withAuthenticationData(sha1AuthData);
+ notifyBuilder.withAuthDataLength((short) calcAuthData.length);
+ notifyBuilder.withAuthenticationData(calcAuthData);
notifyBuilder.withNonce(register.getNonce());
notifyBuilder.withMapRecords(register.getMapRecords());
@@ -123,10 +121,10 @@
}
/**
- * Checks the integrity of the received Map-Register message by calculating
- * authentication data from received Map-Register message.
+ * Checks the integrity of the received map-register message by calculating
+ * authentication data from received map-register message.
*
- * @param register Map-Register message
+ * @param register map-register message
* @return evaluation result
*/
private boolean checkAuthData(LispMapRegister register) {
@@ -154,8 +152,8 @@
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
- byte[] calculatedAuthData =
- factory.createAuthenticationData(valueOf(register.getKeyId()), AUTH_KEY, bytes);
+ byte[] calculatedAuthData = factory.createAuthenticationData(
+ valueOf(register.getKeyId()), AUTH_KEY, bytes);
return Arrays.equals(calculatedAuthData, register.getAuthenticationData());
}
}
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispAuthenticationKeyEnum.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispAuthenticationKeyEnum.java
index ecf6c8e..2bd3df9 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispAuthenticationKeyEnum.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispAuthenticationKeyEnum.java
@@ -26,23 +26,25 @@
public enum LispAuthenticationKeyEnum {
/** No authentication. */
- NONE(0, null),
+ NONE(0, null, 0),
/** HMAC SHA1 encryption. */
- SHA1(1, "HmacSHA1"),
+ SHA1(1, "HmacSHA1", 20),
/** HMAC SHA256 encryption. */
- SHA256(2, "HmacSHA256"),
+ SHA256(2, "HmacSHA256", 32),
/** Unsupported authentication type. */
- UNKNOWN(-1, "UNKNOWN");
+ UNKNOWN(-1, "UNKNOWN", 0);
private short keyId;
private String name;
+ private short length;
- LispAuthenticationKeyEnum(int keyId, String name) {
+ LispAuthenticationKeyEnum(int keyId, String name, int length) {
this.keyId = (short) keyId;
this.name = name;
+ this.length = (short) length;
}
/**
@@ -64,6 +66,15 @@
}
/**
+ * Obtains hash length.
+ *
+ * @return hash length
+ */
+ public short getHashLength() {
+ return length;
+ }
+
+ /**
* Obtains LISP authentication key enum by providing key identifier.
*
* @param keyId LISP authentication key identifier