| // Copyright 2023 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| // use this file except in compliance with the License. You may obtain a copy of |
| // the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| // License for the specific language governing permissions and limitations under |
| // the License. |
| |
| #include <stddef.h> |
| #include <stdint.h> |
| #include <string.h> |
| |
| #include "dice/cbor_writer.h" |
| #include "dice/ops/trait/cose.h" |
| |
| #if DICE_PUBLIC_KEY_SIZE != 32 |
| #error "Only Ed25519 is supported; 32 bytes needed to store the public key." |
| #endif |
| #if DICE_SIGNATURE_SIZE != 64 |
| #error "Only Ed25519 is supported; 64 bytes needed to store the signature." |
| #endif |
| |
| DiceResult DiceCoseEncodePublicKey( |
| void* context_not_used, const uint8_t public_key[DICE_PUBLIC_KEY_SIZE], |
| size_t buffer_size, uint8_t* buffer, size_t* encoded_size) { |
| (void)context_not_used; |
| |
| // Constants per RFC 8152. |
| const int64_t kCoseKeyKtyLabel = 1; |
| const int64_t kCoseKeyAlgLabel = 3; |
| const int64_t kCoseKeyOpsLabel = 4; |
| const int64_t kCoseOkpCrvLabel = -1; |
| const int64_t kCoseOkpXLabel = -2; |
| const int64_t kCoseKeyTypeOkp = 1; |
| const int64_t kCoseAlgEdDSA = DICE_COSE_KEY_ALG_VALUE; |
| const int64_t kCoseKeyOpsVerify = 2; |
| const int64_t kCoseCrvEd25519 = 6; |
| |
| struct CborOut out; |
| CborOutInit(buffer, buffer_size, &out); |
| CborWriteMap(/*num_pairs=*/5, &out); |
| // Add the key type. |
| CborWriteInt(kCoseKeyKtyLabel, &out); |
| CborWriteInt(kCoseKeyTypeOkp, &out); |
| // Add the algorithm. |
| CborWriteInt(kCoseKeyAlgLabel, &out); |
| CborWriteInt(kCoseAlgEdDSA, &out); |
| // Add the KeyOps. |
| CborWriteInt(kCoseKeyOpsLabel, &out); |
| CborWriteArray(/*num_elements=*/1, &out); |
| CborWriteInt(kCoseKeyOpsVerify, &out); |
| // Add the curve. |
| CborWriteInt(kCoseOkpCrvLabel, &out); |
| CborWriteInt(kCoseCrvEd25519, &out); |
| // Add the public key. |
| CborWriteInt(kCoseOkpXLabel, &out); |
| CborWriteBstr(/*data_size=*/DICE_PUBLIC_KEY_SIZE, public_key, &out); |
| |
| *encoded_size = CborOutSize(&out); |
| if (CborOutOverflowed(&out)) { |
| return kDiceResultBufferTooSmall; |
| } |
| return kDiceResultOk; |
| } |