1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
#include <openssl/evp.h>
#include <string.h>
#include "base/memory/scoped_ptr.h"
#include "net/quic/crypto/scoped_evp_cipher_ctx.h"
using base::StringPiece;
namespace net {
namespace {
const size_t kKeySize = 16;
const size_t kNoncePrefixSize = 4;
} // namespace
Aes128Gcm12Encrypter::Aes128Gcm12Encrypter() {
}
// static
bool Aes128Gcm12Encrypter::IsSupported() { return true; }
bool Aes128Gcm12Encrypter::SetKey(StringPiece key) {
DCHECK_EQ(key.size(), sizeof(key_));
if (key.size() != sizeof(key_)) {
return false;
}
memcpy(key_, key.data(), key.size());
return true;
}
bool Aes128Gcm12Encrypter::SetNoncePrefix(StringPiece nonce_prefix) {
DCHECK_EQ(nonce_prefix.size(), kNoncePrefixSize);
if (nonce_prefix.size() != kNoncePrefixSize) {
return false;
}
memcpy(nonce_, nonce_prefix.data(), nonce_prefix.size());
return true;
}
bool Aes128Gcm12Encrypter::Encrypt(StringPiece nonce,
StringPiece associated_data,
StringPiece plaintext,
unsigned char* output) {
// |output_len| is passed to an OpenSSL function to receive the output
// length.
int output_len;
if (nonce.size() != kNoncePrefixSize + sizeof(QuicPacketSequenceNumber)) {
return false;
}
ScopedEVPCipherCtx ctx;
// Set the cipher type and the key. The IV (nonce) is set below.
if (EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_gcm(), NULL, key_, NULL) == 0) {
return false;
}
// Set the IV (nonce) length.
if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN, nonce.size(),
NULL) == 0) {
return false;
}
// Set the IV (nonce).
if (EVP_EncryptInit_ex(
ctx.get(), NULL, NULL, NULL,
reinterpret_cast<const unsigned char*>(nonce.data())) == 0) {
return false;
}
// If we pass a NULL, zero-length associated data to OpenSSL then it breaks.
// Thus we only set non-empty associated data.
if (!associated_data.empty()) {
// Set the associated data. The second argument (output buffer) must be
// NULL.
if (EVP_EncryptUpdate(
ctx.get(), NULL, &output_len,
reinterpret_cast<const unsigned char*>(associated_data.data()),
associated_data.size()) == 0) {
return false;
}
}
if (EVP_EncryptUpdate(
ctx.get(), output, &output_len,
reinterpret_cast<const unsigned char*>(plaintext.data()),
plaintext.size()) == 0) {
return false;
}
output += output_len;
if (EVP_EncryptFinal_ex(ctx.get(), output, &output_len) == 0) {
return false;
}
output += output_len;
if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, kAuthTagSize,
output) == 0) {
return false;
}
return true;
}
QuicData* Aes128Gcm12Encrypter::EncryptPacket(
QuicPacketSequenceNumber sequence_number,
StringPiece associated_data,
StringPiece plaintext) {
COMPILE_ASSERT(sizeof(nonce_) == kNoncePrefixSize + sizeof(sequence_number),
incorrect_nonce_size);
memcpy(nonce_ + kNoncePrefixSize, &sequence_number, sizeof(sequence_number));
size_t ciphertext_size = GetCiphertextSize(plaintext.length());
scoped_ptr<char[]> ciphertext(new char[ciphertext_size]);
if (!Encrypt(StringPiece(reinterpret_cast<char*>(nonce_), sizeof(nonce_)),
associated_data, plaintext,
reinterpret_cast<unsigned char*>(ciphertext.get()))) {
return NULL;
}
return new QuicData(ciphertext.release(), ciphertext_size, true);
}
size_t Aes128Gcm12Encrypter::GetKeySize() const { return kKeySize; }
size_t Aes128Gcm12Encrypter::GetNoncePrefixSize() const {
return kNoncePrefixSize;
}
size_t Aes128Gcm12Encrypter::GetMaxPlaintextSize(size_t ciphertext_size) const {
return ciphertext_size - kAuthTagSize;
}
// An AEAD_AES_128_GCM_12 ciphertext is exactly 12 bytes longer than its
// corresponding plaintext.
size_t Aes128Gcm12Encrypter::GetCiphertextSize(size_t plaintext_size) const {
return plaintext_size + kAuthTagSize;
}
StringPiece Aes128Gcm12Encrypter::GetKey() const {
return StringPiece(reinterpret_cast<const char*>(key_), sizeof(key_));
}
StringPiece Aes128Gcm12Encrypter::GetNoncePrefix() const {
return StringPiece(reinterpret_cast<const char*>(nonce_), kNoncePrefixSize);
}
} // namespace net
|