1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stddef.h>
6 
7 #include <crypto/digest.h>
8 #include <lib/fdio/debug.h>
9 #include <zircon/assert.h>
10 #include <zircon/types.h>
11 
12 // See note in //zircon/third_party/ulib/uboringssl/rules.mk
13 #define BORINGSSL_NO_CXX
14 #include <openssl/digest.h>
15 
16 #define ZXDEBUG 0
17 
18 namespace crypto {
19 namespace digest {
20 
21 // Gets a pointer to the opaque crypto implementation of the digest algorithm.
GetDigest(Algorithm digest,uintptr_t * out)22 zx_status_t GetDigest(Algorithm digest, uintptr_t* out) {
23     ZX_DEBUG_ASSERT(out);
24     const EVP_MD* md;
25     switch (digest) {
26     case digest::kUninitialized:
27         xprintf("not initialized\n");
28         return ZX_ERR_INVALID_ARGS;
29 
30     case digest::kSHA256:
31         md = EVP_sha256();
32         break;
33 
34     default:
35         xprintf("invalid digest = %u\n", digest);
36         return ZX_ERR_NOT_SUPPORTED;
37     }
38     *out = reinterpret_cast<uintptr_t>(md);
39 
40     return ZX_OK;
41 }
42 
43 // Gets the minimum number of bytes needed for the digest produced by the given |version|.
GetDigestLen(Algorithm digest,size_t * out)44 zx_status_t GetDigestLen(Algorithm digest, size_t* out) {
45     zx_status_t rc;
46 
47     ZX_DEBUG_ASSERT(out);
48     uintptr_t ptr;
49     if ((rc = GetDigest(digest, &ptr)) != ZX_OK) {
50         return rc;
51     }
52     *out = EVP_MD_size(reinterpret_cast<const EVP_MD*>(ptr));
53 
54     return ZX_OK;
55 }
56 
57 } // namespace digest
58 } // namespace crypto
59