port.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (c) 2014, Robert Escriva
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of this project nor the names of its contributors may
  13. * be used to endorse or promote products derived from this software
  14. * without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. /* C */
  32. #include <assert.h>
  33. #include <string.h>
  34. /* macaroons */
  35. #include "port.h"
  36. #include <sodium/utils.h>
  37. #include <sodium/randombytes.h>
  38. #include <sodium/crypto_auth_hmacsha256.h>
  39. #include <sodium/crypto_secretbox.h>
  40. #include <sodium/crypto_box_curve25519xsalsa20poly1305.h>
  41. #if crypto_secretbox_xsalsa20poly1305_KEYBYTES != MACAROON_SECRET_KEY_BYTES
  42. #error set your constants right
  43. #endif
  44. #if crypto_secretbox_xsalsa20poly1305_NONCEBYTES != MACAROON_SECRET_NONCE_BYTES
  45. #error set your constants right
  46. #endif
  47. #if crypto_secretbox_xsalsa20poly1305_ZEROBYTES != MACAROON_SECRET_TEXT_ZERO_BYTES
  48. #error set your constants right
  49. #endif
  50. #if crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES != MACAROON_SECRET_BOX_ZERO_BYTES
  51. #error set your constants right
  52. #endif
  53. /* So why this port file? Why add a level of indirection? It makes the API
  54. * consistent with the coding style throughout the rest of the code. A reader
  55. * familiar with the macaroons code can intuitively follow the primitives,
  56. * without needing to understand the sodium API.
  57. *
  58. * As a bonus, it makes it ridiculously easy to swap out sodium for something a
  59. * little more hipstery, like TweetNACL if that's your thing.
  60. */
  61. void
  62. macaroon_memzero(void* data, size_t data_sz)
  63. {
  64. sodium_memzero(data, data_sz);
  65. }
  66. int
  67. macaroon_memcmp(const void* data1, const void* data2, size_t data_sz)
  68. {
  69. return sodium_memcmp(data1, data2, data_sz);
  70. }
  71. int
  72. macaroon_randombytes(void* data, const size_t data_sz)
  73. {
  74. randombytes((unsigned char*)data, data_sz);
  75. return 0;
  76. }
  77. int
  78. macaroon_hmac(const unsigned char* _key, size_t _key_sz,
  79. const unsigned char* text, size_t text_sz,
  80. unsigned char* hash)
  81. {
  82. unsigned char key[MACAROON_HASH_BYTES];
  83. sodium_memzero(key, MACAROON_HASH_BYTES);
  84. memmove(key, _key, _key_sz < sizeof(key) ? _key_sz : sizeof(key));
  85. crypto_auth_hmacsha256(hash, text, text_sz, key);
  86. return 0;
  87. }
  88. int
  89. macaroon_secretbox(const unsigned char* enc_key,
  90. const unsigned char* enc_nonce,
  91. const unsigned char* plaintext, size_t plaintext_sz,
  92. unsigned char* ciphertext)
  93. {
  94. return crypto_secretbox_xsalsa20poly1305(ciphertext, plaintext, plaintext_sz, enc_nonce, enc_key);
  95. }
  96. int
  97. macaroon_secretbox_open(const unsigned char* enc_key,
  98. const unsigned char* enc_nonce,
  99. const unsigned char* ciphertext, size_t ciphertext_sz,
  100. unsigned char* plaintext)
  101. {
  102. return crypto_secretbox_xsalsa20poly1305_open(plaintext, ciphertext, ciphertext_sz, enc_nonce, enc_key);
  103. }
  104. void
  105. macaroon_bin2hex(const unsigned char* bin, size_t bin_sz, char* hex)
  106. {
  107. static const char hexes[] = "0123456789abcdef";
  108. size_t i;
  109. for (i = 0; i < bin_sz; ++i)
  110. {
  111. hex[2 * i + 0] = hexes[(bin[i] >> 4) & 0xfu];
  112. hex[2 * i + 1] = hexes[bin[i] & 0xfU];
  113. }
  114. hex[2 * bin_sz] = '\0';
  115. }
  116. int
  117. macaroon_hex2bin(const char* hex, size_t hex_sz, unsigned char* bin)
  118. {
  119. size_t idx = 0;
  120. static const char bet[] = "0123456789abcdef";
  121. const char* tmp = NULL;
  122. unsigned byte;
  123. if(hex_sz & 1)
  124. {
  125. return -1;
  126. }
  127. for (idx = 0; idx < hex_sz; idx += 2)
  128. {
  129. byte = 0;
  130. tmp = strchr(bet, hex[idx]);
  131. if (!tmp)
  132. {
  133. return -1;
  134. }
  135. byte |= tmp - bet;
  136. byte <<= 4;
  137. tmp = strchr(bet, hex[idx + 1]);
  138. if (!tmp)
  139. {
  140. return -1;
  141. }
  142. byte |= tmp - bet;
  143. bin[idx >> 1] = byte & 0xffU;
  144. }
  145. return 0;
  146. }