@@ -2481,8 +2481,8 @@ class GenerateSymmetricKeyTask : public WebCryptoTask {
2481
2481
class DeriveX25519BitsTask : public ReturnArrayBufferViewTask {
2482
2482
public:
2483
2483
DeriveX25519BitsTask (JSContext* aCx, const ObjectOrString& aAlgorithm,
2484
- CryptoKey& aKey, uint32_t aLength)
2485
- : mLength (Some( aLength) ), mPrivKey (aKey.GetPrivateKey()) {
2484
+ CryptoKey& aKey, const Nullable< uint32_t >& aLength)
2485
+ : mLength (aLength), mPrivKey (aKey.GetPrivateKey()) {
2486
2486
Init (aCx, aAlgorithm, aKey);
2487
2487
}
2488
2488
@@ -2504,12 +2504,12 @@ class DeriveX25519BitsTask : public ReturnArrayBufferViewTask {
2504
2504
2505
2505
// If specified, length must be a multiple of 8 bigger than zero
2506
2506
// (otherwise, the full output of the key derivation is used).
2507
- if (mLength ) {
2508
- if (* mLength == 0 || * mLength % 8 ) {
2507
+ if (! mLength . IsNull () ) {
2508
+ if (mLength . Value () == 0 || mLength . Value () % 8 ) {
2509
2509
mEarlyRv = NS_ERROR_DOM_DATA_ERR;
2510
2510
return ;
2511
2511
}
2512
- * mLength = * mLength >> 3 ; // bits to bytes
2512
+ mLength . SetValue ( mLength . Value () >> 3 ) ; // bits to bytes
2513
2513
}
2514
2514
2515
2515
// Retrieve the peer's public key.
@@ -2532,7 +2532,7 @@ class DeriveX25519BitsTask : public ReturnArrayBufferViewTask {
2532
2532
}
2533
2533
2534
2534
private:
2535
- Maybe< size_t > mLength ;
2535
+ Nullable< uint32_t > mLength ;
2536
2536
UniqueSECKEYPrivateKey mPrivKey ;
2537
2537
UniqueSECKEYPublicKey mPubKey ;
2538
2538
@@ -2563,11 +2563,11 @@ class DeriveX25519BitsTask : public ReturnArrayBufferViewTask {
2563
2563
// data, so mResult manages one copy, while symKey manages another.
2564
2564
ATTEMPT_BUFFER_ASSIGN (mResult , PK11_GetKeyData (symKey.get ()));
2565
2565
2566
- if (mLength ) {
2567
- if (* mLength > mResult .Length ()) {
2566
+ if (! mLength . IsNull () ) {
2567
+ if (mLength . Value () > mResult .Length ()) {
2568
2568
return NS_ERROR_DOM_OPERATION_ERR;
2569
2569
}
2570
- if (!mResult .SetLength (* mLength , fallible)) {
2570
+ if (!mResult .SetLength (mLength . Value () , fallible)) {
2571
2571
return NS_ERROR_DOM_UNKNOWN_ERR;
2572
2572
}
2573
2573
}
@@ -2785,7 +2785,7 @@ void GenerateAsymmetricKeyTask::Cleanup() { mKeyPair = nullptr; }
2785
2785
class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2786
2786
public:
2787
2787
DeriveHkdfBitsTask (JSContext* aCx, const ObjectOrString& aAlgorithm,
2788
- CryptoKey& aKey, uint32_t aLength)
2788
+ CryptoKey& aKey, const Nullable< uint32_t >& aLength)
2789
2789
: mMechanism (CKM_INVALID_MECHANISM) {
2790
2790
Init (aCx, aAlgorithm, aKey, aLength);
2791
2791
}
@@ -2802,7 +2802,7 @@ class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2802
2802
}
2803
2803
2804
2804
void Init (JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey,
2805
- uint32_t aLength) {
2805
+ Nullable< uint32_t > aLength) {
2806
2806
Telemetry::Accumulate (Telemetry::WEBCRYPTO_ALG, TA_HKDF);
2807
2807
CHECK_KEY_ALGORITHM (aKey.Algorithm (), WEBCRYPTO_ALG_HKDF);
2808
2808
@@ -2818,8 +2818,8 @@ class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2818
2818
return ;
2819
2819
}
2820
2820
2821
- // length must be greater than zero and multiple of eight.
2822
- if (aLength == 0 || aLength % 8 != 0 ) {
2821
+ // length must be non-null and greater than zero and multiple of eight.
2822
+ if (aLength. IsNull () || aLength. Value () == 0 || aLength. Value () % 8 != 0 ) {
2823
2823
mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
2824
2824
return ;
2825
2825
}
@@ -2852,8 +2852,8 @@ class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2852
2852
2853
2853
ATTEMPT_BUFFER_INIT (mSalt , params.mSalt )
2854
2854
ATTEMPT_BUFFER_INIT (mInfo , params.mInfo )
2855
- mLengthInBytes = ceil ((double )aLength / 8 );
2856
- mLengthInBits = aLength;
2855
+ mLengthInBytes = ceil ((double )aLength. Value () / 8 );
2856
+ mLengthInBits = aLength. Value () ;
2857
2857
}
2858
2858
2859
2859
private:
@@ -2931,7 +2931,7 @@ class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2931
2931
class DerivePbkdfBitsTask : public ReturnArrayBufferViewTask {
2932
2932
public:
2933
2933
DerivePbkdfBitsTask (JSContext* aCx, const ObjectOrString& aAlgorithm,
2934
- CryptoKey& aKey, uint32_t aLength)
2934
+ CryptoKey& aKey, Nullable< uint32_t > aLength)
2935
2935
: mHashOidTag (SEC_OID_UNKNOWN) {
2936
2936
Init (aCx, aAlgorithm, aKey, aLength);
2937
2937
}
@@ -2948,7 +2948,7 @@ class DerivePbkdfBitsTask : public ReturnArrayBufferViewTask {
2948
2948
}
2949
2949
2950
2950
void Init (JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey,
2951
- uint32_t aLength) {
2951
+ Nullable< uint32_t > aLength) {
2952
2952
Telemetry::Accumulate (Telemetry::WEBCRYPTO_ALG, TA_PBKDF2);
2953
2953
CHECK_KEY_ALGORITHM (aKey.Algorithm (), WEBCRYPTO_ALG_PBKDF2);
2954
2954
@@ -2964,8 +2964,8 @@ class DerivePbkdfBitsTask : public ReturnArrayBufferViewTask {
2964
2964
return ;
2965
2965
}
2966
2966
2967
- // length must be a multiple of 8 bigger than zero.
2968
- if (aLength == 0 || aLength % 8 ) {
2967
+ // length must be non-null and greater than zero and multiple of eight .
2968
+ if (aLength. IsNull () || aLength. Value () == 0 || aLength. Value () % 8 ) {
2969
2969
mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
2970
2970
return ;
2971
2971
}
@@ -2997,7 +2997,7 @@ class DerivePbkdfBitsTask : public ReturnArrayBufferViewTask {
2997
2997
}
2998
2998
2999
2999
ATTEMPT_BUFFER_INIT (mSalt , params.mSalt )
3000
- mLength = aLength >> 3 ; // bits to bytes
3000
+ mLength = aLength. Value () >> 3 ; // bits to bytes
3001
3001
mIterations = params.mIterations ;
3002
3002
}
3003
3003
@@ -3101,16 +3101,22 @@ class DeriveKeyTask : public DeriveBitsTask {
3101
3101
class DeriveEcdhBitsTask : public ReturnArrayBufferViewTask {
3102
3102
public:
3103
3103
DeriveEcdhBitsTask (JSContext* aCx, const ObjectOrString& aAlgorithm,
3104
- CryptoKey& aKey, uint32_t aLength)
3105
- : mLengthInBits (Some( aLength) ), mPrivKey (aKey.GetPrivateKey()) {
3104
+ CryptoKey& aKey, const Nullable< uint32_t >& aLength)
3105
+ : mLengthInBits (aLength), mPrivKey (aKey.GetPrivateKey()) {
3106
3106
Init (aCx, aAlgorithm, aKey);
3107
3107
}
3108
3108
3109
3109
DeriveEcdhBitsTask (JSContext* aCx, const ObjectOrString& aAlgorithm,
3110
3110
CryptoKey& aKey, const ObjectOrString& aTargetAlgorithm)
3111
3111
: mPrivKey (aKey.GetPrivateKey()) {
3112
+ Maybe<size_t > lengthInBits;
3112
3113
mEarlyRv = GetKeyLengthForAlgorithmIfSpecified (aCx, aTargetAlgorithm,
3113
- mLengthInBits );
3114
+ lengthInBits);
3115
+ if (lengthInBits.isNothing ()) {
3116
+ mLengthInBits .SetNull ();
3117
+ } else {
3118
+ mLengthInBits .SetValue (*lengthInBits);
3119
+ }
3114
3120
if (NS_SUCCEEDED(mEarlyRv )) {
3115
3121
Init (aCx, aAlgorithm, aKey);
3116
3122
}
@@ -3128,8 +3134,8 @@ class DeriveEcdhBitsTask : public ReturnArrayBufferViewTask {
3128
3134
3129
3135
// If specified, length must be bigger than zero
3130
3136
// (otherwise, the full output of the key derivation is used).
3131
- if (mLengthInBits ) {
3132
- if (* mLengthInBits == 0 ) {
3137
+ if (! mLengthInBits . IsNull () ) {
3138
+ if (mLengthInBits . Value () == 0 ) {
3133
3139
mEarlyRv = NS_ERROR_DOM_DATA_ERR;
3134
3140
return ;
3135
3141
}
@@ -3163,7 +3169,7 @@ class DeriveEcdhBitsTask : public ReturnArrayBufferViewTask {
3163
3169
}
3164
3170
3165
3171
private:
3166
- Maybe< size_t > mLengthInBits ;
3172
+ Nullable< uint32_t > mLengthInBits ;
3167
3173
UniqueSECKEYPrivateKey mPrivKey ;
3168
3174
UniqueSECKEYPublicKey mPubKey ;
3169
3175
@@ -3189,21 +3195,21 @@ class DeriveEcdhBitsTask : public ReturnArrayBufferViewTask {
3189
3195
// data, so mResult manages one copy, while symKey manages another.
3190
3196
ATTEMPT_BUFFER_ASSIGN (mResult , PK11_GetKeyData (symKey.get ()));
3191
3197
3192
- if (mLengthInBits ) {
3193
- size_t mLengthInBytes =
3194
- ceil ((double )* mLengthInBits / 8 ); // bits to bytes
3195
- if (mLengthInBytes > mResult .Length ()) {
3198
+ if (! mLengthInBits . IsNull () ) {
3199
+ size_t length = mLengthInBits . Value ();
3200
+ size_t lengthInBytes = ceil ((double )length / 8 ); // bits to bytes
3201
+ if (lengthInBytes > mResult .Length ()) {
3196
3202
return NS_ERROR_DOM_OPERATION_ERR;
3197
3203
}
3198
3204
3199
- if (!mResult .SetLength (mLengthInBytes , fallible)) {
3205
+ if (!mResult .SetLength (lengthInBytes , fallible)) {
3200
3206
return NS_ERROR_DOM_UNKNOWN_ERR;
3201
3207
}
3202
3208
3203
3209
// If the number of bits to derive is not a multiple of 8 we need to
3204
3210
// zero out the remaining bits that were derived but not requested.
3205
- if (* mLengthInBits % 8 ) {
3206
- mResult [mResult .Length () - 1 ] &= 0xff << (8 - (* mLengthInBits % 8 ));
3211
+ if (length % 8 ) {
3212
+ mResult [mResult .Length () - 1 ] &= 0xff << (8 - (length % 8 ));
3207
3213
}
3208
3214
}
3209
3215
@@ -3558,7 +3564,7 @@ WebCryptoTask* WebCryptoTask::CreateDeriveKeyTask(
3558
3564
3559
3565
WebCryptoTask* WebCryptoTask::CreateDeriveBitsTask (
3560
3566
JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey,
3561
- uint32_t aLength) {
3567
+ const Nullable< uint32_t >& aLength) {
3562
3568
Telemetry::Accumulate (Telemetry::WEBCRYPTO_METHOD, TM_DERIVEBITS);
3563
3569
3564
3570
// Ensure baseKey is usable for this operation
0 commit comments