mirror of
https://github.com/samba-team/samba.git
synced 2024-12-22 13:34:15 +03:00
lib/crypto: add aes encryption routines to main cryto lib.
Guenther
This commit is contained in:
parent
8703d9f5bf
commit
87acb96fc3
114
lib/crypto/aes.c
Normal file
114
lib/crypto/aes.c
Normal file
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) 2003 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "replace.h"
|
||||
|
||||
#include "rijndael-alg-fst.h"
|
||||
#include "aes.h"
|
||||
|
||||
int
|
||||
AES_set_encrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
|
||||
{
|
||||
key->rounds = rijndaelKeySetupEnc(key->key, userkey, bits);
|
||||
if (key->rounds == 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
AES_set_decrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
|
||||
{
|
||||
key->rounds = rijndaelKeySetupDec(key->key, userkey, bits);
|
||||
if (key->rounds == 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
|
||||
{
|
||||
rijndaelEncrypt(key->key, key->rounds, in, out);
|
||||
}
|
||||
|
||||
void
|
||||
AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
|
||||
{
|
||||
rijndaelDecrypt(key->key, key->rounds, in, out);
|
||||
}
|
||||
|
||||
void
|
||||
AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
||||
unsigned long size, const AES_KEY *key,
|
||||
unsigned char *iv, int forward_encrypt)
|
||||
{
|
||||
unsigned char tmp[AES_BLOCK_SIZE];
|
||||
int i;
|
||||
|
||||
if (forward_encrypt) {
|
||||
while (size >= AES_BLOCK_SIZE) {
|
||||
for (i = 0; i < AES_BLOCK_SIZE; i++)
|
||||
tmp[i] = in[i] ^ iv[i];
|
||||
AES_encrypt(tmp, out, key);
|
||||
memcpy(iv, out, AES_BLOCK_SIZE);
|
||||
size -= AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
out += AES_BLOCK_SIZE;
|
||||
}
|
||||
if (size) {
|
||||
for (i = 0; i < size; i++)
|
||||
tmp[i] = in[i] ^ iv[i];
|
||||
for (i = size; i < AES_BLOCK_SIZE; i++)
|
||||
tmp[i] = iv[i];
|
||||
AES_encrypt(tmp, out, key);
|
||||
memcpy(iv, out, AES_BLOCK_SIZE);
|
||||
}
|
||||
} else {
|
||||
while (size >= AES_BLOCK_SIZE) {
|
||||
memcpy(tmp, in, AES_BLOCK_SIZE);
|
||||
AES_decrypt(tmp, out, key);
|
||||
for (i = 0; i < AES_BLOCK_SIZE; i++)
|
||||
out[i] ^= iv[i];
|
||||
memcpy(iv, tmp, AES_BLOCK_SIZE);
|
||||
size -= AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
out += AES_BLOCK_SIZE;
|
||||
}
|
||||
if (size) {
|
||||
memcpy(tmp, in, AES_BLOCK_SIZE);
|
||||
AES_decrypt(tmp, out, key);
|
||||
for (i = 0; i < size; i++)
|
||||
out[i] ^= iv[i];
|
||||
memcpy(iv, tmp, AES_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
79
lib/crypto/aes.h
Normal file
79
lib/crypto/aes.h
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (c) 2003-2004 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef LIB_CRYPTO_AES_H
|
||||
#define LIB_CRYPTO_AES_H 1
|
||||
|
||||
/* symbol renaming */
|
||||
#define AES_set_encrypt_key samba_AES_set_encrypt_key
|
||||
#define AES_set_decrypt_key samba_AES_decrypt_key
|
||||
#define AES_encrypt samba_AES_encrypt
|
||||
#define AES_decrypt samba_AES_decrypt
|
||||
#define AES_cbc_encrypt samba_AES_cbc_encrypt
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#define AES_BLOCK_SIZE 16
|
||||
#define AES_MAXNR 14
|
||||
|
||||
#define AES_ENCRYPT 1
|
||||
#define AES_DECRYPT 0
|
||||
|
||||
typedef struct aes_key {
|
||||
uint32_t key[(AES_MAXNR+1)*4];
|
||||
int rounds;
|
||||
} AES_KEY;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int AES_set_encrypt_key(const unsigned char *, const int, AES_KEY *);
|
||||
int AES_set_decrypt_key(const unsigned char *, const int, AES_KEY *);
|
||||
|
||||
void AES_encrypt(const unsigned char *, unsigned char *, const AES_KEY *);
|
||||
void AES_decrypt(const unsigned char *, unsigned char *, const AES_KEY *);
|
||||
|
||||
void AES_cbc_encrypt(const unsigned char *, unsigned char *,
|
||||
const unsigned long, const AES_KEY *,
|
||||
unsigned char *, int);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LIB_CRYPTO_AES_H */
|
1223
lib/crypto/rijndael-alg-fst.c
Normal file
1223
lib/crypto/rijndael-alg-fst.c
Normal file
File diff suppressed because it is too large
Load Diff
46
lib/crypto/rijndael-alg-fst.h
Normal file
46
lib/crypto/rijndael-alg-fst.h
Normal file
@ -0,0 +1,46 @@
|
||||
/* $NetBSD: rijndael-alg-fst.h,v 1.2 2000/10/02 17:19:15 itojun Exp $ */
|
||||
/* $KAME: rijndael-alg-fst.h,v 1.5 2003/07/15 10:47:16 itojun Exp $ */
|
||||
/**
|
||||
* rijndael-alg-fst.h
|
||||
*
|
||||
* @version 3.0 (December 2000)
|
||||
*
|
||||
* Optimised ANSI C code for the Rijndael cipher (now AES)
|
||||
*
|
||||
* @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
|
||||
* @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
|
||||
* @author Paulo Barreto <paulo.barreto@terra.com.br>
|
||||
*
|
||||
* This code is hereby placed in the public domain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||||
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef LIB_CRYPTO_RIJNDAEL_ALG_FST_H
|
||||
#define LIB_CRYPTO_RIJNDAEL_ALG_FST_H
|
||||
|
||||
/* symbol renaming */
|
||||
#define rijndaelKeySetupEnc _samba_rijndaelKeySetupEnc
|
||||
#define rijndaelKeySetupDec _samba_rijndaelKeySetupDec
|
||||
#define rijndaelEncrypt _samba_rijndaelEncrypt
|
||||
#define rijndaelDecrypt _samba_rijndaelDecrypt
|
||||
|
||||
#define RIJNDAEL_MAXKC (256/32)
|
||||
#define RIJNDAEL_MAXKB (256/8)
|
||||
#define RIJNDAEL_MAXNR 14
|
||||
|
||||
int rijndaelKeySetupEnc(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], int keyBits);
|
||||
int rijndaelKeySetupDec(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], int keyBits);
|
||||
void rijndaelEncrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, const uint8_t pt[16], uint8_t ct[16]);
|
||||
void rijndaelDecrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, const uint8_t ct[16], uint8_t pt[16]);
|
||||
|
||||
#endif /* LIB_CRYPTO_RIJNDAEL_ALG_FST_H */
|
@ -372,7 +372,8 @@ UTIL_OBJ = ../lib/util/rbtree.o ../lib/util/signal.o ../lib/util/time.o \
|
||||
CRYPTO_OBJ = ../lib/crypto/crc32.o ../lib/crypto/md5.o \
|
||||
../lib/crypto/hmacmd5.o ../lib/crypto/arcfour.o \
|
||||
../lib/crypto/md4.o \
|
||||
../lib/crypto/sha256.o ../lib/crypto/hmacsha256.o
|
||||
../lib/crypto/sha256.o ../lib/crypto/hmacsha256.o \
|
||||
../lib/crypto/aes.o ../lib/crypto/rijndael-alg-fst.o
|
||||
|
||||
LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) \
|
||||
lib/messages.o librpc/gen_ndr/ndr_messaging.o lib/messages_local.o \
|
||||
|
Loading…
Reference in New Issue
Block a user