head	1.0;
access;
symbols;
locks
	ntcommon:1.0; strict;
comment	@ * @;


1.0
date	95.12.17.19.47.28;	author ntcommon;	state Exp;
branches;
next	;


desc
@This is the first working netrek version, plasmas and other special weapons do not work.
@


1.0
log
@Initial revision
@
text
@#ifndef lint
#include <mp.h>
#include "defs.h"

#ifndef u_char
#define u_char 	unsigned char
#endif

/****************************************************************************
 * Note: libraries are searched in order. This file uses the pow() function
 * of the libmp library, not the usual one. The rest of netrek does not use
 * the usual pow() function. Therefore you must specifiy the -lmp library 
 * before the -lm math libary on the compile line (i.e. in the Makefile as
 * the LIBS definition). 
 */

/****************************************************************************
 * these function convert between the unsigned char form (as used by the
 * netrek RSA scheme) and the MP form.
 */
raw_to_num(out, in, digits)
MINT *out;
u_char *in;
int digits;
{
    int i;
    MINT *temp, *twofiftysix, *thisval;
 
    temp = itom(0);
    twofiftysix = itom(256);
 
    for (i = 0; i < digits; i++)
    {
        thisval = itom(in[digits - i - 1]);
        mult(temp, twofiftysix, temp);
        madd(temp, thisval, temp);
        mfree(thisval);
    }
    madd(temp, out, out); /* There is no copy function! */
    mfree(temp);
    mfree(twofiftysix);
}
 
num_to_raw(out, in, digits)  /* Destroys in */
u_char *out;
MINT *in;
int digits;
{
    int i;
    short temp;
    MINT *zero;

    zero = itom(0);
 
    for (i = 0; i < digits; i++)
    {
        if (!mcmp(in, zero))  /* gnu can't handle 0/256.. bug */
          temp = 0;
        else
          sdiv(in, 256, in, &temp);
        out[i] = temp & 0xFF;
    }
    mfree (zero);
}

/****************************************************************************
 * The final product.  Take in a message, a key, and the global N to mod by,
 * and do the actual RSA calculation.  Return the new stuff.
 */
 
rsa_encode(out, message, key, global, digits)
char *out, *message, *key, *global;
int digits;
{
    MINT *result, *n_message, *n_key, *n_global;
    result = itom(0);
    n_message = itom(0);
    n_key = itom(0);
    n_global = itom(0);
 
 
    raw_to_num(n_global, global, digits);
    raw_to_num(n_message, message, digits);
    raw_to_num(n_key, key, digits);

    pow(n_message, n_key, n_global, result);
 
    num_to_raw(out, result, digits);
}
#endif /* lint */
@
