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),
13 * byte strings, text strings, and arrays.
14 */
15
16#include <stdbool.h>
17#include <stddef.h>
18#include <stdint.h>
19
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 */
26bool uw_macaroon_encoding_get_item_len_(const uint8_t* cbor,
27                                        size_t cbor_len,
28                                        size_t* first_item_len);
29
30bool uw_macaroon_encoding_encode_uint_(const uint32_t unsigned_int,
31                                       uint8_t* buffer,
32                                       size_t buffer_size,
33                                       size_t* resulting_cbor_len);
34bool uw_macaroon_encoding_encode_array_len_(const uint32_t array_len,
35                                            uint8_t* buffer,
36                                            size_t buffer_size,
37                                            size_t* resulting_cbor_len);
38bool uw_macaroon_encoding_encode_byte_str_(const uint8_t* str,
39                                           size_t str_len,
40                                           uint8_t* buffer,
41                                           size_t buffer_size,
42                                           size_t* resulting_cbor_len);
43bool uw_macaroon_encoding_encode_text_str_(const uint8_t* str,
44                                           size_t str_len,
45                                           uint8_t* buffer,
46                                           size_t buffer_size,
47                                           size_t* resulting_cbor_len);
48
49/** Only encode the header (major type and length) of the byte string */
50bool 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
55bool uw_macaroon_encoding_decode_uint_(const uint8_t* cbor,
56                                       size_t cbor_len,
57                                       uint32_t* unsigned_int);
58bool uw_macaroon_encoding_decode_array_len_(const uint8_t* cbor,
59                                            size_t cbor_len,
60                                            uint32_t* array_len);
61bool uw_macaroon_encoding_decode_byte_str_(const uint8_t* cbor,
62                                           size_t cbor_len,
63                                           const uint8_t** str,
64                                           size_t* str_len);
65bool uw_macaroon_encoding_decode_text_str_(const uint8_t* cbor,
66                                           size_t cbor_len,
67                                           const uint8_t** str,
68                                           size_t* str_len);
69
70#endif  // UW_LIBUWEAVE_SRC_MACAROON_ENCODING_
71