t_cose
Loading...
Searching...
No Matches
t_cose_sign1_sign.h
Go to the documentation of this file.
1/*
2 * t_cose_sign1_sign.h
3 *
4 * Copyright (c) 2018-2021, Laurence Lundblade. All rights reserved.
5 * Copyright (c) 2020, Michael Eckel
6 *
7 * SPDX-License-Identifier: BSD-3-Clause
8 *
9 * See BSD-3-Clause license in README.md
10 */
11
12#ifndef __T_COSE_SIGN1_H__
13#define __T_COSE_SIGN1_H__
14
15#include <stdint.h>
16#include <stdbool.h>
17#include "qcbor/qcbor.h"
18#include "t_cose/q_useful_buf.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25
70 /* Private data structure */
71 struct q_useful_buf_c protected_parameters; /* Encoded protected paramssy */
72 int32_t cose_algorithm_id;
73 struct t_cose_key signing_key;
74 uint32_t option_flags;
75 struct q_useful_buf_c kid;
76#ifndef T_COSE_DISABLE_CONTENT_TYPE
77 uint32_t content_type_uint;
78 const char * content_type_tstr;
79#endif
80
81#ifndef T_COSE_DISABLE_EDDSA
87 struct q_useful_buf auxiliary_buffer;
88
89 /* The size of the serialized Sig_Structure used in the last
90 * signing operation. This can be used by the user to determine
91 * a suitable auxiliary buffer size.
92 */
93 size_t auxiliary_buffer_size;
94#endif
95};
96
97
120#define T_COSE_OPT_SHORT_CIRCUIT_SIG 0x00000001
121
122
133#define T_COSE_OPT_OMIT_CBOR_TAG 0x00000002
134
135
160static void
162 uint32_t option_flags,
163 int32_t cose_algorithm_id);
164
165
184static void
186 struct t_cose_key signing_key,
187 struct q_useful_buf_c kid);
188
210static void
212 struct q_useful_buf auxiliary_buffer);
213
233static size_t
235
236
237
238#ifndef T_COSE_DISABLE_CONTENT_TYPE
254static inline void
256 uint16_t content_type);
257
274static inline void
276 const char *content_type);
277#endif /* T_COSE_DISABLE_CONTENT_TYPE */
278
279
280
328static enum t_cose_err_t
330 struct q_useful_buf_c payload,
331 struct q_useful_buf out_buf,
332 struct q_useful_buf_c *result);
333
334
354static enum t_cose_err_t
356 struct q_useful_buf_c aad,
357 struct q_useful_buf_c payload,
358 struct q_useful_buf out_buf,
359 struct q_useful_buf_c *result);
360
361
382static enum t_cose_err_t
384 struct q_useful_buf_c aad,
385 struct q_useful_buf_c detached_payload,
386 struct q_useful_buf out_buf,
387 struct q_useful_buf_c *result);
388
389
390
426static enum t_cose_err_t
428 QCBOREncodeContext *cbor_encode_ctx);
429
430
447static enum t_cose_err_t
449 QCBOREncodeContext *cbor_encode_ctx);
450
451
471static inline enum t_cose_err_t
473 struct q_useful_buf_c aad,
474 QCBOREncodeContext *cbor_encode_ctx);
475
476
477
478
479
480
481/* ------------------------------------------------------------------------
482 * Inline implementations of public functions defined above.
483 */
484static inline void
486 uint32_t option_flags,
487 int32_t cose_algorithm_id)
488{
489 memset(me, 0, sizeof(*me));
490#ifndef T_COSE_DISABLE_CONTENT_TYPE
491 me->content_type_uint = T_COSE_EMPTY_UINT_CONTENT_TYPE;
492#endif
493
494 me->cose_algorithm_id = cose_algorithm_id;
495 me->option_flags = option_flags;
496
497#ifndef T_COSE_DISABLE_EDDSA
498 /* Start with large (but NULL) auxiliary buffer. If EdDSA is used,
499 * the Sig_Structure data will be serialized here.
500 */
501 me->auxiliary_buffer.len = SIZE_MAX;
502#endif
503}
504
505
506static inline void
508 struct t_cose_key signing_key,
509 struct q_useful_buf_c kid)
510{
511 me->kid = kid;
512 me->signing_key = signing_key;
513}
514
515static inline void
517 struct q_useful_buf auxiliary_buffer)
518{
519#ifndef T_COSE_DISABLE_EDDSA
520 me->auxiliary_buffer = auxiliary_buffer;
521#else
522 (void)me;
523 (void)auxiliary_buffer;
524#endif
525}
526
527static inline size_t
529{
530#ifndef T_COSE_DISABLE_EDDSA
531 return me->auxiliary_buffer_size;
532#else
533 /* If EdDSA is disabled we don't ever need an auxiliary buffer. */
534 (void)me;
535 return 0;
536#endif
537}
538
539
555enum t_cose_err_t
557 bool payload_is_detached,
558 QCBOREncodeContext *cbor_encode_ctx);
559
560
561static inline enum t_cose_err_t
563 QCBOREncodeContext *cbor_encode_ctx)
564{
566 false,
567 cbor_encode_ctx);
568}
569
570
591enum t_cose_err_t
593 struct q_useful_buf_c aad,
594 struct q_useful_buf_c detached_payload,
595 QCBOREncodeContext *cbor_encode_ctx);
596
618enum t_cose_err_t
620 bool payload_is_detached,
621 struct q_useful_buf_c aad,
622 struct q_useful_buf_c payload,
623 struct q_useful_buf out_buf,
624 struct q_useful_buf_c *result);
625
626
627static inline enum t_cose_err_t
629 struct q_useful_buf_c aad,
630 struct q_useful_buf_c payload,
631 struct q_useful_buf out_buf,
632 struct q_useful_buf_c *result)
633{
635 false,
636 aad,
637 payload,
638 out_buf,
639 result);
640}
641
642
643static inline enum t_cose_err_t
645 struct q_useful_buf_c payload,
646 struct q_useful_buf out_buf,
647 struct q_useful_buf_c *result)
648{
650 false,
651 payload,
652 NULL_Q_USEFUL_BUF_C,
653 out_buf,
654 result);
655}
656
657
658static inline enum t_cose_err_t
660 struct q_useful_buf_c aad,
661 struct q_useful_buf_c detached_payload,
662 struct q_useful_buf out_buf,
663 struct q_useful_buf_c *result)
664{
666 true,
667 detached_payload,
668 aad,
669 out_buf,
670 result);
671}
672
673
674static inline enum t_cose_err_t
676 struct q_useful_buf_c aad,
677 QCBOREncodeContext *cbor_encode_ctx)
678{
680 aad,
681 NULL_Q_USEFUL_BUF_C,
682 cbor_encode_ctx);
683}
684
685
686static inline enum t_cose_err_t
688 QCBOREncodeContext *cbor_encode_ctx)
689{
691 NULL_Q_USEFUL_BUF_C,
692 NULL_Q_USEFUL_BUF_C,
693 cbor_encode_ctx);
694}
695
696
697#ifndef T_COSE_DISABLE_CONTENT_TYPE
698static inline void
700 uint16_t content_type)
701{
702 me->content_type_uint = content_type;
703}
704
705
706static inline void
708 const char *content_type)
709{
710 me->content_type_tstr = content_type;
711}
712#endif
713
714#ifdef __cplusplus
715}
716#endif
717
718#endif /* __T_COSE_SIGN1_H__ */
This is a TF-M coding style version of UsefulBuf. See UsefulBuf for documentation of these functions.
Definition t_cose_common.h:186
Definition t_cose_sign1_sign.h:69
struct q_useful_buf auxiliary_buffer
Definition t_cose_sign1_sign.h:87
This file contains definitions common to all public t_cose interfaces.
t_cose_err_t
Definition t_cose_common.h:250
#define T_COSE_EMPTY_UINT_CONTENT_TYPE
Definition t_cose_common.h:441
static void t_cose_sign1_set_signing_key(struct t_cose_sign1_sign_ctx *context, struct t_cose_key signing_key, struct q_useful_buf_c kid)
Set the key and kid (key ID) for signing.
Definition t_cose_sign1_sign.h:507
static enum t_cose_err_t t_cose_sign1_sign(struct t_cose_sign1_sign_ctx *context, struct q_useful_buf_c payload, struct q_useful_buf out_buf, struct q_useful_buf_c *result)
Create and sign a COSE_Sign1 message with a payload in one call.
Definition t_cose_sign1_sign.h:644
enum t_cose_err_t t_cose_sign1_sign_aad_internal(struct t_cose_sign1_sign_ctx *context, bool payload_is_detached, struct q_useful_buf_c aad, struct q_useful_buf_c payload, struct q_useful_buf out_buf, struct q_useful_buf_c *result)
Semi-private function that does a complete signing in one call.
static enum t_cose_err_t t_cose_sign1_sign_detached(struct t_cose_sign1_sign_ctx *context, struct q_useful_buf_c aad, struct q_useful_buf_c detached_payload, struct q_useful_buf out_buf, struct q_useful_buf_c *result)
Create and sign a COSE_Sign1 message with detached payload in one call.
Definition t_cose_sign1_sign.h:659
static enum t_cose_err_t t_cose_sign1_encode_signature(struct t_cose_sign1_sign_ctx *context, QCBOREncodeContext *cbor_encode_ctx)
Finish a COSE_Sign1 message by outputting the signature.
Definition t_cose_sign1_sign.h:687
static void t_cose_sign1_set_content_type_uint(struct t_cose_sign1_sign_ctx *context, uint16_t content_type)
Set the payload content type using CoAP content types.
Definition t_cose_sign1_sign.h:699
static void t_cose_sign1_sign_set_auxiliary_buffer(struct t_cose_sign1_sign_ctx *context, struct q_useful_buf auxiliary_buffer)
Configure an auxiliary buffer used to serialize the Sig_Structure.
Definition t_cose_sign1_sign.h:516
static size_t t_cose_sign1_sign_auxiliary_buffer_size(struct t_cose_sign1_sign_ctx *context)
Get the required auxiliary buffer size for the most recent signing operation.
Definition t_cose_sign1_sign.h:528
static void t_cose_sign1_sign_init(struct t_cose_sign1_sign_ctx *context, uint32_t option_flags, int32_t cose_algorithm_id)
Initialize to start creating a COSE_Sign1.
Definition t_cose_sign1_sign.h:485
static enum t_cose_err_t t_cose_sign1_sign_aad(struct t_cose_sign1_sign_ctx *context, struct q_useful_buf_c aad, struct q_useful_buf_c payload, struct q_useful_buf out_buf, struct q_useful_buf_c *result)
Create and sign a COSE_Sign1 message with a payload in one call.
Definition t_cose_sign1_sign.h:628
enum t_cose_err_t t_cose_sign1_encode_parameters_internal(struct t_cose_sign1_sign_ctx *context, bool payload_is_detached, QCBOREncodeContext *cbor_encode_ctx)
Semi-private function that ouputs the COSE parameters, startng a COSE_Sign1 message.
static void t_cose_sign1_set_content_type_tstr(struct t_cose_sign1_sign_ctx *context, const char *content_type)
Set the payload content type using MIME content types.
Definition t_cose_sign1_sign.h:707
enum t_cose_err_t t_cose_sign1_encode_signature_aad_internal(struct t_cose_sign1_sign_ctx *context, struct q_useful_buf_c aad, struct q_useful_buf_c detached_payload, QCBOREncodeContext *cbor_encode_ctx)
Semi-private function that ouputs the signature, finishing a COSE_Sign1 message.
static enum t_cose_err_t t_cose_sign1_encode_signature_aad(struct t_cose_sign1_sign_ctx *context, struct q_useful_buf_c aad, QCBOREncodeContext *cbor_encode_ctx)
Finish a COSE_Sign1 message with AAD by outputting the signature.
Definition t_cose_sign1_sign.h:675
static enum t_cose_err_t t_cose_sign1_encode_parameters(struct t_cose_sign1_sign_ctx *context, QCBOREncodeContext *cbor_encode_ctx)
Output first part and parameters for a COSE_Sign1 message.
Definition t_cose_sign1_sign.h:562