OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <sechash.h> |
| 6 #include <vector> |
| 7 |
| 8 #include "content/child/webcrypto/algorithm_implementation.h" |
| 9 #include "content/child/webcrypto/crypto_data.h" |
| 10 #include "content/child/webcrypto/nss/util_nss.h" |
| 11 #include "content/child/webcrypto/status.h" |
| 12 #include "content/child/webcrypto/webcrypto_util.h" |
| 13 #include "crypto/nss_util.h" |
| 14 #include "crypto/scoped_nss_types.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace webcrypto { |
| 19 |
| 20 namespace { |
| 21 |
| 22 HASH_HashType WebCryptoAlgorithmToNSSHashType( |
| 23 blink::WebCryptoAlgorithmId algorithm) { |
| 24 switch (algorithm) { |
| 25 case blink::WebCryptoAlgorithmIdSha1: |
| 26 return HASH_AlgSHA1; |
| 27 case blink::WebCryptoAlgorithmIdSha256: |
| 28 return HASH_AlgSHA256; |
| 29 case blink::WebCryptoAlgorithmIdSha384: |
| 30 return HASH_AlgSHA384; |
| 31 case blink::WebCryptoAlgorithmIdSha512: |
| 32 return HASH_AlgSHA512; |
| 33 default: |
| 34 // Not a digest algorithm. |
| 35 return HASH_AlgNULL; |
| 36 } |
| 37 } |
| 38 |
| 39 // Multipart input SHA digestor. |
| 40 class DigestorNSS : public blink::WebCryptoDigestor { |
| 41 public: |
| 42 explicit DigestorNSS(blink::WebCryptoAlgorithmId algorithm_id) |
| 43 : hash_context_(NULL), algorithm_id_(algorithm_id) {} |
| 44 |
| 45 virtual ~DigestorNSS() { |
| 46 if (!hash_context_) |
| 47 return; |
| 48 |
| 49 HASH_Destroy(hash_context_); |
| 50 hash_context_ = NULL; |
| 51 } |
| 52 |
| 53 virtual bool consume(const unsigned char* data, unsigned int size) { |
| 54 return ConsumeWithStatus(data, size).IsSuccess(); |
| 55 } |
| 56 |
| 57 Status ConsumeWithStatus(const unsigned char* data, unsigned int size) { |
| 58 // Initialize everything if the object hasn't been initialized yet. |
| 59 if (!hash_context_) { |
| 60 Status error = Init(); |
| 61 if (!error.IsSuccess()) |
| 62 return error; |
| 63 } |
| 64 |
| 65 HASH_Update(hash_context_, data, size); |
| 66 |
| 67 return Status::Success(); |
| 68 } |
| 69 |
| 70 virtual bool finish(unsigned char*& result_data, |
| 71 unsigned int& result_data_size) { |
| 72 Status error = FinishInternal(result_, &result_data_size); |
| 73 if (!error.IsSuccess()) |
| 74 return false; |
| 75 result_data = result_; |
| 76 return true; |
| 77 } |
| 78 |
| 79 Status FinishWithVectorAndStatus(std::vector<uint8>* result) { |
| 80 if (!hash_context_) |
| 81 return Status::ErrorUnexpected(); |
| 82 |
| 83 unsigned int result_length = HASH_ResultLenContext(hash_context_); |
| 84 result->resize(result_length); |
| 85 unsigned char* digest = Uint8VectorStart(result); |
| 86 unsigned int digest_size; // ignored |
| 87 return FinishInternal(digest, &digest_size); |
| 88 } |
| 89 |
| 90 private: |
| 91 Status Init() { |
| 92 HASH_HashType hash_type = WebCryptoAlgorithmToNSSHashType(algorithm_id_); |
| 93 |
| 94 if (hash_type == HASH_AlgNULL) |
| 95 return Status::ErrorUnsupported(); |
| 96 |
| 97 hash_context_ = HASH_Create(hash_type); |
| 98 if (!hash_context_) |
| 99 return Status::OperationError(); |
| 100 |
| 101 HASH_Begin(hash_context_); |
| 102 |
| 103 return Status::Success(); |
| 104 } |
| 105 |
| 106 Status FinishInternal(unsigned char* result, unsigned int* result_size) { |
| 107 if (!hash_context_) { |
| 108 Status error = Init(); |
| 109 if (!error.IsSuccess()) |
| 110 return error; |
| 111 } |
| 112 |
| 113 unsigned int hash_result_length = HASH_ResultLenContext(hash_context_); |
| 114 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX)); |
| 115 |
| 116 HASH_End(hash_context_, result, result_size, hash_result_length); |
| 117 |
| 118 if (*result_size != hash_result_length) |
| 119 return Status::ErrorUnexpected(); |
| 120 return Status::Success(); |
| 121 } |
| 122 |
| 123 HASHContext* hash_context_; |
| 124 blink::WebCryptoAlgorithmId algorithm_id_; |
| 125 unsigned char result_[HASH_LENGTH_MAX]; |
| 126 }; |
| 127 |
| 128 class ShaImplementation : public AlgorithmImplementation { |
| 129 public: |
| 130 virtual Status Digest(const blink::WebCryptoAlgorithm& algorithm, |
| 131 const CryptoData& data, |
| 132 std::vector<uint8>* buffer) const OVERRIDE { |
| 133 DigestorNSS digestor(algorithm.id()); |
| 134 Status error = digestor.ConsumeWithStatus(data.bytes(), data.byte_length()); |
| 135 // http://crbug.com/366427: the spec does not define any other failures for |
| 136 // digest, so none of the subsequent errors are spec compliant. |
| 137 if (!error.IsSuccess()) |
| 138 return error; |
| 139 return digestor.FinishWithVectorAndStatus(buffer); |
| 140 } |
| 141 }; |
| 142 |
| 143 } // namespace |
| 144 |
| 145 AlgorithmImplementation* CreatePlatformShaImplementation() { |
| 146 return new ShaImplementation(); |
| 147 } |
| 148 |
| 149 scoped_ptr<blink::WebCryptoDigestor> CreatePlatformDigestor( |
| 150 blink::WebCryptoAlgorithmId algorithm) { |
| 151 return scoped_ptr<blink::WebCryptoDigestor>(new DigestorNSS(algorithm)); |
| 152 } |
| 153 |
| 154 } // namespace webcrypto |
| 155 |
| 156 } // namespace content |
OLD | NEW |