Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / crypto / signature_creator.h
1 // Copyright (c) 2012 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.
4
5 #ifndef CRYPTO_SIGNATURE_CREATOR_H_
6 #define CRYPTO_SIGNATURE_CREATOR_H_
7
8 #include <vector>
9
10 #include "build/build_config.h"
11 #include "base/basictypes.h"
12 #include "crypto/crypto_export.h"
13
14 #if defined(USE_OPENSSL)
15 // Forward declaration for openssl/*.h
16 typedef struct env_md_ctx_st EVP_MD_CTX;
17 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
18 // Forward declaration.
19 struct SGNContextStr;
20 #endif
21
22 namespace crypto {
23
24 class RSAPrivateKey;
25
26 // Signs data using a bare private key (as opposed to a full certificate).
27 // Currently can only sign data using SHA-1 with RSA encryption.
28 class CRYPTO_EXPORT SignatureCreator {
29  public:
30   ~SignatureCreator();
31
32   // Create an instance. The caller must ensure that the provided PrivateKey
33   // instance outlives the created SignatureCreator.
34   static SignatureCreator* Create(RSAPrivateKey* key);
35
36   // Signs the precomputed SHA-1 digest |data| using private |key| as
37   // specified in PKCS #1 v1.5.
38   static bool Sign(RSAPrivateKey* key,
39                    const uint8* data,
40                    int data_len,
41                    std::vector<uint8>* signature);
42
43   // Update the signature with more data.
44   bool Update(const uint8* data_part, int data_part_len);
45
46   // Finalize the signature.
47   bool Final(std::vector<uint8>* signature);
48
49  private:
50   // Private constructor. Use the Create() method instead.
51   SignatureCreator();
52
53   RSAPrivateKey* key_;
54
55 #if defined(USE_OPENSSL)
56   EVP_MD_CTX* sign_context_;
57 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
58   SGNContextStr* sign_context_;
59 #endif
60
61   DISALLOW_COPY_AND_ASSIGN(SignatureCreator);
62 };
63
64 }  // namespace crypto
65
66 #endif  // CRYPTO_SIGNATURE_CREATOR_H_