2
0

address.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "impl/aes.h"
  17. #include "impl/ec.h"
  18. #include "impl/misc.h"
  19. #include "impl/types.h"
  20. #include "anode.h"
  21. int AnodeAddress_calc_short_id(
  22. const AnodeAddress *address,
  23. AnodeAddressId *short_address_id)
  24. {
  25. unsigned char digest[16];
  26. switch(AnodeAddress_get_type(address)) {
  27. case ANODE_ADDRESS_ANODE_256_40:
  28. Anode_aes_digest(address->bits,ANODE_ADDRESS_LENGTH_ANODE_256_40,digest);
  29. break;
  30. default:
  31. return ANODE_ERR_ADDRESS_INVALID;
  32. }
  33. *((uint64_t *)short_address_id->bits) = ((uint64_t *)digest)[0] ^ ((uint64_t *)digest)[1];
  34. return 0;
  35. }
  36. int AnodeAddress_get_zone(const AnodeAddress *address,AnodeZone *zone)
  37. {
  38. switch(AnodeAddress_get_type(address)) {
  39. case ANODE_ADDRESS_ANODE_256_40:
  40. *((uint32_t *)&(zone->bits[0])) = *((uint32_t *)&(address->bits[1]));
  41. return 0;
  42. }
  43. return ANODE_ERR_ADDRESS_INVALID;
  44. }
  45. int AnodeAddress_to_string(const AnodeAddress *address,char *buf,int len)
  46. {
  47. const unsigned char *inptr;
  48. char *outptr;
  49. unsigned int i;
  50. switch(AnodeAddress_get_type(address)) {
  51. case ANODE_ADDRESS_ANODE_256_40:
  52. if (len < (((ANODE_ADDRESS_LENGTH_ANODE_256_40 / 5) * 8) + 1))
  53. return ANODE_ERR_BUFFER_TOO_SMALL;
  54. inptr = (const unsigned char *)address->bits;
  55. outptr = buf;
  56. for(i=0;i<(ANODE_ADDRESS_LENGTH_ANODE_256_40 / 5);++i) {
  57. Anode_base32_5_to_8(inptr,outptr);
  58. inptr += 5;
  59. outptr += 8;
  60. }
  61. *outptr = (char)0;
  62. return ((ANODE_ADDRESS_LENGTH_ANODE_256_40 / 5) * 8);
  63. }
  64. return ANODE_ERR_ADDRESS_INVALID;
  65. }
  66. int AnodeAddress_from_string(const char *str,AnodeAddress *address)
  67. {
  68. const char *blk_start = str;
  69. const char *ptr = str;
  70. unsigned int address_len = 0;
  71. while (*ptr) {
  72. if ((unsigned long)(ptr - blk_start) == 8) {
  73. if ((address_len + 5) > sizeof(address->bits))
  74. return ANODE_ERR_ADDRESS_INVALID;
  75. Anode_base32_8_to_5(blk_start,(unsigned char *)&(address->bits[address_len]));
  76. address_len += 5;
  77. blk_start = ptr;
  78. }
  79. ++ptr;
  80. }
  81. if (ptr != blk_start)
  82. return ANODE_ERR_ADDRESS_INVALID;
  83. if (AnodeAddress_get_type(address) != ANODE_ADDRESS_ANODE_256_40)
  84. return ANODE_ERR_ADDRESS_INVALID;
  85. return 0;
  86. }