ffc_params.c 9.5 KB

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