Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
SMPKeyMask.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
28/**
29 * SMP Key Type for Distribution, indicates keys distributed in the Transport Specific Key Distribution phase.
30 * <p>
31 * {@link SMPKeyMask} {@link SMPKeyMask.KeyType} Bit Mask
32 * </p>
33 * <pre>
34 * Field format and usage: Vol 3, Part H, 3.6.1 SMP - LE Security - Key distribution and generation.
35 * See also Vol 3, Part H, 2.4.3 SM - LE Security - Distribution of keys.
36 * </pre>
37 * </p>
38 * Layout LSB -> MSB
39 * <pre>
40 * uint8_t EncKey : 1, IdKey : 1, SignKey : 1, LinkKey : 1, RFU : 4;
41 * </pre>
42 * @since 2.2.0
43 */
44public class SMPKeyMask {
45 /**
46 * {@link SMPKeyMask} Key Type
47 */
48 static public enum KeyType {
49 NONE ((byte)0),
50 /**
51 * LE legacy pairing: Indicates device shall distribute LTK using the Encryption Information command,
52 * followed by EDIV and Rand using the Master Identification command.
53 * <p>
54 * LE Secure Connections pairing (SMP on LE transport): Ignored,
55 * EDIV and Rand shall be zero and shall not be distributed.
56 * </p>
57 * <p>
58 * SMP on BR/EDR transport: Indicates device likes to derive LTK from BR/EDR Link Key.<br>
59 * When EncKey is set to 1 by both devices in the initiator and responder Key Distribution / Generation fields,
60 * the procedures for calculating the LTK from the BR/EDR Link Key shall be used.
61 * </p>
62 */
63 ENC_KEY ((byte) 0b00000001),
64 /**
65 * Indicates that the device shall distribute IRK using the Identity Information command
66 * followed by its public device or status random address using Identity Address Information.
67 */
68 ID_KEY ((byte) 0b00000010),
69 /**
70 * Indicates that the device shall distribute CSRK using the Signing Information command.
71 */
72 SIGN_KEY ((byte) 0b00000100),
73 /**
74 * SMP on the LE transport: Indicate that the device would like to derive the Link Key from the LTK.<br>
75 * When LinkKey is set to 1 by both devices in the initiator and responder Key Distribution / Generation fields,
76 * the procedures for calculating the BR/EDR link key from the LTK shall be used.<br>
77 * Devices not supporting LE Secure Connections shall set this bit to zero and ignore it on reception.
78 * <p>
79 * SMP on BR/EDR transport: Reserved for future use.
80 * </p>
81 */
82 LINK_KEY ((byte) 0b00001000),
83 /** Reserved for future use */
84 RFU_1 ((byte) 0b00010000),
85 /** Reserved for future use */
86 RFU_2 ((byte) 0b00100000),
87 /** Reserved for future use */
88 RFU_3 ((byte) 0b01000000),
89 /** Reserved for future use */
90 RFU_4 ((byte) 0b10000000);
91
92 public final byte value;
93
94 /**
95 * Maps the specified name to a constant of {@link KeyType}.
96 * <p>
97 * Implementation simply returns {@link #valueOf(String)}.
98 * This maps the constant names itself to their respective constant.
99 * </p>
100 * @param name the string name to be mapped to a constant of this enum type.
101 * @return the corresponding constant of this enum type.
102 * @throws IllegalArgumentException if the specified name can't be mapped to a constant of this enum type
103 * as described above.
104 */
105 public static KeyType get(final String name) throws IllegalArgumentException {
106 return valueOf(name);
107 }
108
109 /**
110 * Maps the specified integer value to a constant of {@link KeyType}.
111 * @param value the integer value to be mapped to a constant of this enum type.
112 * @return the corresponding constant of this enum type, using {@link #NONE} if not supported.
113 */
114 public static KeyType get(final byte value) {
115 switch(value) {
116 case (byte) 0b00000001: return ENC_KEY;
117 case (byte) 0b00000010: return ID_KEY;
118 case (byte) 0b00000100: return SIGN_KEY;
119 case (byte) 0b00001000: return LINK_KEY;
120 case (byte) 0b00010000: return RFU_1;
121 case (byte) 0b00100000: return RFU_2;
122 case (byte) 0b01000000: return RFU_3;
123 case (byte) 0b10000000: return RFU_4;
124 default: return NONE;
125 }
126 }
127
128 KeyType(final byte v) {
129 value = v;
130 }
131 }
132
133 /** The {@link KeyType} bit mask */
134 public byte mask;
135
136 public SMPKeyMask(final byte v) {
137 mask = v;
138 }
139
140 public SMPKeyMask() {
141 mask = (byte)0;
142 }
143
144 public boolean isEmpty() { return 0 == mask; }
145 public boolean isSet(final KeyType bit) { return 0 != ( mask & bit.value ); }
146 public void set(final KeyType bit) { mask = (byte) ( mask | bit.value ); }
147
148 @Override
149 public String toString() {
150 boolean has_pre = false;
151 final StringBuilder out = new StringBuilder();
152 out.append("[");
153 for(int i=0; i<8; i++) {
154 final KeyType key_type = KeyType.get( (byte) ( 1 << i ) );
155 if( isSet( key_type ) ) {
156 if( has_pre ) { out.append(", "); }
157 out.append( key_type.toString() );
158 has_pre = true;
159 }
160 }
161 out.append("]");
162 return out.toString();
163 }
164
165};
SMP Key Type for Distribution, indicates keys distributed in the Transport Specific Key Distribution ...
Definition: SMPKeyMask.java:44
byte mask
The KeyType bit mask.
boolean isSet(final KeyType bit)
SMPKeyMask(final byte v)
LINK_KEY
SMP on the LE transport: Indicate that the device would like to derive the Link Key from the LTK.
Definition: SMPKeyMask.java:82
ID_KEY
Indicates that the device shall distribute IRK using the Identity Information command followed by its...
Definition: SMPKeyMask.java:68
static KeyType get(final String name)
Maps the specified name to a constant of KeyType.
RFU_1
Reserved for future use.
Definition: SMPKeyMask.java:84
SIGN_KEY
Indicates that the device shall distribute CSRK using the Signing Information command.
Definition: SMPKeyMask.java:72
RFU_2
Reserved for future use.
Definition: SMPKeyMask.java:86
RFU_4
Reserved for future use.
Definition: SMPKeyMask.java:90
RFU_3
Reserved for future use.
Definition: SMPKeyMask.java:88
ENC_KEY
LE legacy pairing: Indicates device shall distribute LTK using the Encryption Information command,...
Definition: SMPKeyMask.java:63