1 // Copyright 2022 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 // An implementation of the ed25519 signature operations using boringssl.
16
17 #include <stdint.h>
18
19 #include "dice/config/cose_key_config.h"
20 #include "dice/dice.h"
21 #include "dice/ops.h"
22 #include "openssl/curve25519.h"
23
24 #if DICE_PRIVATE_KEY_SEED_SIZE != 32
25 #error "Private key seed is expected to be 32 bytes."
26 #endif
27 #if DICE_PUBLIC_KEY_BUFFER_SIZE != 32
28 #error "Ed25519 needs 32 bytes to store the public key."
29 #endif
30 #if DICE_PRIVATE_KEY_BUFFER_SIZE != 64
31 #error "This Ed25519 implementation needs 64 bytes for the private key."
32 #endif
33 #if DICE_SIGNATURE_BUFFER_SIZE != 64
34 #error "Ed25519 needs 64 bytes to store the signature."
35 #endif
36
37 #define DICE_PROFILE_NAME NULL
38
DiceGetKeyParam(void * context_not_used,DicePrincipal principal_not_used,DiceKeyParam * key_param)39 DiceResult DiceGetKeyParam(void* context_not_used,
40 DicePrincipal principal_not_used,
41 DiceKeyParam* key_param) {
42 (void)context_not_used;
43 (void)principal_not_used;
44 key_param->profile_name = DICE_PROFILE_NAME;
45 key_param->public_key_size = DICE_PUBLIC_KEY_BUFFER_SIZE;
46 key_param->signature_size = DICE_SIGNATURE_BUFFER_SIZE;
47
48 key_param->cose_key_type = kCoseKeyKtyOkp;
49 key_param->cose_key_algorithm = kCoseAlgEdDsa;
50 key_param->cose_key_curve = kCoseCrvEd25519;
51 return kDiceResultOk;
52 }
53
DiceKeypairFromSeed(void * context_not_used,DicePrincipal principal_not_used,const uint8_t seed[DICE_PRIVATE_KEY_SEED_SIZE],uint8_t public_key[DICE_PUBLIC_KEY_BUFFER_SIZE],uint8_t private_key[DICE_PRIVATE_KEY_BUFFER_SIZE])54 DiceResult DiceKeypairFromSeed(
55 void* context_not_used, DicePrincipal principal_not_used,
56 const uint8_t seed[DICE_PRIVATE_KEY_SEED_SIZE],
57 uint8_t public_key[DICE_PUBLIC_KEY_BUFFER_SIZE],
58 uint8_t private_key[DICE_PRIVATE_KEY_BUFFER_SIZE]) {
59 (void)context_not_used;
60 (void)principal_not_used;
61 ED25519_keypair_from_seed(public_key, private_key, seed);
62 return kDiceResultOk;
63 }
64
DiceSign(void * context_not_used,const uint8_t * message,size_t message_size,const uint8_t private_key[DICE_PRIVATE_KEY_BUFFER_SIZE],uint8_t signature[DICE_SIGNATURE_BUFFER_SIZE])65 DiceResult DiceSign(void* context_not_used, const uint8_t* message,
66 size_t message_size,
67 const uint8_t private_key[DICE_PRIVATE_KEY_BUFFER_SIZE],
68 uint8_t signature[DICE_SIGNATURE_BUFFER_SIZE]) {
69 (void)context_not_used;
70 if (1 != ED25519_sign(signature, message, message_size, private_key)) {
71 return kDiceResultPlatformError;
72 }
73 return kDiceResultOk;
74 }
75
DiceVerify(void * context_not_used,const uint8_t * message,size_t message_size,const uint8_t signature[DICE_SIGNATURE_BUFFER_SIZE],const uint8_t public_key[DICE_PUBLIC_KEY_BUFFER_SIZE])76 DiceResult DiceVerify(void* context_not_used, const uint8_t* message,
77 size_t message_size,
78 const uint8_t signature[DICE_SIGNATURE_BUFFER_SIZE],
79 const uint8_t public_key[DICE_PUBLIC_KEY_BUFFER_SIZE]) {
80 (void)context_not_used;
81 if (1 != ED25519_verify(message, message_size, signature, public_key)) {
82 return kDiceResultPlatformError;
83 }
84 return kDiceResultOk;
85 }
86