-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Implement openSSL based AES 256 CCM #326
Closed
bhaskar-apple
wants to merge
10
commits into
project-chip:master
from
bhaskar-apple:BS-AESCCMopenSSL
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b7fbd4a
Missing crypto PAL layer #287
Bhaskar-Sarma b9c3400
PR Feedback - Add doxygen, make params const, update sizes to be size_t
Bhaskar-Sarma a08cc46
Add @return to doxygen comment
Bhaskar-Sarma c783e82
Add tag_length to the encrypt API
Bhaskar-Sarma 9817553
Add ciphertext_length as an out param
Bhaskar-Sarma be85374
Implement AES 256 bit
Bhaskar-Sarma 2f76efd
Implement AES-256-CCM
Bhaskar-Sarma aa72461
Fix conflict
Bhaskar-Sarma feb0534
Fix a comment
Bhaskar-Sarma 1de9e64
Fix formatting
Bhaskar-Sarma 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
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 was deleted.
Oops, something went wrong.
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,170 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* openSSL based implementation of CHIP crypto primitives | ||
*/ | ||
|
||
#include "CHIPCryptoPAL.h" | ||
|
||
#include <openssl/conf.h> | ||
#include <openssl/evp.h> | ||
#include <openssl/err.h> | ||
#include <support/CodeUtils.h> | ||
#include <string.h> | ||
|
||
#define kKeyLengthInBits 256 | ||
|
||
bool _isValidTagLength(size_t tag_length) | ||
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. can this be |
||
{ | ||
if (tag_length == 8 || tag_length == 12 || tag_length == 16) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
CHIP_ERROR CHIP_aes_ccm_256_encrypt(const unsigned char * plaintext, size_t plaintext_length, const unsigned char * aad, | ||
size_t aad_length, const unsigned char * key, const unsigned char * iv, size_t iv_length, | ||
unsigned char * ciphertext, unsigned char * tag, size_t tag_length) | ||
{ | ||
EVP_CIPHER_CTX * context = NULL; | ||
int bytesWritten = 0; | ||
size_t ciphertext_length = 0; | ||
CHIP_ERROR error = CHIP_NO_ERROR; | ||
int result = 1; | ||
|
||
VerifyOrExit((plaintext != NULL && plaintext_length > 0), error = CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
VerifyOrExit(key != NULL, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrExit(iv != NULL && iv_length > 0, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrExit(_isValidTagLength(tag_length), error = CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
context = EVP_CIPHER_CTX_new(); | ||
VerifyOrExit(context != NULL, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in cipher | ||
result = EVP_EncryptInit_ex(context, EVP_aes_256_ccm(), NULL, NULL, NULL); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in IV length | ||
result = EVP_CIPHER_CTX_ctrl(context, EVP_CTRL_AEAD_SET_IVLEN, iv_length, NULL); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in tag length | ||
result = EVP_CIPHER_CTX_ctrl(context, EVP_CTRL_CCM_SET_TAG, tag_length, NULL); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in key + iv | ||
result = EVP_EncryptInit_ex(context, NULL, NULL, key, iv); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in plain text length | ||
result = EVP_EncryptUpdate(context, NULL, &bytesWritten, NULL, plaintext_length); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in AAD | ||
if (aad_length > 0) | ||
{ | ||
result = EVP_EncryptUpdate(context, NULL, &bytesWritten, aad, aad_length); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
} | ||
|
||
// Encrypt | ||
result = EVP_EncryptUpdate(context, ciphertext, &bytesWritten, plaintext, plaintext_length); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
ciphertext_length = bytesWritten; | ||
|
||
// Finalize encryption | ||
result = EVP_EncryptFinal_ex(context, ciphertext + ciphertext_length, &bytesWritten); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
ciphertext_length += bytesWritten; | ||
|
||
// Get tag | ||
EVP_CIPHER_CTX_ctrl(context, EVP_CTRL_CCM_GET_TAG, tag_length, tag); | ||
|
||
exit: | ||
if (context != NULL) | ||
{ | ||
EVP_CIPHER_CTX_free(context); | ||
context = NULL; | ||
} | ||
|
||
return error; | ||
} | ||
|
||
CHIP_ERROR CHIP_aes_ccm_256_decrypt(const unsigned char * ciphertext, size_t ciphertext_length, const unsigned char * aad, | ||
size_t aad_length, const unsigned char * tag, size_t tag_length, const unsigned char * key, | ||
const unsigned char * iv, size_t iv_length, unsigned char * plaintext) | ||
{ | ||
EVP_CIPHER_CTX * context = NULL; | ||
CHIP_ERROR error = CHIP_NO_ERROR; | ||
int bytesOutput = 0; | ||
int result = 1; | ||
|
||
VerifyOrExit(ciphertext != NULL, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrExit(ciphertext_length > 0, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrExit(tag != NULL, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrExit(tag_length > 0, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrExit(key != NULL, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrExit(iv != NULL, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrExit(iv_length > 0, error = CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
context = EVP_CIPHER_CTX_new(); | ||
VerifyOrExit(context != NULL, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in cipher | ||
result = EVP_DecryptInit_ex(context, EVP_aes_256_ccm(), NULL, NULL, NULL); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in IV length | ||
result = EVP_CIPHER_CTX_ctrl(context, EVP_CTRL_AEAD_SET_IVLEN, iv_length, NULL); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in expected tag | ||
result = EVP_CIPHER_CTX_ctrl(context, EVP_CTRL_AEAD_SET_TAG, tag_length, (void *) tag); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in key + iv | ||
result = EVP_DecryptInit_ex(context, NULL, NULL, key, iv); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in cipher text length | ||
result = EVP_DecryptUpdate(context, NULL, &bytesOutput, NULL, ciphertext_length); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
// Pass in aad | ||
if (aad_length > 0) | ||
{ | ||
result = EVP_DecryptUpdate(context, NULL, &bytesOutput, aad, aad_length); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
} | ||
|
||
// Pass in ciphertext | ||
result = EVP_DecryptUpdate(context, plaintext, &bytesOutput, ciphertext, ciphertext_length); | ||
VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); | ||
|
||
exit: | ||
if (context != NULL) | ||
{ | ||
EVP_CIPHER_CTX_free(context); | ||
context = NULL; | ||
} | ||
|
||
return error; | ||
} |
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
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.
is
build/x86_64-unknown-linux-gnu
working for you?reason I ask is that tasks.json has been changed to build into
build/default