netwerk/srtp/src/crypto/ae_xfm/xfm.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * xfm.c
michael@0 3 *
michael@0 4 * Crypto transform implementation
michael@0 5 *
michael@0 6 * David A. McGrew
michael@0 7 * Cisco Systems, Inc.
michael@0 8 */
michael@0 9 /*
michael@0 10 *
michael@0 11 * Copyright (c) 2001-2006, Cisco Systems, Inc.
michael@0 12 * All rights reserved.
michael@0 13 *
michael@0 14 * Redistribution and use in source and binary forms, with or without
michael@0 15 * modification, are permitted provided that the following conditions
michael@0 16 * are met:
michael@0 17 *
michael@0 18 * Redistributions of source code must retain the above copyright
michael@0 19 * notice, this list of conditions and the following disclaimer.
michael@0 20 *
michael@0 21 * Redistributions in binary form must reproduce the above
michael@0 22 * copyright notice, this list of conditions and the following
michael@0 23 * disclaimer in the documentation and/or other materials provided
michael@0 24 * with the distribution.
michael@0 25 *
michael@0 26 * Neither the name of the Cisco Systems, Inc. nor the names of its
michael@0 27 * contributors may be used to endorse or promote products derived
michael@0 28 * from this software without specific prior written permission.
michael@0 29 *
michael@0 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
michael@0 33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
michael@0 34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
michael@0 35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
michael@0 37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
michael@0 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
michael@0 41 * OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 42 *
michael@0 43 */
michael@0 44
michael@0 45 #include "cryptoalg.h"
michael@0 46 #include "aes_cbc.h"
michael@0 47 #include "hmac.h"
michael@0 48 #include "crypto_kernel.h" /* for crypto_get_random() */
michael@0 49
michael@0 50 #define KEY_LEN 16
michael@0 51 #define ENC_KEY_LEN 16
michael@0 52 #define MAC_KEY_LEN 16
michael@0 53 #define IV_LEN 16
michael@0 54 #define TAG_LEN 12
michael@0 55 #define MAX_EXPAND 27
michael@0 56
michael@0 57 err_status_t
michael@0 58 aes_128_cbc_hmac_sha1_96_func(void *key,
michael@0 59 void *clear,
michael@0 60 unsigned clear_len,
michael@0 61 void *iv,
michael@0 62 void *opaque,
michael@0 63 unsigned *opaque_len,
michael@0 64 void *auth_tag) {
michael@0 65 aes_cbc_ctx_t aes_ctx;
michael@0 66 hmac_ctx_t hmac_ctx;
michael@0 67 unsigned char enc_key[ENC_KEY_LEN];
michael@0 68 unsigned char mac_key[MAC_KEY_LEN];
michael@0 69 err_status_t status;
michael@0 70
michael@0 71 /* check if we're doing authentication only */
michael@0 72 if ((iv == NULL) && (opaque == NULL) && (opaque_len == NULL)) {
michael@0 73
michael@0 74 /* perform authentication only */
michael@0 75
michael@0 76 } else if ((iv == NULL) || (opaque == NULL) || (opaque_len == NULL)) {
michael@0 77
michael@0 78 /*
michael@0 79 * bad parameter - we expect either all three pointers to be NULL,
michael@0 80 * or none of those pointers to be NULL
michael@0 81 */
michael@0 82 return err_status_fail;
michael@0 83
michael@0 84 } else {
michael@0 85
michael@0 86 /* derive encryption and authentication keys from the input key */
michael@0 87 status = hmac_init(&hmac_ctx, key, KEY_LEN);
michael@0 88 if (status) return status;
michael@0 89 status = hmac_compute(&hmac_ctx, "ENC", 3, ENC_KEY_LEN, enc_key);
michael@0 90 if (status) return status;
michael@0 91
michael@0 92 status = hmac_init(&hmac_ctx, key, KEY_LEN);
michael@0 93 if (status) return status;
michael@0 94 status = hmac_compute(&hmac_ctx, "MAC", 3, MAC_KEY_LEN, mac_key);
michael@0 95 if (status) return status;
michael@0 96
michael@0 97
michael@0 98 /* perform encryption and authentication */
michael@0 99
michael@0 100 /* set aes key */
michael@0 101 status = aes_cbc_context_init(&aes_ctx, key, ENC_KEY_LEN, direction_encrypt);
michael@0 102 if (status) return status;
michael@0 103
michael@0 104 /* set iv */
michael@0 105 status = crypto_get_random(iv, IV_LEN);
michael@0 106 if (status) return status;
michael@0 107 status = aes_cbc_set_iv(&aes_ctx, iv);
michael@0 108
michael@0 109 /* encrypt the opaque data */
michael@0 110 status = aes_cbc_nist_encrypt(&aes_ctx, opaque, opaque_len);
michael@0 111 if (status) return status;
michael@0 112
michael@0 113 /* authenticate clear and opaque data */
michael@0 114 status = hmac_init(&hmac_ctx, mac_key, MAC_KEY_LEN);
michael@0 115 if (status) return status;
michael@0 116
michael@0 117 status = hmac_start(&hmac_ctx);
michael@0 118 if (status) return status;
michael@0 119
michael@0 120 status = hmac_update(&hmac_ctx, clear, clear_len);
michael@0 121 if (status) return status;
michael@0 122
michael@0 123 status = hmac_compute(&hmac_ctx, opaque, *opaque_len, TAG_LEN, auth_tag);
michael@0 124 if (status) return status;
michael@0 125
michael@0 126 }
michael@0 127
michael@0 128 return err_status_ok;
michael@0 129 }
michael@0 130
michael@0 131 err_status_t
michael@0 132 aes_128_cbc_hmac_sha1_96_inv(void *key,
michael@0 133 void *clear,
michael@0 134 unsigned clear_len,
michael@0 135 void *iv,
michael@0 136 void *opaque,
michael@0 137 unsigned *opaque_len,
michael@0 138 void *auth_tag) {
michael@0 139 aes_cbc_ctx_t aes_ctx;
michael@0 140 hmac_ctx_t hmac_ctx;
michael@0 141 unsigned char enc_key[ENC_KEY_LEN];
michael@0 142 unsigned char mac_key[MAC_KEY_LEN];
michael@0 143 unsigned char tmp_tag[TAG_LEN];
michael@0 144 unsigned char *tag = auth_tag;
michael@0 145 err_status_t status;
michael@0 146 int i;
michael@0 147
michael@0 148 /* check if we're doing authentication only */
michael@0 149 if ((iv == NULL) && (opaque == NULL) && (opaque_len == NULL)) {
michael@0 150
michael@0 151 /* perform authentication only */
michael@0 152
michael@0 153 } else if ((iv == NULL) || (opaque == NULL) || (opaque_len == NULL)) {
michael@0 154
michael@0 155 /*
michael@0 156 * bad parameter - we expect either all three pointers to be NULL,
michael@0 157 * or none of those pointers to be NULL
michael@0 158 */
michael@0 159 return err_status_fail;
michael@0 160
michael@0 161 } else {
michael@0 162
michael@0 163 /* derive encryption and authentication keys from the input key */
michael@0 164 status = hmac_init(&hmac_ctx, key, KEY_LEN);
michael@0 165 if (status) return status;
michael@0 166 status = hmac_compute(&hmac_ctx, "ENC", 3, ENC_KEY_LEN, enc_key);
michael@0 167 if (status) return status;
michael@0 168
michael@0 169 status = hmac_init(&hmac_ctx, key, KEY_LEN);
michael@0 170 if (status) return status;
michael@0 171 status = hmac_compute(&hmac_ctx, "MAC", 3, MAC_KEY_LEN, mac_key);
michael@0 172 if (status) return status;
michael@0 173
michael@0 174 /* perform encryption and authentication */
michael@0 175
michael@0 176 /* set aes key */
michael@0 177 status = aes_cbc_context_init(&aes_ctx, key, ENC_KEY_LEN, direction_decrypt);
michael@0 178 if (status) return status;
michael@0 179
michael@0 180 /* set iv */
michael@0 181 status = rand_source_get_octet_string(iv, IV_LEN);
michael@0 182 if (status) return status;
michael@0 183 status = aes_cbc_set_iv(&aes_ctx, iv);
michael@0 184
michael@0 185 /* encrypt the opaque data */
michael@0 186 status = aes_cbc_nist_decrypt(&aes_ctx, opaque, opaque_len);
michael@0 187 if (status) return status;
michael@0 188
michael@0 189 /* authenticate clear and opaque data */
michael@0 190 status = hmac_init(&hmac_ctx, mac_key, MAC_KEY_LEN);
michael@0 191 if (status) return status;
michael@0 192
michael@0 193 status = hmac_start(&hmac_ctx);
michael@0 194 if (status) return status;
michael@0 195
michael@0 196 status = hmac_update(&hmac_ctx, clear, clear_len);
michael@0 197 if (status) return status;
michael@0 198
michael@0 199 status = hmac_compute(&hmac_ctx, opaque, *opaque_len, TAG_LEN, tmp_tag);
michael@0 200 if (status) return status;
michael@0 201
michael@0 202 /* compare the computed tag with the one provided as input */
michael@0 203 for (i=0; i < TAG_LEN; i++)
michael@0 204 if (tmp_tag[i] != tag[i])
michael@0 205 return err_status_auth_fail;
michael@0 206
michael@0 207 }
michael@0 208
michael@0 209 return err_status_ok;
michael@0 210 }
michael@0 211
michael@0 212
michael@0 213 #define ENC 1
michael@0 214
michael@0 215 #define DEBUG 0
michael@0 216
michael@0 217 err_status_t
michael@0 218 aes_128_cbc_hmac_sha1_96_enc(void *key,
michael@0 219 const void *clear,
michael@0 220 unsigned clear_len,
michael@0 221 void *iv,
michael@0 222 void *opaque,
michael@0 223 unsigned *opaque_len) {
michael@0 224 aes_cbc_ctx_t aes_ctx;
michael@0 225 hmac_ctx_t hmac_ctx;
michael@0 226 unsigned char enc_key[ENC_KEY_LEN];
michael@0 227 unsigned char mac_key[MAC_KEY_LEN];
michael@0 228 unsigned char *auth_tag;
michael@0 229 err_status_t status;
michael@0 230
michael@0 231 /* check if we're doing authentication only */
michael@0 232 if ((iv == NULL) && (opaque == NULL) && (opaque_len == NULL)) {
michael@0 233
michael@0 234 /* perform authentication only */
michael@0 235
michael@0 236 } else if ((iv == NULL) || (opaque == NULL) || (opaque_len == NULL)) {
michael@0 237
michael@0 238 /*
michael@0 239 * bad parameter - we expect either all three pointers to be NULL,
michael@0 240 * or none of those pointers to be NULL
michael@0 241 */
michael@0 242 return err_status_fail;
michael@0 243
michael@0 244 } else {
michael@0 245
michael@0 246 #if DEBUG
michael@0 247 printf("ENC using key %s\n", octet_string_hex_string(key, KEY_LEN));
michael@0 248 #endif
michael@0 249
michael@0 250 /* derive encryption and authentication keys from the input key */
michael@0 251 status = hmac_init(&hmac_ctx, key, KEY_LEN);
michael@0 252 if (status) return status;
michael@0 253 status = hmac_compute(&hmac_ctx, "ENC", 3, ENC_KEY_LEN, enc_key);
michael@0 254 if (status) return status;
michael@0 255
michael@0 256 status = hmac_init(&hmac_ctx, key, KEY_LEN);
michael@0 257 if (status) return status;
michael@0 258 status = hmac_compute(&hmac_ctx, "MAC", 3, MAC_KEY_LEN, mac_key);
michael@0 259 if (status) return status;
michael@0 260
michael@0 261
michael@0 262 /* perform encryption and authentication */
michael@0 263
michael@0 264 /* set aes key */
michael@0 265 status = aes_cbc_context_init(&aes_ctx, key, ENC_KEY_LEN, direction_encrypt);
michael@0 266 if (status) return status;
michael@0 267
michael@0 268 /* set iv */
michael@0 269 status = rand_source_get_octet_string(iv, IV_LEN);
michael@0 270 if (status) return status;
michael@0 271 status = aes_cbc_set_iv(&aes_ctx, iv);
michael@0 272 if (status) return status;
michael@0 273
michael@0 274 #if DEBUG
michael@0 275 printf("plaintext len: %d\n", *opaque_len);
michael@0 276 printf("iv: %s\n", octet_string_hex_string(iv, IV_LEN));
michael@0 277 printf("plaintext: %s\n", octet_string_hex_string(opaque, *opaque_len));
michael@0 278 #endif
michael@0 279
michael@0 280 #if ENC
michael@0 281 /* encrypt the opaque data */
michael@0 282 status = aes_cbc_nist_encrypt(&aes_ctx, opaque, opaque_len);
michael@0 283 if (status) return status;
michael@0 284 #endif
michael@0 285
michael@0 286 #if DEBUG
michael@0 287 printf("ciphertext len: %d\n", *opaque_len);
michael@0 288 printf("ciphertext: %s\n", octet_string_hex_string(opaque, *opaque_len));
michael@0 289 #endif
michael@0 290
michael@0 291 /*
michael@0 292 * authenticate clear and opaque data, then write the
michael@0 293 * authentication tag to the location immediately following the
michael@0 294 * ciphertext
michael@0 295 */
michael@0 296 status = hmac_init(&hmac_ctx, mac_key, MAC_KEY_LEN);
michael@0 297 if (status) return status;
michael@0 298
michael@0 299 status = hmac_start(&hmac_ctx);
michael@0 300 if (status) return status;
michael@0 301
michael@0 302 status = hmac_update(&hmac_ctx, clear, clear_len);
michael@0 303 if (status) return status;
michael@0 304 #if DEBUG
michael@0 305 printf("hmac input: %s\n",
michael@0 306 octet_string_hex_string(clear, clear_len));
michael@0 307 #endif
michael@0 308 auth_tag = (unsigned char *)opaque;
michael@0 309 auth_tag += *opaque_len;
michael@0 310 status = hmac_compute(&hmac_ctx, opaque, *opaque_len, TAG_LEN, auth_tag);
michael@0 311 if (status) return status;
michael@0 312 #if DEBUG
michael@0 313 printf("hmac input: %s\n",
michael@0 314 octet_string_hex_string(opaque, *opaque_len));
michael@0 315 #endif
michael@0 316 /* bump up the opaque_len to reflect the authentication tag */
michael@0 317 *opaque_len += TAG_LEN;
michael@0 318
michael@0 319 #if DEBUG
michael@0 320 printf("prot data len: %d\n", *opaque_len);
michael@0 321 printf("prot data: %s\n", octet_string_hex_string(opaque, *opaque_len));
michael@0 322 #endif
michael@0 323 }
michael@0 324
michael@0 325 return err_status_ok;
michael@0 326 }
michael@0 327
michael@0 328 err_status_t
michael@0 329 aes_128_cbc_hmac_sha1_96_dec(void *key,
michael@0 330 const void *clear,
michael@0 331 unsigned clear_len,
michael@0 332 void *iv,
michael@0 333 void *opaque,
michael@0 334 unsigned *opaque_len) {
michael@0 335 aes_cbc_ctx_t aes_ctx;
michael@0 336 hmac_ctx_t hmac_ctx;
michael@0 337 unsigned char enc_key[ENC_KEY_LEN];
michael@0 338 unsigned char mac_key[MAC_KEY_LEN];
michael@0 339 unsigned char tmp_tag[TAG_LEN];
michael@0 340 unsigned char *auth_tag;
michael@0 341 unsigned ciphertext_len;
michael@0 342 err_status_t status;
michael@0 343 int i;
michael@0 344
michael@0 345 /* check if we're doing authentication only */
michael@0 346 if ((iv == NULL) && (opaque == NULL) && (opaque_len == NULL)) {
michael@0 347
michael@0 348 /* perform authentication only */
michael@0 349
michael@0 350 } else if ((iv == NULL) || (opaque == NULL) || (opaque_len == NULL)) {
michael@0 351
michael@0 352 /*
michael@0 353 * bad parameter - we expect either all three pointers to be NULL,
michael@0 354 * or none of those pointers to be NULL
michael@0 355 */
michael@0 356 return err_status_fail;
michael@0 357
michael@0 358 } else {
michael@0 359 #if DEBUG
michael@0 360 printf("DEC using key %s\n", octet_string_hex_string(key, KEY_LEN));
michael@0 361 #endif
michael@0 362
michael@0 363 /* derive encryption and authentication keys from the input key */
michael@0 364 status = hmac_init(&hmac_ctx, key, KEY_LEN);
michael@0 365 if (status) return status;
michael@0 366 status = hmac_compute(&hmac_ctx, "ENC", 3, ENC_KEY_LEN, enc_key);
michael@0 367 if (status) return status;
michael@0 368
michael@0 369 status = hmac_init(&hmac_ctx, key, KEY_LEN);
michael@0 370 if (status) return status;
michael@0 371 status = hmac_compute(&hmac_ctx, "MAC", 3, MAC_KEY_LEN, mac_key);
michael@0 372 if (status) return status;
michael@0 373
michael@0 374 #if DEBUG
michael@0 375 printf("prot data len: %d\n", *opaque_len);
michael@0 376 printf("prot data: %s\n", octet_string_hex_string(opaque, *opaque_len));
michael@0 377 #endif
michael@0 378
michael@0 379 /*
michael@0 380 * set the protected data length to that of the ciphertext, by
michael@0 381 * subtracting out the length of the authentication tag
michael@0 382 */
michael@0 383 ciphertext_len = *opaque_len - TAG_LEN;
michael@0 384
michael@0 385 #if DEBUG
michael@0 386 printf("ciphertext len: %d\n", ciphertext_len);
michael@0 387 #endif
michael@0 388 /* verify the authentication tag */
michael@0 389
michael@0 390 /*
michael@0 391 * compute the authentication tag for the clear and opaque data,
michael@0 392 * and write it to a temporary location
michael@0 393 */
michael@0 394 status = hmac_init(&hmac_ctx, mac_key, MAC_KEY_LEN);
michael@0 395 if (status) return status;
michael@0 396
michael@0 397 status = hmac_start(&hmac_ctx);
michael@0 398 if (status) return status;
michael@0 399
michael@0 400 status = hmac_update(&hmac_ctx, clear, clear_len);
michael@0 401 if (status) return status;
michael@0 402
michael@0 403 #if DEBUG
michael@0 404 printf("hmac input: %s\n",
michael@0 405 octet_string_hex_string(clear, clear_len));
michael@0 406 #endif
michael@0 407
michael@0 408 status = hmac_compute(&hmac_ctx, opaque, ciphertext_len, TAG_LEN, tmp_tag);
michael@0 409 if (status) return status;
michael@0 410
michael@0 411 #if DEBUG
michael@0 412 printf("hmac input: %s\n",
michael@0 413 octet_string_hex_string(opaque, ciphertext_len));
michael@0 414 #endif
michael@0 415
michael@0 416 /*
michael@0 417 * compare the computed tag with the one provided as input (which
michael@0 418 * immediately follows the ciphertext)
michael@0 419 */
michael@0 420 auth_tag = (unsigned char *)opaque;
michael@0 421 auth_tag += ciphertext_len;
michael@0 422 #if DEBUG
michael@0 423 printf("auth_tag: %s\n", octet_string_hex_string(auth_tag, TAG_LEN));
michael@0 424 printf("tmp_tag: %s\n", octet_string_hex_string(tmp_tag, TAG_LEN));
michael@0 425 #endif
michael@0 426 for (i=0; i < TAG_LEN; i++) {
michael@0 427 if (tmp_tag[i] != auth_tag[i])
michael@0 428 return err_status_auth_fail;
michael@0 429 }
michael@0 430
michael@0 431 /* bump down the opaque_len to reflect the authentication tag */
michael@0 432 *opaque_len -= TAG_LEN;
michael@0 433
michael@0 434 /* decrypt the confidential data */
michael@0 435 status = aes_cbc_context_init(&aes_ctx, key, ENC_KEY_LEN, direction_decrypt);
michael@0 436 if (status) return status;
michael@0 437 status = aes_cbc_set_iv(&aes_ctx, iv);
michael@0 438 if (status) return status;
michael@0 439
michael@0 440 #if DEBUG
michael@0 441 printf("ciphertext: %s\n", octet_string_hex_string(opaque, *opaque_len));
michael@0 442 printf("iv: %s\n", octet_string_hex_string(iv, IV_LEN));
michael@0 443 #endif
michael@0 444
michael@0 445 #if ENC
michael@0 446 status = aes_cbc_nist_decrypt(&aes_ctx, opaque, &ciphertext_len);
michael@0 447 if (status) return status;
michael@0 448 #endif
michael@0 449
michael@0 450 #if DEBUG
michael@0 451 printf("plaintext len: %d\n", ciphertext_len);
michael@0 452 printf("plaintext: %s\n",
michael@0 453 octet_string_hex_string(opaque, ciphertext_len));
michael@0 454 #endif
michael@0 455
michael@0 456 /* indicate the length of the plaintext */
michael@0 457 *opaque_len = ciphertext_len;
michael@0 458 }
michael@0 459
michael@0 460 return err_status_ok;
michael@0 461 }
michael@0 462
michael@0 463 cryptoalg_ctx_t cryptoalg_ctx = {
michael@0 464 aes_128_cbc_hmac_sha1_96_enc,
michael@0 465 aes_128_cbc_hmac_sha1_96_dec,
michael@0 466 KEY_LEN,
michael@0 467 IV_LEN,
michael@0 468 TAG_LEN,
michael@0 469 MAX_EXPAND,
michael@0 470 };
michael@0 471
michael@0 472 cryptoalg_t cryptoalg = &cryptoalg_ctx;
michael@0 473
michael@0 474 #define NULL_TAG_LEN 12
michael@0 475
michael@0 476 err_status_t
michael@0 477 null_enc(void *key,
michael@0 478 const void *clear,
michael@0 479 unsigned clear_len,
michael@0 480 void *iv,
michael@0 481 void *opaque,
michael@0 482 unsigned *opaque_len) {
michael@0 483 int i;
michael@0 484 unsigned char *auth_tag;
michael@0 485 unsigned char *init_vec = iv;
michael@0 486
michael@0 487 /* check if we're doing authentication only */
michael@0 488 if ((iv == NULL) && (opaque == NULL) && (opaque_len == NULL)) {
michael@0 489
michael@0 490 /* perform authentication only */
michael@0 491
michael@0 492 } else if ((iv == NULL) || (opaque == NULL) || (opaque_len == NULL)) {
michael@0 493
michael@0 494 /*
michael@0 495 * bad parameter - we expect either all three pointers to be NULL,
michael@0 496 * or none of those pointers to be NULL
michael@0 497 */
michael@0 498 return err_status_fail;
michael@0 499
michael@0 500 } else {
michael@0 501
michael@0 502 #if DEBUG
michael@0 503 printf("NULL ENC using key %s\n", octet_string_hex_string(key, KEY_LEN));
michael@0 504 printf("NULL_TAG_LEN: %d\n", NULL_TAG_LEN);
michael@0 505 printf("plaintext len: %d\n", *opaque_len);
michael@0 506 #endif
michael@0 507 for (i=0; i < IV_LEN; i++)
michael@0 508 init_vec[i] = i + (i * 16);
michael@0 509 #if DEBUG
michael@0 510 printf("iv: %s\n",
michael@0 511 octet_string_hex_string(iv, IV_LEN));
michael@0 512 printf("plaintext: %s\n",
michael@0 513 octet_string_hex_string(opaque, *opaque_len));
michael@0 514 #endif
michael@0 515 auth_tag = opaque;
michael@0 516 auth_tag += *opaque_len;
michael@0 517 for (i=0; i < NULL_TAG_LEN; i++)
michael@0 518 auth_tag[i] = i + (i * 16);
michael@0 519 *opaque_len += NULL_TAG_LEN;
michael@0 520 #if DEBUG
michael@0 521 printf("protected data len: %d\n", *opaque_len);
michael@0 522 printf("protected data: %s\n",
michael@0 523 octet_string_hex_string(opaque, *opaque_len));
michael@0 524 #endif
michael@0 525
michael@0 526 }
michael@0 527
michael@0 528 return err_status_ok;
michael@0 529 }
michael@0 530
michael@0 531 err_status_t
michael@0 532 null_dec(void *key,
michael@0 533 const void *clear,
michael@0 534 unsigned clear_len,
michael@0 535 void *iv,
michael@0 536 void *opaque,
michael@0 537 unsigned *opaque_len) {
michael@0 538 unsigned char *auth_tag;
michael@0 539
michael@0 540 /* check if we're doing authentication only */
michael@0 541 if ((iv == NULL) && (opaque == NULL) && (opaque_len == NULL)) {
michael@0 542
michael@0 543 /* perform authentication only */
michael@0 544
michael@0 545 } else if ((iv == NULL) || (opaque == NULL) || (opaque_len == NULL)) {
michael@0 546
michael@0 547 /*
michael@0 548 * bad parameter - we expect either all three pointers to be NULL,
michael@0 549 * or none of those pointers to be NULL
michael@0 550 */
michael@0 551 return err_status_fail;
michael@0 552
michael@0 553 } else {
michael@0 554
michael@0 555 #if DEBUG
michael@0 556 printf("NULL DEC using key %s\n", octet_string_hex_string(key, KEY_LEN));
michael@0 557
michael@0 558 printf("protected data len: %d\n", *opaque_len);
michael@0 559 printf("protected data: %s\n",
michael@0 560 octet_string_hex_string(opaque, *opaque_len));
michael@0 561 #endif
michael@0 562 auth_tag = opaque;
michael@0 563 auth_tag += (*opaque_len - NULL_TAG_LEN);
michael@0 564 #if DEBUG
michael@0 565 printf("iv: %s\n", octet_string_hex_string(iv, IV_LEN));
michael@0 566 #endif
michael@0 567 *opaque_len -= NULL_TAG_LEN;
michael@0 568 #if DEBUG
michael@0 569 printf("plaintext len: %d\n", *opaque_len);
michael@0 570 printf("plaintext: %s\n",
michael@0 571 octet_string_hex_string(opaque, *opaque_len));
michael@0 572 #endif
michael@0 573 }
michael@0 574
michael@0 575 return err_status_ok;
michael@0 576 }
michael@0 577
michael@0 578 cryptoalg_ctx_t null_cryptoalg_ctx = {
michael@0 579 null_enc,
michael@0 580 null_dec,
michael@0 581 KEY_LEN,
michael@0 582 IV_LEN,
michael@0 583 NULL_TAG_LEN,
michael@0 584 MAX_EXPAND,
michael@0 585 };
michael@0 586
michael@0 587 cryptoalg_t null_cryptoalg = &null_cryptoalg_ctx;
michael@0 588
michael@0 589 int
michael@0 590 cryptoalg_get_id(cryptoalg_t c) {
michael@0 591 if (c == cryptoalg)
michael@0 592 return 1;
michael@0 593 return 0;
michael@0 594 }
michael@0 595
michael@0 596 cryptoalg_t
michael@0 597 cryptoalg_find_by_id(int id) {
michael@0 598 switch(id) {
michael@0 599 case 1:
michael@0 600 return cryptoalg;
michael@0 601 default:
michael@0 602 break;
michael@0 603 }
michael@0 604 return 0;
michael@0 605 }

mercurial