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_encrypter.h"
9 #include "base/logging.h"
11 using base::StringPiece
;
17 const size_t kKeySize
= 32;
18 const size_t kNoncePrefixSize
= 0;
24 // System NSS doesn't support ChaCha20+Poly1305 yet.
26 ChaCha20Poly1305Encrypter::ChaCha20Poly1305Encrypter()
27 : AeadBaseEncrypter(CKM_INVALID_MECHANISM
, nullptr, kKeySize
,
28 kAuthTagSize
, kNoncePrefixSize
) {
32 ChaCha20Poly1305Encrypter::~ChaCha20Poly1305Encrypter() {}
35 bool ChaCha20Poly1305Encrypter::IsSupported() {
39 void ChaCha20Poly1305Encrypter::FillAeadParams(StringPiece nonce
,
40 StringPiece associated_data
,
42 AeadParams
* aead_params
) const {
46 #else // defined(USE_NSS)
48 ChaCha20Poly1305Encrypter::ChaCha20Poly1305Encrypter()
49 : AeadBaseEncrypter(CKM_NSS_CHACHA20_POLY1305
, PK11_Encrypt
, kKeySize
,
50 kAuthTagSize
, kNoncePrefixSize
) {
51 static_assert(kKeySize
<= kMaxKeySize
, "key size too big");
52 static_assert(kNoncePrefixSize
<= kMaxNoncePrefixSize
,
53 "nonce prefix size too big");
56 ChaCha20Poly1305Encrypter::~ChaCha20Poly1305Encrypter() {}
59 bool ChaCha20Poly1305Encrypter::IsSupported() {
63 void ChaCha20Poly1305Encrypter::FillAeadParams(StringPiece nonce
,
64 StringPiece associated_data
,
66 AeadParams
* aead_params
) const {
67 aead_params
->len
= sizeof(aead_params
->data
.nss_aead_params
);
68 CK_NSS_AEAD_PARAMS
* nss_aead_params
= &aead_params
->data
.nss_aead_params
;
69 nss_aead_params
->pIv
=
70 reinterpret_cast<CK_BYTE
*>(const_cast<char*>(nonce
.data()));
71 nss_aead_params
->ulIvLen
= nonce
.size();
72 nss_aead_params
->pAAD
=
73 reinterpret_cast<CK_BYTE
*>(const_cast<char*>(associated_data
.data()));
74 nss_aead_params
->ulAADLen
= associated_data
.size();
75 nss_aead_params
->ulTagLen
= auth_tag_size
;
78 #endif // defined(USE_NSS)