Assert that the cipher mode and key length are valid.

This commit is contained in:
Dag-Erling Smørgrav 2017-03-19 15:20:32 +01:00
parent e21ec8afbc
commit 5c98dc1084
5 changed files with 21 additions and 6 deletions

View file

@ -33,6 +33,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <cryb/assert.h>
#include <cryb/bitwise.h> #include <cryb/bitwise.h>
#include <cryb/endian.h> #include <cryb/endian.h>
@ -577,6 +578,8 @@ void
aes_init(aes_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen) aes_init(aes_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen)
{ {
assert(mode == CIPHER_MODE_ENCRYPT || mode == CIPHER_MODE_DECRYPT);
assert(keylen == 16 || keylen == 24 || keylen == 32);
memset(ctx, 0, sizeof *ctx); memset(ctx, 0, sizeof *ctx);
ctx->mode = mode; ctx->mode = mode;
if (ctx->mode == CIPHER_MODE_DECRYPT) if (ctx->mode == CIPHER_MODE_DECRYPT)

View file

@ -32,6 +32,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <cryb/assert.h>
#include <cryb/bitwise.h> #include <cryb/bitwise.h>
#include <cryb/endian.h> #include <cryb/endian.h>
#include <cryb/memset_s.h> #include <cryb/memset_s.h>
@ -56,10 +57,12 @@ static const char magic256[] = "expand 32-byte k";
* set to 20, the most commonly used value. * set to 20, the most commonly used value.
*/ */
void void
chacha_init(chacha_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen) chacha_init(chacha_ctx *ctx, cipher_mode mode CRYB_UNUSED,
const uint8_t *key, size_t keylen)
{ {
(void)mode; assert(mode == CIPHER_MODE_ENCRYPT || mode == CIPHER_MODE_DECRYPT);
assert(keylen == 16 || keylen == 32);
memset(ctx, 0, sizeof *ctx); memset(ctx, 0, sizeof *ctx);
if (keylen == 32) { if (keylen == 32) {
/* magic */ /* magic */

View file

@ -33,6 +33,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <cryb/assert.h>
#include <cryb/bitwise.h> #include <cryb/bitwise.h>
#include <cryb/endian.h> #include <cryb/endian.h>
#include <cryb/memset_s.h> #include <cryb/memset_s.h>
@ -371,6 +372,8 @@ des_init(des_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen)
{ {
cipher_mode m1, m2, m3; cipher_mode m1, m2, m3;
assert(mode == CIPHER_MODE_ENCRYPT || mode == CIPHER_MODE_DECRYPT);
assert(keylen == 8 || keylen == 16 || keylen == 24);
if (mode == CIPHER_MODE_DECRYPT) { if (mode == CIPHER_MODE_DECRYPT) {
m1 = m3 = CIPHER_MODE_DECRYPT; m1 = m3 = CIPHER_MODE_DECRYPT;
m2 = CIPHER_MODE_ENCRYPT; m2 = CIPHER_MODE_ENCRYPT;

View file

@ -32,6 +32,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <cryb/assert.h>
#include <cryb/bitwise.h> #include <cryb/bitwise.h>
#include <cryb/endian.h> #include <cryb/endian.h>
#include <cryb/memset_s.h> #include <cryb/memset_s.h>
@ -39,12 +40,14 @@
#include <cryb/rc4.h> #include <cryb/rc4.h>
void void
rc4_init(rc4_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen) rc4_init(rc4_ctx *ctx, cipher_mode mode CRYB_UNUSED,
const uint8_t *key, size_t keylen)
{ {
unsigned int i, j; unsigned int i, j;
uint8_t t; uint8_t t;
(void)mode; assert(mode == CIPHER_MODE_ENCRYPT || mode == CIPHER_MODE_DECRYPT);
assert(keylen > 0);
memset(ctx, 0, sizeof *ctx); memset(ctx, 0, sizeof *ctx);
for (i = 0; i < 256; ++i) for (i = 0; i < 256; ++i)
ctx->s[i] = i; ctx->s[i] = i;

View file

@ -32,6 +32,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <cryb/assert.h>
#include <cryb/bitwise.h> #include <cryb/bitwise.h>
#include <cryb/endian.h> #include <cryb/endian.h>
#include <cryb/memset_s.h> #include <cryb/memset_s.h>
@ -56,10 +57,12 @@ static const char magic256[] = "expand 32-byte k";
* set to 20, the most commonly used value. * set to 20, the most commonly used value.
*/ */
void void
salsa_init(salsa_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen) salsa_init(salsa_ctx *ctx, cipher_mode mode CRYB_UNUSED,
const uint8_t *key, size_t keylen)
{ {
(void)mode; assert(mode == CIPHER_MODE_ENCRYPT || mode == CIPHER_MODE_DECRYPT);
assert(keylen == 16 || keylen == 32);
memset(ctx, 0, sizeof *ctx); memset(ctx, 0, sizeof *ctx);
if (keylen == 32) { if (keylen == 32) {
/* magic */ /* magic */