ffc_params.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. dst->flags = src->flags;
  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. static const OSSL_ITEM flag_map[] = {
  186. { FFC_PARAM_FLAG_VALIDATE_PQ, OSSL_FFC_PARAM_VALIDATE_PQ },
  187. { FFC_PARAM_FLAG_VALIDATE_G, OSSL_FFC_PARAM_VALIDATE_G },
  188. { FFC_PARAM_FLAG_VALIDATE_ALL, OSSL_FFC_PARAM_VALIDATE_PQG },
  189. { 0, "" }
  190. };
  191. int ossl_ffc_params_flags_from_name(const char *name)
  192. {
  193. size_t i;
  194. for (i = 0; i < OSSL_NELEM(flag_map); ++i) {
  195. if (strcasecmp(flag_map[i].ptr, name) == 0)
  196. return flag_map[i].id;
  197. }
  198. return NID_undef;
  199. }
  200. const char *ossl_ffc_params_flags_to_name(int flags)
  201. {
  202. size_t i;
  203. flags &= FFC_PARAM_FLAG_VALIDATE_ALL;
  204. for (i = 0; i < OSSL_NELEM(flag_map); ++i) {
  205. if ((int)flag_map[i].id == flags)
  206. return flag_map[i].ptr;
  207. }
  208. return "";
  209. }
  210. int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
  211. OSSL_PARAM params[])
  212. {
  213. if (ffc == NULL)
  214. return 0;
  215. if (ffc->p != NULL
  216. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
  217. return 0;
  218. if (ffc->q != NULL
  219. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
  220. return 0;
  221. if (ffc->g != NULL
  222. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
  223. return 0;
  224. if (ffc->j != NULL
  225. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
  226. ffc->j))
  227. return 0;
  228. if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
  229. ffc->gindex))
  230. return 0;
  231. if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
  232. ffc->pcounter))
  233. return 0;
  234. if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
  235. return 0;
  236. if (ffc->seed != NULL
  237. && !ossl_param_build_set_octet_string(bld, params,
  238. OSSL_PKEY_PARAM_FFC_SEED,
  239. ffc->seed, ffc->seedlen))
  240. return 0;
  241. if (ffc->nid != NID_undef) {
  242. const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
  243. const char *name = ossl_ffc_named_group_get_name(group);
  244. if (name == NULL
  245. || !ossl_param_build_set_utf8_string(bld, params,
  246. OSSL_PKEY_PARAM_GROUP_NAME,
  247. name))
  248. return 0;
  249. }
  250. if (!ossl_param_build_set_utf8_string(bld, params,
  251. OSSL_PKEY_PARAM_FFC_VALIDATE_TYPE,
  252. ossl_ffc_params_flags_to_name(ffc->flags)))
  253. return 0;
  254. if (ffc->mdname != NULL
  255. && !ossl_param_build_set_utf8_string(bld, params,
  256. OSSL_PKEY_PARAM_FFC_DIGEST,
  257. ffc->mdname))
  258. return 0;
  259. if (ffc->mdprops != NULL
  260. && !ossl_param_build_set_utf8_string(bld, params,
  261. OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
  262. ffc->mdprops))
  263. return 0;
  264. return 1;
  265. }
  266. #ifndef FIPS_MODULE
  267. int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
  268. {
  269. if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
  270. goto err;
  271. if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
  272. goto err;
  273. if (ffc->q != NULL
  274. && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
  275. goto err;
  276. if (ffc->j != NULL
  277. && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
  278. goto err;
  279. if (ffc->seed != NULL) {
  280. size_t i;
  281. if (!BIO_indent(bp, indent, 128)
  282. || BIO_puts(bp, "seed:") <= 0)
  283. goto err;
  284. for (i = 0; i < ffc->seedlen; i++) {
  285. if ((i % 15) == 0) {
  286. if (BIO_puts(bp, "\n") <= 0
  287. || !BIO_indent(bp, indent + 4, 128))
  288. goto err;
  289. }
  290. if (BIO_printf(bp, "%02x%s", ffc->seed[i],
  291. ((i + 1) == ffc->seedlen) ? "" : ":") <= 0)
  292. goto err;
  293. }
  294. if (BIO_write(bp, "\n", 1) <= 0)
  295. return 0;
  296. }
  297. if (ffc->pcounter != -1) {
  298. if (!BIO_indent(bp, indent, 128)
  299. || BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
  300. goto err;
  301. }
  302. return 1;
  303. err:
  304. return 0;
  305. }
  306. #endif /* FIPS_MODULE */