Commit a7826587 authored by Niels Möller's avatar Niels Möller

Use size_t for bignums, rsa and dsa.

parent 64bb6b36
......@@ -46,7 +46,7 @@
*/
/* Including extra sign bit, if needed. Also one byte for zero. */
unsigned
size_t
nettle_mpz_sizeinbase_256_s(const mpz_t x)
{
if (mpz_sgn(x) >= 0)
......@@ -54,7 +54,7 @@ nettle_mpz_sizeinbase_256_s(const mpz_t x)
else
{
/* We'll output ~~x, so we need as many bits as for ~x */
unsigned size;
size_t size;
mpz_t c;
mpz_init(c);
......@@ -66,24 +66,24 @@ nettle_mpz_sizeinbase_256_s(const mpz_t x)
}
}
unsigned
size_t
nettle_mpz_sizeinbase_256_u(const mpz_t x)
{
return (mpz_sizeinbase(x,2) + 7) / 8;
}
static void
nettle_mpz_to_octets(unsigned length, uint8_t *s,
nettle_mpz_to_octets(size_t length, uint8_t *s,
const mpz_t x, uint8_t sign)
{
uint8_t *dst = s + length - 1;
unsigned size = mpz_size(x);
unsigned i;
size_t size = mpz_size(x);
size_t i;
for (i = 0; i<size; i++)
{
mp_limb_t limb = mpz_getlimbn(x, i);
unsigned j;
size_t j;
for (j = 0; length && j < sizeof(mp_limb_t); j++)
{
......@@ -98,7 +98,7 @@ nettle_mpz_to_octets(unsigned length, uint8_t *s,
}
void
nettle_mpz_get_str_256(unsigned length, uint8_t *s, const mpz_t x)
nettle_mpz_get_str_256(size_t length, uint8_t *s, const mpz_t x)
{
if (!length)
{
......@@ -134,9 +134,9 @@ nettle_mpz_get_str_256(unsigned length, uint8_t *s, const mpz_t x)
#else
static void
nettle_mpz_from_octets(mpz_t x,
unsigned length, const uint8_t *s)
size_t length, const uint8_t *s)
{
unsigned i;
size_t i;
mpz_set_ui(x, 0);
......@@ -150,14 +150,14 @@ nettle_mpz_from_octets(mpz_t x,
void
nettle_mpz_set_str_256_u(mpz_t x,
unsigned length, const uint8_t *s)
size_t length, const uint8_t *s)
{
nettle_mpz_from_octets(x, length, s);
}
void
nettle_mpz_init_set_str_256_u(mpz_t x,
unsigned length, const uint8_t *s)
size_t length, const uint8_t *s)
{
mpz_init(x);
nettle_mpz_from_octets(x, length, s);
......@@ -165,7 +165,7 @@ nettle_mpz_init_set_str_256_u(mpz_t x,
void
nettle_mpz_set_str_256_s(mpz_t x,
unsigned length, const uint8_t *s)
size_t length, const uint8_t *s)
{
if (!length)
{
......@@ -188,7 +188,7 @@ nettle_mpz_set_str_256_s(mpz_t x,
void
nettle_mpz_init_set_str_256_s(mpz_t x,
unsigned length, const uint8_t *s)
size_t length, const uint8_t *s)
{
mpz_init(x);
nettle_mpz_set_str_256_s(x, length, s);
......
......@@ -37,36 +37,36 @@ extern "C" {
/* Size needed for signed encoding, including extra sign byte if
* necessary. */
unsigned
size_t
nettle_mpz_sizeinbase_256_s(const mpz_t x);
/* Size needed for unsigned encoding */
unsigned
size_t
nettle_mpz_sizeinbase_256_u(const mpz_t x);
/* Writes an integer as length octets, using big endian byte order,
* and two's complement for negative numbers. */
void
nettle_mpz_get_str_256(unsigned length, uint8_t *s, const mpz_t x);
nettle_mpz_get_str_256(size_t length, uint8_t *s, const mpz_t x);
/* Reads a big endian, two's complement, integer. */
void
nettle_mpz_set_str_256_s(mpz_t x,
unsigned length, const uint8_t *s);
size_t length, const uint8_t *s);
void
nettle_mpz_init_set_str_256_s(mpz_t x,
unsigned length, const uint8_t *s);
size_t length, const uint8_t *s);
/* Similar, but for unsigned format. These function don't interpret
* the most significant bit as the sign. */
void
nettle_mpz_set_str_256_u(mpz_t x,
unsigned length, const uint8_t *s);
size_t length, const uint8_t *s);
void
nettle_mpz_init_set_str_256_u(mpz_t x,
unsigned length, const uint8_t *s);
size_t length, const uint8_t *s);
/* Returns a uniformly distributed random number 0 <= x < 2^n */
void
......
......@@ -39,7 +39,7 @@ int
_dsa_sign(const struct dsa_public_key *pub,
const struct dsa_private_key *key,
void *random_ctx, nettle_random_func *random,
unsigned digest_size,
size_t digest_size,
const uint8_t *digest,
struct dsa_signature *signature)
{
......
......@@ -35,7 +35,7 @@
int
_dsa_verify(const struct dsa_public_key *key,
unsigned digest_size,
size_t digest_size,
const uint8_t *digest,
const struct dsa_signature *signature)
{
......
......@@ -238,13 +238,13 @@ int
dsa_sha1_keypair_from_sexp(struct dsa_public_key *pub,
struct dsa_private_key *priv,
unsigned p_max_bits,
unsigned length, const uint8_t *expr);
size_t length, const uint8_t *expr);
int
dsa_sha256_keypair_from_sexp(struct dsa_public_key *pub,
struct dsa_private_key *priv,
unsigned p_max_bits,
unsigned length, const uint8_t *expr);
size_t length, const uint8_t *expr);
/* Keys in X.509 andd OpenSSL format. */
struct asn1_der_iterator;
......@@ -276,13 +276,13 @@ int
_dsa_sign(const struct dsa_public_key *pub,
const struct dsa_private_key *key,
void *random_ctx, nettle_random_func *random,
unsigned digest_size,
size_t digest_size,
const uint8_t *digest,
struct dsa_signature *signature);
int
_dsa_verify(const struct dsa_public_key *key,
unsigned digest_size,
size_t digest_size,
const uint8_t *digest,
const struct dsa_signature *signature);
......
......@@ -198,7 +198,7 @@ main(int argc, char **argv)
struct rsa_session ctx;
struct rsa_session_info session;
unsigned length;
size_t length;
mpz_t x;
mpz_init(x);
......
......@@ -34,14 +34,14 @@
#include "nettle-internal.h"
int
pkcs1_decrypt (unsigned key_size,
pkcs1_decrypt (size_t key_size,
const mpz_t m,
unsigned *length, uint8_t *message)
size_t *length, uint8_t *message)
{
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
uint8_t *terminator;
unsigned padding;
unsigned message_length;
size_t padding;
size_t message_length;
TMP_ALLOC(em, key_size);
nettle_mpz_get_str_256(key_size, em, m);
......
......@@ -37,15 +37,15 @@
#include "nettle-internal.h"
int
pkcs1_encrypt (unsigned key_size,
pkcs1_encrypt (size_t key_size,
/* For padding */
void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *message,
size_t length, const uint8_t *message,
mpz_t m)
{
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
unsigned padding;
unsigned i;
size_t padding;
size_t i;
/* The message is encoded as a string of the same length as the
* modulo n, of the form
......
......@@ -32,8 +32,8 @@
#include "nettle-internal.h"
int
pkcs1_rsa_digest_encode(mpz_t m, unsigned key_size,
unsigned di_length, const uint8_t *digest_info)
pkcs1_rsa_digest_encode(mpz_t m, size_t key_size,
size_t di_length, const uint8_t *digest_info)
{
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
TMP_ALLOC(em, key_size);
......
......@@ -62,7 +62,7 @@ md5_prefix[] =
};
int
pkcs1_rsa_md5_encode(mpz_t m, unsigned key_size, struct md5_ctx *hash)
pkcs1_rsa_md5_encode(mpz_t m, size_t key_size, struct md5_ctx *hash)
{
uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......@@ -83,7 +83,7 @@ pkcs1_rsa_md5_encode(mpz_t m, unsigned key_size, struct md5_ctx *hash)
}
int
pkcs1_rsa_md5_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
pkcs1_rsa_md5_encode_digest(mpz_t m, size_t key_size, const uint8_t *digest)
{
uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......
......@@ -62,7 +62,7 @@ sha1_prefix[] =
};
int
pkcs1_rsa_sha1_encode(mpz_t m, unsigned key_size, struct sha1_ctx *hash)
pkcs1_rsa_sha1_encode(mpz_t m, size_t key_size, struct sha1_ctx *hash)
{
uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......@@ -83,7 +83,7 @@ pkcs1_rsa_sha1_encode(mpz_t m, unsigned key_size, struct sha1_ctx *hash)
}
int
pkcs1_rsa_sha1_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
pkcs1_rsa_sha1_encode_digest(mpz_t m, size_t key_size, const uint8_t *digest)
{
uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......
......@@ -60,7 +60,7 @@ sha256_prefix[] =
};
int
pkcs1_rsa_sha256_encode(mpz_t m, unsigned key_size, struct sha256_ctx *hash)
pkcs1_rsa_sha256_encode(mpz_t m, size_t key_size, struct sha256_ctx *hash)
{
uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......@@ -81,7 +81,7 @@ pkcs1_rsa_sha256_encode(mpz_t m, unsigned key_size, struct sha256_ctx *hash)
}
int
pkcs1_rsa_sha256_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
pkcs1_rsa_sha256_encode_digest(mpz_t m, size_t key_size, const uint8_t *digest)
{
uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......
......@@ -60,7 +60,7 @@ sha512_prefix[] =
};
int
pkcs1_rsa_sha512_encode(mpz_t m, unsigned key_size, struct sha512_ctx *hash)
pkcs1_rsa_sha512_encode(mpz_t m, size_t key_size, struct sha512_ctx *hash)
{
uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......@@ -81,7 +81,7 @@ pkcs1_rsa_sha512_encode(mpz_t m, unsigned key_size, struct sha512_ctx *hash)
}
int
pkcs1_rsa_sha512_encode_digest(mpz_t m, unsigned key_size, const uint8_t *digest)
pkcs1_rsa_sha512_encode_digest(mpz_t m, size_t key_size, const uint8_t *digest)
{
uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......
......@@ -60,44 +60,44 @@ _pkcs1_signature_prefix(unsigned key_size,
unsigned digest_size);
int
pkcs1_encrypt (unsigned key_size,
pkcs1_encrypt (size_t key_size,
/* For padding */
void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *message,
size_t length, const uint8_t *message,
mpz_t m);
int
pkcs1_decrypt (unsigned key_size,
pkcs1_decrypt (size_t key_size,
const mpz_t m,
unsigned *length, uint8_t *message);
size_t *length, uint8_t *message);
int
pkcs1_rsa_digest_encode(mpz_t m, unsigned key_size,
unsigned di_length, const uint8_t *digest_info);
pkcs1_rsa_digest_encode(mpz_t m, size_t key_size,
size_t di_length, const uint8_t *digest_info);
int
pkcs1_rsa_md5_encode(mpz_t m, unsigned length, struct md5_ctx *hash);
pkcs1_rsa_md5_encode(mpz_t m, size_t length, struct md5_ctx *hash);
int
pkcs1_rsa_md5_encode_digest(mpz_t m, unsigned length, const uint8_t *digest);
pkcs1_rsa_md5_encode_digest(mpz_t m, size_t length, const uint8_t *digest);
int
pkcs1_rsa_sha1_encode(mpz_t m, unsigned length, struct sha1_ctx *hash);
pkcs1_rsa_sha1_encode(mpz_t m, size_t length, struct sha1_ctx *hash);
int
pkcs1_rsa_sha1_encode_digest(mpz_t m, unsigned length, const uint8_t *digest);
pkcs1_rsa_sha1_encode_digest(mpz_t m, size_t length, const uint8_t *digest);
int
pkcs1_rsa_sha256_encode(mpz_t m, unsigned length, struct sha256_ctx *hash);
pkcs1_rsa_sha256_encode(mpz_t m, size_t length, struct sha256_ctx *hash);
int
pkcs1_rsa_sha256_encode_digest(mpz_t m, unsigned length, const uint8_t *digest);
pkcs1_rsa_sha256_encode_digest(mpz_t m, size_t length, const uint8_t *digest);
int
pkcs1_rsa_sha512_encode(mpz_t m, unsigned length, struct sha512_ctx *hash);
pkcs1_rsa_sha512_encode(mpz_t m, size_t length, struct sha512_ctx *hash);
int
pkcs1_rsa_sha512_encode_digest(mpz_t m, unsigned length, const uint8_t *digest);
pkcs1_rsa_sha512_encode_digest(mpz_t m, size_t length, const uint8_t *digest);
#ifdef __cplusplus
}
......
......@@ -37,7 +37,7 @@ int
rsa_decrypt_tr(const struct rsa_public_key *pub,
const struct rsa_private_key *key,
void *random_ctx, nettle_random_func *random,
unsigned *length, uint8_t *message,
size_t *length, uint8_t *message,
const mpz_t gibberish)
{
mpz_t m, ri;
......
......@@ -33,7 +33,7 @@
int
rsa_decrypt(const struct rsa_private_key *key,
unsigned *length, uint8_t *message,
size_t *length, uint8_t *message,
const mpz_t gibberish)
{
mpz_t m;
......
......@@ -35,7 +35,7 @@ int
rsa_encrypt(const struct rsa_public_key *key,
/* For padding */
void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *message,
size_t length, const uint8_t *message,
mpz_t gibberish)
{
if (pkcs1_encrypt (key->size, random_ctx, random,
......
......@@ -35,7 +35,7 @@ int
rsa_pkcs1_sign_tr(const struct rsa_public_key *pub,
const struct rsa_private_key *key,
void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *digest_info,
size_t length, const uint8_t *digest_info,
mpz_t s)
{
mpz_t ri;
......
......@@ -33,7 +33,7 @@
int
rsa_pkcs1_sign(const struct rsa_private_key *key,
unsigned length, const uint8_t *digest_info,
size_t length, const uint8_t *digest_info,
mpz_t s)
{
if (pkcs1_rsa_digest_encode (s, key->size, length, digest_info))
......
......@@ -33,7 +33,7 @@
int
rsa_pkcs1_verify(const struct rsa_public_key *key,
unsigned length, const uint8_t *digest_info,
size_t length, const uint8_t *digest_info,
const mpz_t s)
{
int res;
......
......@@ -52,11 +52,11 @@ rsa_public_key_clear(struct rsa_public_key *key)
/* Computes the size, in octets, of a the modulo. Returns 0 if the
* modulo is too small to be useful. */
unsigned
size_t
_rsa_check_size(mpz_t n)
{
/* Round upwards */
unsigned size = (mpz_sizeinbase(n, 2) + 7) / 8;
size_t size = (mpz_sizeinbase(n, 2) + 7) / 8;
if (size < RSA_MINIMUM_N_OCTETS)
return 0;
......
......@@ -95,7 +95,7 @@ struct rsa_public_key
{
/* Size of the modulo, in octets. This is also the size of all
* signatures that are created or verified with this key. */
unsigned size;
size_t size;
/* Modulo */
mpz_t n;
......@@ -106,7 +106,7 @@ struct rsa_public_key
struct rsa_private_key
{
unsigned size;
size_t size;
/* d is filled in by the key generation function; otherwise it's
* completely unused. */
......@@ -174,18 +174,18 @@ rsa_private_key_prepare(struct rsa_private_key *key);
/* PKCS#1 style signatures */
int
rsa_pkcs1_sign(const struct rsa_private_key *key,
unsigned length, const uint8_t *digest_info,
size_t length, const uint8_t *digest_info,
mpz_t s);
int
rsa_pkcs1_sign_tr(const struct rsa_public_key *pub,
const struct rsa_private_key *key,
void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *digest_info,
size_t length, const uint8_t *digest_info,
mpz_t s);
int
rsa_pkcs1_verify(const struct rsa_public_key *key,
unsigned length, const uint8_t *digest_info,
size_t length, const uint8_t *digest_info,
const mpz_t signature);
int
......@@ -281,7 +281,7 @@ int
rsa_encrypt(const struct rsa_public_key *key,
/* For padding */
void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *cleartext,
size_t length, const uint8_t *cleartext,
mpz_t cipher);
/* Message must point to a buffer of size *LENGTH. KEY->size is enough
......@@ -291,7 +291,7 @@ rsa_encrypt(const struct rsa_public_key *key,
* didn't fit. */
int
rsa_decrypt(const struct rsa_private_key *key,
unsigned *length, uint8_t *cleartext,
size_t *length, uint8_t *cleartext,
const mpz_t ciphertext);
/* Timing-resistant version, using randomized RSA blinding. */
......@@ -299,7 +299,7 @@ int
rsa_decrypt_tr(const struct rsa_public_key *pub,
const struct rsa_private_key *key,
void *random_ctx, nettle_random_func *random,
unsigned *length, uint8_t *message,
size_t *length, uint8_t *message,
const mpz_t gibberish);
/* Compute x, the e:th root of m. Calling it with x == m is allowed. */
......@@ -364,7 +364,7 @@ int
rsa_keypair_from_sexp(struct rsa_public_key *pub,
struct rsa_private_key *priv,
unsigned limit,
unsigned length, const uint8_t *expr);
size_t length, const uint8_t *expr);
/* Keys in PKCS#1 format. */
......@@ -402,7 +402,7 @@ _rsa_verify(const struct rsa_public_key *key,
const mpz_t m,
const mpz_t s);
unsigned
size_t
_rsa_check_size(mpz_t n);
void
......
......@@ -78,7 +78,7 @@ int
dsa_sha1_keypair_from_sexp(struct dsa_public_key *pub,
struct dsa_private_key *priv,
unsigned p_max_bits,
unsigned length, const uint8_t *expr)
size_t length, const uint8_t *expr)
{
struct sexp_iterator i;
......@@ -92,7 +92,7 @@ int
dsa_sha256_keypair_from_sexp(struct dsa_public_key *pub,
struct dsa_private_key *priv,
unsigned p_max_bits,
unsigned length, const uint8_t *expr)
size_t length, const uint8_t *expr)
{
struct sexp_iterator i;
......
......@@ -89,7 +89,7 @@ int
rsa_keypair_from_sexp(struct rsa_public_key *pub,
struct rsa_private_key *priv,
unsigned limit,
unsigned length, const uint8_t *expr)
size_t length, const uint8_t *expr)
{
struct sexp_iterator i;
static const uint8_t * const names[3]
......
......@@ -12,10 +12,10 @@ test_main(void)
/* FIXME: How is this spelled? */
const uint8_t *msg = "Squemish ossifrage";
unsigned msg_length;
size_t msg_length;
uint8_t *decrypted;
unsigned decrypted_length;
size_t decrypted_length;
uint8_t after;
mpz_t gibberish;
......@@ -30,7 +30,7 @@ test_main(void)
msg_length = strlen(msg);
if (verbose)
fprintf(stderr, "msg: `%s', length = %d\n", msg, msg_length);
fprintf(stderr, "msg: `%s', length = %d\n", msg, (int) msg_length);
ASSERT(rsa_encrypt(&pub,
&lfib, (nettle_random_func *) knuth_lfib_random,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment