Skip to content

Commit 3686f24

Browse files
committed
remove NRF_CRYPTOCELL->ENABLE guard, since it is manged by libcrypto already
fix begun check
1 parent 0661e44 commit 3686f24

File tree

7 files changed

+10
-118
lines changed

7 files changed

+10
-118
lines changed

src/Adafruit_nRFCrypto.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "nrf_cc310/include/sns_silib.h"
3030
#include "Adafruit_nRFCrypto.h"
3131

32+
#include <Adafruit_TinyUSB.h> // for Serial
33+
3234
// Only nRF52840 has CC310
3335
#ifndef NRF_CRYPTOCELL
3436
#error CryptoCell CC310 is not supported on this board
@@ -53,27 +55,22 @@ Adafruit_nRFCrypto nRFCrypto;
5355
Adafruit_nRFCrypto::Adafruit_nRFCrypto(void)
5456
{
5557
_begun = false;
56-
_en_count = 0;
5758
}
5859

5960
bool Adafruit_nRFCrypto::begin(void)
6061
{
6162
// skip if already called begin before
6263
if (_begun) return true;
63-
_begun = false;
64+
_begun = true;
6465

6566
#ifndef USE_CC310_LIB_NO_INTERRUPT
6667
NVIC_SetPriority(CRYPTOCELL_IRQn, 2);
6768
NVIC_EnableIRQ(CRYPTOCELL_IRQn);
6869
#endif
6970

70-
enable();
71-
7271
VERIFY_ERROR(SaSi_LibInit(), false);
7372
VERIFY( Random.begin() );
7473

75-
disable();
76-
7774
return true;
7875
}
7976

@@ -83,32 +80,12 @@ void Adafruit_nRFCrypto::end(void)
8380
if (!_begun) return;
8481
_begun = false;
8582

86-
enable();
87-
8883
SaSi_LibFini();
8984
Random.end();
9085

9186
#ifndef USE_CC310_LIB_NO_INTERRUPT
9287
NVIC_DisableIRQ(CRYPTOCELL_IRQn);
9388
#endif
94-
95-
disable();
96-
}
97-
98-
void Adafruit_nRFCrypto::enable(void)
99-
{
100-
_en_count++;
101-
NRF_CRYPTOCELL->ENABLE = 1;
102-
}
103-
104-
void Adafruit_nRFCrypto::disable(void)
105-
{
106-
_en_count--;
107-
108-
if ( _en_count == 0)
109-
{
110-
NRF_CRYPTOCELL->ENABLE = 0;
111-
}
11289
}
11390

11491
//--------------------------------------------------------------------+

src/Adafruit_nRFCrypto.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,9 @@ class Adafruit_nRFCrypto
4040
bool begin(void);
4141
void end(void);
4242

43-
void enable(void);
44-
void disable(void);
45-
4643
nRFCrypto_Random Random;
4744

4845
private:
49-
uint32_t _en_count;
5046
bool _begun;
5147
};
5248

