Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / quic / crypto / chacha20_poly1305_decrypter_nss.cc
blob44f52c496ce2f1587856ad5aeb3174b7b26c0389
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.
5 #include "net/quic/crypto/chacha20_poly1305_decrypter.h"
7 #include <pk11pub.h>
9 #include "base/logging.h"
11 using base::StringPiece;
13 namespace net {
15 namespace {
17 const size_t kKeySize = 32;
18 const size_t kNoncePrefixSize = 0;
20 } // namespace
22 #if defined(USE_NSS_CERTS)
24 // System NSS doesn't support ChaCha20+Poly1305 yet.
26 ChaCha20Poly1305Decrypter::ChaCha20Poly1305Decrypter()
27 : AeadBaseDecrypter(CKM_INVALID_MECHANISM, nullptr, kKeySize,
28 kAuthTagSize, kNoncePrefixSize) {
29 NOTIMPLEMENTED();
32 ChaCha20Poly1305Decrypter::~ChaCha20Poly1305Decrypter() {}
34 // static
35 bool ChaCha20Poly1305Decrypter::IsSupported() {
36 return false;
39 void ChaCha20Poly1305Decrypter::FillAeadParams(
40 StringPiece nonce,
41 const StringPiece& associated_data,
42 size_t auth_tag_size,
43 AeadParams* aead_params) const {
44 NOTIMPLEMENTED();
47 #else // defined(USE_NSS_CERTS)
49 ChaCha20Poly1305Decrypter::ChaCha20Poly1305Decrypter()
50 : AeadBaseDecrypter(CKM_NSS_CHACHA20_POLY1305, PK11_Decrypt, kKeySize,
51 kAuthTagSize, kNoncePrefixSize) {
52 static_assert(kKeySize <= kMaxKeySize, "key size too big");
53 static_assert(kNoncePrefixSize <= kMaxNoncePrefixSize,
54 "nonce prefix size too big");
57 ChaCha20Poly1305Decrypter::~ChaCha20Poly1305Decrypter() {}
59 // static
60 bool ChaCha20Poly1305Decrypter::IsSupported() {
61 return true;
64 void ChaCha20Poly1305Decrypter::FillAeadParams(
65 StringPiece nonce,
66 const StringPiece& associated_data,
67 size_t auth_tag_size,
68 AeadParams* aead_params) const {
69 aead_params->len = sizeof(aead_params->data.nss_aead_params);
70 CK_NSS_AEAD_PARAMS* nss_aead_params = &aead_params->data.nss_aead_params;
71 nss_aead_params->pIv =
72 reinterpret_cast<CK_BYTE*>(const_cast<char*>(nonce.data()));
73 nss_aead_params->ulIvLen = nonce.size();
74 nss_aead_params->pAAD =
75 reinterpret_cast<CK_BYTE*>(const_cast<char*>(associated_data.data()));
76 nss_aead_params->ulAADLen = associated_data.size();
77 nss_aead_params->ulTagLen = auth_tag_size;
80 #endif // defined(USE_NSS_CERTS)
82 const char* ChaCha20Poly1305Decrypter::cipher_name() const {
83 // TODO(rtenneti): Use TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305 instead of
84 // hard coded string.
85 // return TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305;
86 return "ECDHE-RSA-CHACHA20-POLY1305";
89 uint32 ChaCha20Poly1305Decrypter::cipher_id() const {
90 // TODO(rtenneti): when Chromium requires NSS 3.15.2 or later, use
91 // TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 instead of 0xCC13.
92 // "OR" 0x03000000 to match OpenSSL/BoringSSL implementations.
93 return 0x03000000 | 0xCC13;
96 } // namespace net