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 | |
Joel Galenson | 46d6fd0 | 2020-11-19 17:58:33 -0800 | [diff] [blame] | 15 | // TODO: Once this is complete, remove this and document everything public. |
| 16 | #![allow(missing_docs)] |
| 17 | |
Joel Galenson | ca0efb1 | 2020-10-01 14:32:30 -0700 | [diff] [blame] | 18 | #[cfg(test)] |
| 19 | mod tests { |
| 20 | |
| 21 | use keystore2_crypto_bindgen::{ |
| 22 | generateKeyFromPassword, AES_gcm_decrypt, AES_gcm_encrypt, CreateKeyId, |
| 23 | }; |
| 24 | |
| 25 | #[test] |
| 26 | fn test_encrypt_decrypt() { |
| 27 | let input = vec![0; 16]; |
| 28 | let mut out = vec![0; 16]; |
| 29 | let mut out2 = vec![0; 16]; |
| 30 | let key = vec![0; 16]; |
| 31 | let iv = vec![0; 12]; |
| 32 | let mut tag = vec![0; 16]; |
| 33 | unsafe { |
| 34 | let res = AES_gcm_encrypt( |
| 35 | input.as_ptr(), |
| 36 | out.as_mut_ptr(), |
| 37 | 16, |
| 38 | key.as_ptr(), |
| 39 | 16, |
| 40 | iv.as_ptr(), |
| 41 | tag.as_mut_ptr(), |
| 42 | ); |
| 43 | assert!(res); |
| 44 | assert_ne!(out, input); |
| 45 | assert_ne!(tag, input); |
| 46 | let res = AES_gcm_decrypt( |
| 47 | out.as_ptr(), |
| 48 | out2.as_mut_ptr(), |
| 49 | 16, |
| 50 | key.as_ptr(), |
| 51 | 16, |
| 52 | iv.as_ptr(), |
| 53 | tag.as_ptr(), |
| 54 | ); |
| 55 | assert!(res); |
| 56 | assert_eq!(out2, input); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn test_create_key_id() { |
| 62 | let blob = vec![0; 16]; |
| 63 | let mut out: u64 = 0; |
| 64 | unsafe { |
| 65 | let res = CreateKeyId(blob.as_ptr(), 16, &mut out); |
| 66 | assert!(res); |
| 67 | assert_ne!(out, 0); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn test_generate_key_from_password() { |
| 73 | let mut key = vec![0; 16]; |
| 74 | let pw = vec![0; 16]; |
| 75 | let mut salt = vec![0; 16]; |
| 76 | unsafe { |
| 77 | generateKeyFromPassword(key.as_mut_ptr(), 16, pw.as_ptr(), 16, salt.as_mut_ptr()); |
| 78 | } |
| 79 | assert_ne!(key, vec![0; 16]); |
| 80 | } |
| 81 | } |