err_status_t aes_test_inverse(void) { v128_t x, y; aes_expanded_key_t expanded_key, decrypt_key; uint8_t plaintext[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; v128_t k; v128_set_to_zero(&x); v128_copy_octet_string(&k, key); v128_copy_octet_string(&x, plaintext); aes_expand_encryption_key(k, expanded_key); aes_expand_decryption_key(k, decrypt_key); aes_encrypt(&x, expanded_key); aes_decrypt(&x, decrypt_key); /* compare to expected value then report */ v128_copy_octet_string(&y, plaintext); if (v128_is_eq(&x, &y)) return err_status_ok; return err_status_algo_fail; }
err_status_t x917_prng_init(rand_source_func_t random_source) { v128_t tmp_key; err_status_t status; /* initialize output count to zero */ x917_prng.octet_count = 0; /* set random source */ x917_prng.rand = random_source; /* initialize secret key from random source */ status = random_source((uint8_t *)&tmp_key, 16); if (status) return status; /* expand aes key */ aes_expand_encryption_key(&tmp_key, x917_prng.key); /* initialize prng state from random source */ status = x917_prng.rand((uint8_t *)&x917_prng.state, 16); if (status) return status; return err_status_ok; }
err_status_t aes_icm_context_init(aes_icm_ctx_t *c, const octet_t *key) { v128_t tmp_key; /* set counter and initial values to 'offset' value */ /* FIX!!! this assumes the salt is at key + 16, and thus that the */ /* FIX!!! cipher key length is 16! Also note this copies past the end of the 'key' array by 2 bytes! */ v128_copy_octet_string(&c->counter, key + 16); v128_copy_octet_string(&c->offset, key + 16); /* force last two octets of the offset to zero (for srtp compatibility) */ c->offset.octet[14] = c->offset.octet[15] = 0; c->counter.octet[14] = c->counter.octet[15] = 0; /* set tmp_key (for alignment) */ v128_copy_octet_string(&tmp_key, key); debug_print(mod_aes_icm, "key: %s", v128_hex_string(&tmp_key)); debug_print(mod_aes_icm, "offset: %s", v128_hex_string(&c->offset)); /* expand key */ aes_expand_encryption_key(tmp_key, c->expanded_key); /* indicate that the keystream_buffer is empty */ c->bytes_in_buffer = 0; return err_status_ok; }
err_status_t aes_cbc_set_iv(aes_cbc_ctx_t *c, void *iv, int direction) { err_status_t status; int i; /* v128_t *input = iv; */ uint8_t *input = (uint8_t*) iv; /* set state and 'previous' block to iv */ for (i=0; i < 16; i++) c->previous.v8[i] = c->state.v8[i] = input[i]; debug_print(mod_aes_cbc, "setting iv: %s", v128_hex_string(&c->state)); /* expand key for the appropriate direction */ switch (direction) { case (direction_encrypt): status = aes_expand_encryption_key(c->key, c->key_len, &c->expanded_key); memset(c->key, 0, 32); if (status) return status; break; case (direction_decrypt): status = aes_expand_decryption_key(c->key, c->key_len, &c->expanded_key); memset(c->key, 0, 32); if (status) return status; break; default: return err_status_bad_param; } return err_status_ok; }
err_status_t aes_cbc_context_init(aes_cbc_ctx_t *c, const uint8_t *key, cipher_direction_t dir) { v128_t tmp_key; /* set tmp_key (for alignment) */ v128_copy_octet_string(&tmp_key, key); debug_print(mod_aes_cbc, "key: %s", v128_hex_string(&tmp_key)); /* expand key for the appropriate direction */ switch (dir) { case (direction_encrypt): aes_expand_encryption_key(&tmp_key, c->expanded_key); break; case (direction_decrypt): aes_expand_decryption_key(&tmp_key, c->expanded_key); break; default: return err_status_bad_param; } return err_status_ok; }
// int main(int argc, char *argv[]) { int aes_calc(char *key8, char *phrase8, char *cipher8) { v128_t data, key; aes_expanded_key_t exp_key; int len; int verbose = 0; /* read in key, checking length */ if (strlen(key8) > AES_KEY_LEN * 2) { fprintf(stderr, "error: too many digits in key " "(should be %d hexadecimal digits, found %u)\n", AES_KEY_LEN * 2, (unsigned) strlen(key8)); return(1); } len = hex_string_to_octet_string((char *) &key, key8, AES_KEY_LEN*2); /* check that hex string is the right length */ if (len < AES_KEY_LEN * 2) { fprintf(stderr, "error: too few digits in key " "(should be %d hexadecimal digits, found %d)\n", AES_KEY_LEN * 2, len); return(1); } /* read in plaintext, checking length */ if (strlen(phrase8) > 16 * 2) { fprintf(stderr, "error: too many digits in plaintext " "(should be %d hexadecimal digits, found %u)\n", 16 * 2, (unsigned) strlen(phrase8)); return(1); } len = hex_string_to_octet_string((char *) (&data), phrase8, 16 * 2); /* check that hex string is the right length */ if (len < 16 * 2) { fprintf(stderr, "error: too few digits in plaintext " "(should be %d hexadecimal digits, found %d)\n", 16 * 2, len); return(1); } if (verbose) { /* print out plaintext */ printf("plaintext:\t%s\n", octet_string_hex_string((uint8_t *) &data, 16)); } /* encrypt plaintext */ aes_expand_encryption_key(&key, exp_key); aes_encrypt(&data, exp_key); /* write ciphertext to output */ if (verbose) { printf("key:\t\t%s\n", v128_hex_string(&key)); printf("ciphertext:\t"); } printf("%s\n", v128_hex_string(&data)); return strcmp(v128_hex_string(&data), cipher8); }