]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_asn1.c
Add FFC param/key generation
[thirdparty/openssl.git] / crypto / dh / dh_asn1.c
1 /*
2 * Copyright 2000-2016 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
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include "dh_local.h"
14 #include <openssl/objects.h>
15 #include <openssl/asn1t.h>
16
17 /* Override the default free and new methods */
18 static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
19 void *exarg)
20 {
21 if (operation == ASN1_OP_NEW_PRE) {
22 *pval = (ASN1_VALUE *)DH_new();
23 if (*pval != NULL)
24 return 2;
25 return 0;
26 } else if (operation == ASN1_OP_FREE_PRE) {
27 DH_free((DH *)*pval);
28 *pval = NULL;
29 return 2;
30 } else if (operation == ASN1_OP_D2I_POST) {
31 ((DH *)*pval)->dirty_cnt++;
32 }
33 return 1;
34 }
35
36 ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
37 ASN1_SIMPLE(DH, params.p, BIGNUM),
38 ASN1_SIMPLE(DH, params.g, BIGNUM),
39 ASN1_OPT_EMBED(DH, length, ZINT32),
40 } ASN1_SEQUENCE_END_cb(DH, DHparams)
41
42 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DH, DHparams, DHparams)
43
44 /*
45 * Internal only structures for handling X9.42 DH: this gets translated to or
46 * from a DH structure straight away.
47 */
48
49 typedef struct {
50 ASN1_BIT_STRING *seed;
51 BIGNUM *counter;
52 } int_dhvparams;
53
54 typedef struct {
55 BIGNUM *p;
56 BIGNUM *q;
57 BIGNUM *g;
58 BIGNUM *j;
59 int_dhvparams *vparams;
60 } int_dhx942_dh;
61
62 ASN1_SEQUENCE(DHvparams) = {
63 ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
64 ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
65 } static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
66
67 ASN1_SEQUENCE(DHxparams) = {
68 ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
69 ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
70 ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
71 ASN1_OPT(int_dhx942_dh, j, BIGNUM),
72 ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
73 } static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
74
75 int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a,
76 const unsigned char **pp, long length);
77 int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
78
79 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(int_dhx942_dh, DHxparams, int_dhx)
80
81 /* Application public function: read in X9.42 DH parameters into DH structure */
82
83 DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
84 {
85 FFC_PARAMS *params;
86 int_dhx942_dh *dhx = NULL;
87 DH *dh = NULL;
88
89 dh = DH_new();
90 if (dh == NULL)
91 return NULL;
92 dhx = d2i_int_dhx(NULL, pp, length);
93 if (dhx == NULL) {
94 DH_free(dh);
95 return NULL;
96 }
97
98 if (a != NULL) {
99 DH_free(*a);
100 *a = dh;
101 }
102
103 params = &dh->params;
104 DH_set0_pqg(dh, dhx->p, dhx->q, dhx->g);
105 ffc_params_set0_j(params, dhx->j);
106
107 if (dhx->vparams != NULL) {
108 /* The counter has a maximum value of 4 * numbits(p) - 1 */
109 size_t counter = (size_t)BN_get_word(dhx->vparams->counter);
110 ffc_params_set_validate_params(params, dhx->vparams->seed->data,
111 dhx->vparams->seed->length, counter);
112 ASN1_BIT_STRING_free(dhx->vparams->seed);
113 BN_free(dhx->vparams->counter);
114 OPENSSL_free(dhx->vparams);
115 dhx->vparams = NULL;
116 }
117
118 OPENSSL_free(dhx);
119 return dh;
120 }
121
122 int i2d_DHxparams(const DH *dh, unsigned char **pp)
123 {
124 int ret = 0;
125 int_dhx942_dh dhx;
126 int_dhvparams dhv = { NULL, NULL };
127 ASN1_BIT_STRING seed;
128 size_t seedlen = 0;
129 const FFC_PARAMS *params = &dh->params;
130 int counter;
131
132 ffc_params_get0_pqg(params, (const BIGNUM **)&dhx.p,
133 (const BIGNUM **)&dhx.q, (const BIGNUM **)&dhx.g);
134 dhx.j = params->j;
135 ffc_params_get_validate_params(params, &seed.data, &seedlen, &counter);
136 seed.length = (int)seedlen;
137
138 if (counter != -1 && seed.data != NULL && seed.length > 0) {
139 seed.flags = ASN1_STRING_FLAG_BITS_LEFT;
140 dhv.seed = &seed;
141 dhv.counter = BN_new();
142 if (dhv.counter == NULL)
143 return 0;
144 if (!BN_set_word(dhv.counter, (BN_ULONG)counter))
145 goto err;
146 dhx.vparams = &dhv;
147 } else {
148 dhx.vparams = NULL;
149 }
150 ret = i2d_int_dhx(&dhx, pp);
151 err:
152 BN_free(dhv.counter);
153 return ret;
154 }