-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental code from HACL* for AES128-GCM #18
Open
karthikbhargavan
wants to merge
3
commits into
facebookincubator:main
Choose a base branch
from
karthikbhargavan:hacl_aes_gcm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "Hacl.h" | ||
|
||
#include "Hacl_AesGCM_NI.h" | ||
#include <fizz/crypto/aead/IOBufUtil.h> | ||
#include <folly/Range.h> | ||
#include <folly/lang/Bits.h> | ||
|
||
namespace fizz { | ||
namespace hacl { | ||
|
||
std::array<uint8_t, 12> Hacl::createIV(uint64_t seqNum) const { | ||
std::array<uint8_t, 12> iv; | ||
uint64_t bigEndianSeqNum = folly::Endian::big(seqNum); | ||
const size_t prefixLength = 12 - sizeof(uint64_t); | ||
memset(iv.data(), 0, prefixLength); | ||
memcpy(iv.data() + prefixLength, &bigEndianSeqNum, 8); | ||
XOR(key_.iv->coalesce(), folly::range(iv)); | ||
return iv; | ||
} | ||
|
||
void Hacl::setKey(TrafficKey tk) { | ||
tk.key->coalesce(); | ||
tk.iv->coalesce(); | ||
key_ = std::move(tk); | ||
} | ||
|
||
std::unique_ptr<folly::IOBuf> Hacl::encrypt( | ||
std::unique_ptr<folly::IOBuf>&& plaintext, | ||
const folly::IOBuf* associatedData, | ||
uint64_t seqNum) const { | ||
Lib_Vec128_vec128 ctx[22] = {0}; | ||
// get iv and init hacl | ||
auto iv = createIV(seqNum); | ||
uint8_t* keyData = const_cast<uint8_t*>(key_.key->data()); | ||
Hacl_AesGCM_NI_aes128_gcm_init(ctx, keyData, iv.data()); | ||
|
||
// plaintext needs to be coalesced | ||
plaintext->coalesce(); | ||
auto inputLen = plaintext->computeChainDataLength(); | ||
// output needs to be one contiguous buffer w/ room for the tag | ||
auto out = folly::IOBuf::create(headroom_ + inputLen + getCipherOverhead()); | ||
out->advance(headroom_); | ||
out->append(inputLen + getCipherOverhead()); | ||
auto inData = const_cast<uint8_t*>(plaintext->data()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. writableData() here will do what you want. |
||
|
||
// set up aad | ||
uint8_t* aad = nullptr; | ||
size_t aadLen = 0; | ||
if (associatedData) { | ||
auto adbuf = const_cast<folly::IOBuf*>(associatedData); | ||
adbuf->coalesce(); | ||
aad = const_cast<uint8_t*>(adbuf->data()); | ||
aadLen = adbuf->computeChainDataLength(); | ||
} | ||
|
||
// hacl encrypt! | ||
Hacl_AesGCM_NI_aes128_gcm_encrypt( | ||
ctx, inputLen, out->writableData(), inData, aadLen, aad); | ||
|
||
// assume it worked? | ||
return out; | ||
} | ||
|
||
folly::Optional<std::unique_ptr<folly::IOBuf>> Hacl::tryDecrypt( | ||
std::unique_ptr<folly::IOBuf>&& ciphertext, | ||
const folly::IOBuf* associatedData, | ||
uint64_t seqNum) const { | ||
Lib_Vec128_vec128 ctx[22] = {0}; | ||
// set up | ||
// get iv and init hacl | ||
auto iv = createIV(seqNum); | ||
uint8_t* keyData = const_cast<uint8_t*>(key_.key->data()); | ||
Hacl_AesGCM_NI_aes128_gcm_init(ctx, keyData, iv.data()); | ||
|
||
// set up aad | ||
uint8_t* aad = nullptr; | ||
size_t aadLen = 0; | ||
if (associatedData) { | ||
auto adbuf = const_cast<folly::IOBuf*>(associatedData); | ||
adbuf->coalesce(); | ||
aad = const_cast<uint8_t*>(adbuf->data()); | ||
aadLen = adbuf->computeChainDataLength(); | ||
} | ||
|
||
ciphertext->coalesce(); | ||
auto inputLen = ciphertext->computeChainDataLength(); | ||
if (inputLen <= getCipherOverhead()) { | ||
return folly::none; | ||
} | ||
auto out = folly::IOBuf::create(inputLen - getCipherOverhead()); | ||
out->append(inputLen - getCipherOverhead()); | ||
|
||
auto cipherData = const_cast<uint8_t*>(ciphertext->data()); | ||
|
||
auto res = Hacl_AesGCM_NI_aes128_gcm_decrypt( | ||
ctx, inputLen-16, out->writableData(), cipherData, aadLen, aad); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: spacing here inputLen - 16 |
||
|
||
if (!res) { | ||
return folly::none; | ||
} | ||
return out; | ||
} | ||
|
||
} // namespace hacl | ||
} // namespace fizz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2018-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
#pragma once | ||
|
||
#include <fizz/crypto/aead/Aead.h> | ||
|
||
namespace fizz { | ||
namespace hacl { | ||
|
||
class Hacl : public Aead { | ||
public: | ||
size_t keyLength() const override { | ||
return 16; | ||
} | ||
|
||
size_t ivLength() const override { | ||
return 12; | ||
} | ||
|
||
void setEncryptedBufferHeadroom(size_t headroom) override { | ||
headroom_ = headroom; | ||
} | ||
|
||
void setKey(TrafficKey trafficKey) override; | ||
|
||
std::unique_ptr<folly::IOBuf> encrypt( | ||
std::unique_ptr<folly::IOBuf>&& plaintext, | ||
const folly::IOBuf* associatedData, | ||
uint64_t seqNum) const override; | ||
|
||
folly::Optional<std::unique_ptr<folly::IOBuf>> tryDecrypt( | ||
std::unique_ptr<folly::IOBuf>&& ciphertext, | ||
const folly::IOBuf* associatedData, | ||
uint64_t seqNum) const override; | ||
|
||
size_t getCipherOverhead() const override { | ||
return 16; | ||
} | ||
|
||
std::array<uint8_t, 12> createIV(uint64_t seqNum) const; | ||
|
||
private: | ||
size_t headroom_{5}; | ||
TrafficKey key_; | ||
}; | ||
|
||
} // namespace hacl | ||
} // namespace fizz |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you need to modify the key here? or is it just a limitation of the hacl interface?
iobuf has a writableData() as well which will give you a non const ptr to the data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const cast is fine if you dont have to change the key but its just takes a non const ptr