Exemplo n.º 1
0
/**
 * xmlSecBnAdd:
 * @bn:		the pointer to BN.
 * @delta:	the delta.
 *
 * Adds @delta to @bn.
 *
 * Returns 0 on success or a negative value if an error occurs.
 */
EXPORT_C
int 
xmlSecBnAdd(xmlSecBnPtr bn, int delta) {
    int over, tmp;
    xmlSecByte* data;
    xmlSecSize i;
    xmlSecByte ch;
    int ret;

    xmlSecAssert2(bn != NULL, -1);

    if(delta == 0) {
    	return(0);
    }

    data = xmlSecBufferGetData(bn);
    if(delta > 0) {
        for(over = delta, i = xmlSecBufferGetSize(bn); (i > 0) && (over > 0) ;) {
	        xmlSecAssert2(data != NULL, -1);
	
            tmp     = data[--i];
        	over   += tmp;
	        data[i]	= over % 256;
	        over	= over / 256;
        }
    
        while(over > 0) {
	        ch	= over % 256;
	        over	= over / 256;
	
        	ret = xmlSecBufferPrepend(bn, &ch, 1);
	        if(ret < 0) {
	            xmlSecError(XMLSEC_ERRORS_HERE,
			        NULL,
			        "xmlSecBufferPrepend",
			        XMLSEC_ERRORS_R_XMLSEC_FAILED,
			        "size=1");
	            return (-1);
	        }
        }
    } else {
        for(over = -delta, i = xmlSecBufferGetSize(bn); (i > 0) && (over > 0);) {
	        xmlSecAssert2(data != NULL, -1);
	
            tmp     = data[--i];
            if(tmp < over) {
                data[i]	= 0;
                over = (over - tmp) / 256;
            } else {
                data[i] = tmp - over;
                over = 0;
            }
        }
    }
    return(0);
}
Exemplo n.º 2
0
PyObject *xmlsec_BufferPrepend(PyObject *self, PyObject *args) {
  PyObject *buf_obj;
  xmlSecBufferPtr buf;
  const xmlSecByte *data;
  xmlSecSize size;

  if (CheckArgs(args, "OSI:bufferPrepend")) {
    if (!PyArg_ParseTuple(args, "Osi:bufferPrepend", &buf_obj, &data, &size))
      return NULL;
  }
  else return NULL;

  buf = xmlSecBufferPtr_get(buf_obj);
  
  return (wrap_int(xmlSecBufferPrepend(buf, data, size)));
}
Exemplo n.º 3
0
/**
 * xmlSecBnMul:
 * @bn:			the pointer to BN.
 * @multiplier:		the multiplier.
 *
 * Multiplies @bn with @multiplier.
 *
 * Returns 0 on success or a negative value if an error occurs.
 */
EXPORT_C
int 
xmlSecBnMul(xmlSecBnPtr bn, int multiplier) {
    xmlSecByte* data;
    int over;
    xmlSecSize i;
    xmlSecByte ch;
    int ret;

    xmlSecAssert2(bn != NULL, -1);
    xmlSecAssert2(multiplier > 0, -1);

    if(multiplier == 1) {
	return(0);
    }

    data = xmlSecBufferGetData(bn);
    i = xmlSecBufferGetSize(bn);
    over = 0; 
    while(i > 0) {
	xmlSecAssert2(data != NULL, -1);

	over	= over + multiplier * data[--i];
	data[i]	= over % 256;
	over	= over / 256;
    }
    
    while(over > 0) {
	ch	= over % 256;
	over	= over / 256;
	
	ret = xmlSecBufferPrepend(bn, &ch, 1);
	if(ret < 0) {
	    xmlSecError(XMLSEC_ERRORS_HERE,
			NULL,
			"xmlSecBufferPrepend",
			XMLSEC_ERRORS_R_XMLSEC_FAILED,
			"size=1");
	    return (-1);
	}
    }
    
    return(0);
}
Exemplo n.º 4
0
/**
 * xmlSecBnFromString:
 * @bn:		the pointer to BN.
 * @str:	the string with BN.
 * @base:	the base for @str.
 *
 * Reads @bn from string @str assuming it has base @base.
 *
 * Returns 0 on success or a negative value if an error occurs.
 */
