1121259Sphk/*- 2121259Sphk * Copyright (c) 2003 Poul-Henning Kamp 3121259Sphk * All rights reserved. 4121259Sphk * 5121259Sphk * Redistribution and use in source and binary forms, with or without 6121259Sphk * modification, are permitted provided that the following conditions 7121259Sphk * are met: 8121259Sphk * 1. Redistributions of source code must retain the above copyright 9121259Sphk * notice, this list of conditions and the following disclaimer. 10121259Sphk * 2. Redistributions in binary form must reproduce the above copyright 11121259Sphk * notice, this list of conditions and the following disclaimer in the 12121259Sphk * documentation and/or other materials provided with the distribution. 13121259Sphk * 14121259Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15121259Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16121259Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17121259Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18121259Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19121259Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20121259Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21121259Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22121259Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23121259Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24121259Sphk * SUCH DAMAGE. 25121259Sphk * 26121259Sphk * $FreeBSD: releng/11.0/sys/crypto/rijndael/test00.c 175360 2008-01-15 18:34:47Z sobomax $ 27121259Sphk * 28121259Sphk * This test checks for inplace decryption working. This is the case 29121259Sphk * where the same buffer is passed as input and output to 30121259Sphk * rijndael_blockDecrypt(). 31121259Sphk */ 32121259Sphk 33121259Sphk#include <stdio.h> 34121259Sphk#include <sys/param.h> 35121259Sphk#include <sys/types.h> 36121259Sphk 37175360Ssobomax#include <crypto/rijndael/rijndael-api-fst.h> 38121259Sphk 39121259Sphk#define LL 32 40121259Sphkint 41121259Sphkmain(int argc, char **argv) 42121259Sphk{ 43121259Sphk keyInstance ki; 44121259Sphk cipherInstance ci; 45121259Sphk uint8_t key[16]; 46121259Sphk uint8_t in[LL]; 47121259Sphk uint8_t out[LL]; 48121259Sphk int i, j; 49121259Sphk 50121259Sphk rijndael_cipherInit(&ci, MODE_CBC, NULL); 51121259Sphk for (i = 0; i < 16; i++) 52121259Sphk key[i] = i; 53121259Sphk rijndael_makeKey(&ki, DIR_DECRYPT, 128, key); 54121259Sphk for (i = 0; i < LL; i++) 55121259Sphk in[i] = i; 56121259Sphk rijndael_blockDecrypt(&ci, &ki, in, LL * 8, out); 57121259Sphk for (i = 0; i < LL; i++) 58121259Sphk printf("%02x", out[i]); 59121259Sphk putchar('\n'); 60121259Sphk rijndael_blockDecrypt(&ci, &ki, in, LL * 8, in); 61121259Sphk j = 0; 62121259Sphk for (i = 0; i < LL; i++) { 63121259Sphk printf("%02x", in[i]); 64121259Sphk if (in[i] != out[i]) 65121259Sphk j++; 66121259Sphk } 67121259Sphk putchar('\n'); 68121259Sphk if (j != 0) { 69121259Sphk fprintf(stderr, 70121259Sphk "Error: inplace decryption fails in %d places\n", j); 71121259Sphk return (1); 72121259Sphk } else { 73121259Sphk return (0); 74121259Sphk } 75121259Sphk} 76