Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: content/renderer/webcrypto_sha_digest_nss.cc

Issue 19757011: WebCrypto: Implement digest() using NSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Initial implementation of SHA1 for NSS. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "content/renderer/webcrypto_sha_digest.h"
6
7 #include <pk11pub.h>
8
9 #include "base/logging.h"
10 #include "crypto/nss_util.h"
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
13
14 namespace content {
15
16 WebCryptoSHADigest::WebCryptoSHADigest(
17 const WebKit::WebCryptoAlgorithmId algorithm_id,
18 WebKit::WebCryptoOperationResult* result)
19 : result_(result),
20 context_(NULL),
21 hash_algorithm_(SEC_OID_UNKNOWN),
22 hash_result_length_(0) {
23 switch (algorithm_id) {
24 case WebKit::WebCryptoAlgorithmIdSha1:
25 hash_algorithm_ = SEC_OID_SHA1;
26 hash_result_length_ = 20;
27 break;
28 case WebKit::WebCryptoAlgorithmIdSha224:
29 hash_algorithm_ = SEC_OID_SHA224;
30 hash_result_length_ = 28;
31 break;
32 case WebKit::WebCryptoAlgorithmIdSha256:
33 hash_algorithm_ = SEC_OID_SHA256;
34 hash_result_length_ = 32;
35 break;
36 case WebKit::WebCryptoAlgorithmIdSha384:
37 hash_algorithm_ = SEC_OID_SHA384;
38 hash_result_length_ = 48;
39 break;
40 case WebKit::WebCryptoAlgorithmIdSha512:
41 hash_algorithm_ = SEC_OID_SHA512;
42 hash_result_length_ = 64;
43 break;
44 default:
45 hash_algorithm_ = SEC_OID_UNKNOWN;
eroman 2013/07/24 01:33:50 This should be unreachable code, put a NOTREACHED(
Bryan Eyler 2013/07/31 00:28:44 Done.
46 hash_result_length_ = 0;
47 }
48 }
49
50 WebCryptoSHADigest::~WebCryptoSHADigest() {
51 if (context_) {
52 PK11_DestroyContext(context_, PR_TRUE);
53 }
54 }
55
56 bool WebCryptoSHADigest::Initialize() {
57 crypto::EnsureNSSInit();
58
59 context_ = PK11_CreateDigestContext(hash_algorithm_);
60 if (!context_) {
61 LOG(ERROR) << "Could not create digest context for hash algorithm: "
62 << hash_algorithm_;
63 // TODO(bryaneyler): Error out.
eroman 2013/07/24 01:33:50 isn't this handled now?
Bryan Eyler 2013/07/31 00:28:44 Yes, nothing left to do. Removed comment.
64 return false;
65 }
66
67 if (PK11_DigestBegin(context_) != SECSuccess) {
68 LOG(ERROR) << "Could not initialize digest context.";
69 // TODO(bryaneyler): Error out.
eroman 2013/07/24 01:33:50 Isn't this handled now?
Bryan Eyler 2013/07/31 00:28:44 Same.
70 return false;
71 }
72
73 return true;
74 }
75
76 void WebCryptoSHADigest::process(const unsigned char* bytes, size_t size) {
77 // If this is the first process request, need to setup the context.
78 if (!context_) {
79 LOG(ERROR) << "No valid context initialized.";
eroman 2013/07/24 01:33:50 This shouldn't be reachable, since failure to crea
Bryan Eyler 2013/07/31 00:28:44 Changed to DCHECK.
80 // TODO(bryaneyler): Error out.
eroman 2013/07/24 01:33:50 result_->completeWithError();
Bryan Eyler 2013/07/31 00:28:44 Removed.
81 return;
82 }
83
84 if (PK11_DigestOp(context_, bytes, size) != SECSuccess) {
85 LOG(ERROR) << "Could not process digest contents of size: " << size;
86 // TODO(bryaneyler): Error out.
eroman 2013/07/24 01:33:50 result_->completeWithError();
Bryan Eyler 2013/07/31 00:28:44 Done.
87 }
88 }
89
90 void WebCryptoSHADigest::abort() {
91 delete this;
92 }
93
94 void WebCryptoSHADigest::finish() {
95 // If no context yet created; create an empty one.
96 if (!context_) {
eroman 2013/07/24 01:33:50 This should be an assertion instead
Bryan Eyler 2013/07/31 00:28:44 Done.
97 LOG(ERROR) << "No valid context initialized.";
98 // TODO(bryaneyler): Error out.
99 return;
100 }
101
102 unsigned char* digest = NULL;
103
104 WebKit::WebArrayBuffer buffer(
105 WebKit::WebArrayBuffer::create(hash_result_length_, 1));
106
107 digest = reinterpret_cast<unsigned char*>(buffer.data());
108 if (!digest) {
109 LOG(ERROR) << "Could not allocate digest data.";
110 // TODO(bryaneyler): Error out.
eroman 2013/07/24 01:33:50 result_->completeWithError(); ... however I don't
Bryan Eyler 2013/07/31 00:28:44 Should this be a DCHECK then?
111 return;
112 }
113
114 unsigned int result_length = 0;
115 if (PK11_DigestFinal(context_, digest, &result_length, hash_result_length_)
116 != SECSuccess || result_length != hash_result_length_) {
117 LOG(ERROR) << "Could not finalize digest data.";
118 // TODO(bryaneyler): Error out.
eroman 2013/07/24 01:33:50 result_->completeWithError();
Bryan Eyler 2013/07/31 00:28:44 Done.
119 return;
120 }
121
122 result_->completeWithArrayBuffer(buffer);
123
124 delete this;
125 }
126
127 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698