blob: 0671eed3313a3ec1cb3b734112dd0c0a824f6139 [file] [log] [blame]
sunishvka1dfc3e2016-04-16 12:24:47 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sunishvka1dfc3e2016-04-16 12:24:47 +05303 *
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 */
16package org.onosproject.isis.io.util;
17
18
19import java.util.Arrays;
20
21/**
22 * Calculates checksum for ISIS LSP packets.
23 */
24public class ChecksumCalculator {
25
26 /**
27 * Verifies the checksum is valid in given LSP packet bytes.
28 *
29 * @param lspPacket lsp as byte array
30 * @param lspChecksumPos1 position of checksum bit in packet
31 * @param lspChecksumPos2 position of checksum bit in packet
32 * @return true if valid else false
33 */
34 public boolean validateLspCheckSum(byte[] lspPacket, int lspChecksumPos1, int lspChecksumPos2) {
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053035
sunishvka1dfc3e2016-04-16 12:24:47 +053036 byte[] checksum = calculateLspChecksum(lspPacket, lspChecksumPos1, lspChecksumPos2);
37 if (lspPacket[lspChecksumPos1] == checksum[0] && lspPacket[lspChecksumPos2] == checksum[1]) {
38 return true;
39 }
40 return false;
41 }
42
43
44 /**
45 * Calculates the LSP checksum.
46 *
47 * @param lspBytes as byte array
48 * @param lspChecksumPos1 position of checksum bit in packet
49 * @param lspChecksumPos2 position of checksum bit in packet
50 * @return checksum bytes
51 */
52 public byte[] calculateLspChecksum(byte[] lspBytes, int lspChecksumPos1, int lspChecksumPos2) {
53
54 byte[] tempLsaByte = Arrays.copyOf(lspBytes, lspBytes.length);
55
56 int[] checksumOut = {0, 0};
57 tempLsaByte[lspChecksumPos1] = 0;
58 tempLsaByte[lspChecksumPos2] = 0;
59 byte[] byteCheckSum = {0, 0};
Ray Milkeyfe0e0852018-01-18 11:14:05 -080060 for (int i = 12; i < tempLsaByte.length; i++) {
61 checksumOut[0] = checksumOut[0] + ((int) tempLsaByte[i] & 0xFF);
62 checksumOut[1] = checksumOut[1] + checksumOut[0];
sunishvka1dfc3e2016-04-16 12:24:47 +053063 }
Ray Milkeyfe0e0852018-01-18 11:14:05 -080064 checksumOut[0] = checksumOut[0] % 255;
65 checksumOut[1] = checksumOut[1] % 255;
sunishvka1dfc3e2016-04-16 12:24:47 +053066 int byte1 = (int) ((tempLsaByte.length - lspChecksumPos1 - 1) * checksumOut[0] - checksumOut[1]) % 255;
67 if (byte1 <= 0) {
68 byte1 += 255;
69 }
70 int byte2 = 510 - checksumOut[0] - byte1;
71 if (byte2 > 255) {
72 byte2 -= 255;
73 }
74
75 byteCheckSum[0] = (byte) byte1;
76 byteCheckSum[1] = (byte) byte2;
77
78 return byteCheckSum;
79 }
80}