e7e7b5
diff -up ./lib/softoken/kbkdf.c.kdf_update ./lib/softoken/kbkdf.c
e7e7b5
--- ./lib/softoken/kbkdf.c.kdf_update	2019-11-27 16:48:01.864135431 -0800
e7e7b5
+++ ./lib/softoken/kbkdf.c	2019-11-27 16:48:51.779661708 -0800
e7e7b5
@@ -160,6 +160,9 @@ static CK_RV kbkdf_ValidateParameter(CK_
e7e7b5
         /* There is no additional data to validate for byte arrays; we can
e7e7b5
          * only assume the context is of the correct size. */
e7e7b5
         break;
e7e7b5
+    default:
e7e7b5
+        /* don't allow unknown types */
e7e7b5
+        return CKR_MECHANISM_PARAM_INVALID;
e7e7b5
     }
e7e7b5
 
e7e7b5
     return CKR_OK;
e7e7b5
@@ -250,14 +253,16 @@ static CK_RV kbkdf_ValidateParameters(CK
e7e7b5
             return CKR_MECHANISM_PARAM_INVALID;
e7e7b5
         }
e7e7b5
 
e7e7b5
-        /* Count that we have a parameter of this type. */
e7e7b5
-        param_type_count[params->pDataParams[offset].type] += 1;
e7e7b5
-
e7e7b5
         /* Validate this parameter has acceptable values. */
e7e7b5
         ret = kbkdf_ValidateParameter(mech, params->pDataParams + offset);
e7e7b5
         if (ret != CKR_OK) {
e7e7b5
-            return CKR_MECHANISM_PARAM_INVALID;
e7e7b5
+            return ret;
e7e7b5
         }
e7e7b5
+        /* Count that we have a parameter of this type. */
e7e7b5
+        /* Do this after we've validated the param to make sure we don't
e7e7b5
+         * overflow our array */
e7e7b5
+        PORT_Assert(params->pDataParams[offset].type < sizeof(param_type_count)/sizeof(param_type_count[0]));
e7e7b5
+        param_type_count[params->pDataParams[offset].type] += 1;
e7e7b5
     }
e7e7b5
 
e7e7b5
     if (mech == CKM_SP800_108_COUNTER_KDF || mech == CKM_NSS_SP800_108_COUNTER_KDF_DERIVE_DATA) {
e7e7b5
@@ -306,18 +311,20 @@ static CK_RV kbkdf_ValidateParameters(CK
e7e7b5
 /* [ section: parameter helpers ] */
e7e7b5
 
e7e7b5
 static void kbkdf_EncodeInteger(uint64_t integer, CK_ULONG num_bits, CK_BBOOL littleEndian, CK_BYTE_PTR output, CK_ULONG_PTR output_len) {
e7e7b5
-    uint64_t reordered;
e7e7b5
+    CK_ULONG num_bytes = num_bits/8;
e7e7b5
+    CK_ULONG i;
e7e7b5
     if (output_len) {
e7e7b5
-        *output_len = (num_bits / 8);
e7e7b5
+        *output_len = num_bytes;
e7e7b5
     }
e7e7b5
 
e7e7b5
     if (littleEndian == CK_TRUE) {
e7e7b5
-        reordered = htole64(integer);
e7e7b5
-        memcpy(output, &reordered, num_bits/8);
e7e7b5
+        for (i=0; i < num_bytes; i++) {
e7e7b5
+            output[i] = (integer >> i*8) & 0xff;
e7e7b5
+        }
e7e7b5
     } else {
e7e7b5
-        reordered = htobe64(integer);
e7e7b5
-        reordered = reordered >> (64 - num_bits);
e7e7b5
-        memcpy(output, &reordered, (num_bits/8));
e7e7b5
+        for (i=num_bytes; i > 0; i--) {
e7e7b5
+            output[num_bytes-i] = (integer >> (i-1)*8) & 0xff;
e7e7b5
+        }
e7e7b5
     }
e7e7b5
 }
e7e7b5