1 // Copyright 2014 The Chromium 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.
5 #ifndef COMPONENTS_WEBCRYPTO_ALGORITHMS_UTIL_OPENSSL_H_
6 #define COMPONENTS_WEBCRYPTO_ALGORITHMS_UTIL_OPENSSL_H_
11 #include <openssl/base.h>
13 #include "crypto/scoped_openssl_types.h"
14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
15 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
20 class GenerateKeyResult
;
23 // The values of these constants correspond with the "enc" parameter of
24 // EVP_CipherInit_ex(), do not change.
25 enum EncryptOrDecrypt
{ DECRYPT
= 0, ENCRYPT
= 1 };
27 const EVP_MD
* GetDigest(blink::WebCryptoAlgorithmId id
);
29 // Does either encryption or decryption for an AEAD algorithm.
30 // * |mode| controls whether encryption or decryption is done
31 // * |aead_alg| the algorithm (for instance AES-GCM)
32 // * |buffer| where the ciphertext or plaintext is written to.
33 Status
AeadEncryptDecrypt(EncryptOrDecrypt mode
,
34 const std::vector
<uint8_t>& raw_key
,
35 const CryptoData
& data
,
36 unsigned int tag_length_bytes
,
38 const CryptoData
& additional_data
,
39 const EVP_AEAD
* aead_alg
,
40 std::vector
<uint8_t>* buffer
);
42 // Creates a WebCrypto public key given an EVP_PKEY. This step includes
43 // exporting the key to SPKI format, for use by serialization later.
44 Status
CreateWebCryptoPublicKey(crypto::ScopedEVP_PKEY public_key
,
45 const blink::WebCryptoKeyAlgorithm
& algorithm
,
47 blink::WebCryptoKeyUsageMask usages
,
48 blink::WebCryptoKey
* key
);
50 // Creates a WebCrypto private key given an EVP_PKEY. This step includes
51 // exporting the key to PKCS8 format, for use by serialization later.
52 Status
CreateWebCryptoPrivateKey(crypto::ScopedEVP_PKEY private_key
,
53 const blink::WebCryptoKeyAlgorithm
& algorithm
,
55 blink::WebCryptoKeyUsageMask usages
,
56 blink::WebCryptoKey
* key
);
58 // Imports SPKI bytes to an EVP_PKEY for a public key. The resulting asymmetric
59 // key may be invalid, and should be verified using something like
60 // RSA_check_key(). The only validation performed by this function is to ensure
61 // the key type matched |expected_pkey_id|.
62 Status
ImportUnverifiedPkeyFromSpki(const CryptoData
& key_data
,
64 crypto::ScopedEVP_PKEY
* pkey
);
66 // Imports PKCS8 bytes to an EVP_PKEY for a private key. The resulting
67 // asymmetric key may be invalid, and should be verified using something like
68 // RSA_check_key(). The only validation performed by this function is to ensure
69 // the key type matched |expected_pkey_id|.
70 Status
ImportUnverifiedPkeyFromPkcs8(const CryptoData
& key_data
,
72 crypto::ScopedEVP_PKEY
* pkey
);
74 // Allocates a new BIGNUM given a std::string big-endian representation.
75 BIGNUM
* CreateBIGNUM(const std::string
& n
);
77 // Converts a BIGNUM to a big endian byte array.
78 std::vector
<uint8_t> BIGNUMToVector(const BIGNUM
* n
);
80 } // namespace webcrypto
82 #endif // COMPONENTS_WEBCRYPTO_ALGORITHMS_UTIL_OPENSSL_H_