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.
4
5#include "net/quic/crypto/chacha20_poly1305_decrypter.h"
6
7#include <pk11pub.h>
8
9#include "base/logging.h"
10
11using base::StringPiece;
12
13namespace net {
14
15namespace {
16
17const size_t kKeySize = 32;
18const size_t kNoncePrefixSize = 0;
19
20}  // namespace
21
22#if defined(USE_NSS)
23
24// System NSS doesn't support ChaCha20+Poly1305 yet.
25
26ChaCha20Poly1305Decrypter::ChaCha20Poly1305Decrypter()
27    : AeadBaseDecrypter(CKM_INVALID_MECHANISM, NULL, kKeySize,
28                        kAuthTagSize, kNoncePrefixSize) {
29  NOTIMPLEMENTED();
30}
31
32ChaCha20Poly1305Decrypter::~ChaCha20Poly1305Decrypter() {}
33
34// static
35bool ChaCha20Poly1305Decrypter::IsSupported() {
36  return false;
37}
38
39void ChaCha20Poly1305Decrypter::FillAeadParams(StringPiece nonce,
40                                               StringPiece associated_data,
41                                               size_t auth_tag_size,
42                                               AeadParams* aead_params) const {
43  NOTIMPLEMENTED();
44}
45
46#else  // defined(USE_NSS)
47
48ChaCha20Poly1305Decrypter::ChaCha20Poly1305Decrypter()
49    : AeadBaseDecrypter(CKM_NSS_CHACHA20_POLY1305, PK11_Decrypt, kKeySize,
50                        kAuthTagSize, kNoncePrefixSize) {
51  COMPILE_ASSERT(kKeySize <= kMaxKeySize, key_size_too_big);
52  COMPILE_ASSERT(kNoncePrefixSize <= kMaxNoncePrefixSize,
53                 nonce_prefix_size_too_big);
54}
55
56ChaCha20Poly1305Decrypter::~ChaCha20Poly1305Decrypter() {}
57
58// static
59bool ChaCha20Poly1305Decrypter::IsSupported() {
60  return true;
61}
62
63void ChaCha20Poly1305Decrypter::FillAeadParams(StringPiece nonce,
64                                               StringPiece associated_data,
65                                               size_t auth_tag_size,
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;
76}
77
78#endif  // defined(USE_NSS)
79
80}  // namespace net
81