1109998Smarkm/* x_long.c */ 2296465Sdelphij/* 3296465Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4296465Sdelphij * 2000. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7109998Smarkm * Copyright (c) 2000 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 14296465Sdelphij * 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 65296465Sdelphij/* 66296465Sdelphij * Custom primitive type for long handling. This converts between an 67296465Sdelphij * ASN1_INTEGER and a long directly. 68109998Smarkm */ 69109998Smarkm 70109998Smarkmstatic int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it); 71109998Smarkmstatic void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 72109998Smarkm 73296465Sdelphijstatic int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, 74296465Sdelphij const ASN1_ITEM *it); 75296465Sdelphijstatic int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 76296465Sdelphij int utype, char *free_cont, const ASN1_ITEM *it); 77109998Smarkm 78109998Smarkmstatic ASN1_PRIMITIVE_FUNCS long_pf = { 79296465Sdelphij NULL, 0, 80296465Sdelphij long_new, 81296465Sdelphij long_free, 82296465Sdelphij long_free, /* Clear should set to initial value */ 83296465Sdelphij long_c2i, 84296465Sdelphij long_i2c 85109998Smarkm}; 86109998Smarkm 87109998SmarkmASN1_ITEM_start(LONG) 88296465Sdelphij ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG" 89109998SmarkmASN1_ITEM_end(LONG) 90109998Smarkm 91109998SmarkmASN1_ITEM_start(ZLONG) 92296465Sdelphij ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG" 93109998SmarkmASN1_ITEM_end(ZLONG) 94109998Smarkm 95109998Smarkmstatic int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 96109998Smarkm{ 97296465Sdelphij *(long *)pval = it->size; 98296465Sdelphij return 1; 99109998Smarkm} 100109998Smarkm 101109998Smarkmstatic void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 102109998Smarkm{ 103296465Sdelphij *(long *)pval = it->size; 104109998Smarkm} 105109998Smarkm 106296465Sdelphijstatic int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, 107296465Sdelphij const ASN1_ITEM *it) 108109998Smarkm{ 109296465Sdelphij long ltmp; 110296465Sdelphij unsigned long utmp; 111296465Sdelphij int clen, pad, i; 112296465Sdelphij /* this exists to bypass broken gcc optimization */ 113296465Sdelphij char *cp = (char *)pval; 114127128Snectar 115296465Sdelphij /* use memcpy, because we may not be long aligned */ 116296465Sdelphij memcpy(<mp, cp, sizeof(long)); 117127128Snectar 118296465Sdelphij if (ltmp == it->size) 119296465Sdelphij return -1; 120296465Sdelphij /* 121296465Sdelphij * Convert the long to positive: we subtract one if negative so we can 122296465Sdelphij * cleanly handle the padding if only the MSB of the leading octet is 123296465Sdelphij * set. 124296465Sdelphij */ 125296465Sdelphij if (ltmp < 0) 126296465Sdelphij utmp = -ltmp - 1; 127296465Sdelphij else 128296465Sdelphij utmp = ltmp; 129296465Sdelphij clen = BN_num_bits_word(utmp); 130296465Sdelphij /* If MSB of leading octet set we need to pad */ 131296465Sdelphij if (!(clen & 0x7)) 132296465Sdelphij pad = 1; 133296465Sdelphij else 134296465Sdelphij pad = 0; 135109998Smarkm 136296465Sdelphij /* Convert number of bits to number of octets */ 137296465Sdelphij clen = (clen + 7) >> 3; 138109998Smarkm 139296465Sdelphij if (cont) { 140296465Sdelphij if (pad) 141296465Sdelphij *cont++ = (ltmp < 0) ? 0xff : 0; 142296465Sdelphij for (i = clen - 1; i >= 0; i--) { 143296465Sdelphij cont[i] = (unsigned char)(utmp & 0xff); 144296465Sdelphij if (ltmp < 0) 145296465Sdelphij cont[i] ^= 0xff; 146296465Sdelphij utmp >>= 8; 147296465Sdelphij } 148296465Sdelphij } 149296465Sdelphij return clen + pad; 150109998Smarkm} 151109998Smarkm 152160814Ssimonstatic int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 153296465Sdelphij int utype, char *free_cont, const ASN1_ITEM *it) 154109998Smarkm{ 155296465Sdelphij int neg, i; 156296465Sdelphij long ltmp; 157296465Sdelphij unsigned long utmp = 0; 158296465Sdelphij char *cp = (char *)pval; 159296465Sdelphij if (len > (int)sizeof(long)) { 160296465Sdelphij ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); 161296465Sdelphij return 0; 162296465Sdelphij } 163296465Sdelphij /* Is it negative? */ 164296465Sdelphij if (len && (cont[0] & 0x80)) 165296465Sdelphij neg = 1; 166296465Sdelphij else 167296465Sdelphij neg = 0; 168296465Sdelphij utmp = 0; 169296465Sdelphij for (i = 0; i < len; i++) { 170296465Sdelphij utmp <<= 8; 171296465Sdelphij if (neg) 172296465Sdelphij utmp |= cont[i] ^ 0xff; 173296465Sdelphij else 174296465Sdelphij utmp |= cont[i]; 175296465Sdelphij } 176296465Sdelphij ltmp = (long)utmp; 177296465Sdelphij if (neg) { 178296465Sdelphij ltmp++; 179296465Sdelphij ltmp = -ltmp; 180296465Sdelphij } 181296465Sdelphij if (ltmp == it->size) { 182296465Sdelphij ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); 183296465Sdelphij return 0; 184296465Sdelphij } 185296465Sdelphij memcpy(cp, <mp, sizeof(long)); 186296465Sdelphij return 1; 187109998Smarkm} 188