1
- // Copyright lowRISC contributors.
1
+ // Copyright lowRISC contributors (OpenTitan project) .
2
2
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3
3
// SPDX-License-Identifier: Apache-2.0
4
4
@@ -21,7 +21,7 @@ uint8_t PRESENT_SBOX4_INV[] = {0x5, 0xe, 0xf, 0x8, 0xc, 0x1, 0x2, 0xd,
21
21
22
22
static const uint32_t kNumAddrSubstPermRounds = 2 ;
23
23
static const uint32_t kNumDataSubstPermRounds = 2 ;
24
- static const uint32_t kNumPrinceHalfRounds = 2 ;
24
+ static const uint32_t kNumPrinceHalfRounds = 3 ;
25
25
26
26
static std::vector<uint8_t > byte_reverse_vector (
27
27
const std::vector<uint8_t > &vec_in) {
@@ -322,7 +322,7 @@ std::vector<uint8_t> scramble_encrypt_data(
322
322
const std::vector<uint8_t > &data_in, uint32_t data_width,
323
323
uint32_t subst_perm_width, const std::vector<uint8_t > &addr,
324
324
uint32_t addr_width, const std::vector<uint8_t > &nonce,
325
- const std::vector<uint8_t > &key, bool repeat_keystream) {
325
+ const std::vector<uint8_t > &key, bool repeat_keystream, bool use_sp_layer ) {
326
326
assert (data_in.size () == ((data_width + 7 ) / 8 ));
327
327
assert (addr.size () == ((addr_width + 7 ) / 8 ));
328
328
@@ -335,28 +335,32 @@ std::vector<uint8_t> scramble_encrypt_data(
335
335
336
336
auto data_enc = xor_vectors (data_in, keystream);
337
337
338
- return scramble_subst_perm_full_width (data_enc, data_width, subst_perm_width,
339
- true );
338
+ if (use_sp_layer) {
339
+ return scramble_subst_perm_full_width (data_enc, data_width,
340
+ subst_perm_width, true );
341
+ } else {
342
+ return data_enc;
343
+ }
340
344
}
341
345
342
346
std::vector<uint8_t > scramble_decrypt_data (
343
347
const std::vector<uint8_t > &data_in, uint32_t data_width,
344
348
uint32_t subst_perm_width, const std::vector<uint8_t > &addr,
345
349
uint32_t addr_width, const std::vector<uint8_t > &nonce,
346
- const std::vector<uint8_t > &key, bool repeat_keystream) {
350
+ const std::vector<uint8_t > &key, bool repeat_keystream, bool use_sp_layer ) {
347
351
assert (data_in.size () == ((data_width + 7 ) / 8 ));
348
352
assert (addr.size () == ((addr_width + 7 ) / 8 ));
349
353
350
- // Data is decrypted by reversing substitution/permutation layer then XORing
351
- // with keystream
352
- auto data_sp_out = scramble_subst_perm_full_width (data_in, data_width,
353
- subst_perm_width, false );
354
-
355
354
auto keystream =
356
355
scramble_gen_keystream (addr, addr_width, nonce, key, data_width,
357
356
kNumPrinceHalfRounds , repeat_keystream);
358
-
359
- auto data_dec = xor_vectors (data_sp_out, keystream);
360
-
361
- return data_dec;
357
+ if (use_sp_layer) {
358
+ // Data is decrypted by reversing substitution/permutation layer then XORing
359
+ // with keystream
360
+ auto data_sp_out = scramble_subst_perm_full_width (data_in, data_width,
361
+ subst_perm_width, false );
362
+ return xor_vectors (data_sp_out, keystream);
363
+ } else {
364
+ return xor_vectors (data_in, keystream);
365
+ }
362
366
}
0 commit comments