Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UW_LIBUWEAVE_SRC_MACAROON_ENCODING_ |
| 6 | #define UW_LIBUWEAVE_SRC_MACAROON_ENCODING_ |
| 7 | |
| 8 | /* |
| 9 | * Utility functions to encode and decode canonical CBOR representations for |
| 10 | * cryptographic use, such as signatures. We only need to support a very small |
| 11 | * subset of the CBOR standard, since only these are used in our cryptographic |
| 12 | * designs. The supported data types are: unsigned integers (maximum 32 bits), |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 13 | * byte strings, text strings, and arrays. |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdbool.h> |
| 17 | #include <stddef.h> |
| 18 | #include <stdint.h> |
| 19 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 20 | #define UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN 5 |
| 21 | |
| 22 | /** |
| 23 | * Gets the number of bytes that is occupied by the first data item in the give |
| 24 | * CBOR string. |
| 25 | */ |
| 26 | bool uw_macaroon_encoding_get_item_len_(const uint8_t* cbor, |
| 27 | size_t cbor_len, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 28 | size_t* first_item_len); |
| 29 | |
| 30 | bool uw_macaroon_encoding_encode_uint_(const uint32_t unsigned_int, |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 31 | uint8_t* buffer, |
| 32 | size_t buffer_size, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 33 | size_t* resulting_cbor_len); |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 34 | bool uw_macaroon_encoding_encode_array_len_(const uint32_t array_len, |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 35 | uint8_t* buffer, |
| 36 | size_t buffer_size, |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 37 | size_t* resulting_cbor_len); |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 38 | bool uw_macaroon_encoding_encode_byte_str_(const uint8_t* str, |
| 39 | size_t str_len, |
| 40 | uint8_t* buffer, |
| 41 | size_t buffer_size, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 42 | size_t* resulting_cbor_len); |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 43 | bool uw_macaroon_encoding_encode_text_str_(const uint8_t* str, |
| 44 | size_t str_len, |
| 45 | uint8_t* buffer, |
| 46 | size_t buffer_size, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 47 | size_t* resulting_cbor_len); |
| 48 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 49 | /** Only encode the header (major type and length) of the byte string */ |
| 50 | bool uw_macaroon_encoding_encode_byte_str_len_(size_t str_len, |
| 51 | uint8_t* buffer, |
| 52 | size_t buffer_size, |
| 53 | size_t* resulting_cbor_len); |
| 54 | |
| 55 | bool uw_macaroon_encoding_decode_uint_(const uint8_t* cbor, |
| 56 | size_t cbor_len, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 57 | uint32_t* unsigned_int); |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 58 | bool uw_macaroon_encoding_decode_array_len_(const uint8_t* cbor, |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 59 | size_t cbor_len, |
| 60 | uint32_t* array_len); |
| 61 | bool uw_macaroon_encoding_decode_byte_str_(const uint8_t* cbor, |
| 62 | size_t cbor_len, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 63 | const uint8_t** str, |
| 64 | size_t* str_len); |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 65 | bool uw_macaroon_encoding_decode_text_str_(const uint8_t* cbor, |
| 66 | size_t cbor_len, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 67 | const uint8_t** str, |
| 68 | size_t* str_len); |
| 69 | |
| 70 | #endif // UW_LIBUWEAVE_SRC_MACAROON_ENCODING_ |