1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef OPENSSL_OPS_H_
26#define OPENSSL_OPS_H_
27
28#include "atap_ops_delegate.h"
29
30namespace atap {
31
32// A partial delegate implementation which implements all crypto ops with
33// openssl. All instances of this class must be created on the same thread.
34class OpensslOps : public AtapOpsDelegate {
35 public:
36  OpensslOps();
37  ~OpensslOps() override;
38
39  // Overridden AtapOpsDelegate methods.
40  AtapResult get_random_bytes(uint8_t* buf, uint32_t buf_size) override;
41
42  AtapResult ecdh_shared_secret_compute(
43      AtapCurveType curve,
44      const uint8_t other_public_key[ATAP_ECDH_KEY_LEN],
45      uint8_t public_key[ATAP_ECDH_KEY_LEN],
46      uint8_t shared_secret[ATAP_ECDH_KEY_LEN]) override;
47
48  AtapResult aes_gcm_128_encrypt(const uint8_t* plaintext,
49                                 uint32_t len,
50                                 const uint8_t iv[ATAP_GCM_IV_LEN],
51                                 const uint8_t key[ATAP_AES_128_KEY_LEN],
52                                 uint8_t* ciphertext,
53                                 uint8_t tag[ATAP_GCM_TAG_LEN]) override;
54
55  AtapResult aes_gcm_128_decrypt(const uint8_t* ciphertext,
56                                 uint32_t len,
57                                 const uint8_t iv[ATAP_GCM_IV_LEN],
58                                 const uint8_t key[ATAP_AES_128_KEY_LEN],
59                                 const uint8_t tag[ATAP_GCM_TAG_LEN],
60                                 uint8_t* plaintext) override;
61
62  AtapResult sha256(const uint8_t* plaintext,
63                    uint32_t plaintext_len,
64                    uint8_t hash[ATAP_SHA256_DIGEST_LEN]) override;
65
66  AtapResult hkdf_sha256(const uint8_t* salt,
67                         uint32_t salt_len,
68                         const uint8_t* ikm,
69                         uint32_t ikm_len,
70                         const uint8_t* info,
71                         uint32_t info_len,
72                         uint8_t* okm,
73                         int32_t okm_len) override;
74
75  // Can be used during testing to get predictable 'ephemeral' ECDH keys. This
76  // must never be called except during testing. For X25519, the expected format
77  // is a 32-byte private key. For P256, the expected format is X9.62 DER.
78  void SetEcdhKeyForTesting(const void* key_data, size_t size_in_bytes);
79
80 private:
81  uint8_t test_key_[512];
82  size_t test_key_size_{0};
83};
84
85}  // namespace atap
86
87#endif /* OPENSSL_OPS_H_ */
88