signature_creator.h 1.85 KB
Newer Older
1
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
aa@chromium.org's avatar
aa@chromium.org committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef CRYPTO_SIGNATURE_CREATOR_H_
#define CRYPTO_SIGNATURE_CREATOR_H_
aa@chromium.org's avatar
aa@chromium.org committed
7

8 9
#include <stdint.h>

10
#include <memory>
aa@chromium.org's avatar
aa@chromium.org committed
11 12
#include <vector>

13
#include "base/macros.h"
14
#include "build/build_config.h"
15
#include "crypto/crypto_export.h"
aa@chromium.org's avatar
aa@chromium.org committed
16

17 18
// Forward declaration for openssl/*.h
typedef struct env_md_ctx_st EVP_MD_CTX;
19

20
namespace crypto {
aa@chromium.org's avatar
aa@chromium.org committed
21

22 23
class RSAPrivateKey;

aa@chromium.org's avatar
aa@chromium.org committed
24
// Signs data using a bare private key (as opposed to a full certificate).
25
// Currently can only sign data using SHA-1 or SHA-256 with RSA PKCS#1v1.5.
26
class CRYPTO_EXPORT SignatureCreator {
aa@chromium.org's avatar
aa@chromium.org committed
27
 public:
28 29 30 31 32 33
  // The set of supported hash functions. Extend as required.
  enum HashAlgorithm {
    SHA1,
    SHA256,
  };

34 35
  ~SignatureCreator();

aa@chromium.org's avatar
aa@chromium.org committed
36
  // Create an instance. The caller must ensure that the provided PrivateKey
37 38
  // instance outlives the created SignatureCreator. Uses the HashAlgorithm
  // specified.
39 40
  static std::unique_ptr<SignatureCreator> Create(RSAPrivateKey* key,
                                                  HashAlgorithm hash_alg);
aa@chromium.org's avatar
aa@chromium.org committed
41

42
  // Signs the precomputed |hash_alg| digest |data| using private |key| as
43 44
  // specified in PKCS #1 v1.5.
  static bool Sign(RSAPrivateKey* key,
45
                   HashAlgorithm hash_alg,
46
                   const uint8_t* data,
47
                   int data_len,
48
                   std::vector<uint8_t>* signature);
49

aa@chromium.org's avatar
aa@chromium.org committed
50
  // Update the signature with more data.
51
  bool Update(const uint8_t* data_part, int data_part_len);
aa@chromium.org's avatar
aa@chromium.org committed
52 53

  // Finalize the signature.
54
  bool Final(std::vector<uint8_t>* signature);
aa@chromium.org's avatar
aa@chromium.org committed
55 56 57

 private:
  // Private constructor. Use the Create() method instead.
58
  SignatureCreator();
aa@chromium.org's avatar
aa@chromium.org committed
59

60
  EVP_MD_CTX* sign_context_;
aa@chromium.org's avatar
aa@chromium.org committed
61 62 63 64

  DISALLOW_COPY_AND_ASSIGN(SignatureCreator);
};

65
}  // namespace crypto
aa@chromium.org's avatar
aa@chromium.org committed
66

67
#endif  // CRYPTO_SIGNATURE_CREATOR_H_