| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/chacha20_poly1305_decrypter.h" | 5 #include "net/quic/crypto/chacha20_poly1305_decrypter.h" |
| 6 | 6 |
| 7 #include <pk11pub.h> | 7 #include <pk11pub.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| 11 using base::StringPiece; | 11 using base::StringPiece; |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 const size_t kKeySize = 32; | 17 const size_t kKeySize = 32; |
| 18 const size_t kNoncePrefixSize = 0; | 18 const size_t kNoncePrefixSize = 0; |
| 19 | 19 |
| 20 } // namespace | 20 } // namespace |
| 21 | 21 |
| 22 #if defined(USE_NSS) | 22 #if defined(USE_NSS) |
| 23 | 23 |
| 24 // System NSS doesn't support ChaCha20+Poly1305 yet. | 24 // System NSS doesn't support ChaCha20+Poly1305 yet. |
| 25 | 25 |
| 26 ChaCha20Poly1305Decrypter::ChaCha20Poly1305Decrypter() | 26 ChaCha20Poly1305Decrypter::ChaCha20Poly1305Decrypter() |
| 27 : AeadBaseDecrypter(CKM_INVALID_MECHANISM, NULL, kKeySize, | 27 : AeadBaseDecrypter(CKM_INVALID_MECHANISM, |
| 28 kAuthTagSize, kNoncePrefixSize) { | 28 NULL, |
| 29 kKeySize, |
| 30 kAuthTagSize, |
| 31 kNoncePrefixSize) { |
| 29 NOTIMPLEMENTED(); | 32 NOTIMPLEMENTED(); |
| 30 } | 33 } |
| 31 | 34 |
| 32 ChaCha20Poly1305Decrypter::~ChaCha20Poly1305Decrypter() {} | 35 ChaCha20Poly1305Decrypter::~ChaCha20Poly1305Decrypter() { |
| 36 } |
| 33 | 37 |
| 34 // static | 38 // static |
| 35 bool ChaCha20Poly1305Decrypter::IsSupported() { | 39 bool ChaCha20Poly1305Decrypter::IsSupported() { |
| 36 return false; | 40 return false; |
| 37 } | 41 } |
| 38 | 42 |
| 39 void ChaCha20Poly1305Decrypter::FillAeadParams(StringPiece nonce, | 43 void ChaCha20Poly1305Decrypter::FillAeadParams(StringPiece nonce, |
| 40 StringPiece associated_data, | 44 StringPiece associated_data, |
| 41 size_t auth_tag_size, | 45 size_t auth_tag_size, |
| 42 AeadParams* aead_params) const { | 46 AeadParams* aead_params) const { |
| 43 NOTIMPLEMENTED(); | 47 NOTIMPLEMENTED(); |
| 44 } | 48 } |
| 45 | 49 |
| 46 #else // defined(USE_NSS) | 50 #else // defined(USE_NSS) |
| 47 | 51 |
| 48 ChaCha20Poly1305Decrypter::ChaCha20Poly1305Decrypter() | 52 ChaCha20Poly1305Decrypter::ChaCha20Poly1305Decrypter() |
| 49 : AeadBaseDecrypter(CKM_NSS_CHACHA20_POLY1305, PK11_Decrypt, kKeySize, | 53 : AeadBaseDecrypter(CKM_NSS_CHACHA20_POLY1305, |
| 50 kAuthTagSize, kNoncePrefixSize) { | 54 PK11_Decrypt, |
| 55 kKeySize, |
| 56 kAuthTagSize, |
| 57 kNoncePrefixSize) { |
| 51 COMPILE_ASSERT(kKeySize <= kMaxKeySize, key_size_too_big); | 58 COMPILE_ASSERT(kKeySize <= kMaxKeySize, key_size_too_big); |
| 52 COMPILE_ASSERT(kNoncePrefixSize <= kMaxNoncePrefixSize, | 59 COMPILE_ASSERT(kNoncePrefixSize <= kMaxNoncePrefixSize, |
| 53 nonce_prefix_size_too_big); | 60 nonce_prefix_size_too_big); |
| 54 } | 61 } |
| 55 | 62 |
| 56 ChaCha20Poly1305Decrypter::~ChaCha20Poly1305Decrypter() {} | 63 ChaCha20Poly1305Decrypter::~ChaCha20Poly1305Decrypter() { |
| 64 } |
| 57 | 65 |
| 58 // static | 66 // static |
| 59 bool ChaCha20Poly1305Decrypter::IsSupported() { | 67 bool ChaCha20Poly1305Decrypter::IsSupported() { |
| 60 return true; | 68 return true; |
| 61 } | 69 } |
| 62 | 70 |
| 63 void ChaCha20Poly1305Decrypter::FillAeadParams(StringPiece nonce, | 71 void ChaCha20Poly1305Decrypter::FillAeadParams(StringPiece nonce, |
| 64 StringPiece associated_data, | 72 StringPiece associated_data, |
| 65 size_t auth_tag_size, | 73 size_t auth_tag_size, |
| 66 AeadParams* aead_params) const { | 74 AeadParams* aead_params) const { |
| 67 aead_params->len = sizeof(aead_params->data.nss_aead_params); | 75 aead_params->len = sizeof(aead_params->data.nss_aead_params); |
| 68 CK_NSS_AEAD_PARAMS* nss_aead_params = &aead_params->data.nss_aead_params; | 76 CK_NSS_AEAD_PARAMS* nss_aead_params = &aead_params->data.nss_aead_params; |
| 69 nss_aead_params->pIv = | 77 nss_aead_params->pIv = |
| 70 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data())); | 78 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data())); |
| 71 nss_aead_params->ulIvLen = nonce.size(); | 79 nss_aead_params->ulIvLen = nonce.size(); |
| 72 nss_aead_params->pAAD = | 80 nss_aead_params->pAAD = |
| 73 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data())); | 81 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data())); |
| 74 nss_aead_params->ulAADLen = associated_data.size(); | 82 nss_aead_params->ulAADLen = associated_data.size(); |
| 75 nss_aead_params->ulTagLen = auth_tag_size; | 83 nss_aead_params->ulTagLen = auth_tag_size; |
| 76 } | 84 } |
| 77 | 85 |
| 78 #endif // defined(USE_NSS) | 86 #endif // defined(USE_NSS) |
| 79 | 87 |
| 80 } // namespace net | 88 } // namespace net |
| OLD | NEW |