Skip to content

Commit bb0862c

Browse files
author
Oscar Franco
authored
publicEncrypt API (#57)
1 parent d299f77 commit bb0862c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4847
-400
lines changed

CPPLINT.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
filter=-build/namespaces,-legal/copyright,-build/header_guard,-readability/casting,-runtime/references,-whitespace/newline,-build/c++11,-build/include_subdir
1+
filter=-build/namespaces,-legal/copyright,-build/header_guard,-readability/casting,-runtime/references,-whitespace/newline,-build/c++11,-build/include_subdir,-whitespace/comments,-runtime/int

cpp/Cipher/MGLCipherHostObject.cpp

+64-48
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include "MGLCipherHostObject.h"
55

66
#ifdef ANDROID
7+
#include "JSIUtils/MGLJSIUtils.h"
78
#include "JSIUtils/MGLTypedArray.h"
89
#else
10+
#include "MGLJSIUtils.h"
911
#include "MGLTypedArray.h"
1012
#endif
1113

@@ -106,7 +108,7 @@ MGLCipherHostObject::MGLCipherHostObject(
106108

107109
int key_len =
108110
EVP_BytesToKey(cipher, EVP_md5(), nullptr, cipher_key->data(runtime),
109-
cipher_key->size(runtime), 1, key, iv);
111+
static_cast<int>(cipher_key->size(runtime)), 1, key, iv);
110112

111113
// TODO(osp) this looks like a macro, check if necessary
112114
// CHECK_NE(key_len, 0);
@@ -445,62 +447,76 @@ void MGLCipherHostObject::installMethods() {
445447
}));
446448

447449
// setAuthTag
448-
this->fields.push_back(HOST_LAMBDA("setAuthTag", {
449-
if (count != 1 || !arguments[0].isObject() ||
450-
!arguments[0].asObject(runtime).isArrayBuffer(runtime)) {
451-
throw jsi::JSError(
452-
runtime, "cipher.setAuthTag requires an ArrayBuffer tag argument");
453-
}
450+
this->fields.push_back(buildPair(
451+
"setAuthTag", JSIF([=]) {
452+
if (count != 1 || !arguments[0].isObject() ||
453+
!arguments[0].asObject(runtime).isArrayBuffer(runtime)) {
454+
jsi::detail::throwJSError(
455+
runtime,
456+
"cipher.setAuthTag requires an ArrayBuffer tag argument");
457+
throw jsi::JSError(
458+
runtime,
459+
"cipher.setAuthTag requires an ArrayBuffer tag argument");
460+
}
454461

455-
if (!ctx_ || !IsAuthenticatedMode() || isCipher_ ||
456-
auth_tag_state_ != kAuthTagUnknown) {
457-
return false;
458-
}
462+
if (!ctx_ || !IsAuthenticatedMode() || isCipher_ ||
463+
auth_tag_state_ != kAuthTagUnknown) {
464+
return false;
465+
}
459466

460-
auto authTagArrayBuffer =
461-
arguments[0].asObject(runtime).getArrayBuffer(runtime);
462-
const unsigned char *data = authTagArrayBuffer.data(runtime);
463-
auto tag_len = authTagArrayBuffer.length(runtime);
467+
auto authTagArrayBuffer =
468+
arguments[0].asObject(runtime).getArrayBuffer(runtime);
469+
if (!CheckSizeInt32(runtime, authTagArrayBuffer)) {
470+
jsi::detail::throwJSError(
471+
runtime,
472+
"cipher.setAuthTag requires an ArrayBuffer tag argument");
473+
throw jsi::JSError(
474+
runtime,
475+
"cipher.setAuthTag requires an ArrayBuffer tag argument");
476+
}
477+
// const unsigned char *data = authTagArrayBuffer.data(runtime);
478+
unsigned int tag_len =
479+
static_cast<unsigned int>(authTagArrayBuffer.length(runtime));
464480

465-
// ArrayBufferOrViewContents<char> auth_tag(args[0]);
466-
// TODO(osp) implement this check
467-
// if (UNLIKELY(!auth_tag.CheckSizeInt32()))
468-
// return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big");
481+
// ArrayBufferOrViewContents<char> auth_tag(args[0]);
482+
// TODO(osp) implement this check
483+
// if (UNLIKELY(!auth_tag.CheckSizeInt32()))
484+
// return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big");
469485

470-
// unsigned int tag_len = auth_tag.size();
486+
// unsigned int tag_len = auth_tag.size();
471487

472-
const int mode = EVP_CIPHER_CTX_mode(ctx_);
473-
bool is_valid;
474-
if (mode == EVP_CIPH_GCM_MODE) {
475-
// Restrict GCM tag lengths according to NIST 800-38d, page 9.
476-
is_valid =
477-
(auth_tag_len_ == kNoAuthTagLength || auth_tag_len_ == tag_len) &&
478-
IsValidGCMTagLength(tag_len);
479-
} else {
480-
// At this point, the tag length is already known and must match the
481-
// length of the given authentication tag.
482-
// TODO(osp) add CHECK here
483-
IsSupportedAuthenticatedMode(ctx_);
484-
// CHECK_NE(cipher->auth_tag_len_, kNoAuthTagLength);
485-
is_valid = auth_tag_len_ == tag_len;
486-
}
488+
const int mode = EVP_CIPHER_CTX_mode(ctx_);
489+
bool is_valid;
490+
if (mode == EVP_CIPH_GCM_MODE) {
491+
// Restrict GCM tag lengths according to NIST 800-38d, page 9.
492+
is_valid =
493+
(auth_tag_len_ == kNoAuthTagLength || auth_tag_len_ == tag_len) &&
494+
IsValidGCMTagLength(tag_len);
495+
} else {
496+
// At this point, the tag length is already known and must match the
497+
// length of the given authentication tag.
498+
// TODO(osp) add CHECK here
499+
IsSupportedAuthenticatedMode(ctx_);
500+
// CHECK_NE(cipher->auth_tag_len_, kNoAuthTagLength);
501+
is_valid = auth_tag_len_ == tag_len;
502+
}
487503

488-
if (!is_valid) {
489-
throw jsi::JSError(runtime,
490-
"Invalid authentication tag length: " + tag_len);
491-
// return THROW_ERR_CRYPTO_INVALID_AUTH_TAG(
492-
// env, "Invalid authentication tag length: %u", tag_len);
493-
}
504+
if (!is_valid) {
505+
jsi::detail::throwJSError(
506+
runtime, "Invalid authentication tag length" + tag_len);
507+
throw jsi::JSError(runtime,
508+
"Invalid authentication tag length: " + tag_len);
509+
}
494510

495-
auth_tag_len_ = tag_len;
496-
auth_tag_state_ = kAuthTagKnown;
497-
// CHECK_LE(cipher->auth_tag_len_, sizeof(cipher->auth_tag_));
511+
auth_tag_len_ = tag_len;
512+
auth_tag_state_ = kAuthTagKnown;
513+
// CHECK_LE(cipher->auth_tag_len_, sizeof(cipher->auth_tag_));
498514

499-
memset(auth_tag_, 0, sizeof(auth_tag_));
500-
CopyTo(runtime, &authTagArrayBuffer, auth_tag_, auth_tag_len_);
515+
memset(auth_tag_, 0, sizeof(auth_tag_));
516+
CopyTo(runtime, &authTagArrayBuffer, auth_tag_, auth_tag_len_);
501517

502-
return true;
503-
}));
518+
return true;
519+
}));
504520
}
505521

506522
bool MGLCipherHostObject::MaybePassAuthTagToOpenSSL() {

0 commit comments

Comments
 (0)