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 hash and kdf crypto operations using boringssl. The
16 // algorithms used are SHA512 and HKDF-SHA512.
17
18 #include <stdint.h>
19
20 #include "dice/dice.h"
21 #include "dice/ops.h"
22 #include "openssl/evp.h"
23 #include "openssl/hkdf.h"
24 #include "openssl/is_boringssl.h"
25 #include "openssl/sha.h"
26
DiceHash(void * context_not_used,const uint8_t * input,size_t input_size,uint8_t output[DICE_HASH_SIZE])27 DiceResult DiceHash(void* context_not_used, const uint8_t* input,
28 size_t input_size, uint8_t output[DICE_HASH_SIZE]) {
29 (void)context_not_used;
30 SHA512(input, input_size, output);
31 return kDiceResultOk;
32 }
33
DiceKdf(void * context_not_used,size_t length,const uint8_t * ikm,size_t ikm_size,const uint8_t * salt,size_t salt_size,const uint8_t * info,size_t info_size,uint8_t * output)34 DiceResult DiceKdf(void* context_not_used, size_t length, const uint8_t* ikm,
35 size_t ikm_size, const uint8_t* salt, size_t salt_size,
36 const uint8_t* info, size_t info_size, uint8_t* output) {
37 (void)context_not_used;
38 if (!HKDF(output, length, EVP_sha512(), ikm, ikm_size, salt, salt_size, info,
39 info_size)) {
40 return kDiceResultPlatformError;
41 }
42 return kDiceResultOk;
43 }
44