Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
SMPSignatureResolvingKey.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 * SMP Signature Resolving Key, used for platform agnostic persistence.
32 * <p>
33 * Notable: No endian wise conversion shall occur on this data,
34 * since the encryption values are interpreted as a byte stream.
35 * </p>
36 * <p>
37 * Byte layout must be synchronized with native direct_bt::SMPSignatureResolvingKeyInfo
38 * </p>
39 * @since 2.2.0
40 */
42 /**
43 * {@link SMPSignatureResolvingKey} Property Bits
44 */
45 static public enum PropertyType {
46 /** No specific property */
47 NONE((byte)0),
48 /** Responder Key (LL slave). Absence indicates Initiator Key (LL master). */
49 RESPONDER((byte)0x01),
50 /** Authentication used. */
51 AUTH((byte)0x02);
52
53 public final byte value;
54
55 /**
56 * Maps the specified name to a constant of {@link PropertyType}.
57 * <p>
58 * Implementation simply returns {@link #valueOf(String)}.
59 * This maps the constant names itself to their respective constant.
60 * </p>
61 * @param name the string name to be mapped to a constant of this enum type.
62 * @return the corresponding constant of this enum type.
63 * @throws IllegalArgumentException if the specified name can't be mapped to a constant of this enum type
64 * as described above.
65 */
66 public static PropertyType get(final String name) throws IllegalArgumentException {
67 return valueOf(name);
68 }
69
70 /**
71 * Maps the specified integer value to a constant of {@link PropertyType}.
72 * @param value the integer value to be mapped to a constant of this enum type.
73 * @return the corresponding constant of this enum type, using {@link #NONE} if not supported.
74 */
75 public static PropertyType get(final byte value) {
76 switch(value) {
77 case (byte) 0x01: return RESPONDER;
78 case (byte) 0x02: return AUTH;
79 default: return NONE;
80 }
81 }
82
83 PropertyType(final byte v) {
84 value = v;
85 }
86 }
87
88 /**
89 * {@link SMPSignatureResolvingKey} {@link PropertyType} Bit Mask
90 */
91 static public class Properties {
92 /** The {@link PropertyType} bit mask */
93 public byte mask;
94
95 public Properties(final byte v) {
96 mask = v;
97 }
98
99 public boolean isEmpty() { return 0 == mask; }
100 public boolean isSet(final PropertyType bit) { return 0 != ( mask & bit.value ); }
101 public void set(final PropertyType bit) { mask = (byte) ( mask | bit.value ); }
102
103 @Override
104 public String toString() {
105 int count = 0;
106 final StringBuilder out = new StringBuilder();
108 out.append(PropertyType.RESPONDER.name()); count++;
109 }
110 if( isSet(PropertyType.AUTH) ) {
111 if( 0 < count ) { out.append(", "); }
112 out.append(PropertyType.AUTH.name()); count++;
113 }
114 return "["+out.toString()+"]";
115 }
116 }
117
118 /** {@link Properties} bit mask. 1 octet or 8 bits. */
120
121 /** Connection Signature Resolving Key (CSRK) */
122 public byte csrk[/*16*/];
123
124 /**
125 * Size of the byte stream representation in bytes
126 * @see #put(byte[], int)
127 */
128 public static final int byte_size = 1+16;
129
130 /** Construct instance via given source byte array */
131 public SMPSignatureResolvingKey(final byte source[], final int pos) {
132 csrk = new byte[16];
133 get(source, pos);
134 }
135
136 /** Construct emoty unset instance. */
138 properties = new Properties((byte)0);
139 csrk = new byte[16];
140 }
141
142 /**
143 * Method transfers all bytes representing a SMPSignatureResolvingKeyInfo 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 properties = new Properties(source[pos++]);
157 System.arraycopy(source, pos, csrk, 0, 16); pos+=16;
158 }
159
160 /**
161 * Method transfers all bytes representing this instance into the given
162 * destination array at the given position.
163 * <p>
164 * Implementation is consistent with {@link #SMPSignatureResolvingKeyInfo(byte[], int)}.
165 * </p>
166 * @param sink the destination array
167 * @param pos starting position in the destination array
168 * @see #SMPSignatureResolvingKeyInfo(byte[], int)
169 * @see #get(byte[], int)
170 */
171 public final void put(final byte[] sink, int pos) {
172 if( byte_size > ( sink.length - pos ) ) {
173 throw new IllegalArgumentException("Stream ( "+sink.length+" - "+pos+" ) < "+byte_size+" bytes");
174 }
175 sink[pos++] = properties.mask;
176 System.arraycopy(csrk, 0, sink, pos, 16); pos+=16;
177 }
178
179 public final boolean isResponder() { return properties.isSet(PropertyType.RESPONDER); }
180
181 @Override
182 public String toString() { // hex-fmt aligned with btmon
183 return "CSRK[props "+properties.toString()+
184 ", csrk "+BasicTypes.bytesHexString(csrk, 0, -1, true /* lsbFirst */)+
185 "]";
186 }
187
188};
SMPSignatureResolvingKey PropertyType Bit Mask
SMP Signature Resolving Key, used for platform agnostic persistence.
final void put(final byte[] sink, int pos)
Method transfers all bytes representing this instance into the given destination array at the given p...
static final int byte_size
Size of the byte stream representation in bytes.
SMPSignatureResolvingKey()
Construct emoty unset instance.
byte csrk[]
Connection Signature Resolving Key (CSRK)
SMPSignatureResolvingKey(final byte source[], final int pos)
Construct instance via given source byte array.