| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bn.h> |
| 16 | #include <openssl/dsa.h> |
| 17 |
| 18 |
| 19 | struct wrapped_callback { |
| 20 | void (*callback)(int, int, void *); |
| 21 | void *arg; |
| 22 | }; |
| 23 |
| 24 | // callback_wrapper converts an “old” style generation callback to the newer |
| 25 | // |BN_GENCB| form. |
| 26 | static int callback_wrapper(int event, int n, BN_GENCB *gencb) { |
| 27 | struct wrapped_callback *wrapped = (struct wrapped_callback *) gencb->arg; |
| 28 | wrapped->callback(event, n, wrapped->arg); |
| 29 | return 1; |
| 30 | } |
| 31 |
| 32 | DSA *DSA_generate_parameters(int bits, uint8_t *seed_in, int seed_len, |
| 33 | int *counter_ret, unsigned long *h_ret, |
| 34 | void (*callback)(int, int, void *), void *cb_arg) { |
| 35 | if (bits < 0 || seed_len < 0) { |
| 36 | return NULL; |
| 37 | } |
| 38 |
| 39 | DSA *ret = DSA_new(); |
| 40 | if (ret == NULL) { |
| 41 | return NULL; |
| 42 | } |
| 43 |
| 44 | BN_GENCB gencb_storage; |
| 45 | BN_GENCB *cb = NULL; |
| 46 |
| 47 | struct wrapped_callback wrapped; |
| 48 |
| 49 | if (callback != NULL) { |
| 50 | wrapped.callback = callback; |
| 51 | wrapped.arg = cb_arg; |
| 52 |
| 53 | cb = &gencb_storage; |
| 54 | BN_GENCB_set(cb, callback_wrapper, &wrapped); |
| 55 | } |
| 56 |
| 57 | if (!DSA_generate_parameters_ex(ret, bits, seed_in, seed_len, counter_ret, |
| 58 | h_ret, cb)) { |
| 59 | goto err; |
| 60 | } |
| 61 |
| 62 | return ret; |
| 63 |
| 64 | err: |
| 65 | DSA_free(ret); |
| 66 | return NULL; |
| 67 | } |
| 68 |