libsodium.vapi 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Vala Bindings for LibSodium
  2. * Copyright (c) 2020 Billy Barrow <billyb@pcthingz.com>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. [CCode (cheader_filename = "sodium.h", lower_case_cprefix = "sodium_")]
  17. namespace Sodium {
  18. namespace Random {
  19. [CCode (cname = "randombytes_SEEDBYTES")]
  20. public const size_t SEED_BYTES;
  21. [CCode (cname = "randombytes_random")]
  22. public uint32 random();
  23. [CCode (cname = "randombytes_uniform")]
  24. public uint32 random_uniform(uint32 upper_bound);
  25. [CCode (cname = "randombytes_buf")]
  26. public void random_bytes(uint8[] buffer);
  27. [CCode (cname = "randombytes_buf_deterministic")]
  28. public void random_bytes_deterministic(uint8[] buffer, uint8[] seed);
  29. }
  30. namespace Symmetric {
  31. [CCode (cname = "crypto_secretbox_KEYBYTES")]
  32. public const size_t KEY_BYTES;
  33. [CCode (cname = "crypto_secretbox_NONCEBYTES")]
  34. public const size_t NONCE_BYTES;
  35. [CCode (cname = "crypto_secretbox_MACBYTES")]
  36. public const size_t MAC_BYTES;
  37. [CCode (cname = "crypto_secretbox_keygen")]
  38. private void key_gen([CCode (array_length = false)]uint8[] key);
  39. public uint8[] generate_key() {
  40. uint8[] key = new uint8[KEY_BYTES];
  41. key_gen(key);
  42. return key;
  43. }
  44. [CCode (cname = "crypto_secretbox_easy")]
  45. private void secretbox(
  46. [CCode (array_length = false)]uint8[] ciphertext,
  47. uint8[] message,
  48. [CCode (array_length = false)]uint8[] nonce,
  49. [CCode (array_length = false)]uint8[] key
  50. );
  51. public uint8[] encrypt(uint8[] message, uint8[] key, uint8[] nonce)
  52. requires (key.length == KEY_BYTES)
  53. requires (nonce.length == NONCE_BYTES)
  54. {
  55. // Initialise array for ciphertext
  56. size_t ciphertext_size = MAC_BYTES + message.length;
  57. uint8[] ciphertext = new uint8[ciphertext_size];
  58. // Encrypt
  59. secretbox(ciphertext, message, nonce, key);
  60. // Return ciphertext
  61. return ciphertext;
  62. }
  63. [CCode (cname = "crypto_secretbox_open_easy")]
  64. private int secretbox_open(
  65. [CCode (array_length = false)]uint8[] message,
  66. uint8[] ciphertext,
  67. [CCode (array_length = false)]uint8[] nonce,
  68. [CCode (array_length = false)]uint8[] key
  69. );
  70. public uint8[]? decrypt(uint8[] ciphertext, uint8[] key, uint8[] nonce)
  71. requires (ciphertext.length > MAC_BYTES)
  72. requires (key.length == KEY_BYTES)
  73. requires (nonce.length == NONCE_BYTES)
  74. {
  75. // Initialise array for message
  76. size_t message_size = ciphertext.length - MAC_BYTES;
  77. uint8[] message = new uint8[message_size];
  78. // Decrypt
  79. int status = secretbox_open(message, ciphertext, nonce, key);
  80. // Did it work?
  81. if(status != 0) {
  82. // No, return null
  83. return null;
  84. }
  85. return message;
  86. }
  87. }
  88. namespace Asymmetric {
  89. namespace Signing {
  90. [CCode (cname = "crypto_sign_PUBLICKEYBYTES")]
  91. public const size_t PUBLIC_KEY_BYTES;
  92. [CCode (cname = "crypto_sign_SECRETKEYBYTES")]
  93. public const size_t SECRET_KEY_BYTES;
  94. [CCode (cname = "crypto_sign_BYTES")]
  95. public const size_t MAX_HEADER_BYTES;
  96. [CCode (cname = "crypto_sign_keypair")]
  97. public void generate_keypair(
  98. [CCode (array_length = false)]uint8[] public_key,
  99. [CCode (array_length = false)]uint8[] secret_key)
  100. requires (public_key.length == PUBLIC_KEY_BYTES)
  101. requires (secret_key.length == SECRET_KEY_BYTES);
  102. [CCode (cname = "crypto_sign")]
  103. private void sign_message(
  104. [CCode (array_length = false)] uint8[] signed_message,
  105. out int signature_length,
  106. uint8[] message,
  107. [CCode (array_length = false)] uint8[] secret_key
  108. );
  109. public uint8[] sign(
  110. uint8[] message,
  111. uint8[] secret_key)
  112. requires (secret_key.length == SECRET_KEY_BYTES)
  113. {
  114. int signature_length;
  115. uint8[] signed_message = new uint8[MAX_HEADER_BYTES + message.length];
  116. sign_message(signed_message, out signature_length, message, secret_key);
  117. signed_message.resize(signature_length);
  118. return signed_message;
  119. }
  120. [CCode (cname = "crypto_sign_open")]
  121. private int sign_open(
  122. [CCode (array_length = false)] uint8[] message,
  123. out int message_length,
  124. uint8[] signed_message,
  125. [CCode (array_length = false)] uint8[] public_key
  126. );
  127. public uint8[]? verify(
  128. uint8[] signed_message,
  129. uint8[] public_key)
  130. requires (public_key.length == PUBLIC_KEY_BYTES)
  131. {
  132. int message_length;
  133. uint8[] message = new uint8[signed_message.length];
  134. if(sign_open(message, out message_length, signed_message, public_key) != 0) {
  135. return null;
  136. }
  137. message.resize(message_length);
  138. return message;
  139. }
  140. }
  141. namespace Sealing {
  142. [CCode (cname = "crypto_box_PUBLICKEYBYTES")]
  143. public const size_t PUBLIC_KEY_BYTES;
  144. [CCode (cname = "crypto_box_SECRETKEYBYTES")]
  145. public const size_t SECRET_KEY_BYTES;
  146. [CCode (cname = "crypto_box_SEALBYTES")]
  147. public const size_t HEADER_BYTES;
  148. [CCode (cname = "crypto_box_keypair")]
  149. public void generate_keypair(
  150. [CCode (array_length = false)]uint8[] public_key,
  151. [CCode (array_length = false)]uint8[] secret_key)
  152. requires (public_key.length == PUBLIC_KEY_BYTES)
  153. requires (secret_key.length == SECRET_KEY_BYTES);
  154. [CCode (cname = "crypto_box_seal")]
  155. private void seal_message(
  156. [CCode (array_length = false)] uint8[] ciphertext,
  157. uint8[] message,
  158. [CCode (array_length = false)] uint8[] public_key
  159. );
  160. public uint8[] seal(uint8[] message, uint8[] public_key)
  161. requires (public_key.length == PUBLIC_KEY_BYTES)
  162. {
  163. uint8[] ciphertext = new uint8[HEADER_BYTES + message.length];
  164. seal_message(ciphertext, message, public_key);
  165. return ciphertext;
  166. }
  167. [CCode (cname = "crypto_box_seal_open")]
  168. private int seal_open(
  169. [CCode (array_length = false)] uint8[] message,
  170. uint8[] ciphertext,
  171. [CCode (array_length = false)] uint8[] public_key,
  172. [CCode (array_length = false)] uint8[] secret_key
  173. );
  174. public uint8[]? unseal(
  175. uint8[] ciphertext,
  176. uint8[] public_key,
  177. uint8[] secret_key)
  178. requires (public_key.length == PUBLIC_KEY_BYTES)
  179. requires (secret_key.length == SECRET_KEY_BYTES)
  180. requires (ciphertext.length > HEADER_BYTES)
  181. {
  182. uint8[] message = new uint8[ciphertext.length - HEADER_BYTES];
  183. if(seal_open(message, ciphertext, public_key, secret_key) != 0){
  184. return null;
  185. }
  186. return message;
  187. }
  188. }
  189. }
  190. }