| 1 | /* Copyright 2015, 2016 OpenMarket Ltd |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #include <cstddef> |
| 16 | #include <cstdint> |
| 17 | #include <cstring> |
| 18 | #include <iomanip> |
| 19 | #include <iostream> |
| 20 | #include <sstream> |
| 21 | #include <type_traits> |
| 22 | |
| 23 | namespace olm { |
| 24 | |
| 25 | /** Clear the memory held in the buffer */ |
| 26 | void unset( |
| 27 | void volatile * buffer, std::size_t buffer_length |
| 28 | ); |
| 29 | |
| 30 | /** Clear the memory backing an object */ |
| 31 | template<typename T> |
| 32 | void unset(T & value) { |
| 33 | unset(buffer: reinterpret_cast<void volatile *>(&value), buffer_length: sizeof(T)); |
| 34 | } |
| 35 | |
| 36 | /** Check if two buffers are equal in constant time. */ |
| 37 | bool is_equal( |
| 38 | std::uint8_t const * buffer_a, |
| 39 | std::uint8_t const * buffer_b, |
| 40 | std::size_t length |
| 41 | ); |
| 42 | |
| 43 | /** Check if two fixed size arrays are equals */ |
| 44 | template<typename T> |
| 45 | bool array_equal( |
| 46 | T const & array_a, |
| 47 | T const & array_b |
| 48 | ) { |
| 49 | static_assert( |
| 50 | std::is_array<T>::value |
| 51 | && std::is_convertible<T, std::uint8_t *>::value |
| 52 | && sizeof(T) > 0, |
| 53 | "Arguments to array_equal must be std::uint8_t arrays[]." |
| 54 | ); |
| 55 | return is_equal(array_a, array_b, sizeof(T)); |
| 56 | } |
| 57 | |
| 58 | /** Copy into a fixed size array */ |
| 59 | template<typename T> |
| 60 | std::uint8_t const * load_array( |
| 61 | T & destination, |
| 62 | std::uint8_t const * source |
| 63 | ) { |
| 64 | static_assert( |
| 65 | std::is_array<T>::value |
| 66 | && std::is_convertible<T, std::uint8_t *>::value |
| 67 | && sizeof(T) > 0, |
| 68 | "The first argument to load_array must be a std::uint8_t array[]." |
| 69 | ); |
| 70 | std::memcpy(dest: destination, src: source, n: sizeof(T)); |
| 71 | return source + sizeof(T); |
| 72 | } |
| 73 | |
| 74 | /** Copy from a fixed size array */ |
| 75 | template<typename T> |
| 76 | std::uint8_t * store_array( |
| 77 | std::uint8_t * destination, |
| 78 | T const & source |
| 79 | ) { |
| 80 | static_assert( |
| 81 | std::is_array<T>::value |
| 82 | && std::is_convertible<T, std::uint8_t *>::value |
| 83 | && sizeof(T) > 0, |
| 84 | "The second argument to store_array must be a std::uint8_t array[]." |
| 85 | ); |
| 86 | std::memcpy(dest: destination, src: source, n: sizeof(T)); |
| 87 | return destination + sizeof(T); |
| 88 | } |
| 89 | |
| 90 | } // namespace olm |
| 91 | |