dsa_import.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. #include "mycrypt.h"
  12. #ifdef MDSA
  13. int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key)
  14. {
  15. unsigned long x, y;
  16. int err;
  17. _ARGCHK(in != NULL);
  18. _ARGCHK(key != NULL);
  19. /* check length */
  20. if ((1+2+PACKET_SIZE) > inlen) {
  21. return CRYPT_INVALID_PACKET;
  22. }
  23. /* check type */
  24. if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DSA, PACKET_SUB_KEY)) != CRYPT_OK) {
  25. return err;
  26. }
  27. y = PACKET_SIZE;
  28. /* init key */
  29. if (mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL) != MP_OKAY) {
  30. return CRYPT_MEM;
  31. }
  32. /* read type/qord */
  33. key->type = in[y++];
  34. key->qord = ((unsigned)in[y]<<8)|((unsigned)in[y+1]);
  35. y += 2;
  36. /* input publics */
  37. INPUT_BIGNUM(&key->g,in,x,y, inlen);
  38. INPUT_BIGNUM(&key->p,in,x,y, inlen);
  39. INPUT_BIGNUM(&key->q,in,x,y, inlen);
  40. INPUT_BIGNUM(&key->y,in,x,y, inlen);
  41. if (key->type == PK_PRIVATE) {
  42. INPUT_BIGNUM(&key->x,in,x,y, inlen);
  43. }
  44. return CRYPT_OK;
  45. error:
  46. mp_clear_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL);
  47. return err;
  48. }
  49. #endif