⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bn16.c

📁 著名的加密软件的应用于电子邮件中
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * bn16.c - the high-level bignum interface
 *
 * Copyright (C) 1995-1997 Pretty Good Privacy, Inc. All rights reserved.
 *
 * Like bni16.c, this reserves the string "16" for textual replacement.
 * The string must not appear anywhere unless it is intended to be replaced
 * to generate other bignum interface functions.
 *
 * Written by Colin Plumb
 *
 * $Id: bn16.c,v 1.37.2.1 1997/06/07 09:49:26 mhw Exp $
 */

#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H 0
#endif
#if HAVE_CONFIG_H
#include "config.h"
#endif

/*
 * Some compilers complain about #if FOO if FOO isn't defined,
 * so do the ANSI-mandated thing explicitly...
 */
#ifndef NO_STRING_H
#define NO_STRING_H 0
#endif
#ifndef HAVE_STRINGS_H
#define HAVE_STRINGS_H 0
#endif
#ifndef NEED_MEMORY_H
#define NEED_MEMORY_H 0
#endif

#if !NO_STRING_H
#include <string.h>	 /* for memmove() in bnMakeOdd */
#elif HAVE_STRINGS_H
#include <strings.h>
#endif
#if NEED_MEMORY_H
#include <memory.h>
#endif

/*
 * This was useful during debugging, so it's left in here.
 * You can ignore it. DBMALLOC is generally undefined.
 */
#ifndef DBMALLOC
#define DBAMLLOC 0
#endif
#if DBMALLOC
#include "../dbmalloc/malloc.h"
#define MALLOCDB malloc_chain_check(1)
#else
#define MALLOCDB (void)0
#endif

#include "bni.h"
#include "bni16.h"
#include "bnimem.h"
#include "bn16.h"
#include "bn.h"

/* Work-arounds for some particularly broken systems */
#include "bnkludge.h"	/* For memmove() */

#include "pgpDebug.h"

/* Functions */
void
bnInit_16(void)
{
			bnEnd = bnEnd_16;
			bnPrealloc = bnPrealloc_16;
			bnCopy = bnCopy_16;
			bnNorm = bnNorm_16;
			bnExtractBigBytes = bnExtractBigBytes_16;
			bnInsertBigBytes = bnInsertBigBytes_16;
			bnExtractLittleBytes = bnExtractLittleBytes_16;
			bnInsertLittleBytes = bnInsertLittleBytes_16;
			bnLSWord = bnLSWord_16;
			bnBits = bnBits_16;
			bnAdd = bnAdd_16;
			bnSub = bnSub_16;
			bnCmpQ = bnCmpQ_16;
			bnSetQ = bnSetQ_16;
			bnAddQ = bnAddQ_16;
			bnSubQ = bnSubQ_16;
			bnCmp = bnCmp_16;
			bnSquare = bnSquare_16;
			bnMul = bnMul_16;
			bnMulQ = bnMulQ_16;
			bnDivMod = bnDivMod_16;
			bnMod = bnMod_16;
			bnModQ = bnModQ_16;
			bnExpMod = bnExpMod_16;
			bnDoubleExpMod = bnDoubleExpMod_16;
			bnTwoExpMod = bnTwoExpMod_16;
			bnGcd = bnGcd_16;
			bnInv = bnInv_16;
			bnLShift = bnLShift_16;
			bnRShift = bnRShift_16;
			bnMakeOdd = bnMakeOdd_16;
	}

void
bnEnd_16(struct BigNum *bn)
{
			if (bn->ptr) {
				 BNIFREE((BNWORD16 *)bn->ptr, bn->allocated);
				 bn->ptr = 0;
			}
			bn->size = 0;
			bn->allocated = 0;

	MALLOCDB;
}

/* Internal function. It operates in words. */
static int
bnResize_16(struct BigNum *bn, unsigned len)
{
			void *p;

			/* Round size up: most mallocs impose 8-byte granularity anyway */
			len = (len + (8/sizeof(BNWORD16) - 1)) & ~(8/sizeof(BNWORD16) - 1);
			p = BNIREALLOC((BNWORD16 *)bn->ptr, bn->allocated, len);
			if (!p)
				 return -1;
			bn->ptr = p;
			bn->allocated = len;

			MALLOCDB;

			return 0;
	}

#define bnSizeCheck(bn, size) \
	if (bn->allocated < size && bnResize_16(bn, size) < 0) \
		return -1

int
bnPrealloc_16(struct BigNum *bn, unsigned bits)
	{
			bits = (bits + 16-1)/16;
			bnSizeCheck(bn, bits);
			MALLOCDB;
			return 0;
	}

	int
