identity.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009-2010 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU 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, see <http://www.gnu.org/licenses/>. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include "impl/types.h"
  19. #include "impl/ec.h"
  20. #include "impl/misc.h"
  21. #include "anode.h"
  22. int AnodeIdentity_generate(AnodeIdentity *identity,const AnodeZone *zone,enum AnodeAddressType type)
  23. {
  24. struct AnodeECKeyPair kp;
  25. switch(type) {
  26. case ANODE_ADDRESS_ANODE_256_40:
  27. if (!AnodeECKeyPair_generate(&kp))
  28. return ANODE_ERR_OUT_OF_MEMORY;
  29. identity->address.bits[0] = (unsigned char)ANODE_ADDRESS_ANODE_256_40;
  30. identity->address.bits[1] = zone->bits[0];
  31. identity->address.bits[2] = zone->bits[1];
  32. identity->address.bits[3] = zone->bits[2];
  33. identity->address.bits[4] = zone->bits[3];
  34. identity->address.bits[5] = 0;
  35. identity->address.bits[6] = 0;
  36. Anode_memcpy((void *)&(identity->address.bits[7]),(const void *)kp.pub.key,ANODE_EC_PUBLIC_KEY_BYTES);
  37. Anode_memcpy((void *)identity->secret,(const void *)kp.priv.key,kp.priv.bytes);
  38. AnodeAddress_calc_short_id(&identity->address,&identity->address_id);
  39. AnodeECKeyPair_destroy(&kp);
  40. return 0;
  41. }
  42. return ANODE_ERR_INVALID_ARGUMENT;
  43. }
  44. int AnodeIdentity_to_string(const AnodeIdentity *identity,char *dest,int dest_len)
  45. {
  46. char hexbuf[128];
  47. char strbuf[128];
  48. int n;
  49. if ((n = AnodeAddress_to_string(&identity->address,strbuf,sizeof(strbuf))) <= 0)
  50. return n;
  51. switch(AnodeAddress_get_type(&identity->address)) {
  52. case ANODE_ADDRESS_ANODE_256_40:
  53. Anode_to_hex((const unsigned char *)identity->secret,ANODE_ADDRESS_SECRET_LENGTH_ANODE_256_40,hexbuf,sizeof(hexbuf));
  54. n = snprintf(dest,dest_len,"ANODE-256-40:%s:%s",strbuf,hexbuf);
  55. if (n >= dest_len)
  56. return ANODE_ERR_BUFFER_TOO_SMALL;
  57. return n;
  58. }
  59. return ANODE_ERR_INVALID_ARGUMENT;
  60. }
  61. int AnodeIdentity_from_string(AnodeIdentity *identity,const char *str)
  62. {
  63. char buf[1024];
  64. char *id_name;
  65. char *address;
  66. char *secret;
  67. int ec;
  68. Anode_str_copy(buf,str,sizeof(buf));
  69. id_name = buf;
  70. if (!id_name) return 0;
  71. if (!*id_name) return 0;
  72. address = (char *)Anode_strchr(id_name,':');
  73. if (!address) return 0;
  74. if (!*address) return 0;
  75. *(address++) = (char)0;
  76. secret = (char *)Anode_strchr(address,':');
  77. if (!secret) return 0;
  78. if (!*secret) return 0;
  79. *(secret++) = (char)0;
  80. if (Anode_strcaseeq("ANODE-256-40",id_name)) {
  81. if ((ec = AnodeAddress_from_string(address,&identity->address)))
  82. return ec;
  83. if (Anode_strlen(secret) != (ANODE_ADDRESS_SECRET_LENGTH_ANODE_256_40 * 2))
  84. return ANODE_ERR_INVALID_ARGUMENT;
  85. Anode_from_hex(secret,(unsigned char *)identity->secret,sizeof(identity->secret));
  86. AnodeAddress_calc_short_id(&identity->address,&identity->address_id);
  87. return 0;
  88. }
  89. return ANODE_ERR_INVALID_ARGUMENT;
  90. }