commit 409e7278622265a3f4483ae71756837e2911dac0
Author: Hans Petter Jansson <hpj@suse.com>
Date:   Tue Sep 19 02:09:01 2017 +0200

    DH, ECDH: Add power-up KATs.

diff --git a/nss/lib/freebl/fipsfreebl.c b/nss/lib/freebl/fipsfreebl.c
index 143d9c9..c7c279f 100644
--- a/nss/lib/freebl/fipsfreebl.c
+++ b/nss/lib/freebl/fipsfreebl.c
@@ -1275,6 +1275,186 @@ freebl_fips_ECDSA_PowerUpSelfTest()
     return (SECSuccess);
 }
 
+/*
+ * Initializes a SECItem from a hexadecimal string
+ *
+ * Warning: This function ignores leading 00's, so any leading 00's
+ * in the hexadecimal string must be optional.
+ */
+static SECItem *
+hexString2SECItem(PLArenaPool *arena, SECItem *item, const char *str)
+{
+    int i = 0;
+    int byteval = 0;
+    int tmp = PORT_Strlen(str);
+
+    PORT_Assert(arena);
+    PORT_Assert(item);
+
+    if ((tmp % 2) != 0)
+        return NULL;
+
+    /* skip leading 00's unless the hex string is "00" */
+    while ((tmp > 2) && (str[0] == '0') && (str[1] == '0')) {
+        str += 2;
+        tmp -= 2;
+    }
+
+    item->data = (unsigned char *)PORT_ArenaAlloc(arena, tmp / 2);
+    if (item->data == NULL)
+        return NULL;
+    item->len = tmp / 2;
+
+    while (str[i]) {
+        if ((str[i] >= '0') && (str[i] <= '9'))
+            tmp = str[i] - '0';
+        else if ((str[i] >= 'a') && (str[i] <= 'f'))
+            tmp = str[i] - 'a' + 10;
+        else if ((str[i] >= 'A') && (str[i] <= 'F'))
+            tmp = str[i] - 'A' + 10;
+        else
+            return NULL;
+
+        byteval = byteval * 16 + tmp;
+        if ((i % 2) != 0) {
+            item->data[i / 2] = byteval;
+            byteval = 0;
+        }
+        i++;
+    }
+
+    return item;
+}
+
+static SECStatus
+populate_params(ECCurveName name, const ECCurveParams *curveParams,
+                ECFieldType field_type, ECParams *params)
+{
+    SECStatus rv = SECFailure;
+    /* 2 ['0'+'4'] + MAX_ECKEY_LEN * 2 [x,y] * 2 [hex string] + 1 ['\0'] */
+    char genenc[3 + 2 * 2 * MAX_ECKEY_LEN];
+
+    if ((name < ECCurve_noName) || (name > ECCurve_pastLastCurve))
+        goto cleanup;
+    params->name = name;
+    params->fieldID.size = curveParams->size;
+    params->fieldID.type = field_type;
+    if (field_type == ec_field_GFp ||
+        field_type == ec_field_plain) {
+        hexString2SECItem(params->arena, &params->fieldID.u.prime,
+                          curveParams->irr);
+    } else {
+        hexString2SECItem(params->arena, &params->fieldID.u.poly,
+                          curveParams->irr);
+    }
+    hexString2SECItem(params->arena, &params->curve.a,
+                      curveParams->curvea);
+    hexString2SECItem(params->arena, &params->curve.b,
+                      curveParams->curveb);
+    genenc[0] = '0';
+    genenc[1] = '4';
+    genenc[2] = '\0';
+    strcat(genenc, curveParams->genx);
+    strcat(genenc, curveParams->geny);
+    hexString2SECItem(params->arena, &params->base, genenc);
+    hexString2SECItem(params->arena, &params->order,
+                      curveParams->order);
+    params->cofactor = curveParams->cofactor;
+
+    rv = SECSuccess;
+
+cleanup:
+    return rv;
+}
+
+#define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
+#define KU_KEY_AGREEMENT (0x08)     /* bit 4 */
+
+static SECStatus
+freebl_fips_ECDH_PowerUpSelfTest(void)
+{
+    static const ECCurveParams ecCurve_NIST_P256 = {
+        "NIST-P256", ECField_GFp, 256,
+        "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
+        "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC",
+        "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B",
+        "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296",
+        "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
+        "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
+        1, 128, 65, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT
+    };
+
+    /* ECDH d (P-256 private key). */
+    static PRUint8 ecdh_d[] = {
+        0x7b, 0x1e, 0x19, 0x18, 0x25, 0x67, 0xe8, 0xc9,
+        0xbc, 0xe4, 0xf5, 0x6b, 0xbf, 0x69, 0x90, 0x7b,
+        0xc0, 0x32, 0xe2, 0x06, 0x66, 0x11, 0x46, 0xe4,
+        0xc8, 0xe4, 0x9b, 0xc0, 0x65, 0x4d, 0x50, 0x9e
+    };
+
+    /* ECDH x (P-256 public key, Qx and Qy for 512 bits total, plus leading byte). */
+    static PRUint8 ecdh_x[] = {
+        EC_POINT_FORM_UNCOMPRESSED, 
+
+        0x5b, 0x5b, 0xab, 0x96, 0x67, 0x24, 0xdf, 0xc4,
+        0x08, 0x27, 0x5d, 0x69, 0x14, 0x18, 0x77, 0xa8,
+        0x71, 0x2c, 0xe1, 0xda, 0x45, 0xca, 0xe6, 0xdc,
+        0x2a, 0x7a, 0xe9, 0x28, 0x51, 0x30, 0xc3, 0x8f, 
+
+        0x07, 0x24, 0x5b, 0x57, 0xd3, 0x11, 0x41, 0xa6,
+        0xb7, 0x50, 0x4d, 0xd5, 0x23, 0x96, 0x0c, 0x2b,
+        0xa4, 0xd6, 0xd8, 0x82, 0x19, 0xf9, 0x9c, 0x29,
+        0xbc, 0xb9, 0xec, 0xda, 0x52, 0x89, 0x98, 0x25
+    };
+
+    /* ECDH known Z (256-bits). */
+    static const PRUint8 ecdh_known_z[] = {
+        0x19, 0xbd, 0xbc, 0xfd, 0xeb, 0x6a, 0xa2, 0xc9,
+        0x05, 0x6d, 0x3a, 0x2f, 0xd6, 0xf7, 0xd5, 0x80,
+        0x97, 0xf9, 0x42, 0x76, 0x66, 0x25, 0xe1, 0x8b,
+        0xe8, 0x9a, 0x1c, 0xa4, 0x77, 0x58, 0x53, 0xb8
+    };
+
+    /* ECDH variables. */
+    ECParams ec_params;
+    SECItem ecdh_d_item;
+    SECItem ecdh_x_item;
+    SECItem ecdh_z_item;
+    SECStatus ecdh_status;
+
+    ecdh_d_item.data = ecdh_d;
+    ecdh_d_item.len = sizeof ecdh_d;
+
+    ecdh_x_item.data = ecdh_x;
+    ecdh_x_item.len = sizeof ecdh_x;
+
+    ecdh_z_item.data = NULL;
+
+    memset (&ec_params, 0, sizeof (ec_params));
+    if (!(ec_params.arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE))) {
+        PORT_SetError(SEC_ERROR_NO_MEMORY);
+        ecdh_status = SECFailure;
+        goto out;
+    }
+    populate_params (ECCurve_X9_62_PRIME_256V1, &ecCurve_NIST_P256, ec_field_GFp, &ec_params);
+
+    ecdh_status = ECDH_Derive (&ecdh_x_item, &ec_params, &ecdh_d_item, PR_TRUE, &ecdh_z_item);
+
+    if (ecdh_status != SECSuccess
+        || ecdh_z_item.len != sizeof ecdh_known_z
+        || PORT_Memcmp(ecdh_z_item.data, ecdh_known_z, sizeof ecdh_known_z) != 0) {
+        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
+        ecdh_status = SECFailure;
+    }
+
+out:
+    if (ec_params.arena != NULL)
+        PORT_FreeArena(ec_params.arena, PR_FALSE);
+    if (ecdh_z_item.data != NULL)
+        PORT_ZFree (ecdh_z_item.data, ecdh_z_item.len);
+    return ecdh_status;
+}
+
 #endif /* !NSS_DISABLE_ECC */
 
 static SECStatus
