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.
5 #include "crypto/ec_signature_creator_impl.h"
12 #include "base/logging.h"
13 #include "crypto/ec_private_key.h"
14 #include "crypto/nss_util.h"
15 #include "crypto/scoped_nss_types.h"
21 SECStatus
SignData(SECItem
* result
,
23 SECKEYPrivateKey
* key
,
24 HASH_HashType hash_type
) {
25 if (key
->keyType
!= ecKey
) {
26 DLOG(FATAL
) << "Should be using an EC key.";
27 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
32 std::vector
<uint8
> hash_data(HASH_ResultLen(hash_type
));
33 SECStatus rv
= HASH_HashBuf(
34 hash_type
, &hash_data
[0], input
->data
, input
->len
);
37 SECItem hash
= {siBuffer
, &hash_data
[0], hash_data
.size()};
39 // Compute signature of hash.
40 int signature_len
= PK11_SignatureLen(key
);
41 std::vector
<uint8
> signature_data(signature_len
);
42 SECItem sig
= {siBuffer
, &signature_data
[0], signature_len
};
43 rv
= PK11_Sign(key
, &sig
, &hash
);
47 // DER encode the signature.
48 return DSAU_EncodeDerSigWithLen(result
, &sig
, sig
.len
);
53 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey
* key
)
58 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {}
60 bool ECSignatureCreatorImpl::Sign(const uint8
* data
,
62 std::vector
<uint8
>* signature
) {
65 secret
.type
= siBuffer
;
66 secret
.len
= data_len
;
67 secret
.data
= const_cast<unsigned char*>(data
);
69 // SECItem to receive the output buffer.
71 result
.type
= siBuffer
;
75 // Sign the secret data and save it to |result|.
77 SignData(&result
, &secret
, key_
->key(), HASH_AlgSHA1
);
78 if (rv
!= SECSuccess
) {
79 DLOG(ERROR
) << "DerSignData: " << PORT_GetError();
83 // Copy the signed data into the output vector.
84 signature
->assign(result
.data
, result
.data
+ result
.len
);
85 SECITEM_FreeItem(&result
, PR_FALSE
/* only free |result.data| */);