• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 a configurable, multi-algorithm implementation of signature
16 // operations using boringssl.
17 
18 #include <stdint.h>
19 #include <stdio.h>
20 
21 #include "dice/boringssl_ecdsa_utils.h"
22 #include "dice/config/cose_key_config.h"
23 #include "dice/dice.h"
24 #include "dice/ops.h"
25 #include "openssl/curve25519.h"
26 
27 #if DICE_PRIVATE_KEY_SEED_SIZE != 32
28 #error "Private key seed is expected to be 32 bytes."
29 #endif
30 #if DICE_PUBLIC_KEY_BUFFER_SIZE != 96
31 #error "Multialg needs 96 bytes to for the public key (P-384)"
32 #endif
33 #if DICE_PRIVATE_KEY_BUFFER_SIZE != 64
34 #error "Multialg needs 64 bytes for the private key (Ed25519)"
35 #endif
36 #if DICE_SIGNATURE_BUFFER_SIZE != 96
37 #error "Multialg needs 96 bytes to store the signature (P-384)"
38 #endif
39 
DiceGetKeyParam(void * context,DicePrincipal principal,DiceKeyParam * key_param)40 DiceResult DiceGetKeyParam(void* context, DicePrincipal principal,
41                            DiceKeyParam* key_param) {
42   DiceKeyAlgorithm alg;
43   DiceResult result = DiceGetKeyAlgorithm(context, principal, &alg);
44   if (result != kDiceResultOk) {
45     return result;
46   }
47   switch (alg) {
48     case kDiceKeyAlgorithmEd25519:
49       key_param->public_key_size = 32;
50       key_param->signature_size = 64;
51 
52       key_param->cose_key_type = kCoseKeyKtyOkp;
53       key_param->cose_key_algorithm = kCoseAlgEdDsa;
54       key_param->cose_key_curve = kCoseCrvEd25519;
55       return kDiceResultOk;
56     case kDiceKeyAlgorithmP256:
57       key_param->public_key_size = 64;
58       key_param->signature_size = 64;
59 
60       key_param->cose_key_type = kCoseKeyKtyEc2;
61       key_param->cose_key_algorithm = kCoseAlgEs256;
62       key_param->cose_key_curve = kCoseCrvP256;
63       return kDiceResultOk;
64     case kDiceKeyAlgorithmP384:
65       key_param->public_key_size = 96;
66       key_param->signature_size = 96;
67 
68       key_param->cose_key_type = kCoseKeyKtyEc2;
69       key_param->cose_key_algorithm = kCoseAlgEs384;
70       key_param->cose_key_curve = kCoseCrvP384;
71       return kDiceResultOk;
72   }
73   return kDiceResultPlatformError;
74 }
75 
DiceKeypairFromSeed(void * context,DicePrincipal principal,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])76 DiceResult DiceKeypairFromSeed(
77     void* context, DicePrincipal principal,
78     const uint8_t seed[DICE_PRIVATE_KEY_SEED_SIZE],
79     uint8_t public_key[DICE_PUBLIC_KEY_BUFFER_SIZE],
80     uint8_t private_key[DICE_PRIVATE_KEY_BUFFER_SIZE]) {
81   DiceKeyAlgorithm alg;
82   DiceResult result = DiceGetKeyAlgorithm(context, principal, &alg);
83   if (result != kDiceResultOk) {
84     return result;
85   }
86   switch (alg) {
87     case kDiceKeyAlgorithmEd25519:
88       ED25519_keypair_from_seed(public_key, private_key, seed);
89       return kDiceResultOk;
90     case kDiceKeyAlgorithmP256:
91       if (1 == P256KeypairFromSeed(public_key, private_key, seed)) {
92         return kDiceResultOk;
93       }
94       break;
95     case kDiceKeyAlgorithmP384:
96       if (1 == P384KeypairFromSeed(public_key, private_key, seed)) {
97         return kDiceResultOk;
98       }
99       break;
100   }
101   return kDiceResultPlatformError;
102 }
103 
DiceSign(void * context,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])104 DiceResult DiceSign(void* context, const uint8_t* message, size_t message_size,
105                     const uint8_t private_key[DICE_PRIVATE_KEY_BUFFER_SIZE],
106                     uint8_t signature[DICE_SIGNATURE_BUFFER_SIZE]) {
107   DiceKeyAlgorithm alg;
108   DiceResult result =
109       DiceGetKeyAlgorithm(context, kDicePrincipalAuthority, &alg);
110   if (result != kDiceResultOk) {
111     return result;
112   }
113   switch (alg) {
114     case kDiceKeyAlgorithmEd25519:
115       if (1 == ED25519_sign(signature, message, message_size, private_key)) {
116         return kDiceResultOk;
117       }
118       break;
119     case kDiceKeyAlgorithmP256:
120       if (1 == P256Sign(signature, message, message_size, private_key)) {
121         return kDiceResultOk;
122       }
123       break;
124     case kDiceKeyAlgorithmP384:
125       if (1 == P384Sign(signature, message, message_size, private_key)) {
126         return kDiceResultOk;
127       }
128       break;
129   }
130   return kDiceResultPlatformError;
131 }
132 
DiceVerify(void * context,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])133 DiceResult DiceVerify(void* context, const uint8_t* message,
134                       size_t message_size,
135                       const uint8_t signature[DICE_SIGNATURE_BUFFER_SIZE],
136                       const uint8_t public_key[DICE_PUBLIC_KEY_BUFFER_SIZE]) {
137   DiceKeyAlgorithm alg;
138   DiceResult result =
139       DiceGetKeyAlgorithm(context, kDicePrincipalAuthority, &alg);
140   if (result != kDiceResultOk) {
141     return result;
142   }
143   switch (alg) {
144     case kDiceKeyAlgorithmEd25519:
145       if (1 == ED25519_verify(message, message_size, signature, public_key)) {
146         return kDiceResultOk;
147       }
148       break;
149     case kDiceKeyAlgorithmP256:
150       if (1 == P256Verify(message, message_size, signature, public_key)) {
151         return kDiceResultOk;
152       }
153       break;
154     case kDiceKeyAlgorithmP384:
155       if (1 == P384Verify(message, message_size, signature, public_key)) {
156         return kDiceResultOk;
157       }
158       break;
159   }
160   return kDiceResultPlatformError;
161 }
162