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

Side by Side Diff: base/openssl_util.h

Issue 3855004: Make USE_OPENSSL and USE_NSS mutually exclusive (Closed)
Patch Set: wtc comments Created 10 years, 2 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
« no previous file with comments | « base/hmac_openssl.cc ('k') | base/sha2_openssl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 BASE_OPENNSSL_UTIL_H_
6 #define BASE_OPENNSSL_UTIL_H_
7 #pragma once
8
9 #include "base/basictypes.h"
10
11 namespace base {
12
13 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
14 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
15 // of the our base wrapper APIs.
16 // This allows the library to write directly to the caller's buffer if it is of
17 // sufficient size, but if not it will write to temporary |min_sized_buffer_|
18 // of required size and then its content is automatically copied out on
19 // destruction, with truncation as appropriate.
20 template<int MIN_SIZE>
21 class ScopedOpenSSLSafeSizeBuffer {
22 public:
23 ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len)
24 : output_(output),
25 output_len_(output_len) {
26 }
27
28 ~ScopedOpenSSLSafeSizeBuffer() {
29 if (output_len_ < MIN_SIZE) {
30 // Copy the temporary buffer out, truncating as needed.
31 memcpy(output_, min_sized_buffer_, output_len_);
32 }
33 // else... any writing already happened directly into |output_|.
34 }
35
36 unsigned char* safe_buffer() {
37 return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_;
38 }
39
40 private:
41 // Pointer to the caller's data area and it's associated size, where data
42 // written via safe_buffer() will [eventually] end up.
43 unsigned char* output_;
44 size_t output_len_;
45
46 // Temporary buffer writen into in the case where the caller's
47 // buffer is not of sufficient size.
48 unsigned char min_sized_buffer_[MIN_SIZE];
49 };
50
51 } // namespace base
52
53 #endif // BASE_NSS_UTIL_H_
OLDNEW
« no previous file with comments | « base/hmac_openssl.cc ('k') | base/sha2_openssl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698