1904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom/* crypto/ec/eck_prn.c */ 2904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom/* 3904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * Written by Nils Larsch for the OpenSSL project. 4904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom */ 5904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom/* ==================================================================== 6904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 7904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 8904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * Redistribution and use in source and binary forms, with or without 9904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * modification, are permitted provided that the following conditions 10904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * are met: 11904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 12904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 1. Redistributions of source code must retain the above copyright 13904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * notice, this list of conditions and the following disclaimer. 14904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 15904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 2. Redistributions in binary form must reproduce the above copyright 16904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * notice, this list of conditions and the following disclaimer in 17904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * the documentation and/or other materials provided with the 18904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * distribution. 19904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 20904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 3. All advertising materials mentioning features or use of this 21904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * software must display the following acknowledgment: 22904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * "This product includes software developed by the OpenSSL Project 23904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 25904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * endorse or promote products derived from this software without 27904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * prior written permission. For written permission, please contact 28904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * openssl-core@openssl.org. 29904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 30904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 5. Products derived from this software may not be called "OpenSSL" 31904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * nor may "OpenSSL" appear in their names without prior written 32904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * permission of the OpenSSL Project. 33904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 34904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 6. Redistributions of any form whatsoever must retain the following 35904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * acknowledgment: 36904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * "This product includes software developed by the OpenSSL Project 37904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 39904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * OF THE POSSIBILITY OF SUCH DAMAGE. 51904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * ==================================================================== 52904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 53904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * This product includes cryptographic software written by Eric Young 54904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * (eay@cryptsoft.com). This product includes software written by Tim 55904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * Hudson (tjh@cryptsoft.com). 56904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * 57904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom */ 58904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom/* ==================================================================== 59904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * Portions originally developed by SUN MICROSYSTEMS, INC., and 61904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom * contributed to the OpenSSL project. 62904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom */ 63904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 64904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom#include <stdio.h> 65904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom#include "cryptlib.h" 66904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom#include <openssl/evp.h> 67904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom#include <openssl/ec.h> 68904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom#include <openssl/bn.h> 69904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 70904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom#ifndef OPENSSL_NO_FP_API 71904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromint ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off) 72904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 73904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO *b; 74904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int ret; 75904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 76904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((b=BIO_new(BIO_s_file())) == NULL) 77904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 78904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB); 79904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return(0); 80904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 81904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO_set_fp(b, fp, BIO_NOCLOSE); 82904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ret = ECPKParameters_print(b, x, off); 83904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO_free(b); 84904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return(ret); 85904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 86904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 87904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromint EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off) 88904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 89904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO *b; 90904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int ret; 91904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 92904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((b=BIO_new(BIO_s_file())) == NULL) 93904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 94904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB); 95904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return(0); 96904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 97904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO_set_fp(b, fp, BIO_NOCLOSE); 98904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ret = EC_KEY_print(b, x, off); 99904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO_free(b); 100904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return(ret); 101904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 102904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 103904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromint ECParameters_print_fp(FILE *fp, const EC_KEY *x) 104904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 105904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO *b; 106904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int ret; 107904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 108904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((b=BIO_new(BIO_s_file())) == NULL) 109904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 110904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB); 111904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return(0); 112904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 113904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO_set_fp(b, fp, BIO_NOCLOSE); 114904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ret = ECParameters_print(b, x); 115904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIO_free(b); 116904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return(ret); 117904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 118904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom#endif 119904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 120904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromint EC_KEY_print(BIO *bp, const EC_KEY *x, int off) 121904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 122904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom EVP_PKEY *pk; 123904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int ret; 124904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom pk = EVP_PKEY_new(); 125904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x)) 126904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 0; 127904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ret = EVP_PKEY_print_private(bp, pk, off, NULL); 128904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom EVP_PKEY_free(pk); 129904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return ret; 130904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 131904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 132904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromint ECParameters_print(BIO *bp, const EC_KEY *x) 133904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 134904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom EVP_PKEY *pk; 135904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int ret; 136904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom pk = EVP_PKEY_new(); 137904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x)) 138904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 0; 139904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ret = EVP_PKEY_print_params(bp, pk, 4, NULL); 140904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom EVP_PKEY_free(pk); 141904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return ret; 142904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 143904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 144904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromstatic int print_bin(BIO *fp, const char *str, const unsigned char *num, 145904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom size_t len, int off); 146904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 147904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromint ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off) 148904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 149904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom unsigned char *buffer=NULL; 150904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom size_t buf_len=0, i; 151904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int ret=0, reason=ERR_R_BIO_LIB; 152904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BN_CTX *ctx=NULL; 153904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom const EC_POINT *point=NULL; 154904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL, 155904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom *order=NULL, *cofactor=NULL; 156904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom const unsigned char *seed; 157904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom size_t seed_len=0; 158904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 159904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom static const char *gen_compressed = "Generator (compressed):"; 160904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom static const char *gen_uncompressed = "Generator (uncompressed):"; 161904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom static const char *gen_hybrid = "Generator (hybrid):"; 162904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 163904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!x) 164904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 165904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_PASSED_NULL_PARAMETER; 166904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 167904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 168904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 169904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ctx = BN_CTX_new(); 170904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (ctx == NULL) 171904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 172904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_MALLOC_FAILURE; 173904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 174904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 175904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 176904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (EC_GROUP_get_asn1_flag(x)) 177904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 178904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom /* the curve parameter are given by an asn1 OID */ 179904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int nid; 180904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 181904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!BIO_indent(bp, off, 128)) 182904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 183904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 184904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom nid = EC_GROUP_get_curve_name(x); 185904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (nid == 0) 186904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 187904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 188904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0) 189904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 190904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_printf(bp, "\n") <= 0) 191904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 192904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 193904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom else 194904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 195904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom /* explicit parameters */ 196904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int is_char_two = 0; 197904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom point_conversion_form_t form; 198904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x)); 199904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 200904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (tmp_nid == NID_X9_62_characteristic_two_field) 201904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom is_char_two = 1; 202904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 203904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((p = BN_new()) == NULL || (a = BN_new()) == NULL || 204904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom (b = BN_new()) == NULL || (order = BN_new()) == NULL || 205904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom (cofactor = BN_new()) == NULL) 206904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 207904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_MALLOC_FAILURE; 208904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 209904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 210392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom#ifndef OPENSSL_NO_EC2M 211904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (is_char_two) 212904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 213904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) 214904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 215904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_EC_LIB; 216904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 217904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 218904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 219904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom else /* prime field */ 220392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom#endif 221904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 222904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) 223904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 224904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_EC_LIB; 225904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 226904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 227904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 228904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 229904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((point = EC_GROUP_get0_generator(x)) == NULL) 230904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 231904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_EC_LIB; 232904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 233904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 234904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!EC_GROUP_get_order(x, order, NULL) || 235904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom !EC_GROUP_get_cofactor(x, cofactor, NULL)) 236904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 237904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_EC_LIB; 238904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 239904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 240904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 241904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom form = EC_GROUP_get_point_conversion_form(x); 242904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 243904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((gen = EC_POINT_point2bn(x, point, 244904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom form, NULL, ctx)) == NULL) 245904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 246904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_EC_LIB; 247904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 248904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 249904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 250904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buf_len = (size_t)BN_num_bytes(p); 251904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (buf_len < (i = (size_t)BN_num_bytes(a))) 252904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buf_len = i; 253904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (buf_len < (i = (size_t)BN_num_bytes(b))) 254904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buf_len = i; 255904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (buf_len < (i = (size_t)BN_num_bytes(gen))) 256904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buf_len = i; 257904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (buf_len < (i = (size_t)BN_num_bytes(order))) 258904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buf_len = i; 259904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (buf_len < (i = (size_t)BN_num_bytes(cofactor))) 260904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buf_len = i; 261904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 262904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((seed = EC_GROUP_get0_seed(x)) != NULL) 263904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom seed_len = EC_GROUP_get_seed_len(x); 264904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 265904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buf_len += 10; 266904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((buffer = OPENSSL_malloc(buf_len)) == NULL) 267904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 268904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom reason = ERR_R_MALLOC_FAILURE; 269904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 270904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 271904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 272904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!BIO_indent(bp, off, 128)) 273904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 274904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 275904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom /* print the 'short name' of the field type */ 276904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) 277904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom <= 0) 278904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 279904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 280904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (is_char_two) 281904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 282904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom /* print the 'short name' of the base type OID */ 283904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom int basis_type = EC_GROUP_get_basis_type(x); 284904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (basis_type == 0) 285904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 286904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 287904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!BIO_indent(bp, off, 128)) 288904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 289904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 290904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_printf(bp, "Basis Type: %s\n", 291904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom OBJ_nid2sn(basis_type)) <= 0) 292904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 293904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 294904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom /* print the polynomial */ 295904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer, 296904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom off)) 297904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 298904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 299904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom else 300904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 301904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer,off)) 302904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 303904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 304904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off)) 305904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 306904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off)) 307904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 308904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (form == POINT_CONVERSION_COMPRESSED) 309904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 310904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen, 311904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buffer, off)) 312904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 313904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 314904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom else if (form == POINT_CONVERSION_UNCOMPRESSED) 315904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 316904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen, 317904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buffer, off)) 318904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 319904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 320904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom else /* form == POINT_CONVERSION_HYBRID */ 321904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 322904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen, 323904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buffer, off)) 324904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 325904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 326904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, 327904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buffer, off)) goto err; 328904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor, 329904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom buffer, off)) goto err; 330904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (seed && !print_bin(bp, "Seed:", seed, seed_len, off)) 331904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom goto err; 332904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 333904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ret=1; 334904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromerr: 335904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (!ret) 336904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom ECerr(EC_F_ECPKPARAMETERS_PRINT, reason); 337904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (p) 338904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BN_free(p); 339904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (a) 340904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BN_free(a); 341904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (b) 342904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BN_free(b); 343904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (gen) 344904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BN_free(gen); 345904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (order) 346904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BN_free(order); 347904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (cofactor) 348904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BN_free(cofactor); 349904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (ctx) 350904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom BN_CTX_free(ctx); 351904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (buffer != NULL) 352904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom OPENSSL_free(buffer); 353904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return(ret); 354904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 355904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 356904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstromstatic int print_bin(BIO *fp, const char *name, const unsigned char *buf, 357904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom size_t len, int off) 358904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 359904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom size_t i; 360904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom char str[128]; 361904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 362904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (buf == NULL) 363904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 1; 364904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (off) 365904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 366904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (off > 128) 367904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom off=128; 368904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom memset(str,' ',off); 369904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_write(fp, str, off) <= 0) 370904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 0; 371904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 372904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 373904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_printf(fp,"%s", name) <= 0) 374904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 0; 375904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 376904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom for (i=0; i<len; i++) 377904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 378904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if ((i%15) == 0) 379904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom { 380904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom str[0]='\n'; 381904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom memset(&(str[1]),' ',off+4); 382904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_write(fp, str, off+1+4) <= 0) 383904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 0; 384904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 385904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0) 386904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 0; 387904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 388904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom if (BIO_write(fp,"\n",1) <= 0) 389904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 0; 390904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom 391904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom return 1; 392904c5bb06deb8e0b17c3673c0ceb7d80420c16f3Brian Carlstrom } 393