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
|
// 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_decrypter.h"
#include <openssl/evp.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
Aes128Gcm12Decrypter::Aes128Gcm12Decrypter() {}
// static
bool Aes128Gcm12Decrypter::IsSupported() { return true; }
bool Aes128Gcm12Decrypter::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 Aes128Gcm12Decrypter::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 Aes128Gcm12Decrypter::Decrypt(StringPiece nonce,
StringPiece associated_data,
StringPiece ciphertext,
unsigned char* output,
size_t* output_length) {
if (ciphertext.length() < kAuthTagSize ||
nonce.size() != kNoncePrefixSize + sizeof(QuicPacketSequenceNumber)) {
return false;
}
const size_t plaintext_size = ciphertext.length() - kAuthTagSize;
// |len| is passed to an OpenSSL function to receive the output length.
int len;
ScopedEVPCipherCtx ctx;
// Set the cipher type and the key. The IV (nonce) is set below.
if (EVP_DecryptInit_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_DecryptInit_ex(
ctx.get(), NULL, NULL, NULL,
reinterpret_cast<const unsigned char*>(nonce.data())) == 0) {
return false;
}
// Set the authentication tag.
if (EVP_CIPHER_CTX_ctrl(
ctx.get(), EVP_CTRL_GCM_SET_TAG, kAuthTagSize,
const_cast<char*>(ciphertext.data()) + plaintext_size) == 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_DecryptUpdate(
ctx.get(), NULL, &len,
reinterpret_cast<const unsigned char*>(associated_data.data()),
associated_data.size()) == 0) {
return false;
}
}
if (EVP_DecryptUpdate(
ctx.get(), output, &len,
reinterpret_cast<const unsigned char*>(ciphertext.data()),
plaintext_size) == 0) {
return false;
}
output += len;
if (EVP_DecryptFinal_ex(ctx.get(), output, &len) == 0) {
return false;
}
output += len;
*output_length = plaintext_size;
return true;
}
QuicData* Aes128Gcm12Decrypter::DecryptPacket(
QuicPacketSequenceNumber sequence_number,
StringPiece associated_data,
StringPiece ciphertext) {
COMPILE_ASSERT(sizeof(nonce_) == kNoncePrefixSize + sizeof(sequence_number),
incorrect_nonce_size);
if (ciphertext.length() < kAuthTagSize) {
return NULL;
}
size_t plaintext_size;
scoped_ptr<char[]> plaintext(new char[ciphertext.length()]);
memcpy(nonce_ + kNoncePrefixSize, &sequence_number, sizeof(sequence_number));
if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce_), sizeof(nonce_)),
associated_data, ciphertext,
reinterpret_cast<unsigned char*>(plaintext.get()),
&plaintext_size)) {
return NULL;
}
return new QuicData(plaintext.release(), plaintext_size, true);
}
StringPiece Aes128Gcm12Decrypter::GetKey() const {
return StringPiece(reinterpret_cast<const char*>(key_), sizeof(key_));
}
StringPiece Aes128Gcm12Decrypter::GetNoncePrefix() const {
return StringPiece(reinterpret_cast<const char*>(nonce_), kNoncePrefixSize);
}
} // namespace net
|