45 #define EACCES_VERIFY \ 46 __einfo_error ( EINFO_EACCES_VERIFY ) 47 #define EINFO_EACCES_VERIFY \ 48 __einfo_uniqify ( EINFO_EACCES, 0x01, "RSA signature incorrect" ) 87 size_t exponent_len ) {
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;
134 memcpy ( integer,
raw,
sizeof ( *integer ) );
138 if ( ( integer->
len > 1 ) &&
145 if ( ! integer->
len )
168 memcpy ( &cursor,
raw,
sizeof ( cursor ) );
230 memset ( context, 0,
sizeof ( *context ) );
234 cursor.
len = key_len;
238 DBGC ( context,
"RSA %p invalid modulus/exponent:\n", context );
243 DBGC ( context,
"RSA %p modulus:\n", context );
245 DBGC ( context,
"RSA %p exponent:\n", context );
286 const void *
in,
void *
out ) {
313 size_t plaintext_len,
void *ciphertext ) {
318 size_t random_nz_len = (
max_len - plaintext_len + 8 );
322 if ( plaintext_len >
max_len ) {
323 DBGC ( context,
"RSA %p plaintext too long (%zd bytes, max " 324 "%zd)\n", context, plaintext_len,
max_len );
327 DBGC ( context,
"RSA %p encrypting:\n", context );
328 DBGC_HDA ( context, 0, plaintext, plaintext_len );
338 DBGC ( context,
"RSA %p could not generate random data: %s\n",
342 encoded[ 2 + random_nz_len ] = 0x00;
344 plaintext, plaintext_len );
348 DBGC ( context,
"RSA %p encrypted:\n", context );
364 size_t ciphertext_len,
void *plaintext ) {
371 size_t plaintext_len;
374 if ( ciphertext_len != context->
max_len ) {
375 DBGC ( context,
"RSA %p ciphertext incorrect length (%zd " 376 "bytes, should be %zd)\n",
377 context, ciphertext_len, context->
max_len );
380 DBGC ( context,
"RSA %p decrypting:\n", context );
381 DBGC_HDA ( context, 0, ciphertext, ciphertext_len );
392 if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) )
402 DBGC ( context,
"RSA %p decrypted:\n", context );
403 DBGC_HDA ( context, 0, plaintext, plaintext_len );
405 return plaintext_len;
408 DBGC ( context,
"RSA %p invalid decrypted message:\n", context );
424 const void *
value,
void *encoded ) {
426 size_t digest_len =
digest->digestsize;
428 size_t digestinfo_len;
435 DBGC ( context,
"RSA %p has no prefix for %s\n",
439 digestinfo_len = (
prefix->len + digest_len );
443 if ( digestinfo_len >
max_len ) {
444 DBGC ( context,
"RSA %p %s digestInfo too long (%zd bytes, max" 449 DBGC ( context,
"RSA %p encoding %s digest:\n",
465 DBGC ( context,
"RSA %p encoded %s digest:\n", context,
digest->name );
486 DBGC ( context,
"RSA %p signing %s digest:\n", context,
digest->name );
498 DBGC ( context,
"RSA %p signed %s digest:\n", context,
digest->name );
516 size_t signature_len ) {
524 if ( signature_len != context->
max_len ) {
525 DBGC ( context,
"RSA %p signature incorrect length (%zd " 526 "bytes, should be %zd)\n",
527 context, signature_len, context->
max_len );
530 DBGC ( context,
"RSA %p verifying %s digest:\n",
541 DBGC ( context,
"RSA %p deciphered signature:\n", context );
554 DBGC ( context,
"RSA %p signature verification failed\n",
559 DBGC ( context,
"RSA %p signature verified successfully\n", context );
584 const void *public_key,
size_t public_key_len ) {
596 public_cursor.
data = public_key;
597 public_cursor.
len = public_key_len;
601 &private_cursor ) ) != 0 )
604 &public_cursor ) ) != 0 )
608 if (
asn1_compare ( &private_modulus, &public_modulus ) != 0 )
static int rsa_parse_mod_exp(struct asn1_cursor *modulus, struct asn1_cursor *exponent, const struct asn1_cursor *raw)
Parse RSA modulus and exponent.
void * tmp
Temporary working space for modular exponentiation.
#define EINVAL
Invalid argument.
struct arbelprm_rc_send_wqe rc
bigint_element_t * output0
Output buffer.
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
#define bigint_mod_exp(base, modulus, exponent, result, tmp)
Perform modular exponentiation of big integers.
static int rsa_encrypt(void *ctx, const void *plaintext, size_t plaintext_len, void *ciphertext)
Encrypt using RSA.
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
static int rsa_decrypt(void *ctx, const void *ciphertext, size_t ciphertext_len, void *plaintext)
Decrypt using RSA.
uint16_t max_len
Maximum length (in bytes)
static int rsa_encode_digest(struct rsa_context *context, struct digest_algorithm *digest, const void *value, void *encoded)
Encode RSA digest.
const void * data
Start of data.
unsigned int exponent_size
Exponent size.
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.
#define bigint_init(value, data, len)
Initialise big integer.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint32_t zero
Must be zero.
struct golan_eq_context ctx
static unsigned int asn1_type(const struct asn1_cursor *cursor)
Extract ASN.1 type.
static struct rsa_digestinfo_prefix * rsa_find_prefix(struct digest_algorithm *digest)
Identify RSA prefix.
REQUIRING_SYMBOL(rsa_algorithm)
struct md4_digest digest
Digest of data already processed.
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
bigint_element_t * modulus0
Modulus.
#define ENOTSUP
Operation not supported.
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.
#define ENOMEM
Not enough space.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static int rsa_parse_integer(struct asn1_cursor *integer, const struct asn1_cursor *raw)
Parse RSA integer.
static void rsa_final(void *ctx)
Finalise RSA cipher.
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.
#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.
#define RSA_CTX_SIZE
RSA context size.
#define bigint_done(value, out, len)
Finalise big integer.
static int rsa_init(void *ctx, const void *key, size_t key_len)
Initialise RSA cipher.
pseudo_bit_t value[0x00020]
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.
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 void(* free)(struct refcnt *refcnt))
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
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.
static int rsa_sign(void *ctx, struct digest_algorithm *digest, const void *value, void *signature)
Sign digest value using RSA.
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
An RSA digestInfo prefix.
#define ASN1_SEQUENCE
ASN.1 sequence.
void * malloc(size_t size)
Allocate memory.
#define ASN1_INTEGER
ASN.1 integer.
bigint_element_t * exponent0
Exponent.
RSA public-key cryptography.
static volatile void * bits
static size_t rsa_max_len(void *ctx)
Calculate RSA maximum output length.
#define ENOTTY
Inappropriate I/O control operation.
int asn1_skip(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object.
struct pubkey_algorithm rsa_algorithm
RSA public-key algorithm.
A message digest algorithm.
uint32_t end
Ending offset.
uint8_t size
Entry size (in 32-bit words)
REQUIRE_OBJECT(config_crypto)
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
#define NULL
NULL pointer (VOID *)
struct private_key private_key
Private key.
static int rsa_alloc(struct rsa_context *context, size_t modulus_len, size_t exponent_len)
Allocate RSA dynamic storage.
const char * name
Algorithm name.
void * memset(void *dest, int character, size_t len) __nonnull
#define RSA_DIGESTINFO_PREFIXES
RSA digestInfo prefix table.