iPXE
rsa.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <ipxe/asn1.h>
32 #include <ipxe/crypto.h>
33 #include <ipxe/bigint.h>
34 #include <ipxe/random_nz.h>
35 #include <ipxe/rsa.h>
36 
37 /** @file
38  *
39  * RSA public-key cryptography
40  *
41  * RSA is documented in RFC 3447.
42  */
43 
44 /* Disambiguate the various error causes */
45 #define EACCES_VERIFY \
46  __einfo_error ( EINFO_EACCES_VERIFY )
47 #define EINFO_EACCES_VERIFY \
48  __einfo_uniqify ( EINFO_EACCES, 0x01, "RSA signature incorrect" )
49 
50 /** An RSA context */
51 struct rsa_context {
52  /** Allocated memory */
53  void *dynamic;
54  /** Modulus */
56  /** Modulus size */
57  unsigned int size;
58  /** Modulus length */
59  size_t max_len;
60  /** Exponent */
62  /** Exponent size */
63  unsigned int exponent_size;
64  /** Input buffer */
66  /** Output buffer */
68  /** Temporary working space for modular exponentiation */
69  void *tmp;
70 };
71 
72 /**
73  * Identify RSA prefix
74  *
75  * @v digest Digest algorithm
76  * @ret prefix RSA prefix, or NULL
77  */
78 static struct rsa_digestinfo_prefix *
81 
83  if ( prefix->digest == digest )
84  return prefix;
85  }
86  return NULL;
87 }
88 
89 /**
90  * Free RSA dynamic storage
91  *
92  * @v context RSA context
93  */
94 static inline void rsa_free ( struct rsa_context *context ) {
95 
96  free ( context->dynamic );
97 }
98 
99 /**
100  * Allocate RSA dynamic storage
101  *
102  * @v context RSA context
103  * @v modulus_len Modulus length
104  * @v exponent_len Exponent length
105  * @ret rc Return status code
106  */
107 static int rsa_alloc ( struct rsa_context *context, size_t modulus_len,
108  size_t exponent_len ) {
109  unsigned int size = bigint_required_size ( modulus_len );
110  unsigned int exponent_size = bigint_required_size ( exponent_len );
111  bigint_t ( size ) *modulus;
112  bigint_t ( exponent_size ) *exponent;
113  size_t tmp_len = bigint_mod_exp_tmp_len ( modulus, exponent );
114  struct {
115  bigint_t ( size ) modulus;
116  bigint_t ( exponent_size ) exponent;
117  bigint_t ( size ) input;
118  bigint_t ( size ) output;
119  uint8_t tmp[tmp_len];
120  } __attribute__ (( packed )) *dynamic;
121 
122  /* Allocate dynamic storage */
123  dynamic = malloc ( sizeof ( *dynamic ) );
124  if ( ! dynamic )
125  return -ENOMEM;
126 
127  /* Assign dynamic storage */
128  context->dynamic = dynamic;
129  context->modulus0 = &dynamic->modulus.element[0];
130  context->size = size;
131  context->max_len = modulus_len;
132  context->exponent0 = &dynamic->exponent.element[0];
133  context->exponent_size = exponent_size;
134  context->input0 = &dynamic->input.element[0];
135  context->output0 = &dynamic->output.element[0];
136  context->tmp = &dynamic->tmp;
137 
138  return 0;
139 }
140 
141 /**
142  * Parse RSA integer
143  *
144  * @v integer Integer to fill in
145  * @v raw ASN.1 cursor
146  * @ret rc Return status code
147  */
148 static int rsa_parse_integer ( struct asn1_cursor *integer,
149  const struct asn1_cursor *raw ) {
150 
151  /* Enter integer */
152  memcpy ( integer, raw, sizeof ( *integer ) );
153  asn1_enter ( integer, ASN1_INTEGER );
154 
155  /* Skip initial sign byte if applicable */
156  if ( ( integer->len > 1 ) &&
157  ( *( ( uint8_t * ) integer->data ) == 0x00 ) ) {
158  integer->data++;
159  integer->len--;
160  }
161 
162  /* Fail if cursor or integer are invalid */
163  if ( ! integer->len )
164  return -EINVAL;
165 
166  return 0;
167 }
168 
169 /**
170  * Parse RSA modulus and exponent
171  *
172  * @v modulus Modulus to fill in
173  * @v exponent Exponent to fill in
174  * @v raw ASN.1 cursor
175  * @ret rc Return status code
176  */
177 static int rsa_parse_mod_exp ( struct asn1_cursor *modulus,
178  struct asn1_cursor *exponent,
179  const struct asn1_cursor *raw ) {
180  struct asn1_bit_string bits;
181  struct asn1_cursor cursor;
182  int is_private;
183  int rc;
184 
185  /* Enter subjectPublicKeyInfo/privateKeyInfo/RSAPrivateKey */
186  memcpy ( &cursor, raw, sizeof ( cursor ) );
187  asn1_enter ( &cursor, ASN1_SEQUENCE );
188 
189  /* Determine key format */
190  if ( asn1_type ( &cursor ) == ASN1_INTEGER ) {
191 
192  /* Private key */
193  is_private = 1;
194 
195  /* Skip version */
196  asn1_skip_any ( &cursor );
197 
198  /* Enter privateKey, if present */
199  if ( asn1_check_algorithm ( &cursor,
200  &rsa_encryption_algorithm ) == 0 ) {
201 
202  /* Skip privateKeyAlgorithm */
203  asn1_skip_any ( &cursor );
204 
205  /* Enter privateKey */
206  asn1_enter ( &cursor, ASN1_OCTET_STRING );
207 
208  /* Enter RSAPrivateKey */
209  asn1_enter ( &cursor, ASN1_SEQUENCE );
210 
211  /* Skip version */
212  asn1_skip ( &cursor, ASN1_INTEGER );
213  }
214 
215  } else {
216 
217  /* Public key */
218  is_private = 0;
219 
220  /* Skip algorithm */
221  asn1_skip ( &cursor, ASN1_SEQUENCE );
222 
223  /* Enter subjectPublicKey */
224  if ( ( rc = asn1_integral_bit_string ( &cursor, &bits ) ) != 0 )
225  return rc;
226  cursor.data = bits.data;
227  cursor.len = bits.len;
228 
229  /* Enter RSAPublicKey */
230  asn1_enter ( &cursor, ASN1_SEQUENCE );
231  }
232 
233  /* Extract modulus */
234  if ( ( rc = rsa_parse_integer ( modulus, &cursor ) ) != 0 )
235  return rc;
236  asn1_skip_any ( &cursor );
237 
238  /* Skip public exponent, if applicable */
239  if ( is_private )
240  asn1_skip ( &cursor, ASN1_INTEGER );
241 
242  /* Extract publicExponent/privateExponent */
243  if ( ( rc = rsa_parse_integer ( exponent, &cursor ) ) != 0 )
244  return rc;
245 
246  return 0;
247 }
248 
249 /**
250  * Initialise RSA cipher
251  *
252  * @v context RSA context
253  * @v key Key
254  * @ret rc Return status code
255  */
256 static int rsa_init ( struct rsa_context *context,
257  const struct asn1_cursor *key ) {
258  struct asn1_cursor modulus;
259  struct asn1_cursor exponent;
260  int rc;
261 
262  /* Initialise context */
263  memset ( context, 0, sizeof ( *context ) );
264 
265  /* Parse modulus and exponent */
266  if ( ( rc = rsa_parse_mod_exp ( &modulus, &exponent, key ) ) != 0 ){
267  DBGC ( context, "RSA %p invalid modulus/exponent:\n", context );
268  DBGC_HDA ( context, 0, key->data, key->len );
269  goto err_parse;
270  }
271 
272  DBGC ( context, "RSA %p modulus:\n", context );
273  DBGC_HDA ( context, 0, modulus.data, modulus.len );
274  DBGC ( context, "RSA %p exponent:\n", context );
275  DBGC_HDA ( context, 0, exponent.data, exponent.len );
276 
277  /* Allocate dynamic storage */
278  if ( ( rc = rsa_alloc ( context, modulus.len, exponent.len ) ) != 0 )
279  goto err_alloc;
280 
281  /* Construct big integers */
282  bigint_init ( ( ( bigint_t ( context->size ) * ) context->modulus0 ),
283  modulus.data, modulus.len );
284  bigint_init ( ( ( bigint_t ( context->exponent_size ) * )
285  context->exponent0 ), exponent.data, exponent.len );
286 
287  return 0;
288 
289  rsa_free ( context );
290  err_alloc:
291  err_parse:
292  return rc;
293 }
294 
295 /**
296  * Calculate RSA maximum output length
297  *
298  * @v key Key
299  * @ret max_len Maximum output length
300  */
301 static size_t rsa_max_len ( const struct asn1_cursor *key ) {
302  struct asn1_cursor modulus;
303  struct asn1_cursor exponent;
304  int rc;
305 
306  /* Parse moduli and exponents */
307  if ( ( rc = rsa_parse_mod_exp ( &modulus, &exponent, key ) ) != 0 ) {
308  /* Return a zero maximum length on error */
309  return 0;
310  }
311 
312  /* Output length can never exceed modulus length */
313  return modulus.len;
314 }
315 
316 /**
317  * Perform RSA cipher operation
318  *
319  * @v context RSA context
320  * @v in Input buffer
321  * @v out Output buffer
322  */
323 static void rsa_cipher ( struct rsa_context *context,
324  const void *in, void *out ) {
325  bigint_t ( context->size ) *input = ( ( void * ) context->input0 );
326  bigint_t ( context->size ) *output = ( ( void * ) context->output0 );
327  bigint_t ( context->size ) *modulus = ( ( void * ) context->modulus0 );
328  bigint_t ( context->exponent_size ) *exponent =
329  ( ( void * ) context->exponent0 );
330 
331  /* Initialise big integer */
332  bigint_init ( input, in, context->max_len );
333 
334  /* Perform modular exponentiation */
335  bigint_mod_exp ( input, modulus, exponent, output, context->tmp );
336 
337  /* Copy out result */
338  bigint_done ( output, out, context->max_len );
339 }
340 
341 /**
342  * Encrypt using RSA
343  *
344  * @v key Key
345  * @v plaintext Plaintext
346  * @v plaintext_len Length of plaintext
347  * @v ciphertext Ciphertext
348  * @ret ciphertext_len Length of ciphertext, or negative error
349  */
350 static int rsa_encrypt ( const struct asn1_cursor *key, const void *plaintext,
351  size_t plaintext_len, void *ciphertext ) {
352  struct rsa_context context;
353  void *temp;
354  uint8_t *encoded;
355  size_t max_len;
356  size_t random_nz_len;
357  int rc;
358 
359  DBGC ( &context, "RSA %p encrypting:\n", &context );
360  DBGC_HDA ( &context, 0, plaintext, plaintext_len );
361 
362  /* Initialise context */
363  if ( ( rc = rsa_init ( &context, key ) ) != 0 )
364  goto err_init;
365 
366  /* Calculate lengths */
367  max_len = ( context.max_len - 11 );
368  random_nz_len = ( max_len - plaintext_len + 8 );
369 
370  /* Sanity check */
371  if ( plaintext_len > max_len ) {
372  DBGC ( &context, "RSA %p plaintext too long (%zd bytes, max "
373  "%zd)\n", &context, plaintext_len, max_len );
374  rc = -ERANGE;
375  goto err_sanity;
376  }
377 
378  /* Construct encoded message (using the big integer output
379  * buffer as temporary storage)
380  */
381  temp = context.output0;
382  encoded = temp;
383  encoded[0] = 0x00;
384  encoded[1] = 0x02;
385  if ( ( rc = get_random_nz ( &encoded[2], random_nz_len ) ) != 0 ) {
386  DBGC ( &context, "RSA %p could not generate random data: %s\n",
387  &context, strerror ( rc ) );
388  goto err_random;
389  }
390  encoded[ 2 + random_nz_len ] = 0x00;
391  memcpy ( &encoded[ context.max_len - plaintext_len ],
392  plaintext, plaintext_len );
393 
394  /* Encipher the encoded message */
395  rsa_cipher ( &context, encoded, ciphertext );
396  DBGC ( &context, "RSA %p encrypted:\n", &context );
397  DBGC_HDA ( &context, 0, ciphertext, context.max_len );
398 
399  /* Free context */
400  rsa_free ( &context );
401 
402  return context.max_len;
403 
404  err_random:
405  err_sanity:
406  rsa_free ( &context );
407  err_init:
408  return rc;
409 }
410 
411 /**
412  * Decrypt using RSA
413  *
414  * @v key Key
415  * @v ciphertext Ciphertext
416  * @v ciphertext_len Ciphertext length
417  * @v plaintext Plaintext
418  * @ret plaintext_len Plaintext length, or negative error
419  */
420 static int rsa_decrypt ( const struct asn1_cursor *key, const void *ciphertext,
421  size_t ciphertext_len, void *plaintext ) {
422  struct rsa_context context;
423  void *temp;
424  uint8_t *encoded;
425  uint8_t *end;
426  uint8_t *zero;
427  uint8_t *start;
428  size_t plaintext_len;
429  int rc;
430 
431  DBGC ( &context, "RSA %p decrypting:\n", &context );
432  DBGC_HDA ( &context, 0, ciphertext, ciphertext_len );
433 
434  /* Initialise context */
435  if ( ( rc = rsa_init ( &context, key ) ) != 0 )
436  goto err_init;
437 
438  /* Sanity check */
439  if ( ciphertext_len != context.max_len ) {
440  DBGC ( &context, "RSA %p ciphertext incorrect length (%zd "
441  "bytes, should be %zd)\n",
442  &context, ciphertext_len, context.max_len );
443  rc = -ERANGE;
444  goto err_sanity;
445  }
446 
447  /* Decipher the message (using the big integer input buffer as
448  * temporary storage)
449  */
450  temp = context.input0;
451  encoded = temp;
452  rsa_cipher ( &context, ciphertext, encoded );
453 
454  /* Parse the message */
455  end = ( encoded + context.max_len );
456  if ( ( encoded[0] != 0x00 ) || ( encoded[1] != 0x02 ) ) {
457  rc = -EINVAL;
458  goto err_invalid;
459  }
460  zero = memchr ( &encoded[2], 0, ( end - &encoded[2] ) );
461  if ( ! zero ) {
462  rc = -EINVAL;
463  goto err_invalid;
464  }
465  start = ( zero + 1 );
466  plaintext_len = ( end - start );
467 
468  /* Copy out message */
469  memcpy ( plaintext, start, plaintext_len );
470  DBGC ( &context, "RSA %p decrypted:\n", &context );
471  DBGC_HDA ( &context, 0, plaintext, plaintext_len );
472 
473  /* Free context */
474  rsa_free ( &context );
475 
476  return plaintext_len;
477 
478  err_invalid:
479  DBGC ( &context, "RSA %p invalid decrypted message:\n", &context );
480  DBGC_HDA ( &context, 0, encoded, context.max_len );
481  err_sanity:
482  rsa_free ( &context );
483  err_init:
484  return rc;
485 }
486 
487 /**
488  * Encode RSA digest
489  *
490  * @v context RSA context
491  * @v digest Digest algorithm
492  * @v value Digest value
493  * @v encoded Encoded digest
494  * @ret rc Return status code
495  */
496 static int rsa_encode_digest ( struct rsa_context *context,
497  struct digest_algorithm *digest,
498  const void *value, void *encoded ) {
500  size_t digest_len = digest->digestsize;
501  uint8_t *temp = encoded;
502  size_t digestinfo_len;
503  size_t max_len;
504  size_t pad_len;
505 
506  /* Identify prefix */
508  if ( ! prefix ) {
509  DBGC ( context, "RSA %p has no prefix for %s\n",
510  context, digest->name );
511  return -ENOTSUP;
512  }
513  digestinfo_len = ( prefix->len + digest_len );
514 
515  /* Sanity check */
516  max_len = ( context->max_len - 11 );
517  if ( digestinfo_len > max_len ) {
518  DBGC ( context, "RSA %p %s digestInfo too long (%zd bytes, "
519  "max %zd)\n", context, digest->name, digestinfo_len,
520  max_len );
521  return -ERANGE;
522  }
523  DBGC ( context, "RSA %p encoding %s digest:\n",
524  context, digest->name );
525  DBGC_HDA ( context, 0, value, digest_len );
526 
527  /* Construct encoded message */
528  *(temp++) = 0x00;
529  *(temp++) = 0x01;
530  pad_len = ( max_len - digestinfo_len + 8 );
531  memset ( temp, 0xff, pad_len );
532  temp += pad_len;
533  *(temp++) = 0x00;
534  memcpy ( temp, prefix->data, prefix->len );
535  temp += prefix->len;
536  memcpy ( temp, value, digest_len );
537  temp += digest_len;
538  assert ( temp == ( encoded + context->max_len ) );
539  DBGC ( context, "RSA %p encoded %s digest:\n", context, digest->name );
540  DBGC_HDA ( context, 0, encoded, context->max_len );
541 
542  return 0;
543 }
544 
545 /**
546  * Sign digest value using RSA
547  *
548  * @v key Key
549  * @v digest Digest algorithm
550  * @v value Digest value
551  * @v signature Signature
552  * @ret signature_len Signature length, or negative error
553  */
554 static int rsa_sign ( const struct asn1_cursor *key,
555  struct digest_algorithm *digest, const void *value,
556  void *signature ) {
557  struct rsa_context context;
558  void *temp;
559  int rc;
560 
561  DBGC ( &context, "RSA %p signing %s digest:\n",
562  &context, digest->name );
563  DBGC_HDA ( &context, 0, value, digest->digestsize );
564 
565  /* Initialise context */
566  if ( ( rc = rsa_init ( &context, key ) ) != 0 )
567  goto err_init;
568 
569  /* Encode digest (using the big integer output buffer as
570  * temporary storage)
571  */
572  temp = context.output0;
573  if ( ( rc = rsa_encode_digest ( &context, digest, value, temp ) ) != 0 )
574  goto err_encode;
575 
576  /* Encipher the encoded digest */
577  rsa_cipher ( &context, temp, signature );
578  DBGC ( &context, "RSA %p signed %s digest:\n", &context, digest->name );
579  DBGC_HDA ( &context, 0, signature, context.max_len );
580 
581  /* Free context */
582  rsa_free ( &context );
583 
584  return context.max_len;
585 
586  err_encode:
587  rsa_free ( &context );
588  err_init:
589  return rc;
590 }
591 
592 /**
593  * Verify signed digest value using RSA
594  *
595  * @v key Key
596  * @v digest Digest algorithm
597  * @v value Digest value
598  * @v signature Signature
599  * @v signature_len Signature length
600  * @ret rc Return status code
601  */
602 static int rsa_verify ( const struct asn1_cursor *key,
603  struct digest_algorithm *digest, const void *value,
604  const void *signature, size_t signature_len ) {
605  struct rsa_context context;
606  void *temp;
607  void *expected;
608  void *actual;
609  int rc;
610 
611  DBGC ( &context, "RSA %p verifying %s digest:\n",
612  &context, digest->name );
613  DBGC_HDA ( &context, 0, value, digest->digestsize );
614  DBGC_HDA ( &context, 0, signature, signature_len );
615 
616  /* Initialise context */
617  if ( ( rc = rsa_init ( &context, key ) ) != 0 )
618  goto err_init;
619 
620  /* Sanity check */
621  if ( signature_len != context.max_len ) {
622  DBGC ( &context, "RSA %p signature incorrect length (%zd "
623  "bytes, should be %zd)\n",
624  &context, signature_len, context.max_len );
625  rc = -ERANGE;
626  goto err_sanity;
627  }
628 
629  /* Decipher the signature (using the big integer input buffer
630  * as temporary storage)
631  */
632  temp = context.input0;
633  expected = temp;
634  rsa_cipher ( &context, signature, expected );
635  DBGC ( &context, "RSA %p deciphered signature:\n", &context );
636  DBGC_HDA ( &context, 0, expected, context.max_len );
637 
638  /* Encode digest (using the big integer output buffer as
639  * temporary storage)
640  */
641  temp = context.output0;
642  actual = temp;
643  if ( ( rc = rsa_encode_digest ( &context, digest, value,
644  actual ) ) != 0 )
645  goto err_encode;
646 
647  /* Verify the signature */
648  if ( memcmp ( actual, expected, context.max_len ) != 0 ) {
649  DBGC ( &context, "RSA %p signature verification failed\n",
650  &context );
651  rc = -EACCES_VERIFY;
652  goto err_verify;
653  }
654 
655  /* Free context */
656  rsa_free ( &context );
657 
658  DBGC ( &context, "RSA %p signature verified successfully\n", &context );
659  return 0;
660 
661  err_verify:
662  err_encode:
663  err_sanity:
664  rsa_free ( &context );
665  err_init:
666  return rc;
667 }
668 
669 /**
670  * Check for matching RSA public/private key pair
671  *
672  * @v private_key Private key
673  * @v public_key Public key
674  * @ret rc Return status code
675  */
676 static int rsa_match ( const struct asn1_cursor *private_key,
677  const struct asn1_cursor *public_key ) {
678  struct asn1_cursor private_modulus;
679  struct asn1_cursor private_exponent;
680  struct asn1_cursor public_modulus;
681  struct asn1_cursor public_exponent;
682  int rc;
683 
684  /* Parse moduli and exponents */
685  if ( ( rc = rsa_parse_mod_exp ( &private_modulus, &private_exponent,
686  private_key ) ) != 0 )
687  return rc;
688  if ( ( rc = rsa_parse_mod_exp ( &public_modulus, &public_exponent,
689  public_key ) ) != 0 )
690  return rc;
691 
692  /* Compare moduli */
693  if ( asn1_compare ( &private_modulus, &public_modulus ) != 0 )
694  return -ENOTTY;
695 
696  return 0;
697 }
698 
699 /** RSA public-key algorithm */
701  .name = "rsa",
702  .max_len = rsa_max_len,
703  .encrypt = rsa_encrypt,
704  .decrypt = rsa_decrypt,
705  .sign = rsa_sign,
706  .verify = rsa_verify,
707  .match = rsa_match,
708 };
709 
710 /* Drag in objects via rsa_algorithm */
712 
713 /* Drag in crypto configuration */
714 REQUIRE_OBJECT ( config_crypto );
static int rsa_parse_mod_exp(struct asn1_cursor *modulus, struct asn1_cursor *exponent, const struct asn1_cursor *raw)
Parse RSA modulus and exponent.
Definition: rsa.c:177
#define __attribute__(x)
Definition: compiler.h:10
void * tmp
Temporary working space for modular exponentiation.
Definition: rsa.c:69
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
bigint_element_t * output0
Output buffer.
Definition: rsa.c:67
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition: asn1.c:480
#define bigint_mod_exp(base, modulus, exponent, result, tmp)
Perform modular exponentiation of big integers.
Definition: bigint.h:314
__be32 in[4]
Definition: CIB_PRM.h:35
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
Error codes.
static int rsa_sign(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, void *signature)
Sign digest value using RSA.
Definition: rsa.c:554
static int rsa_encode_digest(struct rsa_context *context, struct digest_algorithm *digest, const void *value, void *encoded)
Encode RSA digest.
Definition: rsa.c:496
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
unsigned int exponent_size
Exponent size.
Definition: rsa.c:63
int get_random_nz(void *data, size_t len)
Get random non-zero bytes.
Definition: random_nz.c:62
Definition: bnxt_hsi.h:68
static void rsa_cipher(struct rsa_context *context, const void *in, void *out)
Perform RSA cipher operation.
Definition: rsa.c:323
#define bigint_init(value, data, len)
Initialise big integer.
Definition: bigint.h:50
char prefix[4]
Definition: vmconsole.c:53
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Cryptographic API.
static unsigned int asn1_type(const struct asn1_cursor *cursor)
Extract ASN.1 type.
Definition: asn1.h:446
static struct rsa_digestinfo_prefix * rsa_find_prefix(struct digest_algorithm *digest)
Identify RSA prefix.
Definition: rsa.c:79
REQUIRING_SYMBOL(rsa_algorithm)
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
Definition: asn1.c:313
#define EACCES_VERIFY
Definition: rsa.c:45
Big integer support.
bigint_element_t * modulus0
Modulus.
Definition: rsa.c:55
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition: string.c:135
size_t len
Length of data.
Definition: asn1.h:24
uint32_t start
Starting offset.
Definition: netvsc.h:12
unsigned long tmp
Definition: linux_pci.h:63
#define ENOMEM
Not enough space.
Definition: errno.h:534
HMAC_DRBG algorithm.
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.
Definition: rsa.c:148
An RSA context.
Definition: rsa.c:51
ASN.1 encoding.
#define bigint_mod_exp_tmp_len(modulus, exponent)
Calculate temporary working space required for moduluar exponentiation.
Definition: bigint.h:329
static int rsa_match(const struct asn1_cursor *private_key, const struct asn1_cursor *public_key)
Check for matching RSA public/private key pair.
Definition: rsa.c:676
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define DBGC_HDA(...)
Definition: compiler.h:506
unsigned int size
Modulus size.
Definition: rsa.c:57
size_t max_len
Modulus length.
Definition: rsa.c:59
void * dynamic
Allocated memory.
Definition: rsa.c:53
uint32_t bigint_element_t
Element of a big integer.
Definition: bigint.h:15
bigint_element_t * input0
Input buffer.
Definition: rsa.c:65
int asn1_check_algorithm(const struct asn1_cursor *cursor, struct asn1_algorithm *expected)
Check ASN.1 OID-identified algorithm.
Definition: asn1.c:680
__be32 out[4]
Definition: CIB_PRM.h:36
#define bigint_done(value, out, len)
Finalise big integer.
Definition: bigint.h:63
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.
Definition: asn1.c:451
static void rsa_free(struct rsa_context *context)
Free RSA dynamic storage.
Definition: rsa.c:94
#define ERANGE
Result too large.
Definition: errno.h:639
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
Definition: bigint.h:30
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
An RSA digestInfo prefix.
Definition: rsa.h:42
unsigned char uint8_t
Definition: stdint.h:10
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition: asn1.h:89
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
#define ASN1_INTEGER
ASN.1 integer.
Definition: asn1.h:62
bigint_element_t * exponent0
Exponent.
Definition: rsa.c:61
long pad_len
Definition: bigint.h:30
RSA public-key cryptography.
static volatile void * bits
Definition: bitops.h:27
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
size_t digestsize
Digest size.
Definition: crypto.h:26
const char * name
Algorithm name.
Definition: crypto.h:20
int asn1_skip(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object.
Definition: asn1.c:254
struct pubkey_algorithm rsa_algorithm
RSA public-key algorithm.
Definition: rsa.c:700
A message digest algorithm.
Definition: crypto.h:18
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
static int rsa_decrypt(const struct asn1_cursor *key, const void *ciphertext, size_t ciphertext_len, void *plaintext)
Decrypt using RSA.
Definition: rsa.c:420
A private key.
Definition: privkey.h:16
struct digest_algorithm * digest
Digest algorithm.
Definition: rsa.h:44
__be32 raw[7]
Definition: CIB_PRM.h:28
static int rsa_verify(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const void *signature, size_t signature_len)
Verify signed digest value using RSA.
Definition: rsa.c:602
static size_t rsa_max_len(const struct asn1_cursor *key)
Calculate RSA maximum output length.
Definition: rsa.c:301
REQUIRE_OBJECT(config_crypto)
static int rsa_init(struct rsa_context *context, const struct asn1_cursor *key)
Initialise RSA cipher.
Definition: rsa.c:256
static int rsa_encrypt(const struct asn1_cursor *key, const void *plaintext, size_t plaintext_len, void *ciphertext)
Encrypt using RSA.
Definition: rsa.c:350
#define ASN1_OCTET_STRING
ASN.1 octet string.
Definition: asn1.h:68
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
An ASN.1 object cursor.
Definition: asn1.h:20
A public key algorithm.
Definition: crypto.h:121
static int rsa_alloc(struct rsa_context *context, size_t modulus_len, size_t exponent_len)
Allocate RSA dynamic storage.
Definition: rsa.c:107
union @383 key
Sense key.
Definition: scsi.h:18
const char * name
Algorithm name.
Definition: crypto.h:123
typedef bigint_t(X25519_SIZE) x25519_t
An X25519 unsigned big integer used in internal calculations.
An ASN.1 bit string.
Definition: asn1.h:420
void * memset(void *dest, int character, size_t len) __nonnull
#define RSA_DIGESTINFO_PREFIXES
RSA digestInfo prefix table.
Definition: rsa.h:52