OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/quic/crypto/aes_128_gcm_encrypter.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 |
| 11 using base::StringPiece; |
| 12 |
| 13 namespace net { |
| 14 |
| 15 namespace { |
| 16 |
| 17 const size_t kKeySize = 16; |
| 18 const size_t kNoncePrefixSize = 4; |
| 19 const size_t kAuthTagSize = 16; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 // static |
| 24 bool Aes128GcmEncrypter::IsSupported() { |
| 25 return false; |
| 26 } |
| 27 |
| 28 bool Aes128GcmEncrypter::SetKey(StringPiece key) { |
| 29 DCHECK_EQ(key.size(), sizeof(key_)); |
| 30 if (key.size() != sizeof(key_)) { |
| 31 return false; |
| 32 } |
| 33 memcpy(key_, key.data(), key.size()); |
| 34 return true; |
| 35 } |
| 36 |
| 37 bool Aes128GcmEncrypter::SetNoncePrefix(StringPiece nonce_prefix) { |
| 38 DCHECK_EQ(nonce_prefix.size(), kNoncePrefixSize); |
| 39 if (nonce_prefix.size() != kNoncePrefixSize) { |
| 40 return false; |
| 41 } |
| 42 memcpy(nonce_, nonce_prefix.data(), nonce_prefix.size()); |
| 43 return true; |
| 44 } |
| 45 |
| 46 QuicData* Aes128GcmEncrypter::Encrypt(QuicPacketSequenceNumber sequence_number, |
| 47 StringPiece associated_data, |
| 48 StringPiece plaintext) { |
| 49 COMPILE_ASSERT(sizeof(nonce_) == kNoncePrefixSize + sizeof(sequence_number), |
| 50 incorrect_nonce_size); |
| 51 memcpy(nonce_ + kNoncePrefixSize, &sequence_number, sizeof(sequence_number)); |
| 52 return EncryptWithNonce(StringPiece(reinterpret_cast<char*>(nonce_), |
| 53 sizeof(nonce_)), |
| 54 associated_data, plaintext); |
| 55 } |
| 56 |
| 57 size_t Aes128GcmEncrypter::GetKeySize() const { |
| 58 return kKeySize; |
| 59 } |
| 60 |
| 61 size_t Aes128GcmEncrypter::GetNoncePrefixSize() const { |
| 62 return kNoncePrefixSize; |
| 63 } |
| 64 |
| 65 size_t Aes128GcmEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { |
| 66 return ciphertext_size - kAuthTagSize; |
| 67 } |
| 68 |
| 69 // An AEAD_AES_128_GCM ciphertext is exactly 16 bytes longer than its |
| 70 // corresponding plaintext. |
| 71 size_t Aes128GcmEncrypter::GetCiphertextSize(size_t plaintext_size) const { |
| 72 return plaintext_size + kAuthTagSize; |
| 73 } |
| 74 |
| 75 QuicData* Aes128GcmEncrypter::EncryptWithNonce(StringPiece nonce, |
| 76 StringPiece associated_data, |
| 77 StringPiece plaintext) { |
| 78 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); |
| 79 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); |
| 80 |
| 81 // TODO(wtc): implement this function using NSS. |
| 82 |
| 83 return new QuicData(ciphertext.release(), ciphertext_size, true); |
| 84 } |
| 85 |
| 86 StringPiece Aes128GcmEncrypter::GetKey() const { |
| 87 return StringPiece(reinterpret_cast<const char*>(key_), sizeof(key_)); |
| 88 } |
| 89 |
| 90 StringPiece Aes128GcmEncrypter::GetNoncePrefix() const { |
| 91 return StringPiece(reinterpret_cast<const char*>(nonce_), kNoncePrefixSize); |
| 92 } |
| 93 |
| 94 } // namespace net |
OLD | NEW |