port.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/randombytes.h>
  37. #include <sodium/crypto_auth_hmacsha256.h>
  38. #include <sodium/crypto_secretbox.h>
  39. #include <sodium/crypto_box_curve25519xsalsa20poly1305.h>
  40. #if crypto_secretbox_xsalsa20poly1305_KEYBYTES != MACAROON_SECRET_KEY_BYTES
  41. #error set your constants right
  42. #endif
  43. #if crypto_secretbox_xsalsa20poly1305_NONCEBYTES != MACAROON_SECRET_NONCE_BYTES
  44. #error set your constants right
  45. #endif
  46. #if crypto_secretbox_xsalsa20poly1305_ZEROBYTES != MACAROON_SECRET_TEXT_ZERO_BYTES
  47. #error set your constants right
  48. #endif
  49. #if crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES != MACAROON_SECRET_BOX_ZERO_BYTES
  50. #error set your constants right
  51. #endif
  52. /* So why this port file? Why add a level of indirection? It makes the API
  53. * consistent with the coding style throughout the rest of the code. A reader
  54. * familiar with the macaroons code can intuitively follow the primitives,
  55. * without needing to understand the sodium API.
  56. *
  57. * As a bonus, it makes it ridiculously easy to swap out sodium for something a
  58. * little more hipstery, like TweetNACL if that's your thing.
  59. */
  60. void
  61. explicit_bzero(void *buf, size_t len);
  62. void
  63. macaroon_memzero(void* data, size_t data_sz)
  64. {
  65. explicit_bzero(data, data_sz);
  66. }
  67. int
  68. timingsafe_bcmp(const void *b1, const void *b2, size_t n);
  69. int
  70. macaroon_memcmp(const void* data1, const void* data2, size_t data_sz)
  71. {
  72. return timingsafe_bcmp(data1, data2, data_sz);
  73. }
  74. int
  75. macaroon_randombytes(void* data, const size_t data_sz)
  76. {
  77. randombytes((unsigned char*)data, data_sz);
  78. return 0;
  79. }
  80. int
  81. macaroon_hmac(const unsigned char* _key, size_t _key_sz,
  82. const unsigned char* text, size_t text_sz,
  83. unsigned char* hash)
  84. {
  85. unsigned char key[MACAROON_HASH_BYTES];
  86. explicit_bzero(key, MACAROON_HASH_BYTES);
  87. memmove(key, _key, _key_sz < sizeof(key) ? _key_sz : sizeof(key));
  88. crypto_auth_hmacsha256(hash, text, text_sz, key);
  89. return 0;
  90. }
  91. int
  92. macaroon_secretbox(const unsigned char* enc_key,
  93. const unsigned char* enc_nonce,
  94. const unsigned char* plaintext, size_t plaintext_sz,
  95. unsigned char* ciphertext)
  96. {
  97. return crypto_secretbox_xsalsa20poly1305(ciphertext, plaintext, plaintext_sz, enc_nonce, enc_key);
  98. }
  99. int
  100. macaroon_secretbox_open(const unsigned char* enc_key,
  101. const unsigned char* enc_nonce,
  102. const unsigned char* ciphertext, size_t ciphertext_sz,
  103. unsigned char* plaintext)
  104. {
  105. return crypto_secretbox_xsalsa20poly1305_open(plaintext, ciphertext, ciphertext_sz, enc_nonce, enc_key);
  106. }
  107. void
  108. macaroon_bin2hex(const unsigned char* bin, size_t bin_sz, char* hex)
  109. {
  110. static const char hexes[] = "0123456789abcdef";
  111. size_t i;
  112. for (i = 0; i < bin_sz; ++i)
  113. {
  114. hex[2 * i + 0] = hexes[(bin[i] >> 4) & 0xfu];
  115. hex[2 * i + 1] = hexes[bin[i] & 0xfU];
  116. }
  117. hex[2 * bin_sz] = '\0';
  118. }
  119. int
  120. macaroon_hex2bin(const char* hex, size_t hex_sz, unsigned char* bin)
  121. {
  122. size_t idx = 0;
  123. static const char bet[] = "0123456789abcdef";
  124. const char* tmp = NULL;
  125. unsigned byte;
  126. if(hex_sz & 1)
  127. {
  128. return -1;
  129. }
  130. for (idx = 0; idx < hex_sz; idx += 2)
  131. {
  132. byte = 0;
  133. tmp = strchr(bet, hex[idx]);
  134. if (!tmp)
  135. {
  136. return -1;
  137. }
  138. byte |= tmp - bet;
  139. byte <<= 4;
  140. tmp = strchr(bet, hex[idx + 1]);
  141. if (!tmp)
  142. {
  143. return -1;
  144. }
  145. byte |= tmp - bet;
  146. bin[idx >> 1] = byte & 0xffU;
  147. }
  148. return 0;
  149. }