Skip to content

Commit 40802e1

Browse files
Jake ChampionJakeChampion
authored andcommitted
chore: refactor CryptoKeyUsageBitmap into a class
1 parent adb31f7 commit 40802e1

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

c-dependencies/js-compute-runtime/builtins/crypto-key.cpp

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@
33

44
namespace builtins {
55

6+
CryptoKeyUsages::CryptoKeyUsages(uint8_t mask) { this->mask = mask; };
7+
CryptoKeyUsages::CryptoKeyUsages(bool encrypt, bool decrypt, bool sign, bool verify,
8+
bool derive_key, bool derive_bits, bool wrap_key,
9+
bool unwrap_key) {
10+
this->mask = 0;
11+
if (encrypt) {
12+
this->mask |= encrypt_flag;
13+
}
14+
if (decrypt) {
15+
this->mask |= decrypt_flag;
16+
}
17+
if (sign) {
18+
this->mask |= sign_flag;
19+
}
20+
if (verify) {
21+
this->mask |= verify_flag;
22+
}
23+
if (derive_key) {
24+
this->mask |= derive_key_flag;
25+
}
26+
if (derive_bits) {
27+
this->mask |= derive_bits_flag;
28+
}
29+
if (wrap_key) {
30+
this->mask |= wrap_key_flag;
31+
}
32+
if (unwrap_key) {
33+
this->mask |= unwrap_key_flag;
34+
}
35+
};
36+
637
bool CryptoKey::algorithm_get(JSContext *cx, unsigned argc, JS::Value *vp) {
738
METHOD_HEADER(0);
839

@@ -114,7 +145,7 @@ bool CryptoKey::usages_get(JSContext *cx, unsigned argc, JS::Value *vp) {
114145
}
115146
// Else, grab the CryptoKeyUsageBitmap value from Slots::Usages and convert
116147
// it into a JS Array and store the result in Slots::UsagesArray.
117-
auto usage = JS::GetReservedSlot(self, Slots::Usages).toInt32();
148+
auto usage = CryptoKeyUsages(JS::GetReservedSlot(self, Slots::Usages).toInt32());
118149
// The result is ordered alphabetically.
119150
JS::RootedValueVector result(cx);
120151
JS::RootedString str(cx);
@@ -128,42 +159,42 @@ bool CryptoKey::usages_get(JSContext *cx, unsigned argc, JS::Value *vp) {
128159
}
129160
return true;
130161
};
131-
if (usage & CryptoKeyUsageDecrypt) {
162+
if (usage.canDecrypt()) {
132163
if (!append("decrypt")) {
133164
return false;
134165
}
135166
}
136-
if (usage & CryptoKeyUsageDeriveBits) {
167+
if (usage.canDeriveBits()) {
137168
if (!append("deriveBits")) {
138169
return false;
139170
}
140171
}
141-
if (usage & CryptoKeyUsageDeriveKey) {
172+
if (usage.canDeriveKey()) {
142173
if (!append("deriveKey")) {
143174
return false;
144175
}
145176
}
146-
if (usage & CryptoKeyUsageEncrypt) {
177+
if (usage.canEncrypt()) {
147178
if (!append("encrypt")) {
148179
return false;
149180
}
150181
}
151-
if (usage & CryptoKeyUsageSign) {
182+
if (usage.canSign()) {
152183
if (!append("sign")) {
153184
return false;
154185
}
155186
}
156-
if (usage & CryptoKeyUsageUnwrapKey) {
187+
if (usage.canUnwrapKey()) {
157188
if (!append("unwrapKey")) {
158189
return false;
159190
}
160191
}
161-
if (usage & CryptoKeyUsageVerify) {
192+
if (usage.canVerify()) {
162193
if (!append("verify")) {
163194
return false;
164195
}
165196
}
166-
if (usage & CryptoKeyUsageWrapKey) {
197+
if (usage.canWrapKey()) {
167198
if (!append("wrapKey")) {
168199
return false;
169200
}

c-dependencies/js-compute-runtime/builtins/crypto-key.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,31 @@ namespace builtins {
99

1010
enum class CryptoKeyType : uint8_t { Public, Private, Secret };
1111

12-
enum {
13-
CryptoKeyUsageEncrypt = 1 << 0,
14-
CryptoKeyUsageDecrypt = 1 << 1,
15-
CryptoKeyUsageSign = 1 << 2,
16-
CryptoKeyUsageVerify = 1 << 3,
17-
CryptoKeyUsageDeriveKey = 1 << 4,
18-
CryptoKeyUsageDeriveBits = 1 << 5,
19-
CryptoKeyUsageWrapKey = 1 << 6,
20-
CryptoKeyUsageUnwrapKey = 1 << 7
21-
};
12+
class CryptoKeyUsages {
13+
private:
14+
uint8_t mask;
15+
static constexpr const uint8_t encrypt_flag = 1 << 0;
16+
static constexpr const uint8_t decrypt_flag = 1 << 1;
17+
static constexpr const uint8_t sign_flag = 1 << 2;
18+
static constexpr const uint8_t verify_flag = 1 << 3;
19+
static constexpr const uint8_t derive_key_flag = 1 << 4;
20+
static constexpr const uint8_t derive_bits_flag = 1 << 5;
21+
static constexpr const uint8_t wrap_key_flag = 1 << 6;
22+
static constexpr const uint8_t unwrap_key_flag = 1 << 7;
2223

23-
typedef int CryptoKeyUsageBitmap;
24+
public:
25+
CryptoKeyUsages(uint8_t mask);
26+
CryptoKeyUsages(bool encrypt, bool decrypt, bool sign, bool verify, bool derive_key,
27+
bool derive_bits, bool wrap_key, bool unwrap_key);
28+
bool canEncrypt() { return this->mask & encrypt_flag; };
29+
bool canDecrypt() { return this->mask & decrypt_flag; };
30+
bool canSign() { return this->mask & sign_flag; };
31+
bool canVerify() { return this->mask & verify_flag; };
32+
bool canDeriveKey() { return this->mask & derive_key_flag; };
33+
bool canDeriveBits() { return this->mask & derive_bits_flag; };
34+
bool canWrapKey() { return this->mask & wrap_key_flag; };
35+
bool canUnwrapKey() { return this->mask & unwrap_key_flag; };
36+
};
2437

2538
class CryptoKey : public BuiltinImpl<CryptoKey> {
2639
public:

0 commit comments

Comments
 (0)