OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/crypto/aead_base_encrypter.h" | 5 #include "net/quic/crypto/aead_base_encrypter.h" |
6 | 6 |
7 #include <pk11pub.h> | 7 #include <pk11pub.h> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "crypto/scoped_nss_types.h" | 10 #include "crypto/scoped_nss_types.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // workaround. Remove this when we require NSS 3.15. | 70 // workaround. Remove this when we require NSS 3.15. |
71 CK_MECHANISM_TYPE key_mechanism = aead_mechanism_; | 71 CK_MECHANISM_TYPE key_mechanism = aead_mechanism_; |
72 if (key_mechanism == CKM_AES_GCM) { | 72 if (key_mechanism == CKM_AES_GCM) { |
73 key_mechanism = CKM_AES_ECB; | 73 key_mechanism = CKM_AES_ECB; |
74 } | 74 } |
75 | 75 |
76 // The exact value of the |origin| argument doesn't matter to NSS as long as | 76 // The exact value of the |origin| argument doesn't matter to NSS as long as |
77 // it's not PK11_OriginFortezzaHack, so we pass PK11_OriginUnwrap as a | 77 // it's not PK11_OriginFortezzaHack, so we pass PK11_OriginUnwrap as a |
78 // placeholder. | 78 // placeholder. |
79 crypto::ScopedPK11SymKey aead_key(PK11_ImportSymKey( | 79 crypto::ScopedPK11SymKey aead_key(PK11_ImportSymKey( |
80 slot, key_mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &key_item, NULL)); | 80 slot, key_mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &key_item, nullptr)); |
81 PK11_FreeSlot(slot); | 81 PK11_FreeSlot(slot); |
82 slot = NULL; | 82 slot = nullptr; |
83 if (!aead_key) { | 83 if (!aead_key) { |
84 DVLOG(1) << "PK11_ImportSymKey failed"; | 84 DVLOG(1) << "PK11_ImportSymKey failed"; |
85 return false; | 85 return false; |
86 } | 86 } |
87 | 87 |
88 AeadParams aead_params = {0}; | 88 AeadParams aead_params = {0}; |
89 FillAeadParams(nonce, associated_data, auth_tag_size_, &aead_params); | 89 FillAeadParams(nonce, associated_data, auth_tag_size_, &aead_params); |
90 | 90 |
91 SECItem param; | 91 SECItem param; |
92 param.type = siBuffer; | 92 param.type = siBuffer; |
(...skipping 27 matching lines...) Expand all Loading... |
120 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the | 120 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the |
121 // same sequence number twice. | 121 // same sequence number twice. |
122 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; | 122 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; |
123 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); | 123 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); |
124 DCHECK_LE(nonce_size, sizeof(nonce)); | 124 DCHECK_LE(nonce_size, sizeof(nonce)); |
125 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); | 125 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); |
126 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); | 126 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); |
127 if (!Encrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), | 127 if (!Encrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), |
128 associated_data, plaintext, | 128 associated_data, plaintext, |
129 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | 129 reinterpret_cast<unsigned char*>(ciphertext.get()))) { |
130 return NULL; | 130 return nullptr; |
131 } | 131 } |
132 | 132 |
133 return new QuicData(ciphertext.release(), ciphertext_size, true); | 133 return new QuicData(ciphertext.release(), ciphertext_size, true); |
134 } | 134 } |
135 | 135 |
136 size_t AeadBaseEncrypter::GetKeySize() const { return key_size_; } | 136 size_t AeadBaseEncrypter::GetKeySize() const { return key_size_; } |
137 | 137 |
138 size_t AeadBaseEncrypter::GetNoncePrefixSize() const { | 138 size_t AeadBaseEncrypter::GetNoncePrefixSize() const { |
139 return nonce_prefix_size_; | 139 return nonce_prefix_size_; |
140 } | 140 } |
(...skipping 12 matching lines...) Expand all Loading... |
153 | 153 |
154 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { | 154 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { |
155 if (nonce_prefix_size_ == 0) { | 155 if (nonce_prefix_size_ == 0) { |
156 return StringPiece(); | 156 return StringPiece(); |
157 } | 157 } |
158 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 158 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
159 nonce_prefix_size_); | 159 nonce_prefix_size_); |
160 } | 160 } |
161 | 161 |
162 } // namespace net | 162 } // namespace net |
OLD | NEW |