Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cwa App Android
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Felix Foertsch
Cwa App Android
Commits
18c3203b
Unverified
Commit
18c3203b
authored
4 years ago
by
Jakob Möller
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Generate a true DB Secret Key for use in Encryption via AES256 (#189)
Signed-off-by:
d067928
<
jakob.moeller@sap.com
>
parent
73592510
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/security/SecurityHelper.kt
+29
-2
29 additions, 2 deletions
...java/de/rki/coronawarnapp/util/security/SecurityHelper.kt
with
29 additions
and
2 deletions
Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/security/SecurityHelper.kt
+
29
−
2
View file @
18c3203b
...
...
@@ -21,16 +21,23 @@ package de.rki.coronawarnapp.util.security
import
android.content.Context
import
android.content.SharedPreferences
import
android.security.keystore.KeyGenParameterSpec
import
android.security.keystore.KeyProperties
import
androidx.security.crypto.EncryptedSharedPreferences
import
androidx.security.crypto.MasterKeys
import
de.rki.coronawarnapp.CoronaWarnApplication
import
java.security.KeyStore
import
java.security.MessageDigest
import
java.security.SecureRandom
import
javax.crypto.KeyGenerator
import
javax.crypto.SecretKey
/**
* Key Store and Password Access
*/
object
SecurityHelper
{
private
const
val
CWA_APP_SQLITE_DB_PW
=
"CWA_APP_SQLITE_DB_PW"
private
const
val
AES_KEY_SIZE
=
256
private
const
val
SHARED_PREF_NAME
=
"shared_preferences_cwa"
private
val
keyGenParameterSpec
=
MasterKeys
.
AES256_GCM_SPEC
private
val
masterKeyAlias
=
MasterKeys
.
getOrCreate
(
keyGenParameterSpec
)
...
...
@@ -61,11 +68,31 @@ object SecurityHelper {
/**
* Retrieves the Master Key from the Android KeyStore to use in SQLCipher
*/
fun
getDBPassword
()
=
keyStore
.
getKey
(
masterKeyAlias
,
null
)
fun
getDBPassword
()
=
getOrGenerateDBSecretKey
()
.
toString
()
.
toCharArray
()
private
fun
getOrGenerateDBSecretKey
():
SecretKey
=
keyStore
.
getKey
(
CWA_APP_SQLITE_DB_PW
,
null
).
run
{
return
if
(
this
==
null
)
{
val
kg
:
KeyGenerator
=
KeyGenerator
.
getInstance
(
KeyProperties
.
KEY_ALGORITHM_AES
,
AndroidKeyStore
)
val
spec
:
KeyGenParameterSpec
=
KeyGenParameterSpec
.
Builder
(
CWA_APP_SQLITE_DB_PW
,
KeyProperties
.
PURPOSE_ENCRYPT
and
KeyProperties
.
PURPOSE_DECRYPT
)
.
setKeySize
(
AES_KEY_SIZE
)
.
setBlockModes
(
KeyProperties
.
BLOCK_MODE_CBC
)
.
setEncryptionPaddings
(
KeyProperties
.
ENCRYPTION_PADDING_PKCS7
)
.
setRandomizedEncryptionRequired
(
true
)
.
setUserAuthenticationRequired
(
false
)
.
build
()
kg
.
init
(
spec
,
SecureRandom
())
kg
.
generateKey
()
}
else
this
as
SecretKey
}
fun
hash256
(
input
:
String
):
String
=
MessageDigest
.
getInstance
(
"SHA-256"
)
.
digest
(
input
.
toByteArray
())
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment