Update the random PIN generator module to provide high entropy.
[platform/upstream/iotivity.git] / resource / csdk / security / include / pinoxmcommon.h
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef PIN_CALLBACK_DEF_H_
22 #define PIN_CALLBACK_DEF_H_
23
24 #include "securevirtualresourcetypes.h"
25 #include "casecurityinterface.h"
26
27 #ifdef __cplusplus
28  extern "C" {
29 #endif // __cplusplus
30
31 #define OXM_RANDOM_PIN_DEFAULT_SIZE (8)
32 #define OXM_RANDOM_PIN_DEFAULT_PIN_TYPE (NUM_PIN | LOWERCASE_CHAR_PIN | UPPERCASE_CHAR_PIN)
33 #define OXM_RANDOM_PIN_MIN_SIZE (4)
34 #define OXM_RANDOM_PIN_MAX_SIZE (32)
35 #define OXM_PRECONFIG_PIN_MAX_SIZE (OXM_RANDOM_PIN_MAX_SIZE)
36
37 /** Number of PIN type */
38 #define OXM_PIN_TYPE_COUNT 3
39
40 /**
41  * PIN type definition.
42  * This type supports multiple bit set.
43  * e.g.) NUM_PIN | UPPERCASE_CHAR_PIN
44  */
45 typedef enum OicSecPinType{
46     NUM_PIN            = (0x1 << 0),    //Numeric PIN
47     UPPERCASE_CHAR_PIN = (0x1 << 1),    //uppercase character PIN
48     LOWERCASE_CHAR_PIN = (0x1 << 2)     //lowercase character PIN
49 }OicSecPinType_t;
50
51 /**
52  * Function pointer to print pin code.
53  */
54 typedef void (*GeneratePinCallback)(char* pinData, size_t pinSize);
55
56 /**
57  * Function pointer to input pin code.
58  */
59 typedef void (*InputPinCallback)(char* pinBuf, size_t bufSize);
60
61 /**
62  * Function to setting generate PIN callback from user.
63  *
64  * @param pinCB implementation of generate PIN callback.
65  */
66 void SetGeneratePinCB(GeneratePinCallback pinCB);
67
68 /**
69  * Function to setting input PIN callback from user.
70  *
71  * @param pinCB implementation of input PIN callback.
72  */
73 void SetInputPinCB(InputPinCallback pinCB);
74
75 #ifdef _ENABLE_MULTIPLE_OWNER_
76 /**
77  * Function to save the preconfig PIN getter from user.
78  *
79  * @param pinCB implementation of preconfig PIN function.
80  */
81 void SetGetPreconfigPinCB(InputPinCallback pinCB);
82 #endif //_ENABLE_MULTIPLE_OWNER_
83
84 /**
85  * Function to unset the input PIN callback.
86  * NOTE : Do not call this function while PIN based ownership transfer.
87  */
88 void UnsetInputPinCB();
89
90 /**
91  * Function to unset the PIN generation callback.
92  * NOTE : Do not call this function while PIN based ownership transfer.
93  */
94 void UnsetGeneratePinCB();
95
96 /**
97  * Function to generate random PIN.
98  * This function will send generated PIN to user via callback.
99  *
100  * @param pinBuffer is the reference to the buffer to store the generated PIN data.
101  * @param bufferSize is the size of buffer.
102  *
103  * @return ::OC_STACK_OK in case of success or other value in case of error.
104  */
105 OCStackResult GeneratePin(char* pinBuffer, size_t bufferSize);
106
107 /**
108  * Function to input PIN callback via input callback.
109  *
110  * @param[in,out] pinBuffer is the reference to the buffer to store the inputed PIN data.
111  * @param[in] bufferSize is the size of buffer.
112  *
113  * @return ::OC_STACK_OK in case of success or other value in ccase of error.
114  */
115 OCStackResult InputPin(char* pinBuffer, size_t bufferSize);
116
117 #ifdef _ENABLE_MULTIPLE_OWNER_
118 /**
119  * Function to save the Pre-configured PIN.
120  *
121  * @param[in] pinBuffer PIN data
122  * @param[in] pinLength byte length of PIN
123  *
124  * @return ::OC_STACK_SUCCESS in case of success or other value in ccase of error.
125  */
126 OCStackResult SetPreconfigPin(const char* pinBuffer, size_t pinLength);
127
128 /**
129  * Function to read preconfig PIN.
130  *
131  * @param[in,out] pinBuffer is the reference to the buffer to store the preconfigured PIN.
132  * @param[in] bufferSize is the size of buffer.
133  *
134  * @return ::OC_STACK_SUCCESS in case of success or other value in ccase of error.
135  */
136 OCStackResult GetPreconfigPin(char* pinBuffer, size_t bufferSize);
137 #endif
138
139 /**
140  * Function to setting the policy for random PIN generation
141  *
142  * @param[in] pinSize Byte length of random PIN
143  * @param[in] pinType Type of random PIN (ref OicSecPinType)
144  *
145  * @return ::OC_STACK_OK in case of success or other value in case of error.
146  */
147 OCStackResult SetRandomPinPolicy(size_t pinSize, OicSecPinType_t pinType);
148
149 #ifdef __WITH_DTLS__
150
151 /**
152  * This function is used by OTM and SRM to
153  * register device UUID is required to derive the temporal PSK.
154  */
155 void SetUuidForPinBasedOxm(const OicUuid_t* uuid);
156
157 /**
158  * This internal callback is used while Random PIN based OTM.
159  * This callback will be used to establish a temporary secure session according to
160  * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256.
161  *
162  * @param[in]  type type of PSK data required by tinyDTLS layer during DTLS handshake.
163  * @param[in]  UNUSED1 UNUSED.
164  * @param[in]  UNUSED2 UNUSED.
165  * @param[out] result  Must be filled with the requested information.
166  * @param[in]  result_length  Maximum size of @p result.
167  *
168  * @return The number of bytes written to @p result or a value
169  *         less than zero on error.
170  */
171 int32_t GetDtlsPskForRandomPinOxm( CADtlsPskCredType_t type,
172               const unsigned char *UNUSED1, size_t UNUSED2,
173               unsigned char *result, size_t result_length);
174
175 #ifdef _ENABLE_MULTIPLE_OWNER_
176 /**
177  * This internal callback is used while Random PIN based MOT.
178  * This callback will be used to establish a temporary secure session according to
179  * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256.
180  *
181  * @param[in]  type type of PSK data required by tinyDTLS layer during DTLS handshake.
182  * @param[in]  UNUSED1 UNUSED.
183  * @param[in]  UNUSED2 UNUSED.
184  * @param[out] result  Must be filled with the requested information.
185  * @param[in]  result_length  Maximum size of @p result.
186  *
187  * @return The number of bytes written to @p result or a value
188  *         less than zero on error.
189  */
190 int32_t GetDtlsPskForMotRandomPinOxm( CADtlsPskCredType_t type,
191               const unsigned char *UNUSED1, size_t UNUSED2,
192               unsigned char *result, size_t result_length);
193
194
195 /**
196  * This internal callback is used while Preconfigured-PIN OTM.
197  * This callback will be used to establish a temporary secure session according to
198  * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256.
199  *
200  * @param[in]  type type of PSK data required by tinyDTLS layer during DTLS handshake.
201  * @param[in]  UNUSED1 UNUSED.
202  * @param[in]  UNUSED2 UNUSED.
203  * @param[out] result  Must be filled with the requested information.
204  * @param[in]  result_length  Maximum size of @p result.
205  *
206  * @return The number of bytes written to @p result or a value
207  *         less than zero on error.
208  */
209 int32_t GetDtlsPskForPreconfPinOxm( CADtlsPskCredType_t type,
210               const unsigned char *UNUSED1, size_t UNUSED2,
211               unsigned char *result, size_t result_length);
212
213
214 /**
215  * This internal callback is used while Preconfigured-PIN MOT.
216  * This callback will be used to establish a temporary secure session according to
217  * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256.
218  *
219  * @param[in]  type type of PSK data required by tinyDTLS layer during DTLS handshake.
220  * @param[in]  UNUSED1 UNUSED.
221  * @param[in]  UNUSED2 UNUSED.
222  * @param[out] result  Must be filled with the requested information.
223  * @param[in]  result_length  Maximum size of @p result.
224  *
225  * @return The number of bytes written to @p result or a value
226  *         less than zero on error.
227  */
228 int32_t GetDtlsPskForMotPreconfPinOxm( CADtlsPskCredType_t type,
229               const unsigned char *UNUSED1, size_t UNUSED2,
230               unsigned char *result, size_t result_length);
231
232 #endif //_ENABLE_MULTIPLE_OWNER_
233
234
235 /**
236  * API to derive the PSK based on PIN and new device's UUID.
237  * New device's UUID should be set through SetUuidForPinBasedOxm() API before this API is invoked.
238  *
239  * @param[out] result generated PSK
240  *
241  * @return 0 for success, otherwise error.
242  */
243 int DerivePSKUsingPIN(uint8_t* result);
244
245 #endif //__WITH_DTLS__
246
247 #ifdef __cplusplus
248 }
249 #endif
250
251 #endif //PIN_CALLBACK_DEF_H_