]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
fips: add self test CAST for LMS verify
authorPauli <ppzgs1@gmail.com>
Mon, 13 Jan 2025 01:41:49 +0000 (12:41 +1100)
committerPauli <ppzgs1@gmail.com>
Thu, 10 Jul 2025 09:04:37 +0000 (19:04 +1000)
Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/27885)

providers/fips/self_test_data.inc
providers/fips/self_test_kats.c

index 31195cc4e59e1596c1451dc0b89118e7caa32734..c1d84e175c94b6ce6cb469d0253f3b401f5f2fc4 100644 (file)
@@ -307,6 +307,148 @@ static const ST_KAT_CIPHER st_kat_cipher_tests[] = {
 #endif
 };
 
+#ifndef OPENSSL_NO_LMS
+typedef struct st_kat_lms_s {
+    const unsigned char *pub;
+    size_t publen;
+    const unsigned char *msg;
+    size_t msglen;
+    const unsigned char *sig;
+    size_t siglen;
+} ST_KAT_LMS;
+
+/*
+ * Test vector from
+ * https://datatracker.ietf.org/doc/html/draft-fluhrer-lms-more-parm-sets-15#name-test-cases
+ */
+
+static const unsigned char sha256_192_pub[] = {
+    0x00, 0x00, 0x00, 0x0A,
+    0x00, 0x00, 0x00, 0x08,
+    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+    0x2c, 0x57, 0x14, 0x50, 0xae, 0xd9, 0x9c, 0xfb,
+    0x4f, 0x4a, 0xc2, 0x85, 0xda, 0x14, 0x88, 0x27,
+    0x96, 0x61, 0x83, 0x14, 0x50, 0x8b, 0x12, 0xd2
+};
+
+static const unsigned char sha256_192_msg[] = {
+    0x54, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73,
+    0x73, 0x61, 0x67, 0x65, 0x20, 0x66, 0x6f, 0x72,
+    0x20, 0x53, 0x48, 0x41, 0x32, 0x35, 0x36, 0x2d,
+    0x31, 0x39, 0x32, 0x0a
+};
+
+static const unsigned char sha256_192_sig[] = {
+    /* L = 1,  q = 5 */
+    0x00, 0x00, 0x00, 0x05,
+    0x00, 0x00, 0x00, 0x08,
+    0x0b, 0x50, 0x40, 0xa1, 0x8c, 0x1b, 0x5c, 0xab,
+    0xcb, 0xc8, 0x5b, 0x04, 0x74, 0x02, 0xec, 0x62,
+    0x94, 0xa3, 0x0d, 0xd8, 0xda, 0x8f, 0xc3, 0xda,
+    0xe1, 0x3b, 0x9f, 0x08, 0x75, 0xf0, 0x93, 0x61,
+    0xdc, 0x77, 0xfc, 0xc4, 0x48, 0x1e, 0xa4, 0x63,
+    0xc0, 0x73, 0x71, 0x62, 0x49, 0x71, 0x91, 0x93,
+    0x61, 0x4b, 0x83, 0x5b, 0x46, 0x94, 0xc0, 0x59,
+    0xf1, 0x2d, 0x3a, 0xed, 0xd3, 0x4f, 0x3d, 0xb9,
+    0x3f, 0x35, 0x80, 0xfb, 0x88, 0x74, 0x3b, 0x8b,
+    0x3d, 0x06, 0x48, 0xc0, 0x53, 0x7b, 0x7a, 0x50,
+    0xe4, 0x33, 0xd7, 0xea, 0x9d, 0x66, 0x72, 0xff,
+    0xfc, 0x5f, 0x42, 0x77, 0x0f, 0xea, 0xb4, 0xf9,
+    0x8e, 0xb3, 0xf3, 0xb2, 0x3f, 0xd2, 0x06, 0x1e,
+    0x4d, 0x0b, 0x38, 0xf8, 0x32, 0x86, 0x0a, 0xe7,
+    0x66, 0x73, 0xad, 0x1a, 0x1a, 0x52, 0xa9, 0x00,
+    0x5d, 0xcf, 0x1b, 0xfb, 0x56, 0xfe, 0x16, 0xff,
+    0x72, 0x36, 0x27, 0x61, 0x2f, 0x9a, 0x48, 0xf7,
+    0x90, 0xf3, 0xc4, 0x7a, 0x67, 0xf8, 0x70, 0xb8,
+    0x1e, 0x91, 0x9d, 0x99, 0x91, 0x9c, 0x8d, 0xb4,
+    0x81, 0x68, 0x83, 0x8c, 0xec, 0xe0, 0xab, 0xfb,
+    0x68, 0x3d, 0xa4, 0x8b, 0x92, 0x09, 0x86, 0x8b,
+    0xe8, 0xec, 0x10, 0xc6, 0x3d, 0x8b, 0xf8, 0x0d,
+    0x36, 0x49, 0x8d, 0xfc, 0x20, 0x5d, 0xc4, 0x5d,
+    0x0d, 0xd8, 0x70, 0x57, 0x2d, 0x6d, 0x8f, 0x1d,
+    0x90, 0x17, 0x7c, 0xf5, 0x13, 0x7b, 0x8b, 0xbf,
+    0x7b, 0xcb, 0x67, 0xa4, 0x6f, 0x86, 0xf2, 0x6c,
+    0xfa, 0x5a, 0x44, 0xcb, 0xca, 0xa4, 0xe1, 0x8d,
+    0xa0, 0x99, 0xa9, 0x8b, 0x0b, 0x3f, 0x96, 0xd5,
+    0xac, 0x8a, 0xc3, 0x75, 0xd8, 0xda, 0x2a, 0x7c,
+    0x24, 0x80, 0x04, 0xba, 0x11, 0xd7, 0xac, 0x77,
+    0x5b, 0x92, 0x18, 0x35, 0x9c, 0xdd, 0xab, 0x4c,
+    0xf8, 0xcc, 0xc6, 0xd5, 0x4c, 0xb7, 0xe1, 0xb3,
+    0x5a, 0x36, 0xdd, 0xc9, 0x26, 0x5c, 0x08, 0x70,
+    0x63, 0xd2, 0xfc, 0x67, 0x42, 0xa7, 0x17, 0x78,
+    0x76, 0x47, 0x6a, 0x32, 0x4b, 0x03, 0x29, 0x5b,
+    0xfe, 0xd9, 0x9f, 0x2e, 0xaf, 0x1f, 0x38, 0x97,
+    0x05, 0x83, 0xc1, 0xb2, 0xb6, 0x16, 0xaa, 0xd0,
+    0xf3, 0x1c, 0xd7, 0xa4, 0xb1, 0xbb, 0x0a, 0x51,
+    0xe4, 0x77, 0xe9, 0x4a, 0x01, 0xbb, 0xb4, 0xd6,
+    0xf8, 0x86, 0x6e, 0x25, 0x28, 0xa1, 0x59, 0xdf,
+    0x3d, 0x6c, 0xe2, 0x44, 0xd2, 0xb6, 0x51, 0x8d,
+    0x1f, 0x02, 0x12, 0x28, 0x5a, 0x3c, 0x2d, 0x4a,
+    0x92, 0x70, 0x54, 0xa1, 0xe1, 0x62, 0x0b, 0x5b,
+    0x02, 0xaa, 0xb0, 0xc8, 0xc1, 0x0e, 0xd4, 0x8a,
+    0xe5, 0x18, 0xea, 0x73, 0xcb, 0xa8, 0x1f, 0xcf,
+    0xff, 0x88, 0xbf, 0xf4, 0x61, 0xda, 0xc5, 0x1e,
+    0x7a, 0xb4, 0xca, 0x75, 0xf4, 0x7a, 0x62, 0x59,
+    0xd2, 0x48, 0x20, 0xb9, 0x99, 0x57, 0x92, 0xd1,
+    0x39, 0xf6, 0x1a, 0xe2, 0xa8, 0x18, 0x6a, 0xe4,
+    0xe3, 0xc9, 0xbf, 0xe0, 0xaf, 0x2c, 0xc7, 0x17,
+    0xf4, 0x24, 0xf4, 0x1a, 0xa6, 0x7f, 0x03, 0xfa,
+    0xed, 0xb0, 0x66, 0x51, 0x15, 0xf2, 0x06, 0x7a,
+    0x46, 0x84, 0x3a, 0x4c, 0xbb, 0xd2, 0x97, 0xd5,
+    0xe8, 0x3b, 0xc1, 0xaa, 0xfc, 0x18, 0xd1, 0xd0,
+    0x3b, 0x3d, 0x89, 0x4e, 0x85, 0x95, 0xa6, 0x52,
+    0x60, 0x73, 0xf0, 0x2a, 0xb0, 0xf0, 0x8b, 0x99,
+    0xfd, 0x9e, 0xb2, 0x08, 0xb5, 0x9f, 0xf6, 0x31,
+    0x7e, 0x55, 0x45, 0xe6, 0xf9, 0xad, 0x5f, 0x9c,
+    0x18, 0x3a, 0xbd, 0x04, 0x3d, 0x5a, 0xcd, 0x6e,
+    0xb2, 0xdd, 0x4d, 0xa3, 0xf0, 0x2d, 0xbc, 0x31,
+    0x67, 0xb4, 0x68, 0x72, 0x0a, 0x4b, 0x8b, 0x92,
+    0xdd, 0xfe, 0x79, 0x60, 0x99, 0x8b, 0xb7, 0xa0,
+    0xec, 0xf2, 0xa2, 0x6a, 0x37, 0x59, 0x82, 0x99,
+    0x41, 0x3f, 0x7b, 0x2a, 0xec, 0xd3, 0x9a, 0x30,
+    0xce, 0xc5, 0x27, 0xb4, 0xd9, 0x71, 0x0c, 0x44,
+    0x73, 0x63, 0x90, 0x22, 0x45, 0x1f, 0x50, 0xd0,
+    0x1c, 0x04, 0x57, 0x12, 0x5d, 0xa0, 0xfa, 0x44,
+    0x29, 0xc0, 0x7d, 0xad, 0x85, 0x9c, 0x84, 0x6c,
+    0xbb, 0xd9, 0x3a, 0xb5, 0xb9, 0x1b, 0x01, 0xbc,
+    0x77, 0x0b, 0x08, 0x9c, 0xfe, 0xde, 0x6f, 0x65,
+    0x1e, 0x86, 0xdd, 0x7c, 0x15, 0x98, 0x9c, 0x8b,
+    0x53, 0x21, 0xde, 0xa9, 0xca, 0x60, 0x8c, 0x71,
+    0xfd, 0x86, 0x23, 0x23, 0x07, 0x2b, 0x82, 0x7c,
+    0xee, 0x7a, 0x7e, 0x28, 0xe4, 0xe2, 0xb9, 0x99,
+    0x64, 0x72, 0x33, 0xc3, 0x45, 0x69, 0x44, 0xbb,
+    0x7a, 0xef, 0x91, 0x87, 0xc9, 0x6b, 0x3f, 0x5b,
+    0x79, 0xfb, 0x98, 0xbc, 0x76, 0xc3, 0x57, 0x4d,
+    0xd0, 0x6f, 0x0e, 0x95, 0x68, 0x5e, 0x5b, 0x3a,
+    0xef, 0x3a, 0x54, 0xc4, 0x15, 0x5f, 0xe3, 0xad,
+    0x81, 0x77, 0x49, 0x62, 0x9c, 0x30, 0xad, 0xbe,
+    0x89, 0x7c, 0x4f, 0x44, 0x54, 0xc8, 0x6c, 0x49,
+    0x00, 0x00, 0x00, 0x0a,
+    0xe9, 0xca, 0x10, 0xea, 0xa8, 0x11, 0xb2, 0x2a,
+    0xe0, 0x7f, 0xb1, 0x95, 0xe3, 0x59, 0x0a, 0x33,
+    0x4e, 0xa6, 0x42, 0x09, 0x94, 0x2f, 0xba, 0xe3,
+    0x38, 0xd1, 0x9f, 0x15, 0x21, 0x82, 0xc8, 0x07,
+    0xd3, 0xc4, 0x0b, 0x18, 0x9d, 0x3f, 0xcb, 0xea,
+    0x94, 0x2f, 0x44, 0x68, 0x24, 0x39, 0xb1, 0x91,
+    0x33, 0x2d, 0x33, 0xae, 0x0b, 0x76, 0x1a, 0x2a,
+    0x8f, 0x98, 0x4b, 0x56, 0xb2, 0xac, 0x2f, 0xd4,
+    0xab, 0x08, 0x22, 0x3a, 0x69, 0xed, 0x1f, 0x77,
+    0x19, 0xc7, 0xaa, 0x7e, 0x9e, 0xee, 0x96, 0x50,
+    0x4b, 0x0e, 0x60, 0xc6, 0xbb, 0x5c, 0x94, 0x2d,
+    0x69, 0x5f, 0x04, 0x93, 0xeb, 0x25, 0xf8, 0x0a,
+    0x58, 0x71, 0xcf, 0xfd, 0x13, 0x1d, 0x0e, 0x04,
+    0xff, 0xe5, 0x06, 0x5b, 0xc7, 0x87, 0x5e, 0x82,
+    0xd3, 0x4b, 0x40, 0xb6, 0x9d, 0xd9, 0xf3, 0xc1
+};
+
+static const ST_KAT_LMS st_kat_lms_test = {
+    ITM(sha256_192_pub),
+    ITM(sha256_192_msg),
+    ITM(sha256_192_sig)
+};
+#endif  /* OPENSSL_NO_LMS */
+
 static const char hkdf_digest[] = "SHA256";
 /*
  * Input parameters and expected result are from RFC 5869 test case 1, which is
index 1958c6fc3a91e65ead69830dc56f47851cdd72d3..681ced377ca39ab2b8f7f8f21b1bbc9759f7f0ed 100644 (file)
@@ -480,6 +480,52 @@ static int digest_signature(const uint8_t *sig, size_t sig_len,
     return ret;
 }
 
+#ifndef OPENSSL_NO_LMS
+static int self_test_LMS(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
+{
+    int ret = 0;
+    OSSL_PARAM pm[2];
+    const ST_KAT_LMS *t = &st_kat_lms_test;
+    EVP_PKEY_CTX *ctx = NULL;
+    EVP_PKEY *pkey = NULL;
+    EVP_SIGNATURE *sig = NULL;
+
+    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_SIGNATURE,
+                           OSSL_SELF_TEST_DESC_SIGN_LMS);
+
+    pm[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
+                                              (unsigned char *)t->pub,
+                                              t->publen);
+    pm[1] = OSSL_PARAM_construct_end();
+
+    ctx = EVP_PKEY_CTX_new_from_name(libctx, "LMS", "");
+    if (ctx == NULL
+            || EVP_PKEY_fromdata_init(ctx) <= 0
+            || EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, pm) <= 0)
+        goto err;
+    EVP_PKEY_CTX_free(ctx);
+    ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, "");
+    if (ctx == NULL)
+        goto err;
+
+    sig = EVP_SIGNATURE_fetch(libctx, "LMS", NULL);
+    if (sig == NULL
+            || EVP_PKEY_verify_message_init(ctx, sig, NULL) <= 0
+            || EVP_PKEY_verify(ctx, t->sig, t->siglen,
+                               t->msg, t->msglen) <= 0)
+        goto err;
+
+    ret = 1;
+ err:
+    EVP_PKEY_free(pkey);
+    EVP_PKEY_CTX_free(ctx);
+    EVP_SIGNATURE_free(sig);
+
+    OSSL_SELF_TEST_onend(st, ret);
+    return ret;
+}
+#endif  /* OPENSSL_NO_LMS */
+
 static int self_test_digest_sign(const ST_KAT_SIGN *t,
                                  OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
 {
@@ -1080,6 +1126,19 @@ int SELF_TEST_kats(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
         ret = 0;
     if (!self_test_ciphers(st, libctx))
         ret = 0;
+#ifndef OPENSSL_NO_LMS
+    /*
+     * FIPS 140-3 IG 10.3.A Note 5 mandates a CAST for LMS.
+     *
+     * It permits this to be omitted if HSS is also implemented and has
+     * the relevant self tests.  Once HSS is implemented, this test can be
+     * removed.  This IG permits the digest's CAST to be subsumed into this
+     * test, however, because this will be removed, the underlying digest
+     * test has been retained elsewhere lest it is accidentally omitted.
+     */
+    if (!self_test_LMS(st, libctx))
+        ret = 0;
+#endif  /* OPENSSL_NO_LMS */
     if (!self_test_signatures(st, libctx))
         ret = 0;
     if (!self_test_kdfs(st, libctx))