49#define EACCES_VERIFY \
50 __einfo_error ( EINFO_EACCES_VERIFY )
51#define EINFO_EACCES_VERIFY \
52 __einfo_uniqify ( EINFO_EACCES, 0x01, "RSA signature incorrect" )
90 const void *
value,
const void *reference,
132 size_t exponent_len ) {
139 bigint_t ( exponent_size ) exponent;
146 dynamic =
malloc (
sizeof ( *dynamic ) );
152 context->
modulus0 = &dynamic->modulus.element[0];
154 context->
max_len = modulus_len;
155 context->
exponent0 = &dynamic->exponent.element[0];
157 context->
input0 = &dynamic->input.element[0];
158 context->
output0 = &dynamic->output.element[0];
159 context->
tmp = &dynamic->tmp;
180 memcpy ( &cursor,
raw,
sizeof ( cursor ) );
225 memcpy ( modulus, &cursor,
sizeof ( *modulus ) );
235 memcpy ( exponent, &cursor,
sizeof ( *exponent ) );
257 memset ( context, 0,
sizeof ( *context ) );
261 DBGC ( context,
"RSA %p invalid modulus/exponent:\n", context );
266 DBGC ( context,
"RSA %p modulus:\n", context );
268 DBGC ( context,
"RSA %p exponent:\n", context );
274 DBGC ( context,
"RSA %p invalid modulus MSB\n", context );
278 context->
mask = ( ( 1 << (
fls ( msb ) - 1 ) ) - 1 );
308 const void *
in,
void *
out ) {
351 DBGC ( &context,
"RSA %p encrypting:\n", &context );
359 min_len = ( 1 + 1 + 8
360 + 1 + plaintext->
len );
361 if ( min_len > context.
max_len ) {
362 DBGC ( &context,
"RSA %p modulus too small for %zd-byte "
363 "plaintext\n", &context, plaintext->
len );
366 pad_len = ( 8 + context.
max_len - min_len );
376 DBGC ( &context,
"RSA %p could not generate random data: %s\n",
380 encoded[ 2 + pad_len ] = 0x00;
382 plaintext->
data, plaintext->
len );
383 DBGC ( &context,
"RSA %p encoded:\n", &context );
393 DBGC ( &context,
"RSA %p encrypted:\n", &context );
433 DBGC ( &context,
"RSA %p decrypting:\n", &context );
442 DBGC ( &context,
"RSA %p ciphertext incorrect length (%zd "
443 "bytes, should be %zd)\n",
455 DBGC ( &context,
"RSA %p encoded:\n", &context );
458 DBGC ( &context,
"RSA %p ciphertext was not canonical\n",
466 if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) ) {
472 if ( ( ! zero ) || ( ( zero -
pad ) < 8 ) ) {
473 DBGC ( &context,
"RSA %p invalid decrypted message:\n",
479 start = ( zero + 1 );
488 DBGC ( &context,
"RSA %p decrypted:\n", &context );
523 size_t digestinfo_len;
530 DBGC ( context,
"RSA %p has no prefix for %s\n",
536 digestinfo_len = ( digest_len + (
prefix ?
prefix->len : 0 ) );
539 min_len = ( 1 + 1 + 8
540 + 1 + digestinfo_len );
541 if ( min_len > context->
max_len ) {
542 DBGC ( context,
"RSA %p modulus too small for %s digest\n",
546 DBGC ( context,
"RSA %p encoding %s digest using PKCS#1:\n",
553 pad_len = ( 8 + context->
max_len - min_len );
554 memset ( temp, 0xff, pad_len );
564 DBGC ( context,
"RSA %p encoded %s digest using PKCS#1:\n",
582 void *
out,
const void *seed,
void *
xor,
599 for ( i = 0 ;
len && ( i < digest_len ) ; i++,
len-- )
600 *(xor_byte++) ^= out_byte[i];
619 const void *
value,
const void *reference,
636 min_len = (
sizeof ( *head ) + digest_len +
637 digest_len +
sizeof ( *tail ) );
638 if ( context->
max_len < min_len ) {
639 DBGC ( context,
"RSA %p %s formatted digest value too long "
640 "(%zd bytes, max %zd)\n", context,
digest->
name,
644 DBGC ( context,
"RSA %p encoding %s digest using PSS:\n",
649 pad_len = ( context->
max_len - min_len );
651 head = ( msb + pad_len );
652 salt = (
head +
sizeof ( *head ) );
653 hash = ( salt + digest_len );
654 tail = (
hash + digest_len );
655 mask_len = ( pad_len +
sizeof ( *head ) + digest_len );
664 DBGC ( context,
"RSA %p could not generate random "
669 DBGC ( context,
"RSA %p salt:\n", context );
670 DBGC_HDA ( context, 0, salt, digest_len );
680 memset ( encoded, 0, pad_len );
683 *msb &= context->
mask;
685 DBGC ( context,
"RSA %p encoded %s digest using PSS:\n",
711 DBGC ( &context,
"RSA %p signing %s digest:\n",
712 &context, digest->
name );
724 if ( (
rc = encode ( &context, digest,
value,
NULL,
731 DBGC ( &context,
"RSA %p signed %s digest:\n", &context, digest->
name );
768 DBGC ( &context,
"RSA %p verifying %s digest:\n",
769 &context, digest->
name );
779 DBGC ( &context,
"RSA %p signature incorrect length (%zd "
780 "bytes, should be %zd)\n",
792 DBGC ( &context,
"RSA %p deciphered signature:\n", &context );
795 DBGC ( &context,
"RSA %p signature was not canonical\n",
806 if ( (
rc = encode ( &context, digest,
value, expected,
812 DBGC ( &context,
"RSA %p signature verification failed\n",
821 DBGC ( &context,
"RSA %p signature verified successfully\n", &context );
855 public_key ) ) != 0 )
859 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_is_geq(value, reference)
Compare big integers.
#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.
Hybrid MD5+SHA1 hash as used by TLSv1.1 and earlier.
static int is_md5_sha1(struct digest_algorithm *digest)
Check if a digest algorithm is MD5+SHA1.
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 int rsa_cipher(struct rsa_context *context, const void *in, void *out)
Perform RSA cipher operation.
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.
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 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)
struct pubkey_algorithm rsa_algorithm