2
0

ffc_params.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h> /* memset */
  10. #include <openssl/core_names.h>
  11. #include "internal/ffc.h"
  12. #include "internal/param_build_set.h"
  13. #include "internal/nelem.h"
  14. #ifndef FIPS_MODULE
  15. # include <openssl/asn1.h> /* ossl_ffc_params_print */
  16. #endif
  17. void ossl_ffc_params_init(FFC_PARAMS *params)
  18. {
  19. memset(params, 0, sizeof(*params));
  20. params->pcounter = -1;
  21. params->gindex = FFC_UNVERIFIABLE_GINDEX;
  22. params->flags = FFC_PARAM_FLAG_VALIDATE_PQG;
  23. }
  24. void ossl_ffc_params_cleanup(FFC_PARAMS *params)
  25. {
  26. #ifdef FIPS_MODULE
  27. BN_clear_free(params->p);
  28. BN_clear_free(params->q);
  29. BN_clear_free(params->g);
  30. BN_clear_free(params->j);
  31. OPENSSL_clear_free(params->seed, params->seedlen);
  32. #else
  33. BN_free(params->p);
  34. BN_free(params->q);
  35. BN_free(params->g);
  36. BN_free(params->j);
  37. OPENSSL_free(params->seed);
  38. #endif
  39. ossl_ffc_params_init(params);
  40. }
  41. void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  42. {
  43. if (p != NULL && p != d->p) {
  44. BN_free(d->p);
  45. d->p = p;
  46. }
  47. if (q != NULL && q != d->q) {
  48. BN_free(d->q);
  49. d->q = q;
  50. }
  51. if (g != NULL && g != d->g) {
  52. BN_free(d->g);
  53. d->g = g;
  54. }
  55. }
  56. void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
  57. const BIGNUM **q, const BIGNUM **g)
  58. {
  59. if (p != NULL)
  60. *p = d->p;
  61. if (q != NULL)
  62. *q = d->q;
  63. if (g != NULL)
  64. *g = d->g;
  65. }
  66. /* j is the 'cofactor' that is optionally output for ASN1. */
  67. void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
  68. {
  69. BN_free(d->j);
  70. d->j = NULL;
  71. if (j != NULL)
  72. d->j = j;
  73. }
  74. int ossl_ffc_params_set_seed(FFC_PARAMS *params,
  75. const unsigned char *seed, size_t seedlen)
  76. {
  77. if (params->seed != NULL) {
  78. if (params->seed == seed)
  79. return 1;
  80. OPENSSL_free(params->seed);
  81. }
  82. if (seed != NULL && seedlen > 0) {
  83. params->seed = OPENSSL_memdup(seed, seedlen);
  84. if (params->seed == NULL)
  85. return 0;
  86. params->seedlen = seedlen;
  87. } else {
  88. params->seed = NULL;
  89. params->seedlen = 0;
  90. }
  91. return 1;
  92. }
  93. void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index)
  94. {
  95. params->gindex = index;
  96. }
  97. void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index)
  98. {
  99. params->pcounter = index;
  100. }
  101. void ossl_ffc_params_set_h(FFC_PARAMS *params, int index)
  102. {
  103. params->h = index;
  104. }
  105. void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags)
  106. {
  107. params->flags = flags;
  108. }
  109. void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags,
  110. int enable)
  111. {
  112. if (enable)
  113. params->flags |= flags;
  114. else
  115. params->flags &= ~flags;
  116. }
  117. void ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props)
  118. {
  119. params->mdname = alg;
  120. params->mdprops = props;
  121. }
  122. int ossl_ffc_params_set_validate_params(FFC_PARAMS *params,
  123. const unsigned char *seed,
  124. size_t seedlen, int counter)
  125. {
  126. if (!ossl_ffc_params_set_seed(params, seed, seedlen))
  127. return 0;
  128. params->pcounter = counter;
  129. return 1;
  130. }
  131. void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
  132. unsigned char **seed, size_t *seedlen,
  133. int *pcounter)
  134. {
  135. if (seed != NULL)
  136. *seed = params->seed;
  137. if (seedlen != NULL)
  138. *seedlen = params->seedlen;
  139. if (pcounter != NULL)
  140. *pcounter = params->pcounter;
  141. }
  142. static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
  143. {
  144. BIGNUM *a;
  145. /*
  146. * If source is read only just copy the pointer, so
  147. * we don't have to reallocate it.
  148. */
  149. if (src == NULL)
  150. a = NULL;
  151. else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
  152. && !BN_get_flags(src, BN_FLG_MALLOCED))
  153. a = (BIGNUM *)src;
  154. else if ((a = BN_dup(src)) == NULL)
  155. return 0;
  156. BN_clear_free(*dst);
  157. *dst = a;
  158. return 1;
  159. }
  160. int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
  161. {
  162. if (!ffc_bn_cpy(&dst->p, src->p)
  163. || !ffc_bn_cpy(&dst->g, src->g)
  164. || !ffc_bn_cpy(&dst->q, src->q)
  165. || !ffc_bn_cpy(&dst->j, src->j))
  166. return 0;
  167. dst->mdname = src->mdname;
  168. dst->mdprops = src->mdprops;
  169. OPENSSL_free(dst->seed);
  170. dst->seedlen = src->seedlen;
  171. if (src->seed != NULL) {
  172. dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
  173. if (dst->seed == NULL)
  174. return 0;
  175. } else {
  176. dst->seed = NULL;
  177. }
  178. dst->nid = src->nid;
  179. dst->pcounter = src->pcounter;
  180. dst->h = src->h;
  181. dst->gindex = src->gindex;
  182. dst->flags = src->flags;
  183. dst->keylength = src->keylength;
  184. return 1;
  185. }
  186. int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
  187. {
  188. return BN_cmp(a->p, b->p) == 0
  189. && BN_cmp(a->g, b->g) == 0
  190. && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
  191. }
  192. int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
  193. OSSL_PARAM params[])
  194. {
  195. int test_flags;
  196. if (ffc->p != NULL
  197. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
  198. return 0;
  199. if (ffc->q != NULL
  200. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
  201. return 0;
  202. if (ffc->g != NULL
  203. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
  204. return 0;
  205. if (ffc->j != NULL
  206. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
  207. ffc->j))
  208. return 0;
  209. if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
  210. ffc->gindex))
  211. return 0;
  212. if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
  213. ffc->pcounter))
  214. return 0;
  215. if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
  216. return 0;
  217. if (ffc->seed != NULL
  218. && !ossl_param_build_set_octet_string(bld, params,
  219. OSSL_PKEY_PARAM_FFC_SEED,
  220. ffc->seed, ffc->seedlen))
  221. return 0;
  222. if (ffc->nid != NID_undef) {
  223. const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
  224. const char *name = ossl_ffc_named_group_get_name(group);
  225. if (name == NULL
  226. || !ossl_param_build_set_utf8_string(bld, params,
  227. OSSL_PKEY_PARAM_GROUP_NAME,
  228. name))
  229. return 0;
  230. }
  231. test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0);
  232. if (!ossl_param_build_set_int(bld, params,
  233. OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags))
  234. return 0;
  235. test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0);
  236. if (!ossl_param_build_set_int(bld, params,
  237. OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags))
  238. return 0;
  239. test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0);
  240. if (!ossl_param_build_set_int(bld, params,
  241. OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY,
  242. test_flags))
  243. return 0;
  244. if (ffc->mdname != NULL
  245. && !ossl_param_build_set_utf8_string(bld, params,
  246. OSSL_PKEY_PARAM_FFC_DIGEST,
  247. ffc->mdname))
  248. return 0;
  249. if (ffc->mdprops != NULL
  250. && !ossl_param_build_set_utf8_string(bld, params,
  251. OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
  252. ffc->mdprops))
  253. return 0;
  254. return 1;
  255. }
  256. #ifndef FIPS_MODULE
  257. int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
  258. {
  259. if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
  260. goto err;
  261. if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
  262. goto err;
  263. if (ffc->q != NULL
  264. && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
  265. goto err;
  266. if (ffc->j != NULL
  267. && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
  268. goto err;
  269. if (ffc->seed != NULL) {
  270. size_t i;
  271. if (!BIO_indent(bp, indent, 128)
  272. || BIO_puts(bp, "seed:") <= 0)
  273. goto err;
  274. for (i = 0; i < ffc->seedlen; i++) {
  275. if ((i % 15) == 0) {
  276. if (BIO_puts(bp, "\n") <= 0
  277. || !BIO_indent(bp, indent + 4, 128))
  278. goto err;
  279. }
  280. if (BIO_printf(bp, "%02x%s", ffc->seed[i],
  281. ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
  282. goto err;
  283. }
  284. if (BIO_write(bp, "\n", 1) <= 0)
  285. return 0;
  286. }
  287. if (ffc->pcounter != -1) {
  288. if (!BIO_indent(bp, indent, 128)
  289. || BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
  290. goto err;
  291. }
  292. return 1;
  293. err:
  294. return 0;
  295. }
  296. #endif /* FIPS_MODULE */