Isabelle's Lazy Message Protocol
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

110 lignes
2.6 KiB

  1. // namespacing
  2. use crate::Packet;
  3. use crate::Result;
  4. use orion::aead::{self, SecretKey};
  5. use ring::digest;
  6. /// trait that allows for me to be lazy
  7. pub trait Encryption {
  8. /// return the encryption kind
  9. fn kind(&self) -> EncryptKind;
  10. /// returns Option<SecretKey>
  11. fn key(&self) -> Option<&SecretKey>;
  12. /// encrypts the packet contents and updates the integrity hash
  13. fn encrypt(&self, packet: &mut Packet) -> Result<()>;
  14. /// decrypts the packet contents, should only be used after integrity is
  15. /// validated
  16. fn decrypt(&self, packet: &mut Packet) -> Result<()>;
  17. }
  18. /// uses ring's aead module
  19. pub struct SymmetricEncrypt(SecretKey);
  20. impl Encryption for SymmetricEncrypt {
  21. fn kind(&self) -> EncryptKind {
  22. EncryptKind::Symmetric
  23. }
  24. fn key(&self) -> Option<&SecretKey> {
  25. Some(&self.0)
  26. }
  27. fn encrypt(&self, packet: &mut Packet) -> Result<()> {
  28. packet.contents = aead::seal(self.key().unwrap(), &packet.contents)?;
  29. packet.integrity_hash = digest::digest(&digest::SHA256, &packet.contents)
  30. .as_ref()
  31. .to_vec();
  32. Ok(())
  33. }
  34. fn decrypt(&self, packet: &mut Packet) -> Result<()> {
  35. packet.contents = aead::open(self.key().unwrap(), &packet.contents)?;
  36. Ok(())
  37. }
  38. }
  39. impl SymmetricEncrypt {
  40. /// creates a new symmetric encryption key wrapper struct
  41. pub fn new(key: SecretKey) -> SymmetricEncrypt {
  42. SymmetricEncrypt(key)
  43. }
  44. #[doc(hidden)]
  45. /// dear future izzy, this is a really bad idea
  46. pub fn clone(&self) -> Result<SymmetricEncrypt> {
  47. Ok(SymmetricEncrypt::new(aead::SecretKey::from_slice(
  48. self.0.unprotected_as_bytes(),
  49. )?))
  50. }
  51. }
  52. /// literally not encryption whatsoever
  53. pub struct NoEncrypt;
  54. impl NoEncrypt {
  55. /// why
  56. pub fn new() -> NoEncrypt {
  57. NoEncrypt
  58. }
  59. }
  60. impl Encryption for NoEncrypt {
  61. fn kind(&self) -> EncryptKind {
  62. EncryptKind::None
  63. }
  64. // lol
  65. fn key(&self) -> Option<&SecretKey> {
  66. None
  67. }
  68. // lol
  69. fn encrypt(&self, _packet: &mut Packet) -> Result<()> {
  70. Ok(())
  71. }
  72. // lol
  73. fn decrypt(&self, _packet: &mut Packet) -> Result<()> {
  74. Ok(())
  75. }
  76. }
  77. /// encryption kind
  78. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  79. #[repr(u8)]
  80. pub enum EncryptKind {
  81. None = 0x00,
  82. Symmetric = 0xff,
  83. }
  84. impl EncryptKind {
  85. /// returns `EncryptKind` from u8 if returned value is valid
  86. pub fn from_u8(kind: u8) -> Option<EncryptKind> {
  87. match kind {
  88. 0x00 => Some(EncryptKind::None),
  89. 0xff => Some(EncryptKind::Symmetric),
  90. _ => None,
  91. }
  92. }
  93. }