forked from rebolsource/r3
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHANGE: crypt port (work in progress)
- Loading branch information
Showing
38 changed files
with
12,494 additions
and
180 deletions.
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 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,139 @@ | ||
/*********************************************************************** | ||
** | ||
** REBOL [R3] Language Interpreter and Run-time Environment | ||
** | ||
** Copyright 2012 REBOL Technologies | ||
** Copyright 2012-2021 Rebol Open Source Contributors | ||
** REBOL is a trademark of REBOL Technologies | ||
** | ||
** 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. | ||
** | ||
************************************************************************ | ||
** | ||
** Module: n-crypt-aes.c | ||
** Summary: native functions for old AES code | ||
** Section: natives | ||
** Author: Oldes, Cyphre | ||
** Notes: | ||
** | ||
***********************************************************************/ | ||
|
||
#include "sys-core.h" | ||
#include "sys-aes.h" | ||
|
||
|
||
/*********************************************************************** | ||
** | ||
*/ REBNATIVE(aes) | ||
/* | ||
// aes: native [ | ||
// "Encrypt/decrypt data using AES algorithm. Returns stream cipher context handle or encrypted/decrypted data." | ||
// /key "Provided only for the first time to get stream HANDLE!" | ||
// crypt-key [binary!] "Crypt key (16 or 32 bytes)." | ||
// iv [none! binary!] "Optional initialization vector (16 bytes)." | ||
// /decrypt "Use the crypt-key for decryption (default is to encrypt)" | ||
// /stream | ||
// ctx [handle!] "Stream cipher context." | ||
// data [binary!] "Data to encrypt/decrypt." | ||
// ] | ||
***********************************************************************/ | ||
{ | ||
REBOOL ref_key = D_REF(1); | ||
REBVAL *val_crypt_key = D_ARG(2); | ||
REBVAL *val_iv = D_ARG(3); | ||
REBOOL ref_decrypt = D_REF(4); | ||
REBOOL ref_stream = D_REF(5); | ||
REBVAL *val_ctx = D_ARG(6); | ||
REBVAL *val_data = D_ARG(7); | ||
|
||
REBVAL *ret = D_RET; | ||
REBINT len, pad_len; | ||
|
||
//TODO: could be optimized by reusing the handle | ||
|
||
if (ref_key) { | ||
//key defined - setup new context | ||
|
||
uint8_t iv[AES_IV_SIZE]; | ||
|
||
if (IS_BINARY(val_iv)) { | ||
if (VAL_LEN(val_iv) < AES_IV_SIZE) { | ||
return R_NONE; | ||
} | ||
memcpy(iv, VAL_BIN_AT(val_iv), AES_IV_SIZE); | ||
} else { | ||
//TODO: Use ECB encryption if IV is not specified | ||
memset(iv, 0, AES_IV_SIZE); | ||
} | ||
|
||
len = VAL_LEN(val_crypt_key) << 3; | ||
|
||
if (len != 128 && len != 256) { | ||
return R_NONE; | ||
} | ||
|
||
MAKE_HANDLE(ret, SYM_AES); | ||
|
||
AES_set_key( | ||
(AES_CTX*)VAL_HANDLE_CONTEXT_DATA(ret), | ||
VAL_BIN_AT(val_crypt_key), | ||
(const uint8_t *)iv, | ||
(len == 128) ? AES_MODE_128 : AES_MODE_256 | ||
); | ||
|
||
if (ref_decrypt) AES_convert_key((AES_CTX*)VAL_HANDLE_CONTEXT_DATA(ret)); | ||
|
||
} else if(ref_stream) { | ||
|
||
if (NOT_VALID_CONTEXT_HANDLE(val_ctx, SYM_AES)) { | ||
Trap0(RE_INVALID_HANDLE); | ||
return R_NONE; | ||
} | ||
AES_CTX *aes_ctx = (AES_CTX *)VAL_HANDLE_CONTEXT_DATA(val_ctx); | ||
|
||
len = VAL_LEN(val_data); | ||
if (len == 0) return R_NONE; | ||
|
||
pad_len = (((len - 1) >> 4) << 4) + AES_BLOCKSIZE; | ||
|
||
REBYTE *data = VAL_BIN_AT(val_data); | ||
REBYTE *pad_data; | ||
|
||
if (len < pad_len) { | ||
// make new data input with zero-padding | ||
//TODO: instead of making new data, the original could be extended with padding. | ||
pad_data = (REBYTE*)MAKE_MEM(pad_len); | ||
memset(pad_data, 0, pad_len); | ||
memcpy(pad_data, data, len); | ||
data = pad_data; | ||
} | ||
else { | ||
pad_data = NULL; | ||
} | ||
|
||
REBSER *binaryOut = Make_Binary(pad_len); | ||
|
||
if (aes_ctx->key_mode == AES_MODE_DECRYPT) { | ||
AES_cbc_decrypt(aes_ctx, data, BIN_HEAD(binaryOut), pad_len); | ||
} | ||
else { | ||
AES_cbc_encrypt(aes_ctx, data, BIN_HEAD(binaryOut), pad_len); | ||
} | ||
if (pad_data) FREE_MEM(pad_data); | ||
|
||
SET_BINARY(ret, binaryOut); | ||
VAL_TAIL(ret) = pad_len; | ||
|
||
} | ||
return R_RET; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.