LCOV - code coverage report
Current view: top level - app/fips_validation - fips_validation_rsa.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 282 0.0 %
Date: 2026-12-01 19:01:22 Functions: 0 9 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2022 Marvell.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <string.h>
       6                 :            : #include <time.h>
       7                 :            : #include <stdio.h>
       8                 :            : #include <sys/types.h>
       9                 :            : #include <unistd.h>
      10                 :            : 
      11                 :            : #ifdef USE_OPENSSL
      12                 :            : #include <openssl/bn.h>
      13                 :            : #include <openssl/rand.h>
      14                 :            : #endif /* USE_OPENSSL */
      15                 :            : 
      16                 :            : #include <rte_cryptodev.h>
      17                 :            : #include <rte_malloc.h>
      18                 :            : 
      19                 :            : #include "fips_validation.h"
      20                 :            : 
      21                 :            : #define CONFORMANCE_JSON_STR    "conformance"
      22                 :            : #define TESTTYPE_JSON_STR       "testType"
      23                 :            : #define SIGTYPE_JSON_STR "sigType"
      24                 :            : #define MOD_JSON_STR    "modulo"
      25                 :            : #define HASH_JSON_STR   "hashAlg"
      26                 :            : #define SALT_JSON_STR   "saltLen"
      27                 :            : #define RV_JSON_STR     "randomValue"
      28                 :            : #define E_JSON_STR      "e"
      29                 :            : #define N_JSON_STR      "n"
      30                 :            : 
      31                 :            : #define SEED_JSON_STR   "seed"
      32                 :            : #define MSG_JSON_STR    "message"
      33                 :            : #define SIG_JSON_STR    "signature"
      34                 :            : 
      35                 :            : 
      36                 :            : #define RV_BUF_LEN (1024/8)
      37                 :            : #define RV_BIT_LEN (256)
      38                 :            : 
      39                 :            : #ifdef USE_JANSSON
      40                 :            : struct {
      41                 :            :         uint8_t type;
      42                 :            :         const char *desc;
      43                 :            : } rsa_test_types[] = {
      44                 :            :                 {RSA_AFT, "AFT"},
      45                 :            :                 {RSA_GDT, "GDT"},
      46                 :            :                 {RSA_KAT, "KAT"},
      47                 :            : };
      48                 :            : 
      49                 :            : struct {
      50                 :            :         enum rte_crypto_auth_algorithm auth;
      51                 :            :         const char *desc;
      52                 :            : } rsa_auth_algs[] = {
      53                 :            :                 {RTE_CRYPTO_AUTH_SHA1, "SHA-1"},
      54                 :            :                 {RTE_CRYPTO_AUTH_SHA224, "SHA2-224"},
      55                 :            :                 {RTE_CRYPTO_AUTH_SHA256, "SHA2-256"},
      56                 :            :                 {RTE_CRYPTO_AUTH_SHA384, "SHA2-384"},
      57                 :            :                 {RTE_CRYPTO_AUTH_SHA512, "SHA2-512"},
      58                 :            : };
      59                 :            : 
      60                 :            : struct {
      61                 :            :         enum rte_crypto_rsa_padding_type padding;
      62                 :            :         const char *desc;
      63                 :            : } rsa_padding_types[] = {
      64                 :            :                 {RTE_CRYPTO_RSA_PADDING_NONE, "none"},
      65                 :            :                 {RTE_CRYPTO_RSA_PADDING_PKCS1_5, "pkcs1v1.5"},
      66                 :            :                 {RTE_CRYPTO_RSA_PADDING_OAEP, "oaep"},
      67                 :            :                 {RTE_CRYPTO_RSA_PADDING_PSS, "pss"},
      68                 :            : };
      69                 :            : 
      70                 :            : #ifdef USE_OPENSSL
      71                 :            : static int
      72                 :          0 : prepare_vec_rsa(void)
      73                 :            : {
      74                 :          0 :         BIGNUM *p = NULL, *q = NULL, *n = NULL, *d = NULL, *e = NULL;
      75                 :            :         BIGNUM *dp = NULL, *dq = NULL, *qinv = NULL;
      76                 :            :         BIGNUM *r0, *r1, *r2, *r3, *r4;
      77                 :          0 :         BIGNUM *m = NULL, *r = NULL;
      78                 :            :         int bits, ret = -1, i;
      79                 :            :         char modbuf[8], *buf;
      80                 :            :         BN_CTX *ctx = NULL;
      81                 :            :         unsigned long pid;
      82                 :            : 
      83                 :            :         /* Seed PRNG */
      84                 :          0 :         if (vec.rsa.seed.val) {
      85                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.seed);
      86                 :          0 :                 RAND_seed((char *)info.one_line_text, strlen(info.one_line_text));
      87                 :            :         } else {
      88                 :          0 :                 pid = getpid();
      89                 :          0 :                 RAND_seed(&pid, sizeof(pid));
      90                 :            :         }
      91                 :            : 
      92                 :          0 :         if (!RAND_status())
      93                 :            :                 return -1;
      94                 :            : 
      95                 :            :         /* Check if e is known already */
      96                 :          0 :         if (vec.rsa.e.val) {
      97                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.e);
      98                 :          0 :                 ret = BN_hex2bn(&e, info.one_line_text);
      99                 :          0 :                 if ((uint32_t)ret != strlen(info.one_line_text))
     100                 :          0 :                         goto err;
     101                 :            :         }
     102                 :            : 
     103                 :            :         /* BN context initialization */
     104                 :          0 :         ctx = BN_CTX_new();
     105                 :          0 :         if (!ctx)
     106                 :          0 :                 goto err;
     107                 :            : 
     108                 :          0 :         BN_CTX_start(ctx);
     109                 :          0 :         r0 = BN_CTX_get(ctx);
     110                 :          0 :         r1 = BN_CTX_get(ctx);
     111                 :          0 :         r2 = BN_CTX_get(ctx);
     112                 :          0 :         r3 = BN_CTX_get(ctx);
     113                 :          0 :         r4 = BN_CTX_get(ctx);
     114                 :          0 :         if (!r4)
     115                 :          0 :                 goto err;
     116                 :            : 
     117                 :            :         /* Calculate bit length for prime numbers */
     118                 :          0 :         m = BN_new();
     119                 :          0 :         if (!m)
     120                 :          0 :                 goto err;
     121                 :            : 
     122                 :          0 :         snprintf(modbuf, sizeof(modbuf), "%d", info.interim_info.rsa_data.modulo);
     123                 :          0 :         if (!BN_dec2bn(&m, modbuf))
     124                 :          0 :                 goto err;
     125                 :            : 
     126                 :          0 :         r = BN_new();
     127                 :          0 :         if (!r)
     128                 :          0 :                 goto err;
     129                 :            : 
     130                 :          0 :         if (!BN_rshift1(r, m))
     131                 :          0 :                 goto err;
     132                 :            : 
     133                 :          0 :         buf = BN_bn2dec(r);
     134                 :            :         bits = atoi(buf);
     135                 :            : 
     136                 :          0 :         p = BN_new();
     137                 :          0 :         if (!p)
     138                 :          0 :                 goto err;
     139                 :            : 
     140                 :          0 :         q = BN_new();
     141                 :          0 :         if (!q)
     142                 :          0 :                 goto err;
     143                 :            : 
     144                 :          0 :         n = BN_new();
     145                 :          0 :         if (!n)
     146                 :          0 :                 goto err;
     147                 :            : 
     148                 :          0 :         d = BN_new();
     149                 :          0 :         if (!d)
     150                 :          0 :                 goto err;
     151                 :            : 
     152                 :            :         /* Generate p and q suitably for RSA */
     153                 :          0 :         for (i = 0; i < 10; i++) {
     154                 :          0 :                 uint8_t j = 0;
     155                 :            : 
     156                 :          0 :                 if (!BN_generate_prime_ex(p, bits, 0, NULL, NULL, NULL))
     157                 :          0 :                         goto err;
     158                 :            : 
     159                 :            :                 do {
     160                 :          0 :                         RAND_add(&j, sizeof(j), 1);
     161                 :          0 :                         if (!BN_generate_prime_ex(q, bits, 0, NULL, NULL, NULL))
     162                 :          0 :                                 goto err;
     163                 :            : 
     164                 :          0 :                 } while ((BN_cmp(p, q) == 0) && (j++ < 100));
     165                 :            : 
     166                 :          0 :                 if (j >= 100) {
     167                 :          0 :                         RTE_LOG(ERR, USER1, "Error: insufficient %d retries to generate q", j);
     168                 :          0 :                         goto err;
     169                 :            :                 }
     170                 :            : 
     171                 :            :                 /* pq */
     172                 :          0 :                 if (!BN_mul(n, p, q, ctx))
     173                 :          0 :                         goto err;
     174                 :            : 
     175                 :            :                 /* p-1 */
     176                 :          0 :                 if (!BN_sub(r1, p, BN_value_one()))
     177                 :          0 :                         goto err;
     178                 :            : 
     179                 :            :                 /* q-1 */
     180                 :          0 :                 if (!BN_sub(r2, q, BN_value_one()))
     181                 :          0 :                         goto err;
     182                 :            : 
     183                 :            :                 /* (p-1 * q-1) */
     184                 :          0 :                 if (!BN_mul(r0, r1, r2, ctx))
     185                 :          0 :                         goto err;
     186                 :            : 
     187                 :            :                 /* gcd(p-1, q-1)*/
     188                 :          0 :                 if (!BN_gcd(r3, r1, r2, ctx))
     189                 :          0 :                         goto err;
     190                 :            : 
     191                 :            :                 /* lcm(p-1, q-1) */
     192                 :          0 :                 if (!BN_div(r4, r, r0, r3, ctx))
     193                 :          0 :                         goto err;
     194                 :            : 
     195                 :            :                 /* check if div and rem are non-zero */
     196                 :            :                 if (!r4 || !r)
     197                 :            :                         goto err;
     198                 :            : 
     199                 :            :                 /* 0 < e < lcm */
     200                 :          0 :                 if (!e) {
     201                 :          0 :                         int k = 0;
     202                 :            : 
     203                 :          0 :                         e = BN_new();
     204                 :            :                         do {
     205                 :          0 :                                 RAND_add(&k, sizeof(k), 1);
     206                 :          0 :                                 if (!BN_rand(e, 32, 1, 1))
     207                 :          0 :                                         goto err;
     208                 :            : 
     209                 :          0 :                                 if (!BN_gcd(r3, e, r4, ctx))
     210                 :          0 :                                         goto err;
     211                 :            : 
     212                 :          0 :                                 if (BN_is_one(r3))
     213                 :            :                                         break;
     214                 :          0 :                         } while (k++ < 10);
     215                 :            : 
     216                 :          0 :                         if (k >= 10) {
     217                 :          0 :                                 RTE_LOG(ERR, USER1, "Error: insufficient %d retries to generate e",
     218                 :            :                                         k);
     219                 :          0 :                                 goto err;
     220                 :            :                         }
     221                 :            :                 }
     222                 :            : 
     223                 :            :                 /* (de) mod lcm == 1 */
     224                 :          0 :                 if (!BN_mod_inverse(d, e, r4, ctx))
     225                 :          0 :                         goto err;
     226                 :            : 
     227                 :          0 :                 if (!BN_gcd(r3, r1, e, ctx))
     228                 :          0 :                         goto err;
     229                 :            : 
     230                 :          0 :                 if (!BN_gcd(r4, r2, e, ctx))
     231                 :          0 :                         goto err;
     232                 :            : 
     233                 :            :                 /* check if gcd(p-1, e) and gcd(q-1, e) are 1 */
     234                 :          0 :                 if (BN_is_one(r3) && BN_is_one(r4))
     235                 :            :                         break;
     236                 :            :         }
     237                 :            : 
     238                 :          0 :         if (i >= 10) {
     239                 :          0 :                 RTE_LOG(ERR, USER1, "Error: insufficient %d retries to generate p and q", i);
     240                 :          0 :                 goto err;
     241                 :            :         }
     242                 :            : 
     243                 :            :         /* d mod (p-1) */
     244                 :          0 :         dp = BN_new();
     245                 :          0 :         if (!dp)
     246                 :          0 :                 goto err;
     247                 :            : 
     248                 :          0 :         if (!BN_mod(dp, d, r1, ctx))
     249                 :          0 :                 goto err;
     250                 :            : 
     251                 :            :         /* d mod (q-1) */
     252                 :          0 :         dq = BN_new();
     253                 :          0 :         if (!dq)
     254                 :          0 :                 goto err;
     255                 :            : 
     256                 :          0 :         if (!BN_mod(dq, d, r2, ctx))
     257                 :          0 :                 goto err;
     258                 :            : 
     259                 :            :         /* modinv of q and p */
     260                 :          0 :         qinv = BN_new();
     261                 :          0 :         if (!qinv)
     262                 :          0 :                 goto err;
     263                 :            : 
     264                 :          0 :         if (!BN_mod_inverse(qinv, q, p, ctx))
     265                 :          0 :                 goto err;
     266                 :            : 
     267                 :          0 :         if (info.interim_info.rsa_data.random_msg) {
     268                 :          0 :                 if (!BN_generate_prime_ex(r, RV_BIT_LEN, 0, NULL, NULL, NULL))
     269                 :          0 :                         goto err;
     270                 :            : 
     271                 :          0 :                 parse_uint8_hex_str("", BN_bn2hex(r), &vec.rsa.seed);
     272                 :            :         }
     273                 :            : 
     274                 :          0 :         parse_uint8_hex_str("", BN_bn2hex(e), &vec.rsa.e);
     275                 :          0 :         parse_uint8_hex_str("", BN_bn2hex(p), &vec.rsa.p);
     276                 :          0 :         parse_uint8_hex_str("", BN_bn2hex(q), &vec.rsa.q);
     277                 :          0 :         parse_uint8_hex_str("", BN_bn2hex(n), &vec.rsa.n);
     278                 :          0 :         parse_uint8_hex_str("", BN_bn2hex(d), &vec.rsa.d);
     279                 :          0 :         parse_uint8_hex_str("", BN_bn2hex(dp), &vec.rsa.dp);
     280                 :          0 :         parse_uint8_hex_str("", BN_bn2hex(dq), &vec.rsa.dq);
     281                 :          0 :         parse_uint8_hex_str("", BN_bn2hex(qinv), &vec.rsa.qinv);
     282                 :            : 
     283                 :            :         ret = 0;
     284                 :          0 : err:
     285                 :          0 :         BN_CTX_end(ctx);
     286                 :          0 :         BN_CTX_free(ctx);
     287                 :          0 :         BN_free(m);
     288                 :          0 :         BN_free(r);
     289                 :          0 :         BN_free(p);
     290                 :          0 :         BN_free(q);
     291                 :          0 :         BN_free(n);
     292                 :          0 :         BN_free(d);
     293                 :          0 :         BN_free(e);
     294                 :          0 :         return ret;
     295                 :            : }
     296                 :            : #else
     297                 :            : static int
     298                 :            : prepare_vec_rsa(void)
     299                 :            : {
     300                 :            :         /*
     301                 :            :          * Generate RSA values.
     302                 :            :          */
     303                 :            :         return -ENOTSUP;
     304                 :            : }
     305                 :            : #endif /* USE_OPENSSL */
     306                 :            : 
     307                 :            : static int
     308                 :          0 : parse_test_rsa_json_interim_writeback(struct fips_val *val)
     309                 :            : {
     310                 :            :         RTE_SET_USED(val);
     311                 :            : 
     312                 :          0 :         if (info.interim_info.rsa_data.random_msg) {
     313                 :          0 :                 json_object_set_new(json_info.json_write_group, "conformance",
     314                 :            :                                                         json_string("SP800-106"));
     315                 :            :         }
     316                 :            : 
     317                 :          0 :         if (info.op == FIPS_TEST_ASYM_SIGGEN) {
     318                 :            :                 json_t *obj;
     319                 :            : 
     320                 :            :                 /* For siggen tests, RSA values can be created soon after
     321                 :            :                  * the test group data are parsed.
     322                 :            :                  */
     323                 :          0 :                 if (vec.rsa.e.val) {
     324                 :          0 :                         rte_free(vec.rsa.e.val);
     325                 :          0 :                         vec.rsa.e.val = NULL;
     326                 :            :                 }
     327                 :            : 
     328                 :          0 :                 if (prepare_vec_rsa() < 0)
     329                 :            :                         return -1;
     330                 :            : 
     331                 :          0 :                 if (!vec.rsa.e.val)
     332                 :            :                         return -1;
     333                 :            : 
     334                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.n);
     335                 :          0 :                 obj = json_string(info.one_line_text);
     336                 :          0 :                 json_object_set_new(json_info.json_write_group, "n", obj);
     337                 :            : 
     338                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.e);
     339                 :          0 :                 obj = json_string(info.one_line_text);
     340                 :          0 :                 json_object_set_new(json_info.json_write_group, "e", obj);
     341                 :            :         }
     342                 :            : 
     343                 :            :         return 0;
     344                 :            : }
     345                 :            : 
     346                 :            : static int
     347                 :          0 : parse_test_rsa_json_writeback(struct fips_val *val)
     348                 :            : {
     349                 :            :         json_t *tcId;
     350                 :            : 
     351                 :            :         RTE_SET_USED(val);
     352                 :            : 
     353                 :          0 :         tcId = json_object_get(json_info.json_test_case, "tcId");
     354                 :            : 
     355                 :          0 :         json_info.json_write_case = json_object();
     356                 :          0 :         json_object_set(json_info.json_write_case, "tcId", tcId);
     357                 :            : 
     358                 :          0 :         if (info.op == FIPS_TEST_ASYM_KEYGEN) {
     359                 :            :                 json_t *obj;
     360                 :            : 
     361                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.seed);
     362                 :          0 :                 obj = json_string(info.one_line_text);
     363                 :          0 :                 json_object_set_new(json_info.json_write_case, "seed", obj);
     364                 :            : 
     365                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.n);
     366                 :          0 :                 obj = json_string(info.one_line_text);
     367                 :          0 :                 json_object_set_new(json_info.json_write_case, "n", obj);
     368                 :            : 
     369                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.e);
     370                 :          0 :                 obj = json_string(info.one_line_text);
     371                 :          0 :                 json_object_set_new(json_info.json_write_case, "e", obj);
     372                 :            : 
     373                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.p);
     374                 :          0 :                 obj = json_string(info.one_line_text);
     375                 :          0 :                 json_object_set_new(json_info.json_write_case, "p", obj);
     376                 :            : 
     377                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.q);
     378                 :          0 :                 obj = json_string(info.one_line_text);
     379                 :          0 :                 json_object_set_new(json_info.json_write_case, "q", obj);
     380                 :            : 
     381                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.d);
     382                 :          0 :                 obj = json_string(info.one_line_text);
     383                 :          0 :                 json_object_set_new(json_info.json_write_case, "d", obj);
     384                 :          0 :         } else if (info.op == FIPS_TEST_ASYM_SIGGEN) {
     385                 :            :                 json_t *obj;
     386                 :            : 
     387                 :          0 :                 writeback_hex_str("", info.one_line_text, &vec.rsa.signature);
     388                 :          0 :                 obj = json_string(info.one_line_text);
     389                 :          0 :                 json_object_set_new(json_info.json_write_case, "signature", obj);
     390                 :            : 
     391                 :          0 :                 if (info.interim_info.rsa_data.random_msg) {
     392                 :          0 :                         writeback_hex_str("", info.one_line_text, &vec.rsa.seed);
     393                 :          0 :                         obj = json_string(info.one_line_text);
     394                 :          0 :                         json_object_set_new(json_info.json_write_case, "randomValue", obj);
     395                 :          0 :                         json_object_set_new(json_info.json_write_case, "randomValueLen",
     396                 :          0 :                                 json_integer(vec.rsa.seed.len * 8));
     397                 :            :                 }
     398                 :          0 :         } else if (info.op == FIPS_TEST_ASYM_SIGVER) {
     399                 :          0 :                 if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS)
     400                 :          0 :                         json_object_set_new(json_info.json_write_case, "testPassed", json_true());
     401                 :            :                 else
     402                 :          0 :                         json_object_set_new(json_info.json_write_case, "testPassed", json_false());
     403                 :            :         }
     404                 :            : 
     405                 :          0 :         return 0;
     406                 :            : }
     407                 :            : 
     408                 :            : static int
     409                 :          0 : parse_interim_str(const char *key, char *src, struct fips_val *val)
     410                 :            : {
     411                 :            :         uint32_t i;
     412                 :            : 
     413                 :            :         RTE_SET_USED(val);
     414                 :            : 
     415                 :          0 :         if (strcmp(key, SIGTYPE_JSON_STR) == 0) {
     416                 :          0 :                 for (i = 0; i < RTE_DIM(rsa_padding_types); i++)
     417                 :          0 :                         if (strstr(src, rsa_padding_types[i].desc)) {
     418                 :          0 :                                 info.interim_info.rsa_data.padding = rsa_padding_types[i].padding;
     419                 :          0 :                                 break;
     420                 :            :                         }
     421                 :            : 
     422                 :          0 :                 if (i >= RTE_DIM(rsa_padding_types))
     423                 :          0 :                         return -EINVAL;
     424                 :            : 
     425                 :          0 :         }  else if (strcmp(key, MOD_JSON_STR) == 0) {
     426                 :          0 :                 info.interim_info.rsa_data.modulo = atoi(src);
     427                 :          0 :         } else if (strcmp(key, HASH_JSON_STR) == 0) {
     428                 :          0 :                 for (i = 0; i < RTE_DIM(rsa_auth_algs); i++)
     429                 :          0 :                         if (strstr(src, rsa_auth_algs[i].desc)) {
     430                 :          0 :                                 info.interim_info.rsa_data.auth = rsa_auth_algs[i].auth;
     431                 :          0 :                                 break;
     432                 :            :                         }
     433                 :            : 
     434                 :          0 :                 if (i >= RTE_DIM(rsa_auth_algs))
     435                 :          0 :                         return -EINVAL;
     436                 :            : 
     437                 :          0 :         }  else if (strcmp(key, CONFORMANCE_JSON_STR) == 0) {
     438                 :          0 :                 info.interim_info.rsa_data.random_msg = 1;
     439                 :          0 :         }  else if (strcmp(key, SALT_JSON_STR) == 0) {
     440                 :          0 :                 info.interim_info.rsa_data.saltlen = atoi(src);
     441                 :          0 :         } else if (strcmp(key, TESTTYPE_JSON_STR) == 0) {
     442                 :          0 :                 for (i = 0; i < RTE_DIM(rsa_test_types); i++)
     443                 :          0 :                         if (strstr(src, rsa_test_types[i].desc)) {
     444                 :          0 :                                 info.parse_writeback = parse_test_rsa_json_writeback;
     445                 :          0 :                                 break;
     446                 :            :                         }
     447                 :            : 
     448                 :          0 :                 if (!info.parse_writeback || i >= RTE_DIM(rsa_test_types))
     449                 :          0 :                         return -EINVAL;
     450                 :            : 
     451                 :            :         } else {
     452                 :            :                 return -EINVAL;
     453                 :            :         }
     454                 :            : 
     455                 :            :         return 0;
     456                 :            : }
     457                 :            : 
     458                 :            : static int
     459                 :          0 : parse_keygen_e_str(const char *key, char *src, struct fips_val *val)
     460                 :            : {
     461                 :          0 :         parse_uint8_hex_str(key, src, val);
     462                 :            : 
     463                 :            :         /* For keygen tests, key "e" can be the end of input data
     464                 :            :          * to generate RSA values.
     465                 :            :          */
     466                 :          0 :         return prepare_vec_rsa();
     467                 :            : }
     468                 :            : 
     469                 :            : /*
     470                 :            :  * Message randomization function as per NIST SP 800-106.
     471                 :            :  */
     472                 :            : int
     473                 :          0 : fips_test_randomize_message(struct fips_val *msg, struct fips_val *rand)
     474                 :            : {
     475                 :            :         uint8_t m[FIPS_TEST_JSON_BUF_LEN], rv[RV_BUF_LEN];
     476                 :            :         uint32_t m_bitlen, rv_bitlen, count, remain, i, j;
     477                 :            :         uint16_t rv_len;
     478                 :            : 
     479                 :          0 :         if (!msg->val || !rand->val || rand->len > RV_BUF_LEN
     480                 :          0 :                 || msg->len > (FIPS_TEST_JSON_BUF_LEN - 1))
     481                 :            :                 return -EINVAL;
     482                 :            : 
     483                 :            :         memset(rv, 0, sizeof(rv));
     484                 :          0 :         memcpy(rv, rand->val, rand->len);
     485                 :          0 :         rv_bitlen = rand->len * 8;
     486                 :          0 :         rv_len = rand->len;
     487                 :            : 
     488                 :            :         memset(m, 0, sizeof(m));
     489                 :          0 :         memcpy(m, msg->val, msg->len);
     490                 :          0 :         m_bitlen = msg->len * 8;
     491                 :            : 
     492                 :          0 :         if (m_bitlen >= (rv_bitlen - 1)) {
     493                 :          0 :                 m[msg->len] = 0x80;
     494                 :          0 :                 m_bitlen += 8;
     495                 :            :         } else {
     496                 :          0 :                 m[msg->len] = 0x80;
     497                 :          0 :                 m_bitlen += (rv_bitlen - m_bitlen - 8);
     498                 :            :         }
     499                 :            : 
     500                 :          0 :         count = m_bitlen / rv_bitlen;
     501                 :          0 :         remain = m_bitlen % rv_bitlen;
     502                 :          0 :         for (i = 0; i < count * rv_len; i++)
     503                 :          0 :                 m[i] ^= rv[i % rv_len];
     504                 :            : 
     505                 :          0 :         for (j = 0; j < remain / 8; j++)
     506                 :          0 :                 m[i + j] ^= rv[j];
     507                 :            : 
     508                 :          0 :         m[i + j] = ((uint8_t *)&rv_bitlen)[0];
     509                 :          0 :         m[i + j + 1] = ((uint8_t *)&rv_bitlen)[1];
     510                 :            : 
     511                 :          0 :         rte_free(msg->val);
     512                 :          0 :         msg->len = (rv_bitlen + m_bitlen + 16) / 8;
     513                 :          0 :         msg->val = rte_zmalloc(NULL, msg->len, 0);
     514                 :          0 :         if (!msg->val)
     515                 :            :                 return -EPERM;
     516                 :            : 
     517                 :          0 :         memcpy(msg->val, rv, rv_len);
     518                 :          0 :         memcpy(&msg->val[rv_len], m, (m_bitlen + 16) / 8);
     519                 :          0 :         return 0;
     520                 :            : }
     521                 :            : 
     522                 :            : static int
     523                 :          0 : parse_siggen_message_str(const char *key, char *src, struct fips_val *val)
     524                 :            : {
     525                 :            :         int ret = 0;
     526                 :            : 
     527                 :          0 :         parse_uint8_hex_str(key, src, val);
     528                 :          0 :         if (info.interim_info.rsa_data.random_msg)
     529                 :          0 :                 ret = fips_test_randomize_message(val, &vec.rsa.seed);
     530                 :            : 
     531                 :          0 :         return ret;
     532                 :            : }
     533                 :            : 
     534                 :            : static int
     535                 :          0 : parse_sigver_randomvalue_str(const char *key, char *src, struct fips_val *val)
     536                 :            : {
     537                 :            :         int ret = 0;
     538                 :            : 
     539                 :          0 :         parse_uint8_hex_str(key, src, val);
     540                 :          0 :         if (info.interim_info.rsa_data.random_msg)
     541                 :          0 :                 ret = fips_test_randomize_message(&vec.pt, val);
     542                 :            : 
     543                 :          0 :         return ret;
     544                 :            : }
     545                 :            : 
     546                 :            : struct fips_test_callback rsa_keygen_interim_json_vectors[] = {
     547                 :            :                 {MOD_JSON_STR, parse_interim_str, NULL},
     548                 :            :                 {HASH_JSON_STR, parse_interim_str, NULL},
     549                 :            :                 {TESTTYPE_JSON_STR, parse_interim_str, NULL},
     550                 :            :                 {NULL, NULL, NULL} /**< end pointer */
     551                 :            : };
     552                 :            : 
     553                 :            : struct fips_test_callback rsa_siggen_interim_json_vectors[] = {
     554                 :            :                 {SIGTYPE_JSON_STR, parse_interim_str, NULL},
     555                 :            :                 {MOD_JSON_STR, parse_interim_str, NULL},
     556                 :            :                 {HASH_JSON_STR, parse_interim_str, NULL},
     557                 :            :                 {CONFORMANCE_JSON_STR, parse_interim_str, NULL},
     558                 :            :                 {SALT_JSON_STR, parse_interim_str, NULL},
     559                 :            :                 {TESTTYPE_JSON_STR, parse_interim_str, NULL},
     560                 :            :                 {NULL, NULL, NULL} /**< end pointer */
     561                 :            : };
     562                 :            : 
     563                 :            : struct fips_test_callback rsa_sigver_interim_json_vectors[] = {
     564                 :            :                 {SIGTYPE_JSON_STR, parse_interim_str, NULL},
     565                 :            :                 {MOD_JSON_STR, parse_interim_str, NULL},
     566                 :            :                 {HASH_JSON_STR, parse_interim_str, NULL},
     567                 :            :                 {CONFORMANCE_JSON_STR, parse_interim_str, NULL},
     568                 :            :                 {SALT_JSON_STR, parse_interim_str, NULL},
     569                 :            :                 {N_JSON_STR, parse_uint8_hex_str, &vec.rsa.n},
     570                 :            :                 {E_JSON_STR, parse_uint8_hex_str, &vec.rsa.e},
     571                 :            :                 {TESTTYPE_JSON_STR, parse_interim_str, NULL},
     572                 :            :                 {NULL, NULL, NULL} /**< end pointer */
     573                 :            : };
     574                 :            : 
     575                 :            : struct fips_test_callback rsa_keygen_json_vectors[] = {
     576                 :            :                 {SEED_JSON_STR, parse_uint8_hex_str, &vec.rsa.seed},
     577                 :            :                 {E_JSON_STR, parse_keygen_e_str, &vec.rsa.e},
     578                 :            :                 {NULL, NULL, NULL} /**< end pointer */
     579                 :            : };
     580                 :            : 
     581                 :            : struct fips_test_callback rsa_siggen_json_vectors[] = {
     582                 :            :                 {MSG_JSON_STR, parse_siggen_message_str, &vec.pt},
     583                 :            :                 {NULL, NULL, NULL} /**< end pointer */
     584                 :            : };
     585                 :            : 
     586                 :            : struct fips_test_callback rsa_sigver_json_vectors[] = {
     587                 :            :                 {MSG_JSON_STR, parse_uint8_hex_str, &vec.pt},
     588                 :            :                 {SIG_JSON_STR, parse_uint8_hex_str, &vec.rsa.signature},
     589                 :            :                 {RV_JSON_STR, parse_sigver_randomvalue_str, &vec.rsa.seed},
     590                 :            :                 {NULL, NULL, NULL} /**< end pointer */
     591                 :            : };
     592                 :            : 
     593                 :            : int
     594                 :          0 : parse_test_rsa_json_init(void)
     595                 :            : {
     596                 :          0 :         json_t *keyfmt_obj = json_object_get(json_info.json_vector_set, "keyFormat");
     597                 :          0 :         json_t *mode_obj = json_object_get(json_info.json_vector_set, "mode");
     598                 :          0 :         const char *keyfmt_str = json_string_value(keyfmt_obj);
     599                 :          0 :         const char *mode_str = json_string_value(mode_obj);
     600                 :            : 
     601                 :          0 :         info.callbacks = NULL;
     602                 :          0 :         info.parse_writeback = NULL;
     603                 :          0 :         info.interim_callbacks = NULL;
     604                 :          0 :         info.parse_interim_writeback = NULL;
     605                 :          0 :         info.interim_info.rsa_data.random_msg = 0;
     606                 :            : 
     607                 :          0 :         if (strcmp(mode_str, "keyGen") == 0) {
     608                 :          0 :                 info.op = FIPS_TEST_ASYM_KEYGEN;
     609                 :          0 :                 info.callbacks = rsa_keygen_json_vectors;
     610                 :          0 :                 info.interim_callbacks = rsa_keygen_interim_json_vectors;
     611                 :          0 :         } else if (strcmp(mode_str, "sigGen") == 0) {
     612                 :          0 :                 info.op = FIPS_TEST_ASYM_SIGGEN;
     613                 :          0 :                 info.callbacks = rsa_siggen_json_vectors;
     614                 :          0 :                 info.interim_callbacks = rsa_siggen_interim_json_vectors;
     615                 :          0 :                 info.parse_interim_writeback = parse_test_rsa_json_interim_writeback;
     616                 :          0 :         } else if (strcmp(mode_str, "sigVer") == 0) {
     617                 :          0 :                 info.op = FIPS_TEST_ASYM_SIGVER;
     618                 :          0 :                 info.callbacks = rsa_sigver_json_vectors;
     619                 :          0 :                 info.interim_callbacks = rsa_sigver_interim_json_vectors;
     620                 :          0 :                 info.parse_interim_writeback = parse_test_rsa_json_interim_writeback;
     621                 :            :         } else {
     622                 :            :                 return -EINVAL;
     623                 :            :         }
     624                 :            : 
     625                 :          0 :         info.interim_info.rsa_data.privkey = RTE_RSA_KEY_TYPE_QT;
     626                 :          0 :         if (keyfmt_str != NULL && strcmp(keyfmt_str, "standard") == 0)
     627                 :          0 :                 info.interim_info.rsa_data.privkey = RTE_RSA_KEY_TYPE_EXP;
     628                 :            : 
     629                 :            :         return 0;
     630                 :            : }
     631                 :            : 
     632                 :            : #endif /* USE_JANSSON */

Generated by: LCOV version 1.14