EXPORT_C
int 
xmlSecBnFromString(xmlSecBnPtr bn, const xmlChar* str, xmlSecSize base) {
    xmlSecSize i, len, size;
    xmlSecByte ch;
    xmlSecByte* data;
    int positive;
    int nn;
    int ret;

    xmlSecAssert2(bn != NULL, -1);
    xmlSecAssert2(str != NULL, -1);
    xmlSecAssert2(base > 1, -1);
    xmlSecAssert2(base <= sizeof(xmlSecBnRevLookupTable), -1);

    /* trivial case */
    len = xmlStrlen(str);
    if(len == 0) {
        return(0);
    }
    
    /* The result size could not exceed the input string length
     * because each char fits inside a byte in all cases :)
     * In truth, it would be likely less than 1/2 input string length
     * because each byte is represented by 2 chars. If needed, 
     * buffer size would be increased by Mul/Add functions.
     * Finally, we can add one byte for 00 or 10 prefix.
     */
    ret = xmlSecBufferSetMaxSize(bn, xmlSecBufferGetSize(bn) + len / 2 + 1 + 1);
    if(ret < 0) {
        xmlSecError(XMLSEC_ERRORS_HERE,
		        NULL,
		        "xmlSecBnRevLookupTable",
		        XMLSEC_ERRORS_R_XMLSEC_FAILED,
		        "size=%d", len / 2 + 1);
        return (-1);
    }

    /* figure out if it is positive or negative number */
    positive = 1;
    i = 0;
    while(i < len) {
        ch = str[i++];

        /* skip spaces */
        if(isspace(ch)) {
	        continue;
        } 
        
        /* check if it is + or - */
        if(ch == '+') {
            positive = 1;
            break;
        } else if(ch == '-') {
            positive = 0;
            break;
        }

        /* otherwise, it must be start of the number */
        nn = xmlSecBnLookupTable[ch];
        if((nn >= 0) && ((xmlSecSize)nn < base)) {
            xmlSecAssert2(i > 0, -1);

            /* no sign, positive by default */
            positive = 1;
            --i; /* make sure that we will look at this character in next loop */
            break;
        } else {
	        xmlSecError(XMLSEC_ERRORS_HERE,
		        NULL,
		        NULL,
		        XMLSEC_ERRORS_R_INVALID_DATA,
		        "char=%c;base=%d", 
		        ch, base);
    	        return (-1);
        }
    }

    /* now parse the number itself */
    while(i < len) {
        ch = str[i++];
        if(isspace(ch)) {
	        continue;
        }

        xmlSecAssert2(ch <(sizeof(xmlSecBnLookupTable)/sizeof(xmlSecBnLookupTable[0])), -1);
        nn = xmlSecBnLookupTable[ch];
        if((nn < 0) || ((xmlSecSize)nn > base)) {
	        xmlSecError(XMLSEC_ERRORS_HERE,
		        NULL,
		        NULL,
		        XMLSEC_ERRORS_R_INVALID_DATA,
		        "char=%c;base=%d", 
		        ch, base);
    	        return (-1);
        }

        ret = xmlSecBnMul(bn, base);
        if(ret < 0) {
	        xmlSecError(XMLSEC_ERRORS_HERE,
		        NULL,
		        "xmlSecBnMul",
		        XMLSEC_ERRORS_R_XMLSEC_FAILED,
		        "base=%d", base);
	        return (-1);
        }

        ret = xmlSecBnAdd(bn, nn);
        if(ret < 0) {
	        xmlSecError(XMLSEC_ERRORS_HERE,
		        NULL,
		        "xmlSecBnAdd",
		        XMLSEC_ERRORS_R_XMLSEC_FAILED,
		        "base=%d", base);
	        return (-1);
}	
    }

    /* check if we need to add 00 prefix, do this for empty bn too */
    data = xmlSecBufferGetData(bn);
    size = xmlSecBufferGetSize(bn);
    if(((size > 0) && (data[0] > 127)) || (size == 0))  {
        ch = 0;
        ret = xmlSecBufferPrepend(bn, &ch, 1);
        if(ret < 0) {
            xmlSecError(XMLSEC_ERRORS_HERE,
                NULL,
                "xmlSecBufferPrepend",
                XMLSEC_ERRORS_R_XMLSEC_FAILED,
                "base=%d", base);
            return (-1);
        }
    }

    /* do 2's compliment and add 1 to represent negative value */
    if(positive == 0) {
        data = xmlSecBufferGetData(bn);
        size = xmlSecBufferGetSize(bn);
        for(i = 0; i < size; ++i) {
            data[i] ^= 0xFF;
        }
        
        ret = xmlSecBnAdd(bn, 1);
        if(ret < 0) {
            xmlSecError(XMLSEC_ERRORS_HERE,
                NULL,
                "xmlSecBnAdd",
                XMLSEC_ERRORS_R_XMLSEC_FAILED,
                "base=%d", base);
            return (-1);
        }
    }

    return(0);
}