src/ecc/nRFCrypto_ECC.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,9 @@ bool nRFCrypto_ECC::genKeyPair(nRFCrypto_ECC_PrivateKey& private_key, nRFCrypto_
4242
CRYS_ECPKI_KG_TempData_t* tempbuf = (CRYS_ECPKI_KG_TempData_t*) rtos_malloc( sizeof(CRYS_ECPKI_KG_TempData_t) );
4343
VERIFY(tempbuf);
4444

45-
nRFCrypto.enable();
46-
4745
uint32_t err = CRYS_ECPKI_GenKeyPair(nRFCrypto.Random.getContext(), CRYS_RND_GenerateVector, private_key.getDomain(),
4846
&private_key._key, &public_key._key,
4947
tempbuf, NULL);
50-
51-
nRFCrypto.disable();
52-
5348
rtos_free(tempbuf);
5449

5550
VERIFY_CRYS(err, false);
@@ -61,12 +56,8 @@ uint32_t nRFCrypto_ECC::SVDP_DH(nRFCrypto_ECC_PrivateKey& private_key, nRFCrypto
6156
CRYS_ECDH_TempData_t* tempbuf = (CRYS_ECDH_TempData_t *) rtos_malloc(sizeof(CRYS_ECDH_TempData_t));
6257
VERIFY(tempbuf);
6358

64-
nRFCrypto.enable();
65-
6659
uint32_t err = CRYS_ECDH_SVDP_DH(&peer_pubkey._key, &private_key._key, shared_secret, &bufsize, tempbuf);
6760

68-
nRFCrypto.disable();
69-
7061
rtos_free(tempbuf);
7162

7263
VERIFY_CRYS(err, 0);

src/ecc/nRFCrypto_ECC_PrivateKey.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ nRFCrypto_ECC_PrivateKey::nRFCrypto_ECC_PrivateKey(void)
4040

4141
bool nRFCrypto_ECC_PrivateKey::begin(CRYS_ECPKI_DomainID_t id)
4242
{
43-
nRFCrypto.enable();
44-
4543
_domain = CRYS_ECPKI_GetEcDomain(id);
46-
47-
nRFCrypto.disable();
4844
return _domain != NULL;
4945
}
5046

@@ -57,25 +53,13 @@ void nRFCrypto_ECC_PrivateKey::end(void)
5753
// return raw buffer size = keysize + 1 (header)
5854
uint32_t nRFCrypto_ECC_PrivateKey::toRaw(uint8_t* buffer, uint32_t bufsize)
5955
{
60-
nRFCrypto.enable();
61-
62-
uint32_t err = CRYS_ECPKI_ExportPrivKey(&_key, buffer, &bufsize);
63-
64-
nRFCrypto.disable();
65-
66-
VERIFY_CRYS(err, 0);
56+
VERIFY_CRYS(CRYS_ECPKI_ExportPrivKey(&_key, buffer, &bufsize), 0);
6757
return bufsize;
6858
}
6959

7060
// Build public key from raw bytes in Big Endian
7161
bool nRFCrypto_ECC_PrivateKey::fromRaw(uint8_t* buffer, uint32_t bufsize)
7262
{
73-
nRFCrypto.enable();
74-
75-
uint32_t err = CRYS_ECPKI_BuildPrivKey(_domain, buffer, bufsize, &_key);
76-
77-
nRFCrypto.disable();
78-
79-
VERIFY_CRYS(err, false);
63+
VERIFY_CRYS(CRYS_ECPKI_BuildPrivKey(_domain, buffer, bufsize, &_key), false);
8064
return true;
8165
}

src/ecc/nRFCrypto_ECC_PublicKey.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ nRFCrypto_ECC_PublicKey::nRFCrypto_ECC_PublicKey(void)
4040

4141
bool nRFCrypto_ECC_PublicKey::begin(CRYS_ECPKI_DomainID_t id)
4242
{
43-
nRFCrypto.enable();
44-
4543
_domain = CRYS_ECPKI_GetEcDomain(id);
46-
47-
nRFCrypto.disable();
4844
return _domain != NULL;
4945
}
5046

@@ -57,13 +53,7 @@ void nRFCrypto_ECC_PublicKey::end(void)
5753
// return raw buffer size = keysize + 1 (header)
5854
uint32_t nRFCrypto_ECC_PublicKey::toRaw(uint8_t* buffer, uint32_t bufsize)
5955
{
60-
nRFCrypto.enable();
61-
62-
uint32_t err = CRYS_ECPKI_ExportPublKey(&_key, CRYS_EC_PointUncompressed, buffer, &bufsize);
63-
64-
nRFCrypto.disable();
65-
66-
VERIFY_CRYS(err, 0);
56+
VERIFY_CRYS(CRYS_ECPKI_ExportPublKey(&_key, CRYS_EC_PointUncompressed, buffer, &bufsize), 0);
6757
return bufsize;
6858
}
6959

@@ -73,12 +63,8 @@ bool nRFCrypto_ECC_PublicKey::fromRaw(uint8_t* buffer, uint32_t bufsize)
7363
CRYS_ECPKI_BUILD_TempData_t* tempbuf = (CRYS_ECPKI_BUILD_TempData_t*) rtos_malloc( sizeof(CRYS_ECPKI_BUILD_TempData_t) );
7464
VERIFY(tempbuf);
7565

76-
nRFCrypto.enable();
77-
7866
uint32_t err = CRYS_ECPKI_BuildPublKeyPartlyCheck(_domain, buffer, bufsize, &_key, tempbuf);
7967

80-
nRFCrypto.disable();
81-
8268
rtos_free(tempbuf);
8369

8470
VERIFY_CRYS(err, false);

src/nRFCrypto_Hash.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,21 @@ nRFCrypto_Hash::nRFCrypto_Hash(void)
4848

4949
bool nRFCrypto_Hash::begin(CRYS_HASH_OperationMode_t mode)
5050
{
51-
nRFCrypto.enable();
52-
5351
VERIFY_ERROR( CRYS_HASH_Init(&_context, mode), false );
5452
if ( mode < CRYS_HASH_NumOfModes ) _digest_len = digest_len_arr[mode];
5553

56-
nRFCrypto.disable();
5754
return true;
5855
}
5956

6057
bool nRFCrypto_Hash::update(uint8_t data[], size_t size)
6158
{
62-
nRFCrypto.enable();
6359
VERIFY_ERROR( CRYS_HASH_Update(&_context, data, size), false );
64-
nRFCrypto.disable();
6560
return true;
6661
}
6762

6863
uint8_t nRFCrypto_Hash::end(uint32_t result[16])
6964
{
70-
nRFCrypto.enable();
7165
VERIFY_ERROR( CRYS_HASH_Finish(&_context, result), 0);
72-
nRFCrypto.disable();
73-
7466
return _digest_len;
7567
}
7668

src/nRFCrypto_Random.cpp

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,12 @@ bool nRFCrypto_Random::begin(void)
3939
{
4040
// skip if already called begin before
4141
if (_begun) return true;
42-
_begun = false;
42+
_begun = true;
4343

4444
CRYS_RND_WorkBuff_t* workbuf = (CRYS_RND_WorkBuff_t*) rtos_malloc(sizeof(CRYS_RND_WorkBuff_t));
4545
VERIFY(workbuf);
4646

47-
nRFCrypto.enable();
48-
4947
uint32_t err = CRYS_RndInit(&_state, workbuf);
50-
51-
nRFCrypto.disable();
52-
5348
rtos_free(workbuf);
5449

5550
VERIFY_ERROR(err, false);
@@ -62,30 +57,18 @@ void nRFCrypto_Random::end(void)
6257
if (!_begun) return;
6358
_begun = false;
6459

65-
nRFCrypto.enable();
66-
6760
uint32_t err = CRYS_RND_UnInstantiation(&_state);
68-
69-
nRFCrypto.disable();
70-
7161
VERIFY_ERROR(err, );
7262
}
7363

74-
7564
CRYS_RND_State_t* nRFCrypto_Random::getContext(void)
7665
{
7766
return &_state;
7867
}
7968

8069
bool nRFCrypto_Random::addAdditionalInput(uint8_t* input, uint16_t size)
8170
{
82-
nRFCrypto.enable();
83-
84-
uint32_t err = CRYS_RND_AddAdditionalInput(&_state, input, size);
85-
86-
nRFCrypto.disable();
87-
88-
VERIFY_ERROR(err, false);
71+
VERIFY_ERROR(CRYS_RND_AddAdditionalInput(&_state, input, size), false);
8972
return true;
9073
}
9174

@@ -94,12 +77,7 @@ bool nRFCrypto_Random::reseed(void)
9477
CRYS_RND_WorkBuff_t* workbuf = (CRYS_RND_WorkBuff_t*) rtos_malloc(sizeof(CRYS_RND_WorkBuff_t));
9578
VERIFY(workbuf);
9679

97-
nRFCrypto.enable();
98-
9980
uint32_t err = CRYS_RND_Reseeding(&_state, workbuf);
100-
101-
nRFCrypto.disable();
102-
10381
rtos_free(workbuf);
10482

10583
VERIFY_ERROR(err, false);
@@ -108,24 +86,12 @@ bool nRFCrypto_Random::reseed(void)
10886

10987
bool nRFCrypto_Random::generate(uint8_t* buf, uint16_t bufsize)
11088
{
111-
nRFCrypto.enable();
112-
113-
uint32_t err = CRYS_RND_GenerateVector(&_state, bufsize, buf);
114-
115-
nRFCrypto.disable();
116-
117-
VERIFY_ERROR(err, false);
89+
VERIFY_ERROR(CRYS_RND_GenerateVector(&_state, bufsize, buf), false);
11890
return true;
11991
}
12092

12193
bool nRFCrypto_Random::generateInRange(uint8_t* buf, uint32_t bitsize, uint8_t* max)
12294
{
123-
nRFCrypto.enable();
124-
125-
uint32_t err = CRYS_RND_GenerateVectorInRange(&_state, CRYS_RND_GenerateVector, bitsize, max, buf);
126-
127-
nRFCrypto.disable();
128-
129-
VERIFY_ERROR(err, false);
95+
VERIFY_ERROR(CRYS_RND_GenerateVectorInRange(&_state, CRYS_RND_GenerateVector, bitsize, max, buf), false);
13096
return true;
13197
}

0 commit comments

Comments
 (0)