1c87c5fbaSopenharmony_ci/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2c87c5fbaSopenharmony_ci
3c87c5fbaSopenharmony_ci/*
4c87c5fbaSopenharmony_ci * Copyright (c) 2018, SICS, RISE AB
5c87c5fbaSopenharmony_ci * All rights reserved.
6c87c5fbaSopenharmony_ci *
7c87c5fbaSopenharmony_ci * Redistribution and use in source and binary forms, with or without
8c87c5fbaSopenharmony_ci * modification, are permitted provided that the following conditions
9c87c5fbaSopenharmony_ci * are met:
10c87c5fbaSopenharmony_ci * 1. Redistributions of source code must retain the above copyright
11c87c5fbaSopenharmony_ci *    notice, this list of conditions and the following disclaimer.
12c87c5fbaSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
13c87c5fbaSopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
14c87c5fbaSopenharmony_ci *    documentation and/or other materials provided with the distribution.
15c87c5fbaSopenharmony_ci * 3. Neither the name of the Institute nor the names of its contributors
16c87c5fbaSopenharmony_ci *    may be used to endorse or promote products derived from this software
17c87c5fbaSopenharmony_ci *    without specific prior written permission.
18c87c5fbaSopenharmony_ci *
19c87c5fbaSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
20c87c5fbaSopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21c87c5fbaSopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22c87c5fbaSopenharmony_ci * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
23c87c5fbaSopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24c87c5fbaSopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25c87c5fbaSopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26c87c5fbaSopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27c87c5fbaSopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28c87c5fbaSopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29c87c5fbaSopenharmony_ci * SUCH DAMAGE.
30c87c5fbaSopenharmony_ci *
31c87c5fbaSopenharmony_ci */
32c87c5fbaSopenharmony_ci
33c87c5fbaSopenharmony_ci/**
34c87c5fbaSopenharmony_ci * @file oscore.h
35c87c5fbaSopenharmony_ci * @brief An implementation of the Object Security for Constrained RESTful
36c87c5fbaSopenharmony_ci * Environments (RFC 8613).
37c87c5fbaSopenharmony_ci *
38c87c5fbaSopenharmony_ci * \author Martin Gunnarsson  <martin.gunnarsson@ri.se>
39c87c5fbaSopenharmony_ci * major rewrite for libcoap
40c87c5fbaSopenharmony_ci *      Peter van der Stok <consultancy@vanderstok.org>
41c87c5fbaSopenharmony_ci *      on request of Fairhair alliance
42c87c5fbaSopenharmony_ci * adapted for libcoap integration
43c87c5fbaSopenharmony_ci *      Jon Shallow <supjps-libcoap@jpshallow.com>
44c87c5fbaSopenharmony_ci *
45c87c5fbaSopenharmony_ci */
46c87c5fbaSopenharmony_ci
47c87c5fbaSopenharmony_ci#ifndef _OSCORE_H
48c87c5fbaSopenharmony_ci#define _OSCORE_H
49c87c5fbaSopenharmony_ci
50c87c5fbaSopenharmony_ci#include <coap3/coap_internal.h>
51c87c5fbaSopenharmony_ci#include "oscore_cose.h"
52c87c5fbaSopenharmony_ci#include "oscore_context.h"
53c87c5fbaSopenharmony_ci
54c87c5fbaSopenharmony_ci/**
55c87c5fbaSopenharmony_ci * @ingroup internal_api
56c87c5fbaSopenharmony_ci * @addtogroup oscore_internal
57c87c5fbaSopenharmony_ci * @{
58c87c5fbaSopenharmony_ci */
59c87c5fbaSopenharmony_ci
60c87c5fbaSopenharmony_ci/* Estimate your header size, especially when using Proxy-Uri. */
61c87c5fbaSopenharmony_ci#define COAP_MAX_HEADER_SIZE 70
62c87c5fbaSopenharmony_ci
63c87c5fbaSopenharmony_ci/* OSCORE error messages  (to be moved elsewhere  */
64c87c5fbaSopenharmony_ci#define OSCORE_DECRYPTION_ERROR    100
65c87c5fbaSopenharmony_ci#define PACKET_SERIALIZATION_ERROR 102
66c87c5fbaSopenharmony_ci
67c87c5fbaSopenharmony_ci/* oscore_cs_params
68c87c5fbaSopenharmony_ci * returns cbor array [[param_type], [paramtype, param]]
69c87c5fbaSopenharmony_ci */
70c87c5fbaSopenharmony_ciuint8_t *oscore_cs_params(int8_t param, int8_t param_type, size_t *len);
71c87c5fbaSopenharmony_ci
72c87c5fbaSopenharmony_ci/* oscore_cs_key_params
73c87c5fbaSopenharmony_ci * returns cbor array [paramtype, param]
74c87c5fbaSopenharmony_ci */
75c87c5fbaSopenharmony_ciuint8_t *oscore_cs_key_params(cose_curve_t param, int8_t param_type, size_t *len);
76c87c5fbaSopenharmony_ci
77c87c5fbaSopenharmony_ci/*
78c87c5fbaSopenharmony_ci * oscore_encode_option_value
79c87c5fbaSopenharmony_ci */
80c87c5fbaSopenharmony_cisize_t oscore_encode_option_value(uint8_t *option_buffer,
81c87c5fbaSopenharmony_ci                                  size_t option_buf_len,
82c87c5fbaSopenharmony_ci                                  cose_encrypt0_t *cose,
83c87c5fbaSopenharmony_ci                                  uint8_t group,
84c87c5fbaSopenharmony_ci                                  uint8_t appendix_b_2);
85c87c5fbaSopenharmony_ci
86c87c5fbaSopenharmony_ci/*
87c87c5fbaSopenharmony_ci * Decodes the OSCORE option value and places decoded values into the provided
88c87c5fbaSopenharmony_ci * cose structure */
89c87c5fbaSopenharmony_ciint oscore_decode_option_value(const uint8_t *option_value,
90c87c5fbaSopenharmony_ci                               size_t option_len,
91c87c5fbaSopenharmony_ci                               cose_encrypt0_t *cose);
92c87c5fbaSopenharmony_ci
93c87c5fbaSopenharmony_ci/* Creates AAD, creates External AAD and serializes it into the complete AAD
94c87c5fbaSopenharmony_ci * structure. Returns serialized size. */
95c87c5fbaSopenharmony_cisize_t oscore_prepare_aad(const uint8_t *external_aad_buffer,
96c87c5fbaSopenharmony_ci                          size_t external_aad_len,
97c87c5fbaSopenharmony_ci                          uint8_t *aad_buffer,
98c87c5fbaSopenharmony_ci                          size_t aad_size);
99c87c5fbaSopenharmony_ci
100c87c5fbaSopenharmony_cisize_t oscore_prepare_e_aad(oscore_ctx_t *ctx,
101c87c5fbaSopenharmony_ci                            cose_encrypt0_t *cose,
102c87c5fbaSopenharmony_ci                            const uint8_t *oscore_option,
103c87c5fbaSopenharmony_ci                            size_t oscore_option_len,
104c87c5fbaSopenharmony_ci                            coap_bin_const_t *sender_public_key,
105c87c5fbaSopenharmony_ci                            uint8_t *external_aad_ptr,
106c87c5fbaSopenharmony_ci                            size_t external_aad_size);
107c87c5fbaSopenharmony_ci
108c87c5fbaSopenharmony_ci/* Creates Nonce */
109c87c5fbaSopenharmony_civoid oscore_generate_nonce(cose_encrypt0_t *ptr,
110c87c5fbaSopenharmony_ci                           oscore_ctx_t *ctx,
111c87c5fbaSopenharmony_ci                           uint8_t *buffer,
112c87c5fbaSopenharmony_ci                           uint8_t size);
113c87c5fbaSopenharmony_ci
114c87c5fbaSopenharmony_ci/*Return 1 if OK, Error code otherwise */
115c87c5fbaSopenharmony_ciuint8_t oscore_validate_sender_seq(oscore_recipient_ctx_t *ctx,
116c87c5fbaSopenharmony_ci                                   cose_encrypt0_t *cose);
117c87c5fbaSopenharmony_ci
118c87c5fbaSopenharmony_ci/* Return 0 if SEQ MAX, return 1 if OK */
119c87c5fbaSopenharmony_ciuint8_t oscore_increment_sender_seq(oscore_ctx_t *ctx);
120c87c5fbaSopenharmony_ci
121c87c5fbaSopenharmony_ci/* Restore the sequence number and replay-window to the previous state. This is
122c87c5fbaSopenharmony_ci * to be used when decryption fail. */
123c87c5fbaSopenharmony_civoid oscore_roll_back_seq(oscore_recipient_ctx_t *ctx);
124c87c5fbaSopenharmony_ci
125c87c5fbaSopenharmony_ci/** @} */
126c87c5fbaSopenharmony_ci
127c87c5fbaSopenharmony_ci#endif /* _OSCORE_H */
128