Joel Galenson | ca0efb1 | 2020-10-01 14:32:30 -0700 | [diff] [blame^] | 1 | // Copyright 2020, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #[cfg(test)] |
| 16 | mod tests { |
| 17 | |
| 18 | use keystore2_crypto_bindgen::{ |
| 19 | generateKeyFromPassword, AES_gcm_decrypt, AES_gcm_encrypt, CreateKeyId, |
| 20 | }; |
| 21 | |
| 22 | #[test] |
| 23 | fn test_encrypt_decrypt() { |
| 24 | let input = vec![0; 16]; |
| 25 | let mut out = vec![0; 16]; |
| 26 | let mut out2 = vec![0; 16]; |
| 27 | let key = vec![0; 16]; |
| 28 | let iv = vec![0; 12]; |
| 29 | let mut tag = vec![0; 16]; |
| 30 | unsafe { |
| 31 | let res = AES_gcm_encrypt( |
| 32 | input.as_ptr(), |
| 33 | out.as_mut_ptr(), |
| 34 | 16, |
| 35 | key.as_ptr(), |
| 36 | 16, |
| 37 | iv.as_ptr(), |
| 38 | tag.as_mut_ptr(), |
| 39 | ); |
| 40 | assert!(res); |
| 41 | assert_ne!(out, input); |
| 42 | assert_ne!(tag, input); |
| 43 | let res = AES_gcm_decrypt( |
| 44 | out.as_ptr(), |
| 45 | out2.as_mut_ptr(), |
| 46 | 16, |
| 47 | key.as_ptr(), |
| 48 | 16, |
| 49 | iv.as_ptr(), |
| 50 | tag.as_ptr(), |
| 51 | ); |
| 52 | assert!(res); |
| 53 | assert_eq!(out2, input); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn test_create_key_id() { |
| 59 | let blob = vec![0; 16]; |
| 60 | let mut out: u64 = 0; |
| 61 | unsafe { |
| 62 | let res = CreateKeyId(blob.as_ptr(), 16, &mut out); |
| 63 | assert!(res); |
| 64 | assert_ne!(out, 0); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn test_generate_key_from_password() { |
| 70 | let mut key = vec![0; 16]; |
| 71 | let pw = vec![0; 16]; |
| 72 | let mut salt = vec![0; 16]; |
| 73 | unsafe { |
| 74 | generateKeyFromPassword(key.as_mut_ptr(), 16, pw.as_ptr(), 16, salt.as_mut_ptr()); |
| 75 | } |
| 76 | assert_ne!(key, vec![0; 16]); |
| 77 | } |
| 78 | } |