prune off stuff we dont use
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@19741 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,123 +0,0 @@
|
||||
/*
|
||||
Name: imrat.h
|
||||
Purpose: Arbitrary precision rational arithmetic routines.
|
||||
Author: M. J. Fromberger <http://www.dartmouth.edu/~sting/>
|
||||
Info: $Id$
|
||||
|
||||
Copyright (C) 2002 Michael J. Fromberger, All Rights Reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef IMRAT_H_
|
||||
#define IMRAT_H_
|
||||
|
||||
#include "imath.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct mpq {
|
||||
mpz_t num; /* Numerator */
|
||||
mpz_t den; /* Denominator, <> 0 */
|
||||
} mpq_t, *mp_rat;
|
||||
|
||||
#define MP_NUMER_P(Q) (&((Q)->num)) /* Pointer to numerator */
|
||||
#define MP_DENOM_P(Q) (&((Q)->den)) /* Pointer to denominator */
|
||||
|
||||
/* Rounding constants */
|
||||
typedef enum {
|
||||
MP_ROUND_DOWN,
|
||||
MP_ROUND_HALF_UP,
|
||||
MP_ROUND_UP,
|
||||
MP_ROUND_HALF_DOWN
|
||||
} mp_round_mode;
|
||||
|
||||
mp_result mp_rat_init(mp_rat r);
|
||||
mp_rat mp_rat_alloc(void);
|
||||
mp_result mp_rat_init_size(mp_rat r, mp_size n_prec, mp_size d_prec);
|
||||
mp_result mp_rat_init_copy(mp_rat r, mp_rat old);
|
||||
mp_result mp_rat_set_value(mp_rat r, int numer, int denom);
|
||||
void mp_rat_clear(mp_rat r);
|
||||
void mp_rat_free(mp_rat r);
|
||||
mp_result mp_rat_numer(mp_rat r, mp_int z); /* z = num(r) */
|
||||
mp_result mp_rat_denom(mp_rat r, mp_int z); /* z = den(r) */
|
||||
mp_sign mp_rat_sign(mp_rat r);
|
||||
|
||||
mp_result mp_rat_copy(mp_rat a, mp_rat c); /* c = a */
|
||||
void mp_rat_zero(mp_rat r); /* r = 0 */
|
||||
mp_result mp_rat_abs(mp_rat a, mp_rat c); /* c = |a| */
|
||||
mp_result mp_rat_neg(mp_rat a, mp_rat c); /* c = -a */
|
||||
mp_result mp_rat_recip(mp_rat a, mp_rat c); /* c = 1 / a */
|
||||
mp_result mp_rat_add(mp_rat a, mp_rat b, mp_rat c); /* c = a + b */
|
||||
mp_result mp_rat_sub(mp_rat a, mp_rat b, mp_rat c); /* c = a - b */
|
||||
mp_result mp_rat_mul(mp_rat a, mp_rat b, mp_rat c); /* c = a * b */
|
||||
mp_result mp_rat_div(mp_rat a, mp_rat b, mp_rat c); /* c = a / b */
|
||||
|
||||
mp_result mp_rat_add_int(mp_rat a, mp_int b, mp_rat c); /* c = a + b */
|
||||
mp_result mp_rat_sub_int(mp_rat a, mp_int b, mp_rat c); /* c = a - b */
|
||||
mp_result mp_rat_mul_int(mp_rat a, mp_int b, mp_rat c); /* c = a * b */
|
||||
mp_result mp_rat_div_int(mp_rat a, mp_int b, mp_rat c); /* c = a / b */
|
||||
mp_result mp_rat_expt(mp_rat a, int b, mp_rat c); /* c = a ^ b */
|
||||
|
||||
int mp_rat_compare(mp_rat a, mp_rat b); /* a <=> b */
|
||||
int mp_rat_compare_unsigned(mp_rat a, mp_rat b); /* |a| <=> |b| */
|
||||
int mp_rat_compare_zero(mp_rat r); /* r <=> 0 */
|
||||
int mp_rat_compare_value(mp_rat r, int n, int d); /* r <=> n/d */
|
||||
int mp_rat_is_integer(mp_rat r);
|
||||
|
||||
/* Convert to integers, if representable (returns MP_RANGE if not). */
|
||||
mp_result mp_rat_to_ints(mp_rat r, int *num, int *den);
|
||||
|
||||
/* Convert to nul-terminated string with the specified radix, writing
|
||||
at most limit characters including the nul terminator. */
|
||||
mp_result mp_rat_to_string(mp_rat r, mp_size radix, char *str, int limit);
|
||||
|
||||
/* Convert to decimal format in the specified radix and precision,
|
||||
writing at most limit characters including a nul terminator. */
|
||||
mp_result mp_rat_to_decimal(mp_rat r, mp_size radix, mp_size prec,
|
||||
mp_round_mode round, char *str, int limit);
|
||||
|
||||
/* Return the number of characters required to represent r in the given
|
||||
radix. May over-estimate. */
|
||||
mp_result mp_rat_string_len(mp_rat r, mp_size radix);
|
||||
|
||||
/* Return the number of characters required to represent r in decimal
|
||||
format with the given radix and precision. May over-estimate. */
|
||||
mp_result mp_rat_decimal_len(mp_rat r, mp_size radix, mp_size prec);
|
||||
|
||||
/* Read zero-terminated string into r */
|
||||
mp_result mp_rat_read_string(mp_rat r, mp_size radix, const char *str);
|
||||
mp_result mp_rat_read_cstring(mp_rat r, mp_size radix, const char *str,
|
||||
char **end);
|
||||
mp_result mp_rat_read_ustring(mp_rat r, mp_size radix, const char *str,
|
||||
char **end);
|
||||
|
||||
/* Read zero-terminated string in decimal format into r */
|
||||
mp_result mp_rat_read_decimal(mp_rat r, mp_size radix, const char *str);
|
||||
mp_result mp_rat_read_cdecimal(mp_rat r, mp_size radix, const char *str,
|
||||
char **end);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* IMRAT_H_ */
|
@@ -1,205 +0,0 @@
|
||||
/*
|
||||
Name: rsamath.c
|
||||
Purpose: Implements part of PKCS#1, v. 2.1, June 14, 2002 (RSA Labs)
|
||||
Author: M. J. Fromberger <http://www.dartmouth.edu/~sting/>
|
||||
Copyright (C) 2002 Michael J. Fromberger, All Rights Reserved.
|
||||
Info: $Id$
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "rsamath.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
static mp_result s_rsa_transform(mp_int msg, mp_int exp,
|
||||
mp_int mod, mp_int out);
|
||||
|
||||
/* {{{ rsa_i2osp(z, out, len) */
|
||||
|
||||
/* Convert integer to octet string, per PKCS#1 v.2.1 */
|
||||
mp_result rsa_i2osp(mp_int z, unsigned char *out, int len)
|
||||
{
|
||||
int excess_len = mp_int_binary_len(z);
|
||||
|
||||
if(excess_len < len)
|
||||
return MP_RANGE;
|
||||
|
||||
memset(out, 0, len);
|
||||
|
||||
excess_len -= len;
|
||||
mp_int_to_binary(z, out + excess_len, len);
|
||||
|
||||
return MP_OK;
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ rsa_os2ip(z, in, len) */
|
||||
|
||||
/* Convert octet string to integer, per PKCS#1 v.2.1 */
|
||||
mp_result rsa_os2ip(mp_int z, unsigned char *in, int len)
|
||||
{
|
||||
return mp_int_read_binary(z, in, len);
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ rsa_rsaep(msg, exp, mod, cipher) */
|
||||
|
||||
/* Primitive RSA encryption operation */
|
||||
mp_result rsa_rsaep(mp_int msg, mp_int exp, mp_int mod, mp_int cipher)
|
||||
{
|
||||
return s_rsa_transform(msg, exp, mod, cipher);
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ rsa_rsadp(cipher, exp, mod, msg) */
|
||||
|
||||
/* Primitive RSA decryption operation */
|
||||
mp_result rsa_rsadp(mp_int cipher, mp_int exp, mp_int mod, mp_int msg)
|
||||
{
|
||||
return s_rsa_transform(cipher, exp, mod, msg);
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ rsa_rsasp(msg, exp, mod, signature) */
|
||||
|
||||
/* Primitive RSA signing operation */
|
||||
mp_result rsa_rsasp(mp_int msg, mp_int exp, mp_int mod, mp_int signature)
|
||||
{
|
||||
return s_rsa_transform(msg, exp, mod, signature);
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ rsa_rsavp(signature, exp, mod, msg) */
|
||||
|
||||
/* Primitive RSA verification operation */
|
||||
mp_result rsa_rsavp(mp_int signature, mp_int exp, mp_int mod, mp_int msg)
|
||||
{
|
||||
return s_rsa_transform(signature, exp, mod, msg);
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ rsa_max_message_len(mod) */
|
||||
|
||||
/* Compute the maximum length in bytes a message can have using PKCS#1
|
||||
v.1.5 encoding with the given modulus */
|
||||
int rsa_max_message_len(mp_int mod)
|
||||
{
|
||||
int num_bits = mp_int_count_bits(mod);
|
||||
int num_bytes = num_bits / CHAR_BIT;
|
||||
|
||||
if(num_bytes < 11)
|
||||
return 0; /* at least eleven bytes are required for padding */
|
||||
else
|
||||
return num_bytes - 11;
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ rsa_pkcs1v15_encode(buf, msg_len, buf_len, tag, filler) */
|
||||
|
||||
mp_result rsa_pkcs1v15_encode(unsigned char *buf, int msg_len,
|
||||
int buf_len, int tag, random_f filler)
|
||||
{
|
||||
int pad_len, msg_start;
|
||||
|
||||
/* Make sure there is enough space for the encoded output */
|
||||
if(msg_len > (buf_len - 11))
|
||||
return MP_RANGE;
|
||||
|
||||
msg_start = buf_len - msg_len;
|
||||
pad_len = msg_start - 3;
|
||||
|
||||
/* Move message to top of buffer -- these might overlap, so we rely
|
||||
on the semantics of memmove() here */
|
||||
memmove(buf + msg_start, buf, msg_len);
|
||||
|
||||
/* Set initial bytes as required by the specification */
|
||||
buf[0] = 0x00;
|
||||
buf[1] = (unsigned char)tag;
|
||||
|
||||
/* Fill with random padding. We'll just assume the filler function
|
||||
does the right thing and only writes the requested number of
|
||||
nonzero bytes */
|
||||
(filler)(buf + 2, pad_len);
|
||||
|
||||
/* Write separator between pad and message body */
|
||||
buf[msg_start - 1] = 0x00;
|
||||
|
||||
return MP_OK;
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ rsa_pkcs1v15_decode(buf, buf_len, *msg_len) */
|
||||
|
||||
mp_result rsa_pkcs1v15_decode(unsigned char *buf, int buf_len,
|
||||
int tag, int *msg_len)
|
||||
{
|
||||
int pad_len = 0, data_len, data_start, i;
|
||||
|
||||
/* Make sure the buffer is syntactically valid */
|
||||
if(buf_len < 11 || buf[0] != 0x00 || buf[1] != (unsigned char)tag)
|
||||
return MP_UNDEF;
|
||||
|
||||
/* Figure out how many bytes of random padding there are */
|
||||
i = 2;
|
||||
while(buf[i++] != '\0')
|
||||
++pad_len;
|
||||
|
||||
data_start = i;
|
||||
data_len = buf_len - data_start;
|
||||
|
||||
/* Shift the message to the front of the buffer */
|
||||
memmove(buf, buf + data_start, data_len);
|
||||
|
||||
/* Zero out the rest of the buffer */
|
||||
memset(buf + data_len, 0, pad_len + 3);
|
||||
|
||||
*msg_len = data_len;
|
||||
|
||||
return MP_OK;
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ s_rsa_transform(msg, exp, mod, out) */
|
||||
|
||||
static mp_result s_rsa_transform(mp_int msg, mp_int exp,
|
||||
mp_int mod, mp_int out)
|
||||
{
|
||||
if(mp_int_compare_zero(msg) < 0 ||
|
||||
mp_int_compare(msg, mod) >= 0)
|
||||
return MP_RANGE;
|
||||
|
||||
return mp_int_exptmod(msg, exp, mod, out);
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* Here there be dragons */
|
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
Name: rsamath.h
|
||||
Purpose: Implements part of PKCS#1, v. 2.1, June 14, 2002 (RSA Labs)
|
||||
Author: M. J. Fromberger <http://www.dartmouth.edu/~sting/>
|
||||
Info: $Id$
|
||||
|
||||
Copyright (C) 2002 Michael J. Fromberger, All Rights Reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef RSAMATH_H_
|
||||
#define RSAMATH_H_
|
||||
|
||||
#include "imath.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Function to fill a buffer with nonzero random bytes */
|
||||
typedef void (*random_f)(unsigned char *, int);
|
||||
|
||||
/* Convert integer to octet string, per PKCS#1 v.2.1 */
|
||||
mp_result rsa_i2osp(mp_int z, unsigned char *out, int len);
|
||||
|
||||
/* Convert octet string to integer, per PKCS#1 v.2.1 */
|
||||
mp_result rsa_os2ip(mp_int z, unsigned char *in, int len);
|
||||
|
||||
/* The following operations assume that you have converted your keys
|
||||
and message data into mp_int values somehow. */
|
||||
|
||||
/* Primitive RSA encryption operation */
|
||||
mp_result rsa_rsaep(mp_int msg, mp_int exp, mp_int mod, mp_int cipher);
|
||||
|
||||
/* Primitive RSA decryption operation */
|
||||
mp_result rsa_rsadp(mp_int cipher, mp_int exp, mp_int mod, mp_int msg);
|
||||
|
||||
/* Primitive RSA signing operation */
|
||||
mp_result rsa_rsasp(mp_int msg, mp_int exp, mp_int mod, mp_int signature);
|
||||
|
||||
/* Primitive RSA verification operation */
|
||||
mp_result rsa_rsavp(mp_int signature, mp_int exp, mp_int mod, mp_int msg);
|
||||
|
||||
/* Compute the maximum length in bytes a message can have using PKCS#1
|
||||
v.1.5 encoding with the given modulus */
|
||||
int rsa_max_message_len(mp_int mod);
|
||||
|
||||
/* Encode a raw message per PKCS#1 v.1.5
|
||||
buf - the buffer containing the message
|
||||
msg_len - the length in bytes of the message
|
||||
buf_len - the size in bytes of the buffer
|
||||
tag - the message tag (nonzero byte)
|
||||
filler - function to generate pseudorandom nonzero padding
|
||||
|
||||
On input, the message is in the first msg_len bytes of the buffer;
|
||||
on output, the contents of the buffer are replaced by the padded
|
||||
message. If there is not enough room, MP_RANGE is returned.
|
||||
*/
|
||||
mp_result rsa_pkcs1v15_encode(unsigned char *buf, int msg_len,
|
||||
int buf_len, int tag, random_f filler);
|
||||
|
||||
/* Decode a PKCS#1 v.1.5 message back to its raw form
|
||||
buf - the buffer containing the encoded message
|
||||
buf_len - the length in bytes of the buffer
|
||||
tag - the expected message tag (nonzero byte)
|
||||
msg_len - on output, receives the length of the message content
|
||||
|
||||
On output, the message is packed into the first msg_len bytes of
|
||||
the buffer, and the rest of the buffer is zeroed. If the buffer is
|
||||
not of the correct form, MP_UNDEF is returned and msg_len is undefined.
|
||||
*/
|
||||
mp_result rsa_pkcs1v15_decode(unsigned char *buf, int buf_len,
|
||||
int tag, int *msg_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* end RSAMATH_H_ */
|
Reference in New Issue
Block a user