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