@@ -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
}
@@ -2796,13 +2796,14 @@ class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2796
2796
size_t length;
2797
2797
mEarlyRv = GetKeyLengthForAlgorithm (aCx, aTargetAlgorithm, length);
2798
2798
2799
+ const Nullable<uint32_t > keyLength (length);
2799
2800
if (NS_SUCCEEDED(mEarlyRv )) {
2800
- Init (aCx, aAlgorithm, aKey, length );
2801
+ Init (aCx, aAlgorithm, aKey, keyLength );
2801
2802
}
2802
2803
}
2803
2804
2804
2805
void Init (JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey,
2805
- uint32_t aLength) {
2806
+ const Nullable< uint32_t >& aLength) {
2806
2807
Telemetry::Accumulate (Telemetry::WEBCRYPTO_ALG, TA_HKDF);
2807
2808
CHECK_KEY_ALGORITHM (aKey.Algorithm (), WEBCRYPTO_ALG_HKDF);
2808
2809
@@ -2818,8 +2819,8 @@ class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2818
2819
return ;
2819
2820
}
2820
2821
2821
- // length must be greater than zero and multiple of eight.
2822
- if (aLength == 0 || aLength % 8 != 0 ) {
2822
+ // length must be non-null and greater than zero and multiple of eight.
2823
+ if (aLength. IsNull () || aLength. Value () == 0 || aLength. Value () % 8 != 0 ) {
2823
2824
mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
2824
2825
return ;
2825
2826
}
@@ -2852,8 +2853,8 @@ class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2852
2853
2853
2854
ATTEMPT_BUFFER_INIT (mSalt , params.mSalt )
2854
2855
ATTEMPT_BUFFER_INIT (mInfo , params.mInfo )
2855
- mLengthInBytes = ceil ((double )aLength / 8 );
2856
- mLengthInBits = aLength;
2856
+ mLengthInBytes = ceil ((double )aLength. Value () / 8 );
2857
+ mLengthInBits = aLength. Value () ;
2857
2858
}
2858
2859
2859
2860
private:
@@ -2931,7 +2932,7 @@ class DeriveHkdfBitsTask : public ReturnArrayBufferViewTask {
2931
2932
class DerivePbkdfBitsTask : public ReturnArrayBufferViewTask {
2932
2933
public:
2933
2934
DerivePbkdfBitsTask (JSContext* aCx, const ObjectOrString& aAlgorithm,
2934
- CryptoKey& aKey, uint32_t aLength)
2935
+ CryptoKey& aKey, const Nullable< uint32_t >& aLength)
2935
2936
: mHashOidTag (SEC_OID_UNKNOWN) {
2936
2937
Init (aCx, aAlgorithm, aKey, aLength);
2937
2938
}
@@ -2942,13 +2943,14 @@ class DerivePbkdfBitsTask : public ReturnArrayBufferViewTask {
2942
2943
size_t length;
2943
2944
mEarlyRv = GetKeyLengthForAlgorithm (aCx, aTargetAlgorithm, length);
2944
2945
2946
+ const Nullable<uint32_t > keyLength (length);
2945
2947
if (NS_SUCCEEDED(mEarlyRv )) {
2946
- Init (aCx, aAlgorithm, aKey, length );
2948
+ Init (aCx, aAlgorithm, aKey, keyLength );
2947
2949
}
2948
2950
}
2949
2951
2950
2952
void Init (JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey,
2951
- uint32_t aLength) {
2953
+ const Nullable< uint32_t >& aLength) {
2952
2954
Telemetry::Accumulate (Telemetry::WEBCRYPTO_ALG, TA_PBKDF2);
2953
2955
CHECK_KEY_ALGORITHM (aKey.Algorithm (), WEBCRYPTO_ALG_PBKDF2);
2954
2956
@@ -2964,8 +2966,8 @@ class DerivePbkdfBitsTask : public ReturnArrayBufferViewTask {
2964
2966
return ;
2965
2967
}
2966
2968
2967
- // length must be a multiple of 8 bigger than zero.
2968
- if (aLength == 0 || aLength % 8 ) {
2969
+ // length must be non-null and greater than zero and multiple of eight .
2970
+ if (aLength. IsNull () || aLength. Value () == 0 || aLength. Value () % 8 ) {
2969
2971
mEarlyRv = NS_ERROR_DOM_OPERATION_ERR;
2970
2972
return ;
2971
2973
}
@@ -2997,7 +2999,7 @@ class DerivePbkdfBitsTask : public ReturnArrayBufferViewTask {
2997
2999
}
2998
3000
2999
3001
ATTEMPT_BUFFER_INIT (mSalt , params.mSalt )
3000
- mLength = aLength >> 3 ; // bits to bytes
3002
+ mLength = aLength. Value () >> 3 ; // bits to bytes
3001
3003
mIterations = params.mIterations ;
3002
3004
}
3003
3005
@@ -3101,16 +3103,22 @@ class DeriveKeyTask : public DeriveBitsTask {
3101
3103
class DeriveEcdhBitsTask : public ReturnArrayBufferViewTask {
3102
3104
public:
3103
3105
DeriveEcdhBitsTask (JSContext* aCx, const ObjectOrString& aAlgorithm,
3104
- CryptoKey& aKey, uint32_t aLength)
3105
- : mLengthInBits (Some( aLength) ), mPrivKey (aKey.GetPrivateKey()) {
3106
+ CryptoKey& aKey, const Nullable< uint32_t >& aLength)
3107
+ : mLengthInBits (aLength), mPrivKey (aKey.GetPrivateKey()) {
3106
3108
Init (aCx, aAlgorithm, aKey);
3107
3109
}
3108
3110
3109
3111
DeriveEcdhBitsTask (JSContext* aCx, const ObjectOrString& aAlgorithm,
3110
3112
CryptoKey& aKey, const ObjectOrString& aTargetAlgorithm)
3111
3113
: mPrivKey (aKey.GetPrivateKey()) {
3114
+ Maybe<size_t > lengthInBits;
3112
3115
mEarlyRv = GetKeyLengthForAlgorithmIfSpecified (aCx, aTargetAlgorithm,
3113
- mLengthInBits );
3116
+ lengthInBits);
3117
+ if (lengthInBits.isNothing ()) {
3118
+ mLengthInBits .SetNull ();
3119
+ } else {
3120
+ mLengthInBits .SetValue (*lengthInBits);
3121
+ }
3114
3122
if (NS_SUCCEEDED(mEarlyRv )) {
3115
3123
Init (aCx, aAlgorithm, aKey);
3116
3124
}
@@ -3128,8 +3136,8 @@ class DeriveEcdhBitsTask : public ReturnArrayBufferViewTask {
3128
3136
3129
3137
// If specified, length must be bigger than zero
3130
3138
// (otherwise, the full output of the key derivation is used).
3131
- if (mLengthInBits ) {
3132
- if (* mLengthInBits == 0 ) {
3139
+ if (! mLengthInBits . IsNull () ) {
3140
+ if (mLengthInBits . Value () == 0 ) {
3133
3141
mEarlyRv = NS_ERROR_DOM_DATA_ERR;
3134
3142
return ;
3135
3143
}
@@ -3163,7 +3171,7 @@ class DeriveEcdhBitsTask : public ReturnArrayBufferViewTask {
3163
3171
}
3164
3172
3165
3173
private:
3166
- Maybe< size_t > mLengthInBits ;
3174
+ Nullable< uint32_t > mLengthInBits ;
3167
3175
UniqueSECKEYPrivateKey mPrivKey ;
3168
3176
UniqueSECKEYPublicKey mPubKey ;
3169
3177
@@ -3189,21 +3197,21 @@ class DeriveEcdhBitsTask : public ReturnArrayBufferViewTask {
3189
3197
// data, so mResult manages one copy, while symKey manages another.
3190
3198
ATTEMPT_BUFFER_ASSIGN (mResult , PK11_GetKeyData (symKey.get ()));
3191
3199
3192
- if (mLengthInBits ) {
3193
- size_t mLengthInBytes =
3194
- ceil ((double )* mLengthInBits / 8 ); // bits to bytes
3195
- if (mLengthInBytes > mResult .Length ()) {
3200
+ if (! mLengthInBits . IsNull () ) {
3201
+ size_t length = mLengthInBits . Value ();
3202
+ size_t lengthInBytes = ceil ((double )length / 8 ); // bits to bytes
3203
+ if (lengthInBytes > mResult .Length ()) {
3196
3204
return NS_ERROR_DOM_OPERATION_ERR;
3197
3205
}
3198
3206
3199
- if (!mResult .SetLength (mLengthInBytes , fallible)) {
3207
+ if (!mResult .SetLength (lengthInBytes , fallible)) {
3200
3208
return NS_ERROR_DOM_UNKNOWN_ERR;
3201
3209
}
3202
3210
3203
3211
// If the number of bits to derive is not a multiple of 8 we need to
3204
3212
// zero out the remaining bits that were derived but not requested.
3205
- if (* mLengthInBits % 8 ) {
3206
- mResult [mResult .Length () - 1 ] &= 0xff << (8 - (* mLengthInBits % 8 ));
3213
+ if (length % 8 ) {
3214
+ mResult [mResult .Length () - 1 ] &= 0xff << (8 - (length % 8 ));
3207
3215
}
3208
3216
}
3209
3217
@@ -3558,7 +3566,7 @@ WebCryptoTask* WebCryptoTask::CreateDeriveKeyTask(
3558
3566
3559
3567
WebCryptoTask* WebCryptoTask::CreateDeriveBitsTask (
3560
3568
JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey,
3561
- uint32_t aLength) {
3569
+ const Nullable< uint32_t >& aLength) {
3562
3570
Telemetry::Accumulate (Telemetry::WEBCRYPTO_METHOD, TM_DERIVEBITS);
3563
3571
3564
3572
// Ensure baseKey is usable for this operation
0 commit comments