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 @@ ...@@ -46,7 +46,7 @@
*/ */
/* Including extra sign bit, if needed. Also one byte for zero. */ /* Including extra sign bit, if needed. Also one byte for zero. */
unsigned size_t
nettle_mpz_sizeinbase_256_s(const mpz_t x) nettle_mpz_sizeinbase_256_s(const mpz_t x)
{ {
if (mpz_sgn(x) >= 0) if (mpz_sgn(x) >= 0)
...@@ -54,7 +54,7 @@ nettle_mpz_sizeinbase_256_s(const mpz_t x) ...@@ -54,7 +54,7 @@ nettle_mpz_sizeinbase_256_s(const mpz_t x)
else else
{ {
/* We'll output ~~x, so we need as many bits as for ~x */ /* We'll output ~~x, so we need as many bits as for ~x */
unsigned size; size_t size;
mpz_t c; mpz_t c;
mpz_init(c); mpz_init(c);
...@@ -66,24 +66,24 @@ nettle_mpz_sizeinbase_256_s(const mpz_t x) ...@@ -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) nettle_mpz_sizeinbase_256_u(const mpz_t x)
{ {
return (mpz_sizeinbase(x,2) + 7) / 8; return (mpz_sizeinbase(x,2) + 7) / 8;
} }
static void 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) const mpz_t x, uint8_t sign)
{ {
uint8_t *dst = s + length - 1; uint8_t *dst = s + length - 1;
unsigned size = mpz_size(x); size_t size = mpz_size(x);
unsigned i; size_t i;
for (i = 0; i<size; i++) for (i = 0; i<size; i++)
{ {
mp_limb_t limb = mpz_getlimbn(x, i); mp_limb_t limb = mpz_getlimbn(x, i);
unsigned j; size_t j;
for (j = 0; length && j < sizeof(mp_limb_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, ...@@ -98,7 +98,7 @@ nettle_mpz_to_octets(unsigned length, uint8_t *s,
} }
void 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) if (!length)
{ {
...@@ -134,9 +134,9 @@ nettle_mpz_get_str_256(unsigned length, uint8_t *s, const mpz_t x) ...@@ -134,9 +134,9 @@ nettle_mpz_get_str_256(unsigned length, uint8_t *s, const mpz_t x)
#else #else
static void static void
nettle_mpz_from_octets(mpz_t x, 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); mpz_set_ui(x, 0);
...@@ -150,14 +150,14 @@ nettle_mpz_from_octets(mpz_t x, ...@@ -150,14 +150,14 @@ nettle_mpz_from_octets(mpz_t x,
void void
nettle_mpz_set_str_256_u(mpz_t x, 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); nettle_mpz_from_octets(x, length, s);
} }
void void
nettle_mpz_init_set_str_256_u(mpz_t x, 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); mpz_init(x);
nettle_mpz_from_octets(x, length, s); nettle_mpz_from_octets(x, length, s);
...@@ -165,7 +165,7 @@ nettle_mpz_init_set_str_256_u(mpz_t x, ...@@ -165,7 +165,7 @@ nettle_mpz_init_set_str_256_u(mpz_t x,
void void
nettle_mpz_set_str_256_s(mpz_t x, nettle_mpz_set_str_256_s(mpz_t x,
unsigned length, const uint8_t *s) size_t length, const uint8_t *s)
{ {
if (!length) if (!length)
{ {
...@@ -188,7 +188,7 @@ nettle_mpz_set_str_256_s(mpz_t x, ...@@ -188,7 +188,7 @@ nettle_mpz_set_str_256_s(mpz_t x,
void void
nettle_mpz_init_set_str_256_s(mpz_t x, 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); mpz_init(x);
nettle_mpz_set_str_256_s(x, length, s); nettle_mpz_set_str_256_s(x, length, s);
......
...@@ -37,36 +37,36 @@ extern "C" { ...@@ -37,36 +37,36 @@ extern "C" {
/* Size needed for signed encoding, including extra sign byte if /* Size needed for signed encoding, including extra sign byte if
* necessary. */ * necessary. */
unsigned size_t
nettle_mpz_sizeinbase_256_s(const mpz_t x); nettle_mpz_sizeinbase_256_s(const mpz_t x);
/* Size needed for unsigned encoding */ /* Size needed for unsigned encoding */
unsigned size_t
nettle_mpz_sizeinbase_256_u(const mpz_t x); nettle_mpz_sizeinbase_256_u(const mpz_t x);
/* Writes an integer as length octets, using big endian byte order, /* Writes an integer as length octets, using big endian byte order,
* and two's complement for negative numbers. */ * and two's complement for negative numbers. */
void 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. */ /* Reads a big endian, two's complement, integer. */
void void
nettle_mpz_set_str_256_s(mpz_t x, nettle_mpz_set_str_256_s(mpz_t x,
unsigned length, const uint8_t *s); size_t length, const uint8_t *s);
void void
nettle_mpz_init_set_str_256_s(mpz_t x, 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 /* Similar, but for unsigned format. These function don't interpret
* the most significant bit as the sign. */ * the most significant bit as the sign. */
void void
nettle_mpz_set_str_256_u(mpz_t x, nettle_mpz_set_str_256_u(mpz_t x,
unsigned length, const uint8_t *s); size_t length, const uint8_t *s);
void void
nettle_mpz_init_set_str_256_u(mpz_t x, 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 */ /* Returns a uniformly distributed random number 0 <= x < 2^n */
void void
......
...@@ -39,7 +39,7 @@ int ...@@ -39,7 +39,7 @@ int
_dsa_sign(const struct dsa_public_key *pub, _dsa_sign(const struct dsa_public_key *pub,
const struct dsa_private_key *key, const struct dsa_private_key *key,
void *random_ctx, nettle_random_func *random, void *random_ctx, nettle_random_func *random,
unsigned digest_size, size_t digest_size,
const uint8_t *digest, const uint8_t *digest,
struct dsa_signature *signature) struct dsa_signature *signature)
{ {
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
int int
_dsa_verify(const struct dsa_public_key *key, _dsa_verify(const struct dsa_public_key *key,
unsigned digest_size, size_t digest_size,
const uint8_t *digest, const uint8_t *digest,
const struct dsa_signature *signature) const struct dsa_signature *signature)
{ {
......
...@@ -238,13 +238,13 @@ int ...@@ -238,13 +238,13 @@ int
dsa_sha1_keypair_from_sexp(struct dsa_public_key *pub, dsa_sha1_keypair_from_sexp(struct dsa_public_key *pub,
struct dsa_private_key *priv, struct dsa_private_key *priv,
unsigned p_max_bits, unsigned p_max_bits,
unsigned length, const uint8_t *expr); size_t length, const uint8_t *expr);
int int
dsa_sha256_keypair_from_sexp(struct dsa_public_key *pub, dsa_sha256_keypair_from_sexp(struct dsa_public_key *pub,
struct dsa_private_key *priv, struct dsa_private_key *priv,
unsigned p_max_bits, unsigned p_max_bits,
unsigned length, const uint8_t *expr); size_t length, const uint8_t *expr);
/* Keys in X.509 andd OpenSSL format. */ /* Keys in X.509 andd OpenSSL format. */
struct asn1_der_iterator; struct asn1_der_iterator;
...@@ -276,13 +276,13 @@ int ...@@ -276,13 +276,13 @@ int
_dsa_sign(const struct dsa_public_key *pub, _dsa_sign(const struct dsa_public_key *pub,
const struct dsa_private_key *key, const struct dsa_private_key *key,
void *random_ctx, nettle_random_func *random, void *random_ctx, nettle_random_func *random,
unsigned digest_size, size_t digest_size,
const uint8_t *digest, const uint8_t *digest,
struct dsa_signature *signature); struct dsa_signature *signature);
int int
_dsa_verify(const struct dsa_public_key *key, _dsa_verify(const struct dsa_public_key *key,
unsigned digest_size, size_t digest_size,
const uint8_t *digest, const uint8_t *digest,
const struct dsa_signature *signature); const struct dsa_signature *signature);
......
...@@ -198,7 +198,7 @@ main(int argc, char **argv) ...@@ -198,7 +198,7 @@ main(int argc, char **argv)
struct rsa_session ctx; struct rsa_session ctx;
struct rsa_session_info session; struct rsa_session_info session;
unsigned length; size_t length;
mpz_t x; mpz_t x;
mpz_init(x); mpz_init(x);
......
...@@ -34,14 +34,14 @@ ...@@ -34,14 +34,14 @@
#include "nettle-internal.h" #include "nettle-internal.h"
int int
pkcs1_decrypt (unsigned key_size, pkcs1_decrypt (size_t key_size,
const mpz_t m, const mpz_t m,
unsigned *length, uint8_t *message) size_t *length, uint8_t *message)
{ {
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
uint8_t *terminator; uint8_t *terminator;
unsigned padding; size_t padding;
unsigned message_length; size_t message_length;
TMP_ALLOC(em, key_size); TMP_ALLOC(em, key_size);
nettle_mpz_get_str_256(key_size, em, m); nettle_mpz_get_str_256(key_size, em, m);
......
...@@ -37,15 +37,15 @@ ...@@ -37,15 +37,15 @@
#include "nettle-internal.h" #include "nettle-internal.h"
int int
pkcs1_encrypt (unsigned key_size, pkcs1_encrypt (size_t key_size,
/* For padding */ /* For padding */
void *random_ctx, nettle_random_func *random, void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *message, size_t length, const uint8_t *message,
mpz_t m) mpz_t m)
{ {
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
unsigned padding; size_t padding;
unsigned i; size_t i;
/* The message is encoded as a string of the same length as the /* The message is encoded as a string of the same length as the
* modulo n, of the form * modulo n, of the form
......
...@@ -32,8 +32,8 @@ ...@@ -32,8 +32,8 @@
#include "nettle-internal.h" #include "nettle-internal.h"
int int
pkcs1_rsa_digest_encode(mpz_t m, unsigned key_size, pkcs1_rsa_digest_encode(mpz_t m, size_t key_size,
unsigned di_length, const uint8_t *digest_info) size_t di_length, const uint8_t *digest_info)
{ {
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
TMP_ALLOC(em, key_size); TMP_ALLOC(em, key_size);
......
...@@ -62,7 +62,7 @@ md5_prefix[] = ...@@ -62,7 +62,7 @@ md5_prefix[] =
}; };
int 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; uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); 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) ...@@ -83,7 +83,7 @@ pkcs1_rsa_md5_encode(mpz_t m, unsigned key_size, struct md5_ctx *hash)
} }
int 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; uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......
...@@ -62,7 +62,7 @@ sha1_prefix[] = ...@@ -62,7 +62,7 @@ sha1_prefix[] =
}; };
int 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; uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); 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) ...@@ -83,7 +83,7 @@ pkcs1_rsa_sha1_encode(mpz_t m, unsigned key_size, struct sha1_ctx *hash)
} }
int 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; uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......
...@@ -60,7 +60,7 @@ sha256_prefix[] = ...@@ -60,7 +60,7 @@ sha256_prefix[] =
}; };
int 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; uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); 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) ...@@ -81,7 +81,7 @@ pkcs1_rsa_sha256_encode(mpz_t m, unsigned key_size, struct sha256_ctx *hash)
} }
int 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; uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......
...@@ -60,7 +60,7 @@ sha512_prefix[] = ...@@ -60,7 +60,7 @@ sha512_prefix[] =
}; };
int 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; uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); 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) ...@@ -81,7 +81,7 @@ pkcs1_rsa_sha512_encode(mpz_t m, unsigned key_size, struct sha512_ctx *hash)
} }
int 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; uint8_t *p;
TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE); TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
......
...@@ -60,44 +60,44 @@ _pkcs1_signature_prefix(unsigned key_size, ...@@ -60,44 +60,44 @@ _pkcs1_signature_prefix(unsigned key_size,
unsigned digest_size); unsigned digest_size);
int int
pkcs1_encrypt (unsigned key_size, pkcs1_encrypt (size_t key_size,
/* For padding */ /* For padding */
void *random_ctx, nettle_random_func *random, void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *message, size_t length, const uint8_t *message,
mpz_t m); mpz_t m);
int int
pkcs1_decrypt (unsigned key_size, pkcs1_decrypt (size_t key_size,
const mpz_t m, const mpz_t m,
unsigned *length, uint8_t *message); size_t *length, uint8_t *message);
int int
pkcs1_rsa_digest_encode(mpz_t m, unsigned key_size, pkcs1_rsa_digest_encode(mpz_t m, size_t key_size,
unsigned di_length, const uint8_t *digest_info); size_t di_length, const uint8_t *digest_info);
int 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 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 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 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 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 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 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 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 #ifdef __cplusplus
} }
......
...@@ -37,7 +37,7 @@ int ...@@ -37,7 +37,7 @@ int
rsa_decrypt_tr(const struct rsa_public_key *pub, rsa_decrypt_tr(const struct rsa_public_key *pub,
const struct rsa_private_key *key, const struct rsa_private_key *key,
void *random_ctx, nettle_random_func *random, void *random_ctx, nettle_random_func *random,
unsigned *length, uint8_t *message, size_t *length, uint8_t *message,
const mpz_t gibberish) const mpz_t gibberish)
{ {
mpz_t m, ri; mpz_t m, ri;
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
int int
rsa_decrypt(const struct rsa_private_key *key, rsa_decrypt(const struct rsa_private_key *key,
unsigned *length, uint8_t *message, size_t *length, uint8_t *message,
const mpz_t gibberish) const mpz_t gibberish)
{ {
mpz_t m; mpz_t m;
......
...@@ -35,7 +35,7 @@ int ...@@ -35,7 +35,7 @@ int
rsa_encrypt(const struct rsa_public_key *key, rsa_encrypt(const struct rsa_public_key *key,
/* For padding */ /* For padding */
void *random_ctx, nettle_random_func *random, void *random_ctx, nettle_random_func *random,
unsigned length, const uint8_t *message, size_t length, const uint8_t *message,
mpz_t gibberish) mpz_t gibberish)
{ {
if (pkcs1_encrypt (key->size, random_ctx, random, if (pkcs1_encrypt (key->size, random_ctx, random,
......
...@@ -35,7 +35,7 @@ int ...@@ -35,7 +35,7 @@ int
rsa_pkcs1_sign_tr(const struct rsa_public_key *pub, rsa_pkcs1_sign_tr(const struct rsa_public_key *pub,
const struct rsa_private_key *key, const struct rsa_private_key *key,
void *random_ctx, nettle_random_func *random, 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 s)
{ {
mpz_t ri; mpz_t ri;
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
int int
rsa_pkcs1_sign(const struct rsa_private_key *key, 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) mpz_t s)
{ {
if (pkcs1_rsa_digest_encode (s, key->size, length, digest_info)) if (pkcs1_rsa_digest_encode (s, key->size, length, digest_info))
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
int int
rsa_pkcs1_verify(const struct rsa_public_key *key, 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) const mpz_t s)
{