blob: 60f80a6380fdb5a75f4024534fb7856a7f0f7ec3 [file] [log] [blame]
Vitaly Buka45dc9df2015-12-07 21:30:19 -08001// 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 Buka6a8bd5d2015-12-08 21:06:59 -080013 * byte strings, text strings, and arrays.
Vitaly Buka45dc9df2015-12-07 21:30:19 -080014 */
15
16#include <stdbool.h>
17#include <stddef.h>
18#include <stdint.h>
19
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080020#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,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080028 size_t* first_item_len);
29
30bool uw_macaroon_encoding_encode_uint_(const uint32_t unsigned_int,
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080031 uint8_t* buffer,
32 size_t buffer_size,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080033 size_t* resulting_cbor_len);
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -080034bool uw_macaroon_encoding_encode_array_len_(const uint32_t array_len,
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080035 uint8_t* buffer,
36 size_t buffer_size,
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -080037 size_t* resulting_cbor_len);
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080038bool 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 Buka45dc9df2015-12-07 21:30:19 -080042 size_t* resulting_cbor_len);
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080043bool 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 Buka45dc9df2015-12-07 21:30:19 -080047 size_t* resulting_cbor_len);
48
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080049/** 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,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080057 uint32_t* unsigned_int);
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -080058bool uw_macaroon_encoding_decode_array_len_(const uint8_t* cbor,
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080059 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,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080063 const uint8_t** str,
64 size_t* str_len);
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080065bool uw_macaroon_encoding_decode_text_str_(const uint8_t* cbor,
66 size_t cbor_len,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080067 const uint8_t** str,
68 size_t* str_len);
69
70#endif // UW_LIBUWEAVE_SRC_MACAROON_ENCODING_