diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-11-25 23:18:11 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-11-25 23:18:11 +0100 |
commit | c62a0f592c49b41d393fae580ce9f1293ee7a16d (patch) | |
tree | 6d8ef6b7c93fdcaa0fd1bcd590dba531ef8b5140 /src/methods/generic_poly1305 | |
parent | 60c2c11de820687887a643344fc1b0a91fd45226 (diff) | |
download | fastd-c62a0f592c49b41d393fae580ce9f1293ee7a16d.tar fastd-c62a0f592c49b41d393fae580ce9f1293ee7a16d.zip |
Move crypto algorithm information out of implementation
Diffstat (limited to 'src/methods/generic_poly1305')
-rw-r--r-- | src/methods/generic_poly1305/generic_poly1305.c | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/src/methods/generic_poly1305/generic_poly1305.c b/src/methods/generic_poly1305/generic_poly1305.c index 84f9f9a..3820907 100644 --- a/src/methods/generic_poly1305/generic_poly1305.c +++ b/src/methods/generic_poly1305/generic_poly1305.c @@ -36,13 +36,14 @@ struct fastd_method_session_state { fastd_method_common_t common; + const fastd_cipher_info_t *cipher_info; const fastd_cipher_t *cipher; const fastd_cipher_context_t *cipher_ctx; fastd_cipher_state_t *cipher_state; }; -static bool cipher_get(fastd_context_t *ctx, const char *name, const fastd_cipher_t **cipher, const fastd_cipher_context_t **cctx) { +static bool cipher_get(fastd_context_t *ctx, const char *name, const fastd_cipher_info_t **cipher_info, const fastd_cipher_t **cipher, const fastd_cipher_context_t **cctx) { size_t len = strlen(name); if (len < 9) @@ -55,27 +56,39 @@ static bool cipher_get(fastd_context_t *ctx, const char *name, const fastd_ciphe memcpy(cipher_name, name, len-9); cipher_name[len-9] = 0; + const fastd_cipher_info_t *info = NULL; + if (ctx) { - *cipher = fastd_cipher_get_by_name(ctx, cipher_name, cctx); - return *cipher; + *cipher = fastd_cipher_get_by_name(ctx, cipher_name, &info, cctx); + if (!*cipher) + return false; } else { - return fastd_cipher_available(cipher_name); + info = fastd_cipher_info_get_by_name(cipher_name); + if (!info) + return false; } + + if (info->iv_length <= COMMON_NONCEBYTES) + return false; + + if (cipher_info) + *cipher_info = info; + + return true; } static bool method_provides(const char *name) { - return cipher_get(NULL, name, NULL, NULL); + return cipher_get(NULL, name, NULL, NULL, NULL); } static size_t method_key_length(fastd_context_t *ctx, const char *name) { - const fastd_cipher_t *cipher = NULL; - const fastd_cipher_context_t *cctx; - if (!cipher_get(ctx, name, &cipher, &cctx)) + const fastd_cipher_info_t *cipher_info; + if (!cipher_get(NULL, name, &cipher_info, NULL, NULL)) exit_bug(ctx, "generic-poly1305: can't get cipher key length"); - return cipher->key_length; + return cipher_info->key_length; } static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, const char *name, const uint8_t *secret, bool initiator) { @@ -83,12 +96,12 @@ static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, c fastd_method_common_init(ctx, &session->common, initiator); - if (!cipher_get(ctx, name, &session->cipher, &session->cipher_ctx)) + if (!cipher_get(ctx, name, &session->cipher_info, &session->cipher, &session->cipher_ctx)) exit_bug(ctx, "generic-poly1305: can't instanciate cipher"); session->cipher_state = session->cipher->init_state(ctx, session->cipher_ctx, secret); - if (session->cipher->iv_length <= COMMON_NONCEBYTES) + if (session->cipher_info->iv_length <= COMMON_NONCEBYTES) exit_bug(ctx, "generic-poly1305: iv_length to small"); return session; @@ -127,10 +140,10 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast if (tail_len) memset(in.data+in.len, 0, tail_len); - uint8_t nonce[session->cipher->iv_length]; - memset(nonce, 0, session->cipher->iv_length); + uint8_t nonce[session->cipher_info->iv_length]; + memset(nonce, 0, session->cipher_info->iv_length); memcpy(nonce, session->common.send_nonce, COMMON_NONCEBYTES); - nonce[session->cipher->iv_length-1] = 1; + nonce[session->cipher_info->iv_length-1] = 1; int n_blocks = block_count(in.len, sizeof(fastd_block128_t)); @@ -174,10 +187,10 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho if (((const uint8_t*)in.data)[COMMON_NONCEBYTES]) /* flags */ return false; - uint8_t nonce[session->cipher->iv_length]; - memset(nonce, 0, session->cipher->iv_length); + uint8_t nonce[session->cipher_info->iv_length]; + memset(nonce, 0, session->cipher_info->iv_length); memcpy(nonce, in.data, COMMON_NONCEBYTES); - nonce[session->cipher->iv_length-1] = 1; + nonce[session->cipher_info->iv_length-1] = 1; int64_t age; if (!fastd_method_is_nonce_valid(ctx, &session->common, nonce, &age)) |