xref: /aosp_15_r20/external/open-dice/include/dice/ops/trait/cose.h (revision 60b67249c2e226f42f35cc6cfe66c6048e0bae6b)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #ifndef DICE_OPS_TRAIT_COSE_H_
16 #define DICE_OPS_TRAIT_COSE_H_
17 
18 #include <dice/config.h>
19 #include <dice/dice.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 // These functions may optionally be implemented by a COSE based integration.
24 // They aren't directly depended on by the main DICE functions but provide
25 // extra utilities that can be used as part of the integration.
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 // Encodes a public key into |buffer| as a COSE_Key structure. On success,
32 // |encoded_size| is set to the number of bytes used. If
33 // kDiceResultBufferTooSmall is returned |encoded_size| will be set to the
34 // required size of the buffer.
35 DiceResult DiceCoseEncodePublicKey(
36     void* context, DicePrincipal principal,
37     const uint8_t public_key[DICE_PUBLIC_KEY_BUFFER_SIZE], size_t buffer_size,
38     uint8_t* buffer, size_t* encoded_size);
39 
40 // Signs the payload and additional authenticated data, formatting the result
41 // into a COSE_Sign1 structure. There are no unprotected attributes included in
42 // the result.
43 //
44 // |buffer| is used to hold the intermediate To-Be-Signed (TBS) structure and
45 // then the final result. On success, |encoded_size| is set to the size of the
46 // final result in |buffer|. If kDiceResultBufferTooSmall is returned,
47 // |encoded_size| will be set to the required size of the buffer.
48 DiceResult DiceCoseSignAndEncodeSign1(
49     void* context, const uint8_t* payload, size_t payload_size,
50     const uint8_t* aad, size_t aad_size,
51     const uint8_t private_key[DICE_PRIVATE_KEY_BUFFER_SIZE], size_t buffer_size,
52     uint8_t* buffer, size_t* encoded_size);
53 
54 #ifdef __cplusplus
55 }  // extern "C"
56 #endif
57 
58 #endif  // DICE_OPS_TRAIT_COSE_H_
59