box.c 885 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "crypto_onetimeauth_poly1305.h"
  2. #include "crypto_stream_xsalsa20.h"
  3. #include "crypto_secretbox.h"
  4. int crypto_secretbox(
  5. unsigned char *c,
  6. const unsigned char *m,unsigned long long mlen,
  7. const unsigned char *n,
  8. const unsigned char *k
  9. )
  10. {
  11. int i;
  12. if (mlen < 32) return -1;
  13. crypto_stream_xsalsa20_xor(c,m,mlen,n,k);
  14. crypto_onetimeauth_poly1305(c + 16,c + 32,mlen - 32,c);
  15. for (i = 0;i < 16;++i) c[i] = 0;
  16. return 0;
  17. }
  18. int crypto_secretbox_open(
  19. unsigned char *m,
  20. const unsigned char *c,unsigned long long clen,
  21. const unsigned char *n,
  22. const unsigned char *k
  23. )
  24. {
  25. int i;
  26. unsigned char subkey[32];
  27. if (clen < 32) return -1;
  28. crypto_stream_xsalsa20(subkey,32,n,k);
  29. if (crypto_onetimeauth_poly1305_verify(c + 16,c + 32,clen - 32,subkey) != 0) return -1;
  30. crypto_stream_xsalsa20_xor(m,c,clen,n,k);
  31. for (i = 0;i < 32;++i) m[i] = 0;
  32. return 0;
  33. }