1109998Smarkm/* x_bignum.c */ 2280297Sjkim/* 3280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4280297Sjkim * 2000. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7356290Sjkim * Copyright (c) 2000-2019 The OpenSSL Project. All rights reserved. 8109998Smarkm * 9109998Smarkm * Redistribution and use in source and binary forms, with or without 10109998Smarkm * modification, are permitted provided that the following conditions 11109998Smarkm * are met: 12109998Smarkm * 13109998Smarkm * 1. Redistributions of source code must retain the above copyright 14280297Sjkim * notice, this list of conditions and the following disclaimer. 15109998Smarkm * 16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 17109998Smarkm * notice, this list of conditions and the following disclaimer in 18109998Smarkm * the documentation and/or other materials provided with the 19109998Smarkm * distribution. 20109998Smarkm * 21109998Smarkm * 3. All advertising materials mentioning features or use of this 22109998Smarkm * software must display the following acknowledgment: 23109998Smarkm * "This product includes software developed by the OpenSSL Project 24109998Smarkm * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25109998Smarkm * 26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27109998Smarkm * endorse or promote products derived from this software without 28109998Smarkm * prior written permission. For written permission, please contact 29109998Smarkm * licensing@OpenSSL.org. 30109998Smarkm * 31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 32109998Smarkm * nor may "OpenSSL" appear in their names without prior written 33109998Smarkm * permission of the OpenSSL Project. 34109998Smarkm * 35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 36109998Smarkm * acknowledgment: 37109998Smarkm * "This product includes software developed by the OpenSSL Project 38109998Smarkm * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39109998Smarkm * 40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 52109998Smarkm * ==================================================================== 53109998Smarkm * 54109998Smarkm * This product includes cryptographic software written by Eric Young 55109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 56109998Smarkm * Hudson (tjh@cryptsoft.com). 57109998Smarkm * 58109998Smarkm */ 59109998Smarkm 60109998Smarkm#include <stdio.h> 61109998Smarkm#include "cryptlib.h" 62109998Smarkm#include <openssl/asn1t.h> 63160814Ssimon#include <openssl/bn.h> 64109998Smarkm 65280297Sjkim/* 66280297Sjkim * Custom primitive type for BIGNUM handling. This reads in an ASN1_INTEGER 67280297Sjkim * as a BIGNUM directly. Currently it ignores the sign which isn't a problem 68280297Sjkim * since all BIGNUMs used are non negative and anything that looks negative 69280297Sjkim * is normally due to an encoding error. 70109998Smarkm */ 71109998Smarkm 72280297Sjkim#define BN_SENSITIVE 1 73109998Smarkm 74109998Smarkmstatic int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it); 75109998Smarkmstatic void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 76109998Smarkm 77280297Sjkimstatic int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, 78280297Sjkim const ASN1_ITEM *it); 79280297Sjkimstatic int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 80280297Sjkim int utype, char *free_cont, const ASN1_ITEM *it); 81306195Sjkimstatic int bn_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, 82306195Sjkim int indent, const ASN1_PCTX *pctx); 83109998Smarkm 84109998Smarkmstatic ASN1_PRIMITIVE_FUNCS bignum_pf = { 85280297Sjkim NULL, 0, 86280297Sjkim bn_new, 87280297Sjkim bn_free, 88280297Sjkim 0, 89280297Sjkim bn_c2i, 90306195Sjkim bn_i2c, 91306195Sjkim bn_print 92109998Smarkm}; 93109998Smarkm 94109998SmarkmASN1_ITEM_start(BIGNUM) 95280297Sjkim ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM" 96109998SmarkmASN1_ITEM_end(BIGNUM) 97109998Smarkm 98109998SmarkmASN1_ITEM_start(CBIGNUM) 99280297Sjkim ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, BN_SENSITIVE, "BIGNUM" 100109998SmarkmASN1_ITEM_end(CBIGNUM) 101109998Smarkm 102109998Smarkmstatic int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 103109998Smarkm{ 104280297Sjkim *pval = (ASN1_VALUE *)BN_new(); 105356290Sjkim if (*pval != NULL) 106280297Sjkim return 1; 107280297Sjkim else 108280297Sjkim return 0; 109109998Smarkm} 110109998Smarkm 111109998Smarkmstatic void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 112109998Smarkm{ 113356290Sjkim if (*pval == NULL) 114280297Sjkim return; 115280297Sjkim if (it->size & BN_SENSITIVE) 116280297Sjkim BN_clear_free((BIGNUM *)*pval); 117280297Sjkim else 118280297Sjkim BN_free((BIGNUM *)*pval); 119280297Sjkim *pval = NULL; 120109998Smarkm} 121109998Smarkm 122280297Sjkimstatic int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, 123280297Sjkim const ASN1_ITEM *it) 124109998Smarkm{ 125280297Sjkim BIGNUM *bn; 126280297Sjkim int pad; 127356290Sjkim if (*pval == NULL) 128280297Sjkim return -1; 129280297Sjkim bn = (BIGNUM *)*pval; 130280297Sjkim /* If MSB set in an octet we need a padding byte */ 131280297Sjkim if (BN_num_bits(bn) & 0x7) 132280297Sjkim pad = 0; 133280297Sjkim else 134280297Sjkim pad = 1; 135280297Sjkim if (cont) { 136280297Sjkim if (pad) 137280297Sjkim *cont++ = 0; 138280297Sjkim BN_bn2bin(bn, cont); 139280297Sjkim } 140280297Sjkim return pad + BN_num_bytes(bn); 141109998Smarkm} 142109998Smarkm 143160814Ssimonstatic int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 144280297Sjkim int utype, char *free_cont, const ASN1_ITEM *it) 145109998Smarkm{ 146280297Sjkim BIGNUM *bn; 147291719Sjkim 148291719Sjkim if (*pval == NULL && !bn_new(pval, it)) 149291719Sjkim return 0; 150280297Sjkim bn = (BIGNUM *)*pval; 151280297Sjkim if (!BN_bin2bn(cont, len, bn)) { 152280297Sjkim bn_free(pval, it); 153280297Sjkim return 0; 154280297Sjkim } 155280297Sjkim return 1; 156109998Smarkm} 157306195Sjkim 158306195Sjkimstatic int bn_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, 159306195Sjkim int indent, const ASN1_PCTX *pctx) 160306195Sjkim{ 161306195Sjkim if (!BN_print(out, *(BIGNUM **)pval)) 162306195Sjkim return 0; 163306195Sjkim if (BIO_puts(out, "\n") <= 0) 164306195Sjkim return 0; 165306195Sjkim return 1; 166306195Sjkim} 167