Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
SMPLinkKey.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26package org.direct_bt;
27
28import org.jau.util.BasicTypes;
29
30/**
31 * Local SMP Link Key, used for platform agnostic persistence,
32 * mapping to platform specific link keys format.
33 * <p>
34 * Notable: No endian wise conversion shall occur on this data,
35 * since the encryption values are interpreted as a byte stream.
36 * </p>
37 * <p>
38 * Byte layout must be synchronized with native direct_bt::SMPLinkKeyInfo
39 * </p>
40 * @since 2.4.0
41 */
42public final class SMPLinkKey {
43 /**
44 * Link Key Types compatible with Mgmt's MgmtLinkKeyType and hence MgmtLinkKeyInfo
45 */
46 static public enum KeyType {
47 /** Combination key */
48 COMBI((byte)0x00),
49 /** Local Unit key */
50 LOCAL_UNIT((byte)0x01),
51 /** Remote Unit key */
52 REMOTE_UNIT((byte)0x02),
53 /** Debug Combination key */
54 DBG_COMBI((byte)0x03),
55 /** Unauthenticated Combination key from P-192 */
56 UNAUTH_COMBI_P192((byte)0x04),
57 /** Authenticated Combination key from P-192 */
58 AUTH_COMBI_P192((byte)0x05),
59 /** Changed Combination key */
60 CHANGED_COMBI((byte)0x06),
61 /** Unauthenticated Combination key from P-256 */
62 UNAUTH_COMBI_P256((byte)0x07),
63 /** Authenticated Combination key from P-256 */
64 AUTH_COMBI_P256((byte)0x08),
65 /** Denoting no or invalid link key type */
66 NONE((byte)0xff);
67
68 public final byte value;
69
70 /**
71 * Maps the specified name to a constant of {@link KeyType}.
72 * <p>
73 * Implementation simply returns {@link #valueOf(String)}.
74 * This maps the constant names itself to their respective constant.
75 * </p>
76 * @param name the string name to be mapped to a constant of this enum type.
77 * @return the corresponding constant of this enum type.
78 * @throws IllegalArgumentException if the specified name can't be mapped to a constant of this enum type
79 * as described above.
80 */
81 public static KeyType get(final String name) throws IllegalArgumentException {
82 return valueOf(name);
83 }
84
85 /**
86 * Maps the specified integer value to a constant of {@link KeyType}.
87 * @param value the integer value to be mapped to a constant of this enum type.
88 * @return the corresponding constant of this enum type, using {@link #NONE} if not supported.
89 */
90 public static KeyType get(final byte value) {
91 switch(value) {
92 case (byte) 0x00: return COMBI;
93 case (byte) 0x01: return LOCAL_UNIT;
94 case (byte) 0x02: return REMOTE_UNIT;
95 case (byte) 0x03: return DBG_COMBI;
96 case (byte) 0x04: return UNAUTH_COMBI_P192;
97 case (byte) 0x05: return AUTH_COMBI_P192;
98 case (byte) 0x06: return CHANGED_COMBI;
99 case (byte) 0x07: return UNAUTH_COMBI_P256;
100 case (byte) 0x08: return AUTH_COMBI_P256;
101 default: return NONE;
102 }
103 }
104
105 KeyType(final byte v) {
106 value = v;
107 }
108 }
109
110 /** Responder (ll slave) flag. */
111 public boolean responder;
112
113 /** {@link KeyType} value. */
114 public KeyType type;
115
116 /** Link key. */
117 public byte key[/*16*/];
118
119 /** Pin length */
120 public byte pin_length;
121
122 /**
123 * Size of the byte stream representation in bytes
124 * @see #put(byte[], int)
125 */
126 public static final int byte_size = 1+1+16+1;
127
128 /** Construct instance via given source byte array */
129 public SMPLinkKey(final byte source[], final int pos) {
130 key = new byte[16];
131 get(source, pos);
132 }
133
134 /** Construct emoty unset instance. */
135 public SMPLinkKey() {
136 responder = false;
137 type = KeyType.NONE;
138 key = new byte[16];
139 pin_length = 0;
140 }
141
142 /**
143 * Method transfers all bytes representing a SMPLinkKeyInfo from the given
144 * source array at the given position into this instance.
145 * <p>
146 * Implementation is consistent with {@link #put(byte[], int)}.
147 * </p>
148 * @param source the source array
149 * @param pos starting position in the source array
150 * @see #put(byte[], int)
151 */
152 public void get(final byte[] source, int pos) {
153 if( byte_size > ( source.length - pos ) ) {
154 throw new IllegalArgumentException("Stream ( "+source.length+" - "+pos+" ) < "+byte_size+" bytes");
155 }
156 responder = (byte)0 != source[pos++];
157 type = KeyType.get(source[pos++]);
158 System.arraycopy(source, pos, key, 0, 16); pos+=16;
159 pin_length = source[pos++];
160 }
161
162 /**
163 * Method transfers all bytes representing this instance into the given
164 * destination array at the given position.
165 * <p>
166 * Implementation is consistent with {@link #SMPLinkKeyInfo(byte[], int)}.
167 * </p>
168 * @param sink the destination array
169 * @param pos starting position in the destination array
170 * @see #SMPLinkKeyInfo(byte[], int)
171 * @see #get(byte[], int)
172 */
173 public final void put(final byte[] sink, int pos) {
174 if( byte_size > ( sink.length - pos ) ) {
175 throw new IllegalArgumentException("Stream ( "+sink.length+" - "+pos+" ) < "+byte_size+" bytes");
176 }
177 sink[pos++] = responder ? (byte)1 : (byte)0;
178 sink[pos++] = type.value;
179 System.arraycopy(key, 0, sink, pos, 16); pos+=16;
180 sink[pos++] = pin_length;
181 }
182
183 public final boolean isValid() { return KeyType.NONE != type; }
184
185 public final boolean isResponder() { return responder; }
186
187 @Override
188 public String toString() { // hex-fmt aligned with btmon
189 return "LK[res "+responder+", type "+type.toString()+
190 ", key "+BasicTypes.bytesHexString(key, 0, -1, true /* lsbFirst */)+
191 ", plen "+pin_length+
192 "]";
193 }
194
195};
Local SMP Link Key, used for platform agnostic persistence, mapping to platform specific link keys fo...
Definition: SMPLinkKey.java:42
KeyType type
KeyType value.
SMPLinkKey(final byte source[], final int pos)
Construct instance via given source byte array.
final boolean isResponder()
byte pin_length
Pin length.
final boolean isValid()
SMPLinkKey()
Construct emoty unset instance.
static final int byte_size
Size of the byte stream representation in bytes.
boolean responder
Responder (ll slave) flag.
final void put(final byte[] sink, int pos)
Method transfers all bytes representing this instance into the given destination array at the given p...
Link Key Types compatible with Mgmt's MgmtLinkKeyType and hence MgmtLinkKeyInfo.
Definition: SMPLinkKey.java:46
DBG_COMBI
Debug Combination key.
Definition: SMPLinkKey.java:54
UNAUTH_COMBI_P192
Unauthenticated Combination key from P-192.
Definition: SMPLinkKey.java:56
NONE
Denoting no or invalid link key type.
Definition: SMPLinkKey.java:66
CHANGED_COMBI
Changed Combination key.
Definition: SMPLinkKey.java:60
static KeyType get(final String name)
Maps the specified name to a constant of KeyType.
Definition: SMPLinkKey.java:81
AUTH_COMBI_P256
Authenticated Combination key from P-256.
Definition: SMPLinkKey.java:64
AUTH_COMBI_P192
Authenticated Combination key from P-192.
Definition: SMPLinkKey.java:58
UNAUTH_COMBI_P256
Unauthenticated Combination key from P-256.
Definition: SMPLinkKey.java:62