|
| | FILE_LICENCE (GPL2_OR_LATER_OR_UBDL) |
| |
| static struct rsa_digestinfo_prefix * | rsa_find_prefix (struct digest_algorithm *digest) |
| | Identify RSA prefix. More...
|
| |
| static void | rsa_free (struct rsa_context *context) |
| | Free RSA dynamic storage. More...
|
| |
| static int | rsa_alloc (struct rsa_context *context, size_t modulus_len, size_t exponent_len) |
| | Allocate RSA dynamic storage. More...
|
| |
| static int | rsa_parse_integer (struct asn1_cursor *integer, const struct asn1_cursor *raw) |
| | Parse RSA integer. More...
|
| |
| static int | rsa_parse_mod_exp (struct asn1_cursor *modulus, struct asn1_cursor *exponent, const struct asn1_cursor *raw) |
| | Parse RSA modulus and exponent. More...
|
| |
| static int | rsa_init (struct rsa_context *context, const struct asn1_cursor *key) |
| | Initialise RSA cipher. More...
|
| |
| static void | rsa_cipher (struct rsa_context *context, const void *in, void *out) |
| | Perform RSA cipher operation. More...
|
| |
| static int | rsa_encrypt (const struct asn1_cursor *key, const struct asn1_cursor *plaintext, struct asn1_builder *ciphertext) |
| | Encrypt using RSA. More...
|
| |
| static int | rsa_decrypt (const struct asn1_cursor *key, const struct asn1_cursor *ciphertext, struct asn1_builder *plaintext) |
| | Decrypt using RSA. More...
|
| |
| static int | rsa_encode_digest (struct rsa_context *context, struct digest_algorithm *digest, const void *value, void *encoded) |
| | Encode RSA digest. More...
|
| |
| static int | rsa_sign (const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, struct asn1_builder *signature) |
| | Sign digest value using RSA. More...
|
| |
| static int | rsa_verify (const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const struct asn1_cursor *signature) |
| | Verify signed digest value using RSA. More...
|
| |
| static int | rsa_match (const struct asn1_cursor *private_key, const struct asn1_cursor *public_key) |
| | Check for matching RSA public/private key pair. More...
|
| |
| | REQUIRING_SYMBOL (rsa_algorithm) |
| |
| | REQUIRE_OBJECT (config_crypto) |
| |
RSA public-key cryptography.
RSA is documented in RFC 3447.
Definition in file rsa.c.
Allocate RSA dynamic storage.
- Parameters
-
| context | RSA context |
| modulus_len | Modulus length |
| exponent_len | Exponent length |
- Return values
-
Definition at line 107 of file rsa.c.
115 bigint_t ( exponent_size ) exponent;
122 dynamic =
malloc (
sizeof ( *dynamic ) );
128 context->
modulus0 = &dynamic->modulus.element[0];
130 context->
max_len = modulus_len;
131 context->
exponent0 = &dynamic->exponent.element[0];
133 context->
input0 = &dynamic->input.element[0];
134 context->
output0 = &dynamic->output.element[0];
135 context->
tmp = &dynamic->tmp;
void * tmp
Temporary working space for modular exponentiation.
bigint_element_t * output0
Output buffer.
uint16_t size
Buffer size.
unsigned int exponent_size
Exponent size.
bigint_element_t * modulus0
Modulus.
#define ENOMEM
Not enough space.
unsigned int size
Modulus size.
size_t max_len
Modulus length.
void * dynamic
Allocated memory.
bigint_element_t * input0
Input buffer.
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
void * malloc(size_t size)
Allocate memory.
bigint_element_t * exponent0
Exponent.
#define bigint_mod_exp_tmp_len(modulus)
Calculate temporary working space required for moduluar exponentiation.
typedef bigint_t(X25519_SIZE) x25519_t
An X25519 unsigned big integer used in internal calculations.
References __attribute__, bigint_mod_exp_tmp_len, bigint_required_size, bigint_t(), rsa_context::dynamic, ENOMEM, rsa_context::exponent0, rsa_context::exponent_size, rsa_context::input0, malloc(), rsa_context::max_len, rsa_context::modulus0, rsa_context::output0, size, rsa_context::size, tmp, and rsa_context::tmp.
Referenced by rsa_init().
Parse RSA modulus and exponent.
- Parameters
-
| modulus | Modulus to fill in |
| exponent | Exponent to fill in |
| raw | ASN.1 cursor |
- Return values
-
Definition at line 176 of file rsa.c.
184 memcpy ( &cursor,
raw,
sizeof ( cursor ) );
struct arbelprm_rc_send_wqe rc
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
int asn1_enter_bits(struct asn1_cursor *cursor, unsigned int *unused)
Enter ASN.1 bit string.
static unsigned int asn1_type(const struct asn1_cursor *cursor)
Extract ASN.1 type.
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static int rsa_parse_integer(struct asn1_cursor *integer, const struct asn1_cursor *raw)
Parse RSA integer.
#define ASN1_SEQUENCE
ASN.1 sequence.
#define ASN1_INTEGER
ASN.1 integer.
int asn1_skip(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object.
int asn1_check_algorithm(const struct asn1_cursor *cursor, struct asn1_algorithm *expected, struct asn1_cursor *params)
Check ASN.1 OID-identified algorithm.
#define ASN1_OCTET_STRING
ASN.1 octet string.
#define NULL
NULL pointer (VOID *)
References asn1_check_algorithm(), asn1_enter(), asn1_enter_bits(), ASN1_INTEGER, ASN1_OCTET_STRING, ASN1_SEQUENCE, asn1_skip(), asn1_skip_any(), asn1_type(), memcpy(), NULL, raw, rc, and rsa_parse_integer().
Referenced by rsa_init(), and rsa_match().
Initialise RSA cipher.
- Parameters
-
| context | RSA context |
| key | Key |
- Return values
-
Definition at line 251 of file rsa.c.
258 memset ( context, 0,
sizeof ( *context ) );
262 DBGC ( context,
"RSA %p invalid modulus/exponent:\n", context );
267 DBGC ( context,
"RSA %p modulus:\n", context );
268 DBGC_HDA ( context, 0, modulus.data, modulus.len );
269 DBGC ( context,
"RSA %p exponent:\n", context );
270 DBGC_HDA ( context, 0, exponent.data, exponent.len );
273 if ( (
rc =
rsa_alloc ( context, modulus.len, exponent.len ) ) != 0 )
278 modulus.data, modulus.len );
280 context->
exponent0 ), exponent.data, exponent.len );
static int rsa_parse_mod_exp(struct asn1_cursor *modulus, struct asn1_cursor *exponent, const struct asn1_cursor *raw)
Parse RSA modulus and exponent.
struct arbelprm_rc_send_wqe rc
unsigned int exponent_size
Exponent size.
#define bigint_init(value, data, len)
Initialise big integer.
bigint_element_t * modulus0
Modulus.
unsigned int size
Modulus size.
static void rsa_free(struct rsa_context *context)
Free RSA dynamic storage.
bigint_element_t * exponent0
Exponent.
static int rsa_alloc(struct rsa_context *context, size_t modulus_len, size_t exponent_len)
Allocate RSA dynamic storage.
typedef bigint_t(X25519_SIZE) x25519_t
An X25519 unsigned big integer used in internal calculations.
void * memset(void *dest, int character, size_t len) __nonnull
References bigint_init, bigint_t(), asn1_cursor::data, DBGC, DBGC_HDA, rsa_context::exponent0, rsa_context::exponent_size, key, asn1_cursor::len, memset(), rsa_context::modulus0, rc, rsa_alloc(), rsa_free(), rsa_parse_mod_exp(), and rsa_context::size.
Referenced by rsa_decrypt(), rsa_encrypt(), rsa_sign(), and rsa_verify().
| static void rsa_cipher |
( |
struct rsa_context * |
context, |
|
|
const void * |
in, |
|
|
void * |
out |
|
) |
| |
|
static |
Perform RSA cipher operation.
- Parameters
-
| context | RSA context |
| in | Input buffer |
| out | Output buffer |
Definition at line 297 of file rsa.c.
void * tmp
Temporary working space for modular exponentiation.
bigint_element_t * output0
Output buffer.
#define bigint_mod_exp(base, modulus, exponent, result, tmp)
Perform modular exponentiation of big integers.
unsigned int exponent_size
Exponent size.
#define bigint_init(value, data, len)
Initialise big integer.
bigint_element_t * modulus0
Modulus.
unsigned int size
Modulus size.
size_t max_len
Modulus length.
bigint_element_t * input0
Input buffer.
#define bigint_done(value, out, len)
Finalise big integer.
bigint_element_t * exponent0
Exponent.
typedef bigint_t(X25519_SIZE) x25519_t
An X25519 unsigned big integer used in internal calculations.
References bigint_done, bigint_init, bigint_mod_exp, bigint_t(), rsa_context::exponent0, rsa_context::exponent_size, in, rsa_context::input0, rsa_context::max_len, rsa_context::modulus0, out, rsa_context::output0, rsa_context::size, and rsa_context::tmp.
Referenced by rsa_decrypt(), rsa_encrypt(), rsa_sign(), and rsa_verify().
Encrypt using RSA.
- Parameters
-
| key | Key |
| plaintext | Plaintext |
| ciphertext | Ciphertext |
- Return values
-
| ciphertext_len | Length of ciphertext, or negative error |
Definition at line 323 of file rsa.c.
330 size_t random_nz_len;
333 DBGC ( &context,
"RSA %p encrypting:\n", &context );
341 max_len = ( context.max_len - 11 );
342 random_nz_len = (
max_len - plaintext->
len + 8 );
346 DBGC ( &context,
"RSA %p plaintext too long (%zd bytes, max " 347 "%zd)\n", &context, plaintext->
len,
max_len );
355 temp = context.output0;
360 DBGC ( &context,
"RSA %p could not generate random data: %s\n",
364 encoded[ 2 + random_nz_len ] = 0x00;
365 memcpy ( &encoded[ context.max_len - plaintext->
len ],
366 plaintext->
data, plaintext->
len );
369 if ( (
rc =
asn1_grow ( ciphertext, context.max_len ) ) != 0 )
374 DBGC ( &context,
"RSA %p encrypted:\n", &context );
375 DBGC_HDA ( &context, 0, ciphertext->
data, context.max_len );
struct arbelprm_rc_send_wqe rc
const void * data
Start of data.
int get_random_nz(void *data, size_t len)
Get random non-zero bytes.
static void rsa_cipher(struct rsa_context *context, const void *in, void *out)
Perform RSA cipher operation.
size_t len
Length of data.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t max_len
Modulus length.
int asn1_grow(struct asn1_builder *builder, size_t extra)
Grow ASN.1 builder.
static void rsa_free(struct rsa_context *context)
Free RSA dynamic storage.
#define ERANGE
Result too large.
char * strerror(int errno)
Retrieve string representation of error number.
static int rsa_init(struct rsa_context *context, const struct asn1_cursor *key)
Initialise RSA cipher.
References asn1_grow(), asn1_cursor::data, asn1_builder::data, DBGC, DBGC_HDA, ERANGE, get_random_nz(), key, asn1_cursor::len, rsa_context::max_len, memcpy(), rsa_context::output0, rc, rsa_cipher(), rsa_free(), rsa_init(), and strerror().
Decrypt using RSA.
- Parameters
-
| key | Key |
| ciphertext | Ciphertext |
| plaintext | Plaintext |
- Return values
-
Definition at line 398 of file rsa.c.
410 DBGC ( &context,
"RSA %p decrypting:\n", &context );
418 if ( ciphertext->
len != context.max_len ) {
419 DBGC ( &context,
"RSA %p ciphertext incorrect length (%zd " 420 "bytes, should be %zd)\n",
421 &context, ciphertext->
len, context.max_len );
429 temp = context.input0;
434 end = ( encoded + context.max_len );
435 if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) ) {
439 zero =
memchr ( &encoded[2], 0, (
end - &encoded[2] ) );
441 DBGC ( &context,
"RSA %p invalid decrypted message:\n",
443 DBGC_HDA ( &context, 0, encoded, context.max_len );
447 start = ( zero + 1 );
456 DBGC ( &context,
"RSA %p decrypted:\n", &context );
#define EINVAL
Invalid argument.
struct arbelprm_rc_send_wqe rc
const void * data
Start of data.
static void rsa_cipher(struct rsa_context *context, const void *in, void *out)
Perform RSA cipher operation.
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
size_t len
Length of data.
uint32_t start
Starting offset.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
int asn1_grow(struct asn1_builder *builder, size_t extra)
Grow ASN.1 builder.
static void rsa_free(struct rsa_context *context)
Free RSA dynamic storage.
#define ERANGE
Result too large.
uint32_t end
Ending offset.
static int rsa_init(struct rsa_context *context, const struct asn1_cursor *key)
Initialise RSA cipher.
References asn1_grow(), asn1_cursor::data, asn1_builder::data, DBGC, DBGC_HDA, EINVAL, end, ERANGE, rsa_context::input0, key, asn1_cursor::len, len, rsa_context::max_len, memchr(), memcpy(), rc, rsa_cipher(), rsa_free(), rsa_init(), and start.
Encode RSA digest.
- Parameters
-
| context | RSA context |
| digest | Digest algorithm |
| value | Digest value |
| encoded | Encoded digest |
- Return values
-
Definition at line 481 of file rsa.c.
487 size_t digestinfo_len;
494 DBGC ( context,
"RSA %p has no prefix for %s\n",
498 digestinfo_len = (
prefix->len + digest_len );
501 max_len = ( context->
max_len - 11 );
502 if ( digestinfo_len > max_len ) {
503 DBGC ( context,
"RSA %p %s digestInfo too long (%zd bytes, " 504 "max %zd)\n", context,
digest->
name, digestinfo_len,
508 DBGC ( context,
"RSA %p encoding %s digest:\n",
515 pad_len = ( max_len - digestinfo_len + 8 );
524 DBGC ( context,
"RSA %p encoded %s digest:\n", context,
digest->
name );
static struct rsa_digestinfo_prefix * rsa_find_prefix(struct digest_algorithm *digest)
Identify RSA prefix.
#define ENOTSUP
Operation not supported.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
pseudo_bit_t value[0x00020]
size_t max_len
Modulus length.
#define ERANGE
Result too large.
An RSA digestInfo prefix.
size_t digestsize
Digest size.
const char * name
Algorithm name.
struct digest_algorithm * digest
Digest algorithm.
void * memset(void *dest, int character, size_t len) __nonnull
References assert(), DBGC, DBGC_HDA, rsa_digestinfo_prefix::digest, digest_algorithm::digestsize, ENOTSUP, ERANGE, rsa_context::max_len, memcpy(), memset(), digest_algorithm::name, pad_len, prefix, rsa_find_prefix(), and value.
Referenced by rsa_sign(), and rsa_verify().
Sign digest value using RSA.
- Parameters
-
| key | Key |
| digest | Digest algorithm |
| value | Digest value |
| signature | Signature |
- Return values
-
Definition at line 539 of file rsa.c.
545 DBGC ( &context,
"RSA %p signing %s digest:\n",
546 &context, digest->
name );
564 DBGC ( &context,
"RSA %p signed %s digest:\n", &context, digest->
name );
struct arbelprm_rc_send_wqe rc
static int rsa_encode_digest(struct rsa_context *context, struct digest_algorithm *digest, const void *value, void *encoded)
Encode RSA digest.
static void rsa_cipher(struct rsa_context *context, const void *in, void *out)
Perform RSA cipher operation.
pseudo_bit_t value[0x00020]
int asn1_grow(struct asn1_builder *builder, size_t extra)
Grow ASN.1 builder.
static void rsa_free(struct rsa_context *context)
Free RSA dynamic storage.
size_t digestsize
Digest size.
const char * name
Algorithm name.
static int rsa_init(struct rsa_context *context, const struct asn1_cursor *key)
Initialise RSA cipher.
u8 signature
CPU signature.
References asn1_grow(), DBGC, DBGC_HDA, digest_algorithm::digestsize, key, rsa_context::max_len, digest_algorithm::name, rc, rsa_cipher(), rsa_encode_digest(), rsa_free(), rsa_init(), signature, and value.
Verify signed digest value using RSA.
- Parameters
-
| key | Key |
| digest | Digest algorithm |
| value | Digest value |
| signature | Signature |
- Return values
-
Definition at line 588 of file rsa.c.
597 DBGC ( &context,
"RSA %p verifying %s digest:\n",
598 &context, digest->
name );
607 if (
signature->len != context.max_len ) {
608 DBGC ( &context,
"RSA %p signature incorrect length (%zd " 609 "bytes, should be %zd)\n",
610 &context,
signature->len, context.max_len );
618 temp = context.input0;
621 DBGC ( &context,
"RSA %p deciphered signature:\n", &context );
622 DBGC_HDA ( &context, 0, expected, context.max_len );
627 temp = context.output0;
634 if (
memcmp ( actual, expected, context.max_len ) != 0 ) {
635 DBGC ( &context,
"RSA %p signature verification failed\n",
644 DBGC ( &context,
"RSA %p signature verified successfully\n", &context );
struct arbelprm_rc_send_wqe rc
static int rsa_encode_digest(struct rsa_context *context, struct digest_algorithm *digest, const void *value, void *encoded)
Encode RSA digest.
static void rsa_cipher(struct rsa_context *context, const void *in, void *out)
Perform RSA cipher operation.
pseudo_bit_t value[0x00020]
static void rsa_free(struct rsa_context *context)
Free RSA dynamic storage.
#define ERANGE
Result too large.
size_t digestsize
Digest size.
const char * name
Algorithm name.
static int rsa_init(struct rsa_context *context, const struct asn1_cursor *key)
Initialise RSA cipher.
u8 signature
CPU signature.
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
References DBGC, DBGC_HDA, digest_algorithm::digestsize, EACCES_VERIFY, ERANGE, rsa_context::input0, key, rsa_context::max_len, memcmp(), digest_algorithm::name, rsa_context::output0, rc, rsa_cipher(), rsa_encode_digest(), rsa_free(), rsa_init(), signature, and value.