2 Copyright (c) 2015 Atmel Corporation. All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 1. Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 3. Neither the name of Atmel nor the names of its contributors may be used to
15 endorse or promote products derived from this software without specific
16 prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * @brief DTLS Hardware Abstraction Layer API and visible structures.
35 #ifndef _DTLS_DTLS_HAL_H_
36 #define _DTLS_DTLS_HAL_H_
41 // Define return values for HAL
42 #define HAL_SUCCESS (1)
43 #define HAL_FAILURE (0)
44 #define ENC_KEY_SIZE (32)
47 * This structure contains callback functions used by tinydtls to
48 * provide a hardware abstraction layer for secure storage .
53 * Call this function during application initialization to create HAL related resources.
55 * @param[in] ctx The current DTLS context.
56 * @return HAL_SUCCESS or HAL_FAILURE
58 int (*HAL_init)(struct dtls_context_t *ctx);
61 * Call this function during application shut down to clean up any HAL related resources.
63 * @param[in] ctx The current DTLS context.
64 * @return HAL_SUCCESS or HAL_FAILURE
66 int (*HAL_finish)(struct dtls_context_t *ctx);
69 * Call this function to store unsecured data on hardware.
71 * @param[in] ctx The current DTLS context.
72 * @param[in] p_location_handle The handle to the hardware location to store p_data.
73 * @param[in] p_data The data to store.
74 * @param[in] data_size The size of the data to store.
75 * @return HAL_SUCCESS or HAL_FAILURE
77 int (*HAL_write_mem)(struct dtls_context_t *ctx,
78 const uint8_t* p_location,
79 const uint8_t* p_data,
83 * Call this function to read data from unsecured storage from hardware.
85 * @param[in] ctx The current DTLS context.
86 * @param[in] p_location_handle The handle to the hardware location to read p_data.
87 * @param[in] p_data The data to read.
88 * @param[inout] data_size IN: The size of the buffer to receive data. OUT: The actual number of bytes read.
89 * @return HAL_SUCCESS or HAL_FAILURE
91 int (*HAL_read_mem)(struct dtls_context_t *ctx,
92 const uint8_t* p_location,
97 * Call this function to store sensitive data in secure hardware.
98 * Data will be securely stored and encrypted on the wire
100 * @param[in] ctx The current DTLS context.
101 * @param[in] p_location_handle The handle to the hardware location to store p_data.
102 * @param[in] p_data The data to store securely.
103 * @param[in] data_size The size of the data to store securely.
104 * @return HAL_SUCCESS or HAL_FAILURE
106 int (*HAL_write_mem_enc)(struct dtls_context_t *ctx,
107 const uint8_t* p_location,
108 const uint8_t* p_data,
112 * Call this function to read sensitive data from secure hardware.
113 * Data will be read from secure storage and encrypted on the wire
115 * @param[in] ctx The current DTLS context.
116 * @param[in] p_location_handle The handle to the hardware location to read p_data.
117 * @param[in] p_data The data to read securely.
118 * @param[inout] data_size IN: The size of the buffer to receive data. OUT: The actual number of bytes read.
119 * @return HAL_SUCCESS or HAL_FAILURE
121 int (*HAL_read_mem_enc)(struct dtls_context_t *ctx,
122 const uint8_t* p_location,
127 * Call this function to generate a unique encryption key that will be used for secure storage.
128 * This encryption key should be stored as a platform resource
130 * @param[in] ctx The current DTLS context.
131 * @param[out] p_enc_key_out The internally generated unique encryption key that will be used for secure storage.
132 * This key needs to be stored on the platform.
133 * @return HAL_SUCCESS or HAL_FAILURE
135 int (*HAL_init_enckey)(struct dtls_context_t *ctx,
136 uint8_t p_enc_key_out[ENC_KEY_SIZE]);
139 * Call this function to set a unique encryption key that will be used for secure storage.
140 * This encryption key should be stored as a platform resource
142 * @param[in] ctx The current DTLS context.
143 * @param[in] p_enc_key_in The encryption key that will be used for secure storage.
144 * This key needs to be stored on the platform.
145 * @return HAL_SUCCESS or HAL_FAILURE
147 int (*HAL_set_enckey)(struct dtls_context_t *ctx,
148 const uint8_t p_enc_key_in[ENC_KEY_SIZE]);
151 * This function is called internally from HAL_read_mem_enc and HAL_write_mem_enc.
152 * The platform must implement this function to return the encryption key that was
153 * returned by HAL_init_enckey or HAL_set_enckey.
155 * @param[in] ctx The current DTLS context.
156 * @param[out] p_enc_key_out The encryption key that is used for secure storage.
157 * Retrieve from platform in this function.
158 * @return HAL_SUCCESS or HAL_FAILURE
160 int (*HAL_get_enckey)(struct dtls_context_t *ctx,
161 uint8_t p_enc_key_out[ENC_KEY_SIZE]);
164 * Call this function to use the hardware to calculate a SHA256
166 * @param[in] ctx The current DTLS context.
167 * @param[in] p_msg The message to calculate the SHA256.
168 * @param[in] msg_size The size of the message to hash.
169 * @param[out] p_hash The hash of p_msg.
170 * @return HAL_SUCCESS or HAL_FAILURE
172 int (*HAL_sha2_calc)(struct dtls_context_t *ctx,
175 uint8_t p_hash[SHA256_DIGEST_LENGTH]);
178 * Call this function to use the hardware key derivation function (KDF)
180 * @param[in] ctx The current DTLS context.
181 * @param[in] p_seed The seed used to produce the key.
182 * @param[in] seed_size The size of the message to hash.
183 * @param[out] p_keyout The key produced by the KDF.
184 * @param[in] key_size The size of the key to be returned.
185 * @return HAL_SUCCESS or HAL_FAILURE
187 int (*HAL_kdf_calc)(struct dtls_context_t *ctx,
188 const uint8_t* p_seed,
189 const size_t seed_size,
191 const size_t key_size);
194 * Call this function to retrieve the specified certificate from hardware
196 * @param[in] ctx The current DTLS context.
197 * @param[in] cert_id An index into the certificate to returned.
198 * @param[inout] cert The certificate to be returned.
199 * @param[inout] cert_size The size of the certificate.
200 * @return HAL_SUCCESS or HAL_FAILURE
202 int (*HAL_get_x509_cert)(struct dtls_context_t *ctx,
207 } dtls_hal_handler_t;
210 #endif /* _DTLS_DTLS_HAL_H_ */