dsa_export.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key)
  14. {
  15. unsigned long y, z;
  16. int err;
  17. _ARGCHK(out != NULL);
  18. _ARGCHK(outlen != NULL);
  19. _ARGCHK(key != NULL);
  20. /* can we store the static header? */
  21. if (*outlen < (PACKET_SIZE + 1 + 2)) {
  22. return CRYPT_BUFFER_OVERFLOW;
  23. }
  24. if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
  25. return CRYPT_PK_TYPE_MISMATCH;
  26. }
  27. if (type != PK_PUBLIC && type != PK_PRIVATE) {
  28. return CRYPT_INVALID_ARG;
  29. }
  30. /* store header */
  31. packet_store_header(out, PACKET_SECT_DSA, PACKET_SUB_KEY);
  32. y = PACKET_SIZE;
  33. /* store g, p, q, qord */
  34. out[y++] = type;
  35. out[y++] = (key->qord>>8)&255;
  36. out[y++] = key->qord & 255;
  37. OUTPUT_BIGNUM(&key->g,out,y,z);
  38. OUTPUT_BIGNUM(&key->p,out,y,z);
  39. OUTPUT_BIGNUM(&key->q,out,y,z);
  40. /* public exponent */
  41. OUTPUT_BIGNUM(&key->y,out,y,z);
  42. if (type == PK_PRIVATE) {
  43. OUTPUT_BIGNUM(&key->x,out,y,z);
  44. }
  45. *outlen = y;
  46. return CRYPT_OK;
  47. }
  48. #endif