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 // This is an implementation of P-384 signature operations using boringssl.
16
17 #include <stdint.h>
18 #include <stdio.h>
19
20 #include "dice/boringssl_ecdsa_utils.h"
21 #include "dice/dice.h"
22 #include "dice/ops.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 != 96
28 #error "This P-384 implementation needs 96 bytes to store the public key."
29 #endif
30 #if DICE_PRIVATE_KEY_SIZE != 48
31 #error "P-384 needs 48 bytes for the private key."
32 #endif
33 #if DICE_SIGNATURE_SIZE != 96
34 #error "P-384 needs 96 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 if (1 == P384KeypairFromSeed(public_key, private_key, seed)) {
43 return kDiceResultOk;
44 }
45 return kDiceResultPlatformError;
46 }
47
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])48 DiceResult DiceSign(void* context_not_used, const uint8_t* message,
49 size_t message_size,
50 const uint8_t private_key[DICE_PRIVATE_KEY_SIZE],
51 uint8_t signature[DICE_SIGNATURE_SIZE]) {
52 (void)context_not_used;
53 if (1 == P384Sign(signature, message, message_size, private_key)) {
54 return kDiceResultOk;
55 }
56 return kDiceResultPlatformError;
57 }
58
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])59 DiceResult DiceVerify(void* context_not_used, const uint8_t* message,
60 size_t message_size,
61 const uint8_t signature[DICE_SIGNATURE_SIZE],
62 const uint8_t public_key[DICE_PUBLIC_KEY_SIZE]) {
63 (void)context_not_used;
64 if (1 == P384Verify(message, message_size, signature, public_key)) {
65 return kDiceResultOk;
66 }
67 return kDiceResultPlatformError;
68 }
69