1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.
//! AES.
use capsules_core::driver;
/// Syscall driver number.
pub const DRIVER_NUM: usize = driver::NUM::Aes as usize;
use core::cell::Cell;
use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil::symmetric_encryption::{
AES128Ctr, CCMClient, Client, GCMClient, AES128, AES128CBC, AES128CCM, AES128ECB, AES128GCM,
AES128_BLOCK_SIZE,
};
use kernel::processbuffer::{ReadableProcessBuffer, WriteableProcessBuffer};
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::{ErrorCode, ProcessId};
/// Ids for read-only allow buffers
mod ro_allow {
pub const KEY: usize = 0;
pub const IV: usize = 1;
pub const SOURCE: usize = 2;
/// The number of allow buffers the kernel stores for this grant
pub const COUNT: u8 = 3;
}
/// Ids for read-write allow buffers
mod rw_allow {
pub const DEST: usize = 0;
/// The number of allow buffers the kernel stores for this grant
pub const COUNT: u8 = 1;
}
pub struct AesDriver<'a, A: AES128<'a> + AES128CCM<'static> + AES128GCM<'static>> {
aes: &'a A,
active: Cell<bool>,
apps: Grant<
App,
UpcallCount<1>,
AllowRoCount<{ ro_allow::COUNT }>,
AllowRwCount<{ rw_allow::COUNT }>,
>,
processid: OptionalCell<ProcessId>,
source_buffer: TakeCell<'static, [u8]>,
data_copied: Cell<usize>,
dest_buffer: TakeCell<'static, [u8]>,
}
impl<
A: AES128<'static>
+ AES128Ctr
+ AES128CBC
+ AES128ECB
+ AES128CCM<'static>
+ AES128GCM<'static>,
> AesDriver<'static, A>
{
pub fn new(
aes: &'static A,
source_buffer: &'static mut [u8],
dest_buffer: &'static mut [u8],
grant: Grant<
App,
UpcallCount<1>,
AllowRoCount<{ ro_allow::COUNT }>,
AllowRwCount<{ rw_allow::COUNT }>,
>,
) -> AesDriver<'static, A> {
AesDriver {
aes,
active: Cell::new(false),
apps: grant,
processid: OptionalCell::empty(),
source_buffer: TakeCell::new(source_buffer),
data_copied: Cell::new(0),
dest_buffer: TakeCell::new(dest_buffer),
}
}
fn run(&self) -> Result<(), ErrorCode> {
self.processid.map_or(Err(ErrorCode::RESERVE), |processid| {
self.apps
.enter(processid, |app, kernel_data| {
self.aes.enable();
match app.aes_operation {
Some(AesOperation::AES128Ctr(encrypt)) => {
self.aes.set_mode_aes128ctr(encrypt)?
}
Some(AesOperation::AES128CBC(encrypt)) => {
self.aes.set_mode_aes128cbc(encrypt)?
}
Some(AesOperation::AES128ECB(encrypt)) => {
self.aes.set_mode_aes128ecb(encrypt)?
}
Some(AesOperation::AES128CCM(_encrypt)) => {}
Some(AesOperation::AES128GCM(_encrypt)) => {}
_ => return Err(ErrorCode::INVAL),
}
kernel_data
.get_readonly_processbuffer(ro_allow::KEY)
.and_then(|key| {
key.enter(|key| {
let mut static_buffer_len = 0;
self.source_buffer.map_or(Err(ErrorCode::NOMEM), |buf| {
// Determine the size of the static buffer we have
static_buffer_len = buf.len();
if static_buffer_len > key.len() {
static_buffer_len = key.len()
}
// Copy the data into the static buffer
key[..static_buffer_len]
.copy_to_slice(&mut buf[..static_buffer_len]);
if let Some(op) = app.aes_operation.as_ref() {
match op {
AesOperation::AES128Ctr(_)
| AesOperation::AES128CBC(_)
| AesOperation::AES128ECB(_) => {
AES128::set_key(self.aes, buf)?;
Ok(())
}
AesOperation::AES128CCM(_) => {
AES128CCM::set_key(self.aes, buf)?;
Ok(())
}
AesOperation::AES128GCM(_) => {
AES128GCM::set_key(self.aes, buf)?;
Ok(())
}
}
} else {
Err(ErrorCode::FAIL)
}
})
})
})
.unwrap_or(Err(ErrorCode::RESERVE))?;
kernel_data
.get_readonly_processbuffer(ro_allow::IV)
.and_then(|iv| {
iv.enter(|iv| {
let mut static_buffer_len = 0;
self.source_buffer.map_or(Err(ErrorCode::NOMEM), |buf| {
// Determine the size of the static buffer we have
static_buffer_len = buf.len();
if static_buffer_len > iv.len() {
static_buffer_len = iv.len()
}
// Copy the data into the static buffer
iv[..static_buffer_len]
.copy_to_slice(&mut buf[..static_buffer_len]);
if let Some(op) = app.aes_operation.as_ref() {
match op {
AesOperation::AES128Ctr(_)
| AesOperation::AES128CBC(_)
| AesOperation::AES128ECB(_) => {
AES128::set_iv(self.aes, buf)?;
Ok(())
}
AesOperation::AES128CCM(_) => {
AES128CCM::set_nonce(self.aes, &buf[0..13])?;
Ok(())
}
AesOperation::AES128GCM(_) => {
AES128GCM::set_iv(self.aes, &buf[0..13])?;
Ok(())
}
}
} else {
Err(ErrorCode::FAIL)
}
})
})
})
.unwrap_or(Err(ErrorCode::RESERVE))?;
kernel_data
.get_readonly_processbuffer(ro_allow::SOURCE)
.and_then(|source| {
source.enter(|source| {
let mut static_buffer_len = 0;
if let Some(op) = app.aes_operation.as_ref() {
match op {
AesOperation::AES128Ctr(_)
| AesOperation::AES128CBC(_)
| AesOperation::AES128ECB(_) => {
self.source_buffer.map_or(
Err(ErrorCode::NOMEM),
|buf| {
// Determine the size of the static buffer we have
static_buffer_len = buf.len();
if static_buffer_len > source.len() {
static_buffer_len = source.len()
}
// Copy the data into the static buffer
source[..static_buffer_len].copy_to_slice(
&mut buf[..static_buffer_len],
);
self.data_copied.set(static_buffer_len);
Ok(())
},
)?;
}
AesOperation::AES128CCM(_) => {
self.dest_buffer.map_or(
Err(ErrorCode::NOMEM),
|buf| {
// Determine the size of the static buffer we have
static_buffer_len = buf.len();
if static_buffer_len > source.len() {
static_buffer_len = source.len()
}
// Copy the data into the static buffer
source[..static_buffer_len].copy_to_slice(
&mut buf[..static_buffer_len],
);
self.data_copied.set(static_buffer_len);
Ok(())
},
)?;
}
AesOperation::AES128GCM(_) => {
self.dest_buffer.map_or(
Err(ErrorCode::NOMEM),
|buf| {
// Determine the size of the static buffer we have
static_buffer_len = buf.len();
if static_buffer_len > source.len() {
static_buffer_len = source.len()
}
// Copy the data into the static buffer
source[..static_buffer_len].copy_to_slice(
&mut buf[..static_buffer_len],
);
self.data_copied.set(static_buffer_len);
Ok(())
},
)?;
}
}
self.calculate_output(
op,
app.aoff.get(),
app.moff.get(),
app.mlen.get(),
app.mic_len.get(),
app.confidential.get(),
)?;
Ok(())
} else {
Err(ErrorCode::FAIL)
}
})
})
.unwrap_or(Err(ErrorCode::RESERVE))?;
Ok(())
})
.unwrap_or_else(|err| Err(err.into()))
})
}
fn calculate_output(
&self,
op: &AesOperation,
aoff: usize,
moff: usize,
mlen: usize,
mic_len: usize,
confidential: bool,
) -> Result<(), ErrorCode> {
match op {
AesOperation::AES128Ctr(_)
| AesOperation::AES128CBC(_)
| AesOperation::AES128ECB(_) => {
if let Some(dest_buf) = self.dest_buffer.take() {
if let Some((e, source, dest)) = AES128::crypt(
self.aes,
self.source_buffer.take(),
dest_buf,
0,
AES128_BLOCK_SIZE,
) {
// Error, clear the processid and data
self.aes.disable();
self.processid.clear();
if let Some(source_buf) = source {
self.source_buffer.replace(source_buf);
}
self.dest_buffer.replace(dest);
return e;
}
} else {
return Err(ErrorCode::FAIL);
}
}
AesOperation::AES128CCM(encrypting) => {
if let Some(buf) = self.dest_buffer.take() {
if let Err((e, dest)) = AES128CCM::crypt(
self.aes,
buf,
aoff,
moff,
mlen,
mic_len,
confidential,
*encrypting,
) {
// Error, clear the processid and data
self.aes.disable();
self.processid.clear();
self.dest_buffer.replace(dest);
return Err(e);
}
} else {
return Err(ErrorCode::FAIL);
}
}
AesOperation::AES128GCM(encrypting) => {
if let Some(buf) = self.dest_buffer.take() {
if let Err((e, dest)) =
AES128GCM::crypt(self.aes, buf, aoff, moff, mlen, *encrypting)
{
// Error, clear the appid and data
self.aes.disable();
self.processid.clear();
self.dest_buffer.replace(dest);
return Err(e);
}
} else {
return Err(ErrorCode::FAIL);
}
}
}
Ok(())
}
fn check_queue(&self) {
for appiter in self.apps.iter() {
let started_command = appiter.enter(|app, _| {
// If an app is already running let it complete
if self.processid.is_some() {
return true;
}
// If this app has a pending command let's use it.
app.pending_run_app.take().map_or(false, |processid| {
// Mark this driver as being in use.
self.processid.set(processid);
// Actually make the buzz happen.
self.run() == Ok(())
})
});
if started_command {
break;
}
}
}
}
impl<
'a,
A: AES128<'static>
+ AES128Ctr
+ AES128CBC
+ AES128ECB
+ AES128CCM<'static>
+ AES128GCM<'static>,
> Client<'static> for AesDriver<'static, A>
{
fn crypt_done(&'a self, source: Option<&'static mut [u8]>, destination: &'static mut [u8]) {
if let Some(source_buf) = source {
self.source_buffer.replace(source_buf);
}
self.dest_buffer.replace(destination);
self.processid.map(|id| {
self.apps
.enter(id, |app, kernel_data| {
let mut data_len = 0;
let mut exit = false;
let mut static_buffer_len = 0;
let source_len = kernel_data
.get_readonly_processbuffer(ro_allow::SOURCE)
.map_or(0, |source| source.len());
let subtract = self
.source_buffer
.map_or(0, |buf| core::cmp::min(buf.len(), source_len));
self.dest_buffer.map(|buf| {
let ret = kernel_data
.get_readwrite_processbuffer(rw_allow::DEST)
.and_then(|dest| {
dest.mut_enter(|dest| {
let offset = self.data_copied.get() - subtract;
let app_len = dest.len();
let static_len = self.source_buffer.map_or(0, |source_buf| {
core::cmp::min(source_buf.len(), buf.len())
});
if app_len < static_len {
if app_len - offset > 0 {
dest[offset..app_len]
.copy_from_slice(&buf[0..(app_len - offset)]);
}
} else {
if offset + static_len <= app_len {
dest[offset..(offset + static_len)]
.copy_from_slice(&buf[0..static_len]);
}
}
})
});
if let Err(e) = ret {
// No data buffer, clear the processid and data
self.aes.disable();
self.processid.clear();
kernel_data.schedule_upcall(0, (e as usize, 0, 0)).ok();
exit = true;
}
});
if exit {
return;
}
self.source_buffer.map(|buf| {
let ret = kernel_data
.get_readonly_processbuffer(ro_allow::SOURCE)
.and_then(|source| {
source.enter(|source| {
// Determine the size of the static buffer we have
static_buffer_len = buf.len();
// Determine how much data we have already copied
let copied_data = self.data_copied.get();
data_len = source.len();
if data_len > copied_data {
let remaining_data = &source[copied_data..];
let remaining_len = data_len - copied_data;
if remaining_len < static_buffer_len {
remaining_data.copy_to_slice(&mut buf[..remaining_len]);
} else {
remaining_data[..static_buffer_len].copy_to_slice(buf);
}
}
Ok(())
})
})
.unwrap_or(Err(ErrorCode::RESERVE));
if let Err(e) = ret {
// No data buffer, clear the processid and data
self.aes.disable();
self.processid.clear();
kernel_data.schedule_upcall(0, (e as usize, 0, 0)).ok();
exit = true;
}
});
if exit {
return;
}
if static_buffer_len > 0 {
let copied_data = self.data_copied.get();
if data_len > copied_data {
// Update the amount of data copied
self.data_copied.set(copied_data + static_buffer_len);
if let Some(op) = app.aes_operation.as_ref() {
if self
.calculate_output(
op,
app.aoff.get(),
app.moff.get(),
app.mlen.get(),
app.mic_len.get(),
app.confidential.get(),
)
.is_err()
{
// Error, clear the processid and data
self.aes.disable();
self.processid.clear();
self.check_queue();
return;
}
}
// Return as we don't want to run the digest yet
return;
}
}
// If we get here we have finished all the crypto operations
kernel_data
.schedule_upcall(0, (0, self.data_copied.get(), 0))
.ok();
self.data_copied.set(0);
})
.map_err(|err| {
if err == kernel::process::Error::NoSuchApp
|| err == kernel::process::Error::InactiveApp
{
self.processid.clear();
}
})
});
}
}
impl<
A: AES128<'static>
+ AES128Ctr
+ AES128CBC
+ AES128ECB
+ AES128CCM<'static>
+ AES128GCM<'static>,
> CCMClient for AesDriver<'static, A>
{
fn crypt_done(&self, buf: &'static mut [u8], res: Result<(), ErrorCode>, tag_is_valid: bool) {
self.dest_buffer.replace(buf);
self.processid.map(|id| {
self.apps
.enter(id, |_, kernel_data| {
let mut exit = false;
if let Err(e) = res {
kernel_data.schedule_upcall(0, (e as usize, 0, 0)).ok();
return;
}
self.dest_buffer.map(|buf| {
let ret = kernel_data
.get_readwrite_processbuffer(rw_allow::DEST)
.and_then(|dest| {
dest.mut_enter(|dest| {
let offset = self.data_copied.get()
- (core::cmp::min(buf.len(), dest.len()));
let app_len = dest.len();
let static_len = buf.len();
if app_len < static_len {
if app_len - offset > 0 {
dest[offset..app_len]
.copy_from_slice(&buf[0..(app_len - offset)]);
}
} else {
if offset + static_len <= app_len {
dest[offset..(offset + static_len)]
.copy_from_slice(&buf[0..static_len]);
}
}
})
});
if let Err(e) = ret {
// No data buffer, clear the processid and data
self.aes.disable();
self.processid.clear();
kernel_data.schedule_upcall(0, (e as usize, 0, 0)).ok();
exit = true;
}
});
if exit {
return;
}
// AES CCM is online only we can't send any more data in, so
// just report what we did to the app.
kernel_data
.schedule_upcall(0, (0, self.data_copied.get(), tag_is_valid as usize))
.ok();
self.data_copied.set(0);
})
.map_err(|err| {
if err == kernel::process::Error::NoSuchApp
|| err == kernel::process::Error::InactiveApp
{
self.processid.clear();
}
})
});
}
}
impl<
A: AES128<'static>
+ AES128Ctr
+ AES128CBC
+ AES128ECB
+ AES128CCM<'static>
+ AES128GCM<'static>,
> GCMClient for AesDriver<'static, A>
{
fn crypt_done(&self, buf: &'static mut [u8], res: Result<(), ErrorCode>, tag_is_valid: bool) {
self.dest_buffer.replace(buf);
self.processid.map(|id| {
self.apps
.enter(id, |_, kernel_data| {
let mut exit = false;
if let Err(e) = res {
kernel_data.schedule_upcall(0, (e as usize, 0, 0)).ok();
return;
}
self.dest_buffer.map(|buf| {
let ret = kernel_data
.get_readwrite_processbuffer(rw_allow::DEST)
.and_then(|dest| {
dest.mut_enter(|dest| {
let offset = self.data_copied.get()
- (core::cmp::min(buf.len(), dest.len()));
let app_len = dest.len();
let static_len = buf.len();
if app_len < static_len {
if app_len - offset > 0 {
dest[offset..app_len]
.copy_from_slice(&buf[0..(app_len - offset)]);
}
} else {
if offset + static_len <= app_len {
dest[offset..(offset + static_len)]
.copy_from_slice(&buf[0..static_len]);
}
}
})
});
if let Err(e) = ret {
// No data buffer, clear the appid and data
self.aes.disable();
self.processid.clear();
kernel_data.schedule_upcall(0, (e as usize, 0, 0)).ok();
exit = true;
}
});
if exit {
return;
}
// AES GCM is online only we can't send any more data in, so
// just report what we did to the app.
kernel_data
.schedule_upcall(0, (0, self.data_copied.get(), tag_is_valid as usize))
.ok();
self.data_copied.set(0);
})
.map_err(|err| {
if err == kernel::process::Error::NoSuchApp
|| err == kernel::process::Error::InactiveApp
{
self.processid.clear();
}
})
});
}
}
impl<
A: AES128<'static>
+ AES128Ctr
+ AES128CBC
+ AES128ECB
+ AES128CCM<'static>
+ AES128GCM<'static>,
> SyscallDriver for AesDriver<'static, A>
{
fn command(
&self,
command_num: usize,
data1: usize,
data2: usize,
processid: ProcessId,
) -> CommandReturn {
let match_or_empty_or_nonexistant = self.processid.map_or(true, |owning_app| {
// We have recorded that an app has ownership of the HMAC.
// If the HMAC is still active, then we need to wait for the operation
// to finish and the app, whether it exists or not (it may have crashed),
// still owns this capsule. If the HMAC is not active, then
// we need to verify that that application still exists, and remove
// it as owner if not.
if self.active.get() {
owning_app == processid
} else {
// Check the app still exists.
//
// If the `.enter()` succeeds, then the app is still valid, and
// we can check if the owning app matches the one that called
// the command. If the `.enter()` fails, then the owning app no
// longer exists and we return `true` to signify the
// "or_nonexistant" case.
self.apps
.enter(owning_app, |_, _| owning_app == processid)
.unwrap_or(true)
}
});
let app_match = self.processid.map_or(false, |owning_app| {
// We have recorded that an app has ownership of the HMAC.
// If the HMAC is still active, then we need to wait for the operation
// to finish and the app, whether it exists or not (it may have crashed),
// still owns this capsule. If the HMAC is not active, then
// we need to verify that that application still exists, and remove
// it as owner if not.
if self.active.get() {
owning_app == processid
} else {
// Check the app still exists.
//
// If the `.enter()` succeeds, then the app is still valid, and
// we can check if the owning app matches the one that called
// the command. If the `.enter()` fails, then the owning app no
// longer exists and we return `true` to signify the
// "or_nonexistant" case.
self.apps
.enter(owning_app, |_, _| owning_app == processid)
.unwrap_or(true)
}
});
// Try the commands where we want to start an operation *not* entered in
// an app grant first.
if match_or_empty_or_nonexistant && command_num == 2 {
self.processid.set(processid);
let ret = self.run();
return if let Err(e) = ret {
self.aes.disable();
self.processid.clear();
self.check_queue();
CommandReturn::failure(e)
} else {
CommandReturn::success()
};
}
let ret = self
.apps
.enter(processid, |app, kernel_data| {
match command_num {
// check if present
0 => CommandReturn::success(),
// set_algorithm
1 => match data1 {
0 => {
app.aes_operation = Some(AesOperation::AES128Ctr(data2 != 0));
CommandReturn::success()
}
1 => {
app.aes_operation = Some(AesOperation::AES128CBC(data2 != 0));
CommandReturn::success()
}
2 => {
app.aes_operation = Some(AesOperation::AES128ECB(data2 != 0));
CommandReturn::success()
}
3 => {
app.aes_operation = Some(AesOperation::AES128CCM(data2 != 0));
CommandReturn::success()
}
4 => {
app.aes_operation = Some(AesOperation::AES128GCM(data2 != 0));
CommandReturn::success()
}
_ => CommandReturn::failure(ErrorCode::NOSUPPORT),
},
// setup
// Copy in the key and IV and run the first encryption operation
// This will trigger a callback
2 => {
// Some app is using the storage, we must wait.
if app.pending_run_app.is_some() {
// No more room in the queue, nowhere to store this
// request.
CommandReturn::failure(ErrorCode::NOMEM)
} else {
// We can store this, so lets do it.
app.pending_run_app = Some(processid);
CommandReturn::success()
}
}
// crypt
// Generate the encrypted output
// Multiple calls to crypt will re-use the existing state
// This will trigger a callback
3 => {
if app_match {
if let Err(e) = kernel_data
.get_readonly_processbuffer(ro_allow::SOURCE)
.and_then(|source| {
source.enter(|source| {
let mut static_buffer_len = 0;
self.source_buffer.map_or(
Err(ErrorCode::NOMEM),
|buf| {
// Determine the size of the static buffer we have
static_buffer_len = buf.len();
if static_buffer_len > source.len() {
static_buffer_len = source.len()
}
// Copy the data into the static buffer
source[..static_buffer_len]
.copy_to_slice(&mut buf[..static_buffer_len]);
self.data_copied.set(static_buffer_len);
Ok(())
},
)?;
if let Some(op) = app.aes_operation.as_ref() {
self.calculate_output(
op,
app.aoff.get(),
app.moff.get(),
app.mlen.get(),
app.mic_len.get(),
app.confidential.get(),
)?;
Ok(())
} else {
Err(ErrorCode::FAIL)
}
})
})
.unwrap_or(Err(ErrorCode::RESERVE))
{
kernel_data
.schedule_upcall(
0,
(kernel::errorcode::into_statuscode(e.into()), 0, 0),
)
.ok();
}
CommandReturn::success()
} else {
// We don't queue this request, the user has to call
// `setup` first.
CommandReturn::failure(ErrorCode::OFF)
}
}
// Finish
// Complete the operation and reset the AES
// This will not trigger a callback and will not process any data from userspace
4 => {
if app_match {
self.aes.disable();
self.processid.clear();
CommandReturn::success()
} else {
// We don't queue this request, the user has to call
// `setup` first.
CommandReturn::failure(ErrorCode::OFF)
}
}
// Set aoff for CCM
// This will not trigger a callback and will not process any data from userspace
5 => {
app.aoff.set(data1);
CommandReturn::success()
}
// Set moff for CCM
// This will not trigger a callback and will not process any data from userspace
6 => {
app.moff.set(data1);
CommandReturn::success()
}
// Set mic_len for CCM
// This will not trigger a callback and will not process any data from userspace
7 => {
app.mic_len.set(data1);
CommandReturn::success()
}
// Set confidential boolean for CCM
// This will not trigger a callback and will not process any data from userspace
8 => {
app.confidential.set(data1 > 0);
CommandReturn::success()
}
// default
_ => CommandReturn::failure(ErrorCode::NOSUPPORT),
}
})
.unwrap_or_else(|err| err.into());
if command_num == 4
|| command_num == 5
|| command_num == 6
|| command_num == 7
|| command_num == 8
{
self.check_queue();
}
ret
}
fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
self.apps.enter(processid, |_, _| {})
}
}
enum AesOperation {
AES128Ctr(bool),
AES128CBC(bool),
AES128ECB(bool),
AES128CCM(bool),
AES128GCM(bool),
}
#[derive(Default)]
pub struct App {
pending_run_app: Option<ProcessId>,
aes_operation: Option<AesOperation>,
aoff: Cell<usize>,
moff: Cell<usize>,
mlen: Cell<usize>,
mic_len: Cell<usize>,
confidential: Cell<bool>,
}