Home | History | Annotate | Download | only in functional
      1 /*
      2  * Copyright 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 template <typename T, void (*F)(T*)> struct UniquePtrDeleter {
     18     void operator()(T* p) const { F(p); }
     19 };
     20 
     21 typedef UniquePtrDeleter<EVP_PKEY, EVP_PKEY_free> EVP_PKEY_Delete;
     22 
     23 #define MAKE_OPENSSL_PTR_TYPE(type)                                                                \
     24     typedef std::unique_ptr<type, UniquePtrDeleter<type, type##_free>> type##_Ptr;
     25 
     26 MAKE_OPENSSL_PTR_TYPE(ASN1_OBJECT)
     27 MAKE_OPENSSL_PTR_TYPE(EVP_PKEY)
     28 MAKE_OPENSSL_PTR_TYPE(RSA)
     29 MAKE_OPENSSL_PTR_TYPE(X509)
     30 MAKE_OPENSSL_PTR_TYPE(BN_CTX)
     31 
     32 typedef std::unique_ptr<BIGNUM, UniquePtrDeleter<BIGNUM, BN_free>> BIGNUM_Ptr;
     33 
     34 inline const EVP_MD* openssl_digest(android::hardware::keymaster::V3_0::Digest digest) {
     35     switch (digest) {
     36     case android::hardware::keymaster::V3_0::Digest::NONE:
     37         return nullptr;
     38     case android::hardware::keymaster::V3_0::Digest::MD5:
     39         return EVP_md5();
     40     case android::hardware::keymaster::V3_0::Digest::SHA1:
     41         return EVP_sha1();
     42     case android::hardware::keymaster::V3_0::Digest::SHA_2_224:
     43         return EVP_sha224();
     44     case android::hardware::keymaster::V3_0::Digest::SHA_2_256:
     45         return EVP_sha256();
     46     case android::hardware::keymaster::V3_0::Digest::SHA_2_384:
     47         return EVP_sha384();
     48     case android::hardware::keymaster::V3_0::Digest::SHA_2_512:
     49         return EVP_sha512();
     50     }
     51     return nullptr;
     52 }
     53