You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@teaclave.apache.org by GitBox <gi...@apache.org> on 2020/06/05 14:43:17 UTC

[GitHub] [incubator-teaclave] qinkunbao commented on a change in pull request #339: [cli] Update Teaclave Command Line Tool so it can output cmac

qinkunbao commented on a change in pull request #339:
URL: https://github.com/apache/incubator-teaclave/pull/339#discussion_r435969049



##########
File path: cli/src/main.rs
##########
@@ -71,69 +77,88 @@ struct Opt {
     command: Command,
 }
 
-fn decrypt(opt: EncryptDecryptOpt) -> Result<()> {
+fn decrypt(opt: EncryptDecryptOpt) -> Result<CMac> {
     let key = opt.key;
+    let mut cmac: CMac = [0u8; FILE_AUTH_TAG_LENGTH];
     match opt.algorithm.as_str() {
         AesGcm128Key::SCHEMA => {
             let iv = opt.iv.expect("IV is required.");
             let key = AesGcm128Key::new(&key, &iv)?;
             let mut content = fs::read(opt.input_file)?;
-            key.decrypt(&mut content)?;
+            let res = key.decrypt(&mut content)?;
+            cmac.copy_from_slice(&res);
             fs::write(opt.output_file, content)?;
         }
         AesGcm256Key::SCHEMA => {
             let iv = opt.iv.expect("IV is required.");
             let key = AesGcm256Key::new(&key, &iv)?;
             let mut content = fs::read(opt.input_file)?;
-            key.decrypt(&mut content)?;
+            let res = key.decrypt(&mut content)?;
+            cmac.copy_from_slice(&res);
             fs::write(opt.output_file, content)?;
         }
         TeaclaveFile128Key::SCHEMA => {
             let key = TeaclaveFile128Key::new(&key)?;
             let mut content = vec![];
-            key.decrypt(opt.input_file, &mut content)?;
+            let res = key.decrypt(opt.input_file, &mut content)?;
+            cmac.copy_from_slice(&res);
             fs::write(opt.output_file, content)?;
         }
         _ => bail!("Invalid crypto algorithm"),
     }
 
-    Ok(())
+    Ok(cmac)
 }
 
-fn encrypt(opt: EncryptDecryptOpt) -> Result<()> {
+fn encrypt(opt: EncryptDecryptOpt) -> Result<CMac> {
     let key = opt.key;
+    let mut cmac: CMac = [0u8; FILE_AUTH_TAG_LENGTH];
     match opt.algorithm.as_str() {
         AesGcm128Key::SCHEMA => {
             let iv = opt.iv.expect("IV is required.");
             let key = AesGcm128Key::new(&key, &iv)?;
             let mut content = fs::read(opt.input_file)?;
-            key.encrypt(&mut content)?;
+            let res = key.encrypt(&mut content)?;
+            cmac.copy_from_slice(&res);
             fs::write(opt.output_file, content)?;
         }
         AesGcm256Key::SCHEMA => {
             let iv = opt.iv.expect("IV is required.");
             let key = AesGcm256Key::new(&key, &iv)?;
             let mut content = fs::read(opt.input_file)?;
-            key.encrypt(&mut content)?;
+            let res = key.encrypt(&mut content)?;
+            cmac.copy_from_slice(&res);
             fs::write(opt.output_file, content)?;
         }
         TeaclaveFile128Key::SCHEMA => {
             let key = TeaclaveFile128Key::new(&key)?;
             let content = fs::read(opt.input_file)?;
-            key.encrypt(opt.output_file, &content)?;
+            let res = key.encrypt(opt.output_file, &content)?;
+            cmac.copy_from_slice(&res);
         }
         _ => bail!("Invalid crypto algorithm"),
     }
 
-    Ok(())
+    Ok(cmac)
 }
 
 fn main() -> Result<()> {
     let args = Opt::from_args();
-    match args.command {
-        Command::Decrypt(opt) => decrypt(opt)?,
-        Command::Encrypt(opt) => encrypt(opt)?,
-    }
+    let flag: bool;
+    let cmac = match args.command {
+        Command::Decrypt(opt) => {
+            flag = opt.cmac_flag;
+            decrypt(opt)?
+        }
+        Command::Encrypt(opt) => {
+            flag = opt.cmac_flag;
+            encrypt(opt)?
+        }
+    };
 
+    if flag {
+        let cmac_string = hex::encode(cmac);
+        println!("{}", cmac_string);
+    }

Review comment:
       Thanks for the suggestion. Please see the latest commit.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@teaclave.apache.org
For additional commands, e-mail: notifications-help@teaclave.apache.org