bnCopy_16(struct BigNum *dest, struct BigNum const *src)
	{
			bnSizeCheck(dest, src->size);
			dest->size = src->size;
			bniCopy_16((BNWORD16 *)dest->ptr, (BNWORD16 *)src->ptr, src->size);
			MALLOCDB;
			return 0;
	}

void
bnNorm_16(struct BigNum *bn)
{
	bn->size = bniNorm_16((BNWORD16 *)bn->ptr, bn->size);
}

/*
 * Convert a bignum to big-endian bytes. Returns, in big-endian form, a
 * substring of the bignum starting from lsbyte and "len" bytes long.
 * Unused high-order (leading) bytes are filled with 0.
 */
void
bnExtractBigBytes_16(struct BigNum const *bn, unsigned char *dest,
		     unsigned lsbyte, unsigned len)
	{
			unsigned s = bn->size * (16 / 8);

			/* Fill unused leading bytes with 0 */
			while (s < lsbyte+len && len) {
				 *dest++ = 0;
				 len--;
			}

			if (len)
				 bniExtractBigBytes_16((BNWORD16 *)bn->ptr, dest, lsbyte, len);
			MALLOCDB;
	}

	int
bnInsertBigBytes_16(struct BigNum *bn, unsigned char const *src,
		unsigned lsbyte, unsigned len)
	{
			unsigned s = bn->size;
			unsigned words = (len+lsbyte+sizeof(BNWORD16)-1) / sizeof(BNWORD16);

			/* Pad with zeros as required */
			bnSizeCheck(bn, words);

			if (s < words) {
				 bniZero_16((BNWORD16 *)bn->ptr BIGLITTLE(-s,+s), words-s);
				 s = words;
	}

			bniInsertBigBytes_16((BNWORD16 *)bn->ptr, src, lsbyte, len);

			bn->size = bniNorm_16((BNWORD16 *)bn->ptr, s);

			MALLOCDB;
			return 0;
	}


/*
 * Convert a bignum to little-endian bytes. Returns, in little-endian form, a
 * substring of the bignum starting from lsbyte and "len" bytes long.
 * Unused high-order (trailing) bytes are filled with 0.
 */
void
bnExtractLittleBytes_16(struct BigNum const *bn, unsigned char *dest,
		unsigned lsbyte, unsigned len)
	{
			unsigned s = bn->size * (16 / 8);

			/* Fill unused leading bytes with 0 */
			while (s < lsbyte+len && len)
				 dest[--len] = 0;

			if (len)
				 bniExtractLittleBytes_16((BNWORD16 *)bn->ptr, dest,
					lsbyte, len);
			MALLOCDB;
}

int
bnInsertLittleBytes_16(struct BigNum *bn, unsigned char const *src,
		unsigned lsbyte, unsigned len)
	{
			unsigned s = bn->size;
			unsigned words = (len+lsbyte+sizeof(BNWORD16)-1) / sizeof(BNWORD16);

			/* Pad with zeros as required */
			bnSizeCheck(bn, words);

if (s < words) {
	bniZero_16((BNWORD16 *)bn->ptr BIGLITTLE(-s,+s), words-s);
	s = words;
}

bniInsertLittleBytes_16((BNWORD16 *)bn->ptr, src, lsbyte, len);

bn->size = bniNorm_16((BNWORD16 *)bn->ptr, s);

MALLOCDB;
return 0;
}

/* Return the least-significant word of the input. */
unsigned
bnLSWord_16(struct BigNum const *src)
{
	return src->size ? (unsigned)((BNWORD16 *)src->ptr)[BIGLITTLE(-1,0)]
	: 0;
}

unsigned
bnBits_16(struct BigNum const *src)
{
 return bniBits_16((BNWORD16 *)src->ptr, src->size);
}

int
bnAdd_16(struct BigNum *dest, struct BigNum const *src)
	{
			unsigned s = src->size, d = dest->size;
			BNWORD16 t;

if (!s)
	return 0;

bnSizeCheck(dest, s);

if (d < s) {
	bniZero_16((BNWORD16 *)dest->ptr BIGLITTLE(-d,+d), s-d);
	dest->size = d = s;
	MALLOCDB;
}
t = bniAddN_16((BNWORD16 *)dest->ptr, (BNWORD16 *)src->ptr, s);
MALLOCDB;
if (t) {
					if (d > s) {
						t = bniAdd1_16((BNWORD16 *)dest->ptr BIGLITTLE(-s,+s),
							d-s, t);
						MALLOCDB;
					}
					if (t) {
							bnSizeCheck(dest, d+1);
							((BNWORD16 *)dest->ptr)[BIGLITTLE(-1-d,d)] = t;
							dest->size = d+1;
					}
			}
			return 0;
	}

