|
| 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 (void *ctx, const void *key, size_t key_len) |
| Initialise RSA cipher. More...
|
|
static size_t | rsa_max_len (void *ctx) |
| Calculate RSA maximum output length. More...
|
|
static void | rsa_cipher (struct rsa_context *context, const void *in, void *out) |
| Perform RSA cipher operation. More...
|
|
static int | rsa_encrypt (void *ctx, const void *plaintext, size_t plaintext_len, void *ciphertext) |
| Encrypt using RSA. More...
|
|
static int | rsa_decrypt (void *ctx, const void *ciphertext, size_t ciphertext_len, void *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 (void *ctx, struct digest_algorithm *digest, const void *value, void *signature) |
| Sign digest value using RSA. More...
|
|
static int | rsa_verify (void *ctx, struct digest_algorithm *digest, const void *value, const void *signature, size_t signature_len) |
| Verify signed digest value using RSA. More...
|
|
static void | rsa_final (void *ctx) |
| Finalise RSA cipher. More...
|
|
static int | rsa_match (const void *private_key, size_t private_key_len, const void *public_key, size_t public_key_len) |
| 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 86 of file rsa.c.
91 bigint_t ( exponent_size ) *exponent;
105 dynamic =
malloc (
sizeof ( *dynamic ) );
111 context->
modulus0 = &dynamic->modulus.element[0];
113 context->
max_len = modulus_len;
114 context->
exponent0 = &dynamic->exponent.element[0];
116 context->
input0 = &dynamic->input.element[0];
117 context->
output0 = &dynamic->output.element[0];
118 context->
tmp = &dynamic->tmp;
void * tmp
Temporary working space for modular exponentiation.
bigint_element_t * output0
Output buffer.
unsigned int exponent_size
Exponent size.
bigint_element_t * modulus0
Modulus.
#define ENOMEM
Not enough space.
#define bigint_mod_exp_tmp_len(modulus, exponent)
Calculate temporary working space required for moduluar exponentiation.
unsigned int size
Modulus size.
size_t max_len
Modulus length.
void * dynamic
Allocated memory.
bigint_element_t * input0
Input buffer.
#define bigint_t(size)
Define a big-integer type.
static void rsa_free(struct rsa_context *context)
Free RSA dynamic storage.
#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.
uint8_t size
Entry size (in 32-bit words)
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, rsa_free(), 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 159 of file rsa.c.
168 memcpy ( &cursor,
raw,
sizeof ( cursor ) );
182 &rsa_encryption_algorithm ) == 0 ) {
209 cursor.len =
bits.len;
struct arbelprm_rc_send_wqe rc
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
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.
int asn1_check_algorithm(const struct asn1_cursor *cursor, struct asn1_algorithm *expected)
Check ASN.1 OID-identified algorithm.
int asn1_integral_bit_string(const struct asn1_cursor *cursor, struct asn1_bit_string *bits)
Parse ASN.1 bit string that must be an integral number of bytes.
#define ASN1_SEQUENCE
ASN.1 sequence.
#define ASN1_INTEGER
ASN.1 integer.
static volatile void * bits
int asn1_skip(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object.
struct arbelprm_wqe_segment_data_ptr data[ARBEL_MAX_GATHER]
#define ASN1_OCTET_STRING
ASN.1 octet string.
References asn1_check_algorithm(), asn1_enter(), ASN1_INTEGER, asn1_integral_bit_string(), ASN1_OCTET_STRING, ASN1_SEQUENCE, asn1_skip(), asn1_skip_any(), asn1_type(), bits, asn1_cursor::data, asn1_cursor::len, memcpy(), raw, rc, and rsa_parse_integer().
Referenced by rsa_init(), and rsa_match().
static int rsa_init |
( |
void * |
ctx, |
|
|
const void * |
key, |
|
|
size_t |
key_len |
|
) |
| |
|
static |
Initialise RSA cipher.
- Parameters
-
ctx | RSA context |
key | Key |
key_len | Length of key |
- Return values
-
Definition at line 239 of file rsa.c.
247 memset ( context, 0,
sizeof ( *context ) );
251 cursor.len = key_len;
255 DBGC ( context,
"RSA %p invalid modulus/exponent:\n", context );
256 DBGC_HDA ( context, 0, cursor.data, cursor.len );
260 DBGC ( context,
"RSA %p modulus:\n", context );
261 DBGC_HDA ( context, 0, modulus.data, modulus.len );
262 DBGC ( context,
"RSA %p exponent:\n", context );
263 DBGC_HDA ( context, 0, exponent.data, exponent.len );
266 if ( (
rc =
rsa_alloc ( context, modulus.len, exponent.len ) ) != 0 )
271 modulus.data, modulus.len );
273 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.
struct golan_eq_context ctx
bigint_element_t * modulus0
Modulus.
unsigned int size
Modulus size.
#define bigint_t(size)
Define a big-integer type.
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.
void * memset(void *dest, int character, size_t len) __nonnull
References bigint_init, bigint_t, ctx, 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.
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 302 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_t(size)
Define a big-integer type.
#define bigint_done(value, out, len)
Finalise big integer.
bigint_element_t * exponent0
Exponent.
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().
static int rsa_encrypt |
( |
void * |
ctx, |
|
|
const void * |
plaintext, |
|
|
size_t |
plaintext_len, |
|
|
void * |
ciphertext |
|
) |
| |
|
static |
Encrypt using RSA.
- Parameters
-
ctx | RSA context |
plaintext | Plaintext |
plaintext_len | Length of plaintext |
ciphertext | Ciphertext |
- Return values
-
ciphertext_len | Length of ciphertext, or negative error |
Definition at line 329 of file rsa.c.
335 size_t random_nz_len = (
max_len - plaintext_len + 8 );
339 if ( plaintext_len >
max_len ) {
340 DBGC ( context,
"RSA %p plaintext too long (%zd bytes, max " 341 "%zd)\n", context, plaintext_len,
max_len );
344 DBGC ( context,
"RSA %p encrypting:\n", context );
345 DBGC_HDA ( context, 0, plaintext, plaintext_len );
355 DBGC ( context,
"RSA %p could not generate random data: %s\n",
359 encoded[ 2 + random_nz_len ] = 0x00;
361 plaintext, plaintext_len );
365 DBGC ( context,
"RSA %p encrypted:\n", context );
struct arbelprm_rc_send_wqe rc
bigint_element_t * output0
Output buffer.
uint16_t max_len
Maximum length (in bytes)
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.
struct golan_eq_context ctx
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t max_len
Modulus length.
#define ERANGE
Result too large.
char * strerror(int errno)
Retrieve string representation of error number.
References ctx, DBGC, DBGC_HDA, ERANGE, get_random_nz(), max_len, rsa_context::max_len, memcpy(), rsa_context::output0, rc, rsa_cipher(), and strerror().
static int rsa_decrypt |
( |
void * |
ctx, |
|
|
const void * |
ciphertext, |
|
|
size_t |
ciphertext_len, |
|
|
void * |
plaintext |
|
) |
| |
|
static |
Decrypt using RSA.
- Parameters
-
ctx | RSA context |
ciphertext | Ciphertext |
ciphertext_len | Ciphertext length |
plaintext | Plaintext |
- Return values
-
plaintext_len | Plaintext length, or negative error |
Definition at line 380 of file rsa.c.
388 size_t plaintext_len;
391 if ( ciphertext_len != context->
max_len ) {
392 DBGC ( context,
"RSA %p ciphertext incorrect length (%zd " 393 "bytes, should be %zd)\n",
394 context, ciphertext_len, context->
max_len );
397 DBGC ( context,
"RSA %p decrypting:\n", context );
398 DBGC_HDA ( context, 0, ciphertext, ciphertext_len );
409 if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) )
419 DBGC ( context,
"RSA %p decrypted:\n", context );
420 DBGC_HDA ( context, 0, plaintext, plaintext_len );
422 return plaintext_len;
425 DBGC ( context,
"RSA %p invalid decrypted message:\n", context );
#define EINVAL
Invalid argument.
static void rsa_cipher(struct rsa_context *context, const void *in, void *out)
Perform RSA cipher operation.
uint32_t zero
Must be zero.
struct golan_eq_context ctx
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
uint32_t start
Starting offset.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t max_len
Modulus length.
bigint_element_t * input0
Input buffer.
#define ERANGE
Result too large.
uint32_t end
Ending offset.
References ctx, DBGC, DBGC_HDA, EINVAL, end, ERANGE, rsa_context::input0, rsa_context::max_len, memchr(), memcpy(), rsa_cipher(), start, and zero.
Encode RSA digest.
- Parameters
-
context | RSA context |
digest | Digest algorithm |
value | Digest value |
encoded | Encoded digest |
- Return values
-
Definition at line 439 of file rsa.c.
443 size_t digest_len =
digest->digestsize;
445 size_t digestinfo_len;
452 DBGC ( context,
"RSA %p has no prefix for %s\n",
456 digestinfo_len = (
prefix->len + digest_len );
460 if ( digestinfo_len >
max_len ) {
461 DBGC ( context,
"RSA %p %s digestInfo too long (%zd bytes, max" 466 DBGC ( context,
"RSA %p encoding %s digest:\n",
482 DBGC ( context,
"RSA %p encoded %s digest:\n", context,
digest->name );
uint16_t max_len
Maximum length (in bytes)
static struct rsa_digestinfo_prefix * rsa_find_prefix(struct digest_algorithm *digest)
Identify RSA prefix.
struct md4_digest digest
Digest of data already processed.
#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)
size_t max_len
Modulus length.
pseudo_bit_t value[0x00020]
#define ERANGE
Result too large.
An RSA digestInfo prefix.
void * memset(void *dest, int character, size_t len) __nonnull
References assert(), DBGC, DBGC_HDA, digest, ENOTSUP, ERANGE, max_len, rsa_context::max_len, memcpy(), memset(), pad_len, prefix, rsa_find_prefix(), and value.
Referenced by rsa_sign(), and rsa_verify().
static int rsa_sign |
( |
void * |
ctx, |
|
|
struct digest_algorithm * |
digest, |
|
|
const void * |
value, |
|
|
void * |
signature |
|
) |
| |
|
static |
Sign digest value using RSA.
- Parameters
-
ctx | RSA context |
digest | Digest algorithm |
value | Digest value |
signature | Signature |
- Return values
-
signature_len | Signature length, or negative error |
Definition at line 497 of file rsa.c.
503 DBGC ( context,
"RSA %p signing %s digest:\n", context,
digest->name );
515 DBGC ( context,
"RSA %p signed %s digest:\n", context,
digest->name );
struct arbelprm_rc_send_wqe rc
bigint_element_t * output0
Output buffer.
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.
struct golan_eq_context ctx
struct md4_digest digest
Digest of data already processed.
size_t max_len
Modulus length.
pseudo_bit_t value[0x00020]
References ctx, DBGC, DBGC_HDA, digest, rsa_context::max_len, rsa_context::output0, rc, rsa_cipher(), rsa_encode_digest(), signature, and value.
static int rsa_verify |
( |
void * |
ctx, |
|
|
struct digest_algorithm * |
digest, |
|
|
const void * |
value, |
|
|
const void * |
signature, |
|
|
size_t |
signature_len |
|
) |
| |
|
static |
Verify signed digest value using RSA.
- Parameters
-
ctx | RSA context |
digest | Digest algorithm |
value | Digest value |
signature | Signature |
signature_len | Signature length |
- Return values
-
Definition at line 531 of file rsa.c.
541 if ( signature_len != context->
max_len ) {
542 DBGC ( context,
"RSA %p signature incorrect length (%zd " 543 "bytes, should be %zd)\n",
544 context, signature_len, context->
max_len );
547 DBGC ( context,
"RSA %p verifying %s digest:\n",
558 DBGC ( context,
"RSA %p deciphered signature:\n", context );
571 DBGC ( context,
"RSA %p signature verification failed\n",
576 DBGC ( context,
"RSA %p signature verified successfully\n", context );
struct arbelprm_rc_send_wqe rc
bigint_element_t * output0
Output buffer.
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.
struct golan_eq_context ctx
struct md4_digest digest
Digest of data already processed.
size_t max_len
Modulus length.
bigint_element_t * input0
Input buffer.
pseudo_bit_t value[0x00020]
#define ERANGE
Result too large.
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
References ctx, DBGC, DBGC_HDA, digest, EACCES_VERIFY, ERANGE, rsa_context::input0, rsa_context::max_len, memcmp(), rsa_context::output0, rc, rsa_cipher(), rsa_encode_digest(), signature, and value.