48#define EACCES_VERIFY \
49 __einfo_error ( EINFO_EACCES_VERIFY )
50#define EINFO_EACCES_VERIFY \
51 __einfo_uniqify ( EINFO_EACCES, 0x01, "RSA signature incorrect" )
89 const void *
value,
const void *reference,
131 size_t exponent_len ) {
138 bigint_t ( exponent_size ) exponent;
145 dynamic =
malloc (
sizeof ( *dynamic ) );
151 context->
modulus0 = &dynamic->modulus.element[0];
153 context->
max_len = modulus_len;
154 context->
exponent0 = &dynamic->exponent.element[0];
156 context->
input0 = &dynamic->input.element[0];
157 context->
output0 = &dynamic->output.element[0];
158 context->
tmp = &dynamic->tmp;
179 memcpy ( &cursor,
raw,
sizeof ( cursor ) );
224 memcpy ( modulus, &cursor,
sizeof ( *modulus ) );
234 memcpy ( exponent, &cursor,
sizeof ( *exponent ) );
256 memset ( context, 0,
sizeof ( *context ) );
260 DBGC ( context,
"RSA %p invalid modulus/exponent:\n", context );
265 DBGC ( context,
"RSA %p modulus:\n", context );
267 DBGC ( context,
"RSA %p exponent:\n", context );
273 DBGC ( context,
"RSA %p invalid modulus MSB\n", context );
277 context->
mask = ( ( 1 << (
fls ( msb ) - 1 ) ) - 1 );
306 const void *
in,
void *
out ) {
343 DBGC ( &context,
"RSA %p encrypting:\n", &context );
351 min_len = ( 1 + 1 + 8
352 + 1 + plaintext->
len );
353 if ( min_len > context.
max_len ) {
354 DBGC ( &context,
"RSA %p modulus too small for %zd-byte "
355 "plaintext\n", &context, plaintext->
len );
358 pad_len = ( 8 + context.
max_len - min_len );
368 DBGC ( &context,
"RSA %p could not generate random data: %s\n",
372 encoded[ 2 + pad_len ] = 0x00;
374 plaintext->
data, plaintext->
len );
375 DBGC ( &context,
"RSA %p encoded:\n", &context );
384 DBGC ( &context,
"RSA %p encrypted:\n", &context );
422 DBGC ( &context,
"RSA %p decrypting:\n", &context );
431 DBGC ( &context,
"RSA %p ciphertext incorrect length (%zd "
432 "bytes, should be %zd)\n",
444 DBGC ( &context,
"RSA %p encoded:\n", &context );
449 if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) ) {
453 zero =
memchr ( &encoded[2], 0, (
end - &encoded[2] ) );
455 DBGC ( &context,
"RSA %p invalid decrypted message:\n",
461 start = ( zero + 1 );
470 DBGC ( &context,
"RSA %p decrypted:\n", &context );
504 size_t digestinfo_len;
511 DBGC ( context,
"RSA %p has no prefix for %s\n",
515 digestinfo_len = (
prefix->len + digest_len );
518 min_len = ( 1 + 1 + 8
519 + 1 + digestinfo_len );
520 if ( min_len > context->
max_len ) {
521 DBGC ( context,
"RSA %p modulus too small for %s digest\n",
525 DBGC ( context,
"RSA %p encoding %s digest using PKCS#1:\n",
532 pad_len = ( 8 + context->
max_len - min_len );
533 memset ( temp, 0xff, pad_len );
541 DBGC ( context,
"RSA %p encoded %s digest using PKCS#1:\n",
559 void *
out,
const void *seed,
void *
xor,
576 for ( i = 0 ;
len && ( i < digest_len ) ; i++,
len-- )
577 *(xor_byte++) ^= out_byte[i];
596 const void *
value,
const void *reference,
613 min_len = (
sizeof ( *head ) + digest_len +
614 digest_len +
sizeof ( *tail ) );
615 if ( context->
max_len < min_len ) {
616 DBGC ( context,
"RSA %p %s formatted digest value too long "
617 "(%zd bytes, max %zd)\n", context,
digest->
name,
621 DBGC ( context,
"RSA %p encoding %s digest using PSS:\n",
626 pad_len = ( context->
max_len - min_len );
628 head = ( msb + pad_len );
629 salt = (
head +
sizeof ( *head ) );
630 hash = ( salt + digest_len );
631 tail = (
hash + digest_len );
632 mask_len = ( pad_len +
sizeof ( *head ) + digest_len );
641 DBGC ( context,
"RSA %p could not generate random "
646 DBGC ( context,
"RSA %p salt:\n", context );
647 DBGC_HDA ( context, 0, salt, digest_len );
657 memset ( encoded, 0, pad_len );
660 *msb &= context->
mask;
662 DBGC ( context,
"RSA %p encoded %s digest using PSS:\n",
687 DBGC ( &context,
"RSA %p signing %s digest:\n",
688 &context, digest->
name );
700 if ( (
rc = encode ( &context, digest,
value,
NULL,
706 DBGC ( &context,
"RSA %p signed %s digest:\n", &context, digest->
name );
742 DBGC ( &context,
"RSA %p verifying %s digest:\n",
743 &context, digest->
name );
753 DBGC ( &context,
"RSA %p signature incorrect length (%zd "
754 "bytes, should be %zd)\n",
766 DBGC ( &context,
"RSA %p deciphered signature:\n", &context );
774 if ( (
rc = encode ( &context, digest,
value, expected,
780 DBGC ( &context,
"RSA %p signature verification failed\n",
789 DBGC ( &context,
"RSA %p signature verified successfully\n", &context );
822 public_key ) ) != 0 )
826 if (
asn1_compare ( &private_modulus, &public_modulus ) != 0 )
#define NULL
NULL pointer (VOID *).
struct golan_eq_context ctx
u8 signature
CPU signature.
union @162305117151260234136356364136041353210355154177 key
struct arbelprm_rc_send_wqe rc
pseudo_bit_t value[0x00020]
pseudo_bit_t hash[0x00010]
uint32_t bigint_element_t
Element of a big integer.
int asn1_enter_unsigned(struct asn1_cursor *cursor)
Enter ASN.1 unsigned integer.
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
int asn1_check_algorithm(const struct asn1_cursor *cursor, struct asn1_algorithm *expected, struct asn1_cursor *params)
Check ASN.1 OID-identified algorithm.
int asn1_grow(struct asn1_builder *builder, size_t extra)
Grow ASN.1 builder.
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
int asn1_skip(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object.
int asn1_enter_bits(struct asn1_cursor *cursor, unsigned int *unused)
Enter ASN.1 bit string.
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
#define ASN1_INTEGER
ASN.1 integer.
#define ASN1_SEQUENCE
ASN.1 sequence.
#define ASN1_OCTET_STRING
ASN.1 octet string.
static unsigned int asn1_type(const struct asn1_cursor *cursor)
Extract ASN.1 type.
#define assert(condition)
Assert a condition at run-time.
int pubkey_null_decrypt(struct pubkey_algorithm *pubkey __unused, const struct asn1_cursor *key __unused, const struct asn1_cursor *ciphertext __unused, struct asn1_builder *plaintext __unused)
int pubkey_null_encrypt(struct pubkey_algorithm *pubkey __unused, const struct asn1_cursor *key __unused, const struct asn1_cursor *plaintext __unused, struct asn1_builder *ciphertext __unused)
uint8_t data[48]
Additional event data.
#define __unused
Declare a variable or data structure as unused.
uint32_t start
Starting offset.
uint16_t size
Buffer size.
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
#define REQUIRE_OBJECT(object)
Require an object.
#define EINVAL
Invalid argument.
#define ENOMEM
Not enough space.
#define ENOTSUP
Operation not supported.
#define ERANGE
Result too large.
#define ENOTTY
Inappropriate I/O control operation.
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
#define REQUIRING_SYMBOL(symbol)
Specify the file's requiring symbol.
#define bigint_mod_exp(base, modulus, exponent, result, tmp)
Perform modular exponentiation of big integers.
#define bigint_mod_exp_tmp_len(modulus)
Calculate temporary working space required for moduluar exponentiation.
#define bigint_t(size)
Define a big-integer type.
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
#define bigint_done(value, out, len)
Finalise big integer.
#define bigint_init(value, data, len)
Initialise big integer.
static void digest_init(struct digest_algorithm *digest, void *ctx)
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
#define fls(x)
Find last (i.e.
void * malloc(size_t size)
Allocate memory.
void zfree(void *ptr)
Clear and free memory.
uint32_t end
Ending offset.
int get_random_nz(void *data, size_t len)
Get random non-zero bytes.
static int rsa_pkcs1_encrypt(struct pubkey_algorithm *pubkey __unused, const struct asn1_cursor *key, const struct asn1_cursor *plaintext, struct asn1_builder *ciphertext)
Encrypt using RSA PKCS#1.
static void rsa_free(struct rsa_context *context)
Free RSA dynamic storage.
static int rsa_match(struct pubkey_algorithm *pubkey __unused, const struct asn1_cursor *private_key, const struct asn1_cursor *public_key)
Check for matching RSA public/private key pair.
static struct rsa_digestinfo_prefix * rsa_find_prefix(struct digest_algorithm *digest)
Identify RSA prefix.
static int rsa_parse_mod_exp(struct asn1_cursor *modulus, struct asn1_cursor *exponent, const struct asn1_cursor *raw)
Parse RSA modulus and exponent.
static void rsa_xor_mask(struct digest_algorithm *digest, void *ctx, void *out, const void *seed, void *xor, size_t len)
Apply RSA PSS mask generation function.
struct pubkey_algorithm rsa_algorithm
RSA public-key algorithm.
static int rsa_pkcs1_decrypt(struct pubkey_algorithm *pubkey __unused, const struct asn1_cursor *key, const struct asn1_cursor *ciphertext, struct asn1_builder *plaintext)
Decrypt using RSA PKCS#1.
int(* rsa_get_random)(void *data, size_t len)
Generate random data.
static int rsa_init(struct rsa_context *context, const struct asn1_cursor *key)
Initialise RSA cipher.
static void rsa_cipher(struct rsa_context *context, const void *in, void *out)
Perform RSA cipher operation.
static int rsa_sign(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, struct asn1_builder *signature)
Sign digest value using RSA.
static int rsa_pkcs1_encode(struct rsa_context *context, struct digest_algorithm *digest, const void *value, const void *reference __unused, void *encoded)
Encode digest using RSA PKCS#1.
static int rsa_alloc(struct rsa_context *context, size_t modulus_len, size_t exponent_len)
Allocate RSA dynamic storage.
static int rsa_pss_encode(struct rsa_context *context, struct digest_algorithm *digest, const void *value, const void *reference, void *encoded)
Encode digest using RSA PSS.
static int rsa_verify(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const struct asn1_cursor *signature)
Verify signed digest value using RSA.
struct pubkey_algorithm rsa_pss_algorithm
RSA-PSS public-key algorithm.
int rsa_encode_t(struct rsa_context *context, struct digest_algorithm *digest, const void *value, const void *reference, void *encoded)
Encode digest.
RSA public-key cryptography.
#define RSA_DIGESTINFO_PREFIXES
RSA digestInfo prefix table.
char * strerror(int errno)
Retrieve string representation of error number.
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
const void * data
Start of data.
size_t len
Length of data.
A message digest algorithm.
size_t digestsize
Digest size.
size_t ctxsize
Context size.
const char * name
Algorithm name.
void * priv
Algorithm private data.
bigint_element_t * exponent0
Exponent.
bigint_element_t * input0
Input buffer.
void * dynamic
Allocated memory.
void * tmp
Temporary working space for modular exponentiation.
bigint_element_t * modulus0
Modulus.
unsigned int exponent_size
Exponent size.
uint8_t mask
Modulus MSB mask.
bigint_element_t * output0
Output buffer.
size_t max_len
Modulus length.
unsigned int size
Modulus size.
An RSA digestInfo prefix.
struct digest_algorithm * digest
Digest algorithm.
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
static u32 xor(u32 a, u32 b)