ffc_params.c 9.2 KB

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