|
| | 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_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 148 of file rsa.c.
156 memcpy ( &cursor,
raw,
sizeof ( cursor ) );
201 memcpy ( modulus, &cursor,
sizeof ( *modulus ) );
211 memcpy ( exponent, &cursor,
sizeof ( *exponent ) );
int asn1_enter_unsigned(struct asn1_cursor *cursor)
Enter ASN.1 unsigned integer.
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
#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_enter_unsigned(), ASN1_INTEGER, ASN1_OCTET_STRING, ASN1_SEQUENCE, asn1_skip(), asn1_skip_any(), asn1_type(), memcpy(), NULL, raw, and rc.
Referenced by rsa_init(), and rsa_match().
Initialise RSA cipher.
- Parameters
-
| context | RSA context |
| key | Key |
- Return values
-
Definition at line 225 of file rsa.c.
232 memset ( context, 0,
sizeof ( *context ) );
236 DBGC ( context,
"RSA %p invalid modulus/exponent:\n", context );
241 DBGC ( context,
"RSA %p modulus:\n", context );
242 DBGC_HDA ( context, 0, modulus.data, modulus.len );
243 DBGC ( context,
"RSA %p exponent:\n", context );
244 DBGC_HDA ( context, 0, exponent.data, exponent.len );
247 if ( (
rc =
rsa_alloc ( context, modulus.len, exponent.len ) ) != 0 )
252 modulus.data, modulus.len );
254 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 271 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 297 of file rsa.c.
304 size_t random_nz_len;
307 DBGC ( &context,
"RSA %p encrypting:\n", &context );
315 max_len = ( context.max_len - 11 );
316 random_nz_len = (
max_len - plaintext->
len + 8 );
320 DBGC ( &context,
"RSA %p plaintext too long (%zd bytes, max " 321 "%zd)\n", &context, plaintext->
len,
max_len );
329 temp = context.output0;
334 DBGC ( &context,
"RSA %p could not generate random data: %s\n",
338 encoded[ 2 + random_nz_len ] = 0x00;
339 memcpy ( &encoded[ context.max_len - plaintext->
len ],
340 plaintext->
data, plaintext->
len );
343 if ( (
rc =
asn1_grow ( ciphertext, context.max_len ) ) != 0 )
348 DBGC ( &context,
"RSA %p encrypted:\n", &context );
349 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 372 of file rsa.c.
384 DBGC ( &context,
"RSA %p decrypting:\n", &context );
392 if ( ciphertext->
len != context.max_len ) {
393 DBGC ( &context,
"RSA %p ciphertext incorrect length (%zd " 394 "bytes, should be %zd)\n",
395 &context, ciphertext->
len, context.max_len );
403 temp = context.input0;
408 end = ( encoded + context.max_len );
409 if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) ) {
413 zero =
memchr ( &encoded[2], 0, (
end - &encoded[2] ) );
415 DBGC ( &context,
"RSA %p invalid decrypted message:\n",
417 DBGC_HDA ( &context, 0, encoded, context.max_len );
421 start = ( zero + 1 );
430 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 455 of file rsa.c.
461 size_t digestinfo_len;
468 DBGC ( context,
"RSA %p has no prefix for %s\n",
472 digestinfo_len = (
prefix->len + digest_len );
475 max_len = ( context->
max_len - 11 );
476 if ( digestinfo_len > max_len ) {
477 DBGC ( context,
"RSA %p %s digestInfo too long (%zd bytes, " 478 "max %zd)\n", context,
digest->
name, digestinfo_len,
482 DBGC ( context,
"RSA %p encoding %s digest:\n",
489 pad_len = ( max_len - digestinfo_len + 8 );
498 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 513 of file rsa.c.
519 DBGC ( &context,
"RSA %p signing %s digest:\n",
520 &context, digest->
name );
538 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 562 of file rsa.c.
571 DBGC ( &context,
"RSA %p verifying %s digest:\n",
572 &context, digest->
name );
581 if (
signature->len != context.max_len ) {
582 DBGC ( &context,
"RSA %p signature incorrect length (%zd " 583 "bytes, should be %zd)\n",
584 &context,
signature->len, context.max_len );
592 temp = context.input0;
595 DBGC ( &context,
"RSA %p deciphered signature:\n", &context );
596 DBGC_HDA ( &context, 0, expected, context.max_len );
601 temp = context.output0;
608 if (
memcmp ( actual, expected, context.max_len ) != 0 ) {
609 DBGC ( &context,
"RSA %p signature verification failed\n",
618 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.