Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.packet; |
| 18 | |
| 19 | import java.nio.ByteBuffer; |
| 20 | |
| 21 | /** |
| 22 | * Class representing MKPDU ICV Indicator. |
| 23 | * IEEE 802.1X Clause 11; Figure 11-16 |
| 24 | */ |
| 25 | public class EAPOLMkpduICVIndicatorParameterSet extends BasePacket implements EAPOLMkpduParameterSet { |
| 26 | |
| 27 | // Variables for Hash Generation. |
| 28 | private byte[] icv; |
| 29 | |
| 30 | /* |
| 31 | * Body Length is fixed in SAK Use Parameter Set. |
| 32 | * Still variable kept for de-serialization purpose. |
| 33 | */ |
| 34 | private short bodyLength; |
| 35 | |
| 36 | // Total packet length. Currently only 128bit ICV is supported. |
| 37 | public static final short TOTAL_ICVPS_BODY_LENGTH = 20; |
| 38 | |
| 39 | |
| 40 | @Override |
| 41 | public byte[] serialize() { |
| 42 | short length = getTotalLength(); |
| 43 | |
| 44 | // Serialize ICV Indicator Parameter Set. IEEE 802.1x, Figure 11.16 . |
| 45 | ByteBuffer data = ByteBuffer.wrap(new byte[length]); |
| 46 | |
| 47 | /* |
| 48 | * Populate fields |
| 49 | * Octet 1 |
| 50 | */ |
| 51 | data.put(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_ICV_INDICATOR); |
| 52 | |
| 53 | // Octet 2. Reserved. |
| 54 | byte octet = 0x00; |
| 55 | data.put(octet); |
| 56 | |
| 57 | // Octet 3 |
| 58 | length -= EAPOLMkpduParameterSet.BODY_LENGTH_OCTET_OFFSET; |
| 59 | octet |= (byte) (length >> BODY_LENGTH_MSB_SHIFT & BODY_LENGTH_MSB_MASK); |
| 60 | data.put(octet); |
| 61 | |
| 62 | // Octet 4 |
| 63 | data.put((byte) length); |
| 64 | |
| 65 | // Note : ICV generation excluded from serialization. |
| 66 | // Should be done at the level where ethernet packet is being created. |
| 67 | |
| 68 | return data.array(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Deserializer function for ICV Indicator Parameter Set. |
| 73 | * |
| 74 | * @return deserializer function |
| 75 | */ |
| 76 | public static Deserializer<EAPOLMkpduICVIndicatorParameterSet> deserializer() { |
| 77 | return (data, offset, length) -> { |
| 78 | // TODO : Ensure buffer has enough details. |
| 79 | |
| 80 | // Deserialize Basic Parameter Set. |
| 81 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 82 | EAPOLMkpduICVIndicatorParameterSet icvps = new EAPOLMkpduICVIndicatorParameterSet(); |
| 83 | |
| 84 | // Extract fields. Skip type, reserved fields. |
| 85 | byte[] mbField = new byte[2]; |
| 86 | bb.get(mbField, 0, 2); |
| 87 | short bodyLength = (short) (((short) (mbField[1] & EAPOLMkpduParameterSet.BODY_LENGTH_MSB_MASK)) |
| 88 | << EAPOLMkpduParameterSet.BODY_LENGTH_MSB_SHIFT); |
| 89 | bodyLength |= (short) (bb.get()); |
| 90 | icvps.setBodyLength(bodyLength); |
| 91 | |
| 92 | // SAK |
| 93 | byte[] icv = new byte[bodyLength]; |
| 94 | bb.get(icv); |
| 95 | icvps.setICV(icv); |
| 96 | |
| 97 | return icvps; |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public byte getParameterSetType() { |
| 103 | return PARAMETERSET_TYPE_ICV_INDICATOR; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public short getTotalLength() { |
| 108 | return TOTAL_ICVPS_BODY_LENGTH; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public short getBodyLength() { |
| 113 | return TOTAL_ICVPS_BODY_LENGTH - BODY_LENGTH_OCTET_OFFSET; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * To set body length. |
| 118 | * |
| 119 | * @param bodyLength ,type short |
| 120 | */ |
| 121 | public void setBodyLength(short bodyLength) { |
| 122 | this.bodyLength = bodyLength; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * To set ICV. |
| 127 | * |
| 128 | * @param icv , type byte[] |
| 129 | */ |
| 130 | public void setICV(byte[] icv) { |
| 131 | this.icv = icv; |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |