ffc_params.c 9.2 KB

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