/*
 * dest -= src.
 * If dest goes negative, this produces the absolute value of
 * the difference (the negative of the true value) and returns 1.
 * Otherwise, it returls 0.
 */
int
bnSub_16(struct BigNum *dest, struct BigNum const *src)
	{
			unsigned s = src->size, d = dest->size;
			BNWORD16 t;

			if (d < s && d < (s = bniNorm_16((BNWORD16 *)src->ptr, s))) {
					bnSizeCheck(dest, s);
					bniZero_16((BNWORD16 *)dest->ptr BIGLITTLE(-d,+d), s-d);
					dest->size = d = s;
					MALLOCDB;
			}
			if (!s)
					return 0;
			t = bniSubN_16((BNWORD16 *)dest->ptr, (BNWORD16 *)src->ptr, s);
			MALLOCDB;
			if (t) {
					if (d > s) {
						t = bniSub1_16((BNWORD16 *)dest->ptr BIGLITTLE(-s,+s),
							d-s, t);
						MALLOCDB;
					}
					if (t) {
							bniNeg_16((BNWORD16 *)dest->ptr, d);
							dest->size = bniNorm_16((BNWORD16 *)dest->ptr,
							dest->size);
							MALLOCDB;
							return 1;
					}
			}
			dest->size = bniNorm_16((BNWORD16 *)dest->ptr, dest->size);
			return 0;
	}

/*
 * Compare the BigNum to the given value, which must be < 65536.
 * Returns -1. 0 or 1 if a<b, a == b or a>b.
 * a <=> b --> bnCmpQ(a,b) <=> 0
*/
int
bnCmpQ_16(struct BigNum const *a, unsigned b)
	{
			unsigned t;
			BNWORD16 v;

			t = bniNorm_16((BNWORD16 *)a->ptr, a->size);
			/* If a is more than one word long or zero, it's easy... */
			if (t != 1)
				 return (t > 1) ? 1 : (b ? -1 : 0);
			v = (unsigned)((BNWORD16 *)a->ptr)[BIGLITTLE(-1,0)];
			return (v > b) ? 1 : ((v < b) ? -1 : 0);
}

int
bnSetQ_16(struct BigNum *dest, unsigned src)
	{
			if (src) {
				 bnSizeCheck(dest, 1);

((BNWORD16 *)dest->ptr)[BIGLITTLE(-1,0)] = (BNWORD16)src;
dest->size = 1;
} else {
dest->size = 0;
}
return 0;
}

int
bnAddQ_16(struct BigNum *dest, unsigned src)
	{
			BNWORD16 t;

			if (!dest->size)
				 return bnSetQ(dest, src);
		
			t = bniAdd1_16((BNWORD16 *)dest->ptr, dest->size, (BNWORD16)src);
			MALLOCDB;
			if (t) {
					src = dest->size;
					bnSizeCheck(dest, src+1);
					((BNWORD16 *)dest->ptr)[BIGLITTLE(-1-src,src)] = t;
					dest->size = src+1;
			}
			return 0;
	}

/*
 * Return value as for bnSub: 1 if subtract underflowed, in which
 * case the return is the negative of the computed value.
*/
int
bnSubQ_16(struct BigNum *dest, unsigned src)
{
	BNWORD16 t;

	if (!dest->size)
				 return bnSetQ(dest, src) < 0 ? -1 : (src != 0);

			t = bniSub1_16((BNWORD16 *)dest->ptr, dest->size, src);
			MALLOCDB;
			if (t) {
					/* Underflow. <= 1 word, so do it simply. */
					bniNeg_16((BNWORD16 *)dest->ptr, 1);
					dest->size = 1;
					return 1;
			}
/* Try to normalize? Needing this is going to be pretty damn rare. */
/*		dest->size = bniNorm_16((BNWORD16 *)dest->ptr, dest->size); */
	return 0;
}

/*
 * Compare two BigNums. Returns -1. 0 or 1 if a<b, a == b or a>b.
 * a <=> b --> bnCmp(a,b) <=> 0
 */
int
bnCmp_16(struct BigNum const *a, struct BigNum const *b)
	{
			unsigned s, t;

			s = bniNorm_16((BNWORD16 *)a->ptr, a->size);
			t = bniNorm_16((BNWORD16 *)b->ptr, b->size);
		
			if (s != t)
				 return s > t ? 1 : -1;
			return bniCmp_16((BNWORD16 *)a->ptr, (BNWORD16 *)b->ptr, s);
	}

	int
bnSquare_16(struct BigNum *dest, struct BigNum const *src)
	{
			unsigned s;
			BNWORD16 *srcbuf;

			s = bniNorm_16((BNWORD16 *)src->ptr, src->size);
			if (!s) {
				 dest->size = 0;
				 return 0;
			}
			bnSizeCheck(dest, 2*s);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -