1 /* utils.h - TinyCrypt interface to platform-dependent run-time operations */
2
3 /*
4 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Intel Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /**
34 * @file
35 * @brief Interface to platform-dependent run-time operations.
36 *
37 */
38
39 #ifndef __BLE_MESH_TC_UTILS_H__
40 #define __BLE_MESH_TC_UTILS_H__
41
42 #include <stdint.h>
43 #include <stddef.h>
44 #include <string.h>
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /**
51 * @brief Copy the the buffer 'from' to the buffer 'to'.
52 * @return returns TC_CRYPTO_SUCCESS (1)
53 * returns TC_CRYPTO_FAIL (0) if:
54 * from_len > to_len.
55 *
56 * @param to OUT -- destination buffer
57 * @param to_len IN -- length of destination buffer
58 * @param from IN -- origin buffer
59 * @param from_len IN -- length of origin buffer
60 */
61 unsigned int _copy(uint8_t *to, unsigned int to_len,
62 const uint8_t *from, unsigned int from_len);
63
64 /**
65 * @brief Set the value 'val' into the buffer 'to', 'len' times.
66 *
67 * @param to OUT -- destination buffer
68 * @param val IN -- value to be set in 'to'
69 * @param len IN -- number of times the value will be copied
70 */
71 void _set(void *to, uint8_t val, unsigned int len);
72
73 /**
74 * @brief Set the value 'val' into the buffer 'to', 'len' times, in a way
75 * which does not risk getting optimized out by the compiler
76 * In cases where the compiler does not set __GNUC__ and where the
77 * optimization level removes the memset, it may be necessary to
78 * implement a _set_secure function and define the
79 * TINYCRYPT_ARCH_HAS_SET_SECURE, which then can ensure that the
80 * memset does not get optimized out.
81 *
82 * @param to OUT -- destination buffer
83 * @param val IN -- value to be set in 'to'
84 * @param len IN -- number of times the value will be copied
85 */
86 #ifdef TINYCRYPT_ARCH_HAS_SET_SECURE
87 extern void _set_secure(void *to, uint8_t val, unsigned int len);
88 #else /* ! TINYCRYPT_ARCH_HAS_SET_SECURE */
_set_secure(void * to,uint8_t val,unsigned int len)89 static inline void _set_secure(void *to, uint8_t val, unsigned int len)
90 {
91 (void) memset(to, val, len);
92 #ifdef __GNUC__
93 __asm__ __volatile__("" :: "g"(to) : "memory");
94 #endif /* __GNUC__ */
95 }
96 #endif /* TINYCRYPT_ARCH_HAS_SET_SECURE */
97
98 /*
99 * @brief AES specific doubling function, which utilizes
100 * the finite field used by AES.
101 * @return Returns a^2
102 *
103 * @param a IN/OUT -- value to be doubled
104 */
105 uint8_t _double_byte(uint8_t a);
106
107 /*
108 * @brief Constant-time algorithm to compare if two sequences of bytes are equal
109 * @return Returns 0 if equal, and non-zero otherwise
110 *
111 * @param a IN -- sequence of bytes a
112 * @param b IN -- sequence of bytes b
113 * @param size IN -- size of sequences a and b
114 */
115 int _compare(const uint8_t *a, const uint8_t *b, size_t size);
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #endif /* __BLE_MESH_TC_UTILS_H__ */
122