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/dice.h"
20 #include "dice/ops.h"
21 #include "openssl/curve25519.h"
22 #include "openssl/evp.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_SIZE != 32
28 #error "Ed25519 needs 32 bytes to store the public key."
29 #endif
30 #if DICE_PRIVATE_KEY_SIZE != 64
31 #error "This Ed25519 implementation needs 64 bytes for the private key."
32 #endif
33 #if DICE_SIGNATURE_SIZE != 64
34 #error "Ed25519 needs 64 bytes to store the signature."
35 #endif
36
DiceKeypairFromSeed(void * context_not_used,const uint8_t seed[DICE_PRIVATE_KEY_SEED_SIZE],uint8_t public_key[DICE_PUBLIC_KEY_SIZE],uint8_t private_key[DICE_PRIVATE_KEY_SIZE])37 DiceResult DiceKeypairFromSeed(void* context_not_used,
38 const uint8_t seed[DICE_PRIVATE_KEY_SEED_SIZE],
39 uint8_t public_key[DICE_PUBLIC_KEY_SIZE],
40 uint8_t private_key[DICE_PRIVATE_KEY_SIZE]) {
41 (void)context_not_used;
42 ED25519_keypair_from_seed(public_key, private_key, seed);
43 return kDiceResultOk;
44 }
45
DiceSign(void * context_not_used,const uint8_t * message,size_t message_size,const uint8_t private_key[DICE_PRIVATE_KEY_SIZE],uint8_t signature[DICE_SIGNATURE_SIZE])46 DiceResult DiceSign(void* context_not_used, const uint8_t* message,
47 size_t message_size,
48 const uint8_t private_key[DICE_PRIVATE_KEY_SIZE],
49 uint8_t signature[DICE_SIGNATURE_SIZE]) {
50 (void)context_not_used;
51 if (1 != ED25519_sign(signature, message, message_size, private_key)) {
52 return kDiceResultPlatformError;
53 }
54 return kDiceResultOk;
55 }
56
DiceVerify(void * context_not_used,const uint8_t * message,size_t message_size,const uint8_t signature[DICE_SIGNATURE_SIZE],const uint8_t public_key[DICE_PUBLIC_KEY_SIZE])57 DiceResult DiceVerify(void* context_not_used, const uint8_t* message,
58 size_t message_size,
59 const uint8_t signature[DICE_SIGNATURE_SIZE],
60 const uint8_t public_key[DICE_PUBLIC_KEY_SIZE]) {
61 (void)context_not_used;
62 if (1 != ED25519_verify(message, message_size, signature, public_key)) {
63 return kDiceResultPlatformError;
64 }
65 return kDiceResultOk;
66 }
67