Add --key option to set key directly (#1207)

This commit is contained in:
Max Lv
2017-02-06 17:02:04 +08:00
committed by GitHub
parent 43aabb2303
commit ef0402eb10
25 changed files with 363 additions and 103 deletions
+10 -6
View File
@@ -138,7 +138,6 @@ dump(char *tag, char *text, int len)
printf("0x%02x ", (uint8_t)text[i]);
printf("\n");
}
#endif
const char *supported_aead_ciphers[AEAD_CIPHER_NUM] = {
@@ -691,7 +690,7 @@ aead_decrypt(buffer_t *ciphertext, cipher_ctx_t *cipher_ctx, size_t capacity)
}
cipher_t *
aead_key_init(int method, const char *pass)
aead_key_init(int method, const char *pass, const char *key)
{
if (method < AES128GCM || method >= AEAD_CIPHER_NUM) {
LOGE("aead_key_init(): Illegal method");
@@ -724,8 +723,12 @@ aead_key_init(int method, const char *pass)
FATAL("Cannot initialize cipher");
}
cipher->key_len = crypto_derive_key(cipher, pass, cipher->key,
supported_aead_ciphers_key_size[method], 2);
if (key != NULL)
cipher->key_len = crypto_parse_key(key, cipher->key,
supported_aead_ciphers_key_size[method]);
else
cipher->key_len = crypto_derive_key(key, cipher->key,
supported_aead_ciphers_key_size[method]);
if (cipher->key_len == 0) {
FATAL("Cannot generate key and nonce");
@@ -739,7 +742,7 @@ aead_key_init(int method, const char *pass)
}
cipher_t *
aead_init(const char *pass, const char *method)
aead_init(const char *pass, const char *key, const char *method)
{
int m = AES128GCM;
if (method != NULL) {
@@ -753,5 +756,6 @@ aead_init(const char *pass, const char *method)
m = AES256GCM;
}
}
return aead_key_init(m, pass);
return aead_key_init(m, pass, key);
}