blob: 905fe46286ff72e443a6fab57f6a7f38e366e55a [file] [log] [blame]
Jian Lif8c2d4a2016-09-15 02:33:12 +09001/*
2 * Copyright 2016-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 */
16package org.onosproject.lisp.msg.authentication;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21import javax.crypto.Mac;
22import javax.crypto.spec.SecretKeySpec;
23import java.security.InvalidKeyException;
24import java.security.NoSuchAlgorithmException;
25
26import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.SHA1;
27import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.SHA256;
28
29/**
30 * LISP MAC authentication utility class.
31 */
32public class LispMacAuthentication {
33
34 private static final Logger log = LoggerFactory.getLogger(LispMacAuthentication.class);
35
36 private String algorithm;
37 private int authenticationLength;
38
39 public LispMacAuthentication(LispAuthenticationKeyEnum authType) {
40
41 if (authType == SHA1 || authType == SHA256) {
42 algorithm = authType.getName();
43 } else {
44 log.warn("Not support provided algorithm {}", authType.getName());
45 return;
46 }
47
48 try {
49 authenticationLength = Mac.getInstance(algorithm).getMacLength();
50 } catch (NoSuchAlgorithmException e) {
51 log.warn("Not support provided algorithm {}", algorithm);
52 }
53 }
54
55 /**
56 * Obtains dummy authentication data.
57 *
58 * @return dummy authentication data
59 */
60 public byte[] getAuthenticationData() {
61 return new byte[0];
62 }
63
64 /**
65 * Obtains authentication data with given key and algorithm.
66 *
67 * @param key authentication key (e.g., EID)
68 * @param data array of byte buffer for place holder
69 * @return authentication data
70 */
71 public byte[] getAuthenticationData(String key, byte[] data) {
72 try {
73 SecretKeySpec signKey = new SecretKeySpec(key.getBytes(), algorithm);
74 Mac mac = Mac.getInstance(algorithm);
75 mac.init(signKey);
76
77 return mac.doFinal(data);
78 } catch (NoSuchAlgorithmException e) {
79 log.warn("Not support provided algorithm {}", algorithm);
80 } catch (InvalidKeyException e) {
81 log.warn("Provided key {} is invalid", key);
82 }
83 return null;
84 }
85
86 /**
87 * Obtains authentication data length.
88 *
89 * @return authentication data length
90 */
91 public int getAuthenticationLength() {
92 return authenticationLength;
93 }
94
95 /**
96 * Obtains authentication algorithm.
97 *
98 * @return authentication algorithm
99 */
100 public String getAlgorithm() {
101 return algorithm;
102 }
103}