@@ -1458,6 +1638,127 @@ freebl_fips_DSA_PowerUpSelfTest(void)
 }
 
 static SECStatus
+freebl_fips_DH_PowerUpSelfTest(void)
+{
+    /* DH Known P (2048-bits), X (224-bits), and G (224-bits) Values. */
+    static PRUint8 dh_p[] = {
+        0xd9, 0x62, 0xfc, 0xec, 0x70, 0x95, 0x73, 0x70,
+        0x71, 0x77, 0x5a, 0xac, 0xf5, 0xca, 0xce, 0xef,
+        0x6e, 0xd7, 0x72, 0xd2, 0x9b, 0x40, 0xa1, 0xc0,
+        0x98, 0xd4, 0xdc, 0x12, 0x51, 0xa3, 0xab, 0x05,
+        0xca, 0x17, 0x2a, 0x01, 0x87, 0x53, 0x38, 0xb3,
+        0x2d, 0xd7, 0xf4, 0x61, 0xdd, 0x6c, 0x7b, 0xad,
+        0xc3, 0x09, 0xb7, 0x8a, 0x5a, 0xb5, 0xe6, 0x47,
+        0x61, 0x63, 0x27, 0x99, 0xd2, 0x32, 0xbc, 0xbc,
+        0x8d, 0x9c, 0x03, 0x5b, 0xd8, 0x5b, 0x3d, 0x3e,
+        0x26, 0xfb, 0xc8, 0x37, 0x69, 0x86, 0x87, 0x25,
+        0x2e, 0xb3, 0x29, 0x3b, 0x89, 0xe9, 0xea, 0xa1,
+        0x45, 0x0c, 0x6e, 0xf3, 0x43, 0x5b, 0x78, 0x42,
+        0x7e, 0x9f, 0x12, 0x90, 0xe5, 0x1a, 0xad, 0x42,
+        0x20, 0x96, 0x7e, 0x0b, 0xa2, 0x15, 0xa1, 0x2e,
+        0xc6, 0x51, 0xa7, 0x26, 0xad, 0x07, 0x72, 0x42,
+        0x7a, 0x60, 0x64, 0xad, 0x60, 0x63, 0x2e, 0x74,
+        0x54, 0x63, 0xc1, 0xa7, 0xbf, 0xad, 0xb4, 0xae,
+        0x85, 0x37, 0x9c, 0x10, 0x40, 0x8f, 0xba, 0xe1,
+        0x3a, 0x85, 0x26, 0xb5, 0xcf, 0xa1, 0x5c, 0xc0,
+        0xda, 0xf9, 0x75, 0xd9, 0x7e, 0xee, 0x5d, 0x35,
+        0x3f, 0xc7, 0xb1, 0x38, 0x32, 0x06, 0xb9, 0xc7,
+        0x14, 0xd4, 0x01, 0xab, 0xd2, 0x6c, 0x74, 0x1c,
+        0xe3, 0x9d, 0x12, 0x5c, 0x20, 0x1c, 0x3d, 0x10,
+        0x69, 0x94, 0x3d, 0x15, 0x06, 0x11, 0x86, 0x57,
+        0xab, 0xd9, 0xfa, 0x27, 0x77, 0xb1, 0x42, 0x99,
+        0x1a, 0xf8, 0x08, 0x3f, 0x70, 0xc1, 0xbf, 0x79,
+        0xd5, 0xa5, 0x29, 0x79, 0x2a, 0x0c, 0x97, 0x7d,
+        0x76, 0x93, 0x17, 0x52, 0x4d, 0x27, 0x29, 0x50,
+        0xff, 0x3d, 0x27, 0xf9, 0xe1, 0xdc, 0x55, 0x7d,
+        0x77, 0xa4, 0x52, 0x7b, 0x4e, 0xea, 0xbc, 0x97,
+        0x14, 0x37, 0xde, 0xf4, 0x9c, 0xa4, 0xe3, 0x71,
+        0xea, 0x4f, 0x2a, 0x72, 0xf7, 0xc8, 0xeb, 0xd3
+    };
+
+    static PRUint8 dh_x[] = {
+        0x9c, 0x37, 0x7e, 0x3e, 0x5b, 0x6f, 0x07, 0x89,
+        0xe7, 0x6d, 0xd8, 0x1f, 0x08, 0x3d, 0x9d, 0x5e,
+        0x44, 0xed, 0x01, 0xa5, 0x87, 0x92, 0xe7, 0x78,
+        0xa5, 0x6d, 0x18, 0x2b
+    };
+
+    static PRUint8 dh_g[] = {
+        0x84, 0x3a, 0xe2, 0x85, 0xb5, 0xe1, 0x13, 0x8e,
+        0xe3, 0xa1, 0x6d, 0x51, 0x4f, 0xa2, 0x1a, 0x1f,
+        0x65, 0x75, 0x7f, 0x9c, 0x1b, 0x2b, 0x7f, 0xda,
+        0x14, 0xfd, 0xeb, 0xd4
+    };
+
+    /* DH known Z (2048-bits). */
+    static const PRUint8 dh_known_z[] = {
+        0x35, 0x05, 0xc7, 0x88, 0x14, 0x3c, 0x7e, 0x0e,
+        0x19, 0x13, 0xd5, 0x3c, 0x6c, 0x71, 0xe5, 0x10,
+        0x32, 0xf5, 0x03, 0x94, 0x84, 0x0d, 0x35, 0x79,
+        0xe8, 0x0c, 0xe9, 0x92, 0x8a, 0xb7, 0x53, 0xd6,
+        0x83, 0xb6, 0x41, 0x31, 0x90, 0xa5, 0x39, 0xac,
+        0xaf, 0x54, 0x24, 0x64, 0x68, 0x0a, 0xab, 0xb8,
+        0xb7, 0xd3, 0x72, 0x30, 0x60, 0x66, 0x7a, 0x65,
+        0xf3, 0xee, 0x98, 0x17, 0xfc, 0x43, 0x51, 0xc9,
+        0x6d, 0x76, 0x34, 0xb0, 0x32, 0x87, 0x10, 0x59,
+        0x3d, 0xf3, 0x8d, 0x65, 0xdd, 0x9f, 0xa7, 0xac,
+        0x03, 0x7b, 0x5a, 0xe0, 0x5c, 0x8e, 0x78, 0x36,
+        0x7c, 0x46, 0x4c, 0xaf, 0xb7, 0x5e, 0xec, 0x92,
+        0xf1, 0xf6, 0x0f, 0x06, 0xbd, 0xaf, 0x1b, 0x87,
+        0x01, 0xf5, 0x6b, 0xe4, 0xc2, 0x47, 0x39, 0x7a,
+        0xae, 0x29, 0x1d, 0xf6, 0xa0, 0xee, 0xb7, 0x2d,
+        0xdf, 0x5e, 0x7c, 0x29, 0x0e, 0xba, 0xb9, 0xc2,
+        0x45, 0xeb, 0x8b, 0xa5, 0x47, 0x7a, 0x23, 0xbe,
+        0x9f, 0x0b, 0xbe, 0x03, 0xb1, 0xf2, 0xba, 0xd9,
+        0x42, 0x3b, 0xe7, 0x25, 0xd9, 0xc7, 0x68, 0x5d,
+        0x90, 0x82, 0x86, 0x13, 0x90, 0xe9, 0xbd, 0x3b,
+        0x2c, 0xb7, 0x98, 0x80, 0x70, 0xd8, 0x6f, 0x61,
+        0xfa, 0xd4, 0xae, 0x2a, 0x0e, 0x90, 0xf8, 0x4e,
+        0x1f, 0x6f, 0x3a, 0xbe, 0xe8, 0x00, 0xe4, 0x2a,
+        0xc6, 0x79, 0x93, 0xf7, 0x50, 0xe8, 0x7a, 0x46,
+        0x86, 0xb0, 0x5b, 0x23, 0x01, 0x64, 0x80, 0xd5,
+        0x68, 0x94, 0x44, 0x83, 0x56, 0x72, 0xda, 0x2a,
+        0x0d, 0x17, 0xed, 0x04, 0xc0, 0x70, 0x86, 0x9e,
+        0x22, 0x8d, 0x39, 0x57, 0x70, 0x22, 0x78, 0x1f,
+        0xff, 0x24, 0x64, 0x94, 0xf4, 0x3a, 0x92, 0xef,
+        0xe7, 0xa9, 0x7a, 0xd6, 0x94, 0xea, 0xa0, 0x37,
+        0xbe, 0xa6, 0x64, 0x55, 0x64, 0x14, 0xdf, 0xd2,
+        0x5a, 0x71, 0x82, 0x0e, 0x24, 0x34, 0xed, 0x3c
+    };
+
+    /* DH variables. */
+    SECItem dh_p_item;
+    SECItem dh_x_item;
+    SECItem dh_g_item;
+    SECItem dh_z_item;
+    SECStatus dh_status;
+
+    dh_p_item.data = dh_p;
+    dh_p_item.len = sizeof dh_p;
+
+    dh_x_item.data = dh_x;
+    dh_x_item.len = sizeof dh_x;
+
+    dh_g_item.data = dh_g;
+    dh_g_item.len = sizeof dh_g;
+
+    dh_z_item.data = NULL;
+
+    dh_status = DH_Derive (&dh_g_item, &dh_p_item, &dh_x_item, &dh_z_item, 0);
+
+    if (dh_status != SECSuccess
+        || dh_z_item.len != sizeof dh_known_z
+        || PORT_Memcmp(dh_z_item.data, dh_known_z, sizeof dh_known_z) != 0) {
+        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
+        dh_status = SECFailure;
+    }
+
+    if (dh_z_item.data != NULL)
+        PORT_ZFree (dh_z_item.data, dh_z_item.len);
+    return dh_status;
+}
+
+static SECStatus
 freebl_fips_RNG_PowerUpSelfTest(void)
 {
     static const PRUint8 Q[] = {
@@ -1594,12 +1895,24 @@ freebl_fipsPowerUpSelfTest(unsigned int tests)
         if (rv != SECSuccess)
             return rv;
 
+        /* DH Power-Up SelfTest(s). */
+        rv = freebl_fips_DH_PowerUpSelfTest();
+
+        if (rv != SECSuccess)
+            return rv;
+
 #ifndef NSS_DISABLE_ECC
         /* ECDSA Power-Up SelfTest(s). */
         rv = freebl_fips_ECDSA_PowerUpSelfTest();
 
         if (rv != SECSuccess)
             return rv;
+
+        /* ECDH Power-Up SelfTest(s). */
+        rv = freebl_fips_ECDH_PowerUpSelfTest();
+
+        if (rv != SECSuccess)
+            return rv;
 #endif
     }
     /* Passed Power-Up SelfTest(s). */
