summaryrefslogtreecommitdiff
path: root/umac.c
diff options
context:
space:
mode:
Diffstat (limited to 'umac.c')
-rw-r--r--umac.c59
1 files changed, 24 insertions, 35 deletions
diff --git a/umac.c b/umac.c
index 0c62145fa..6eb55b26e 100644
--- a/umac.c
+++ b/umac.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: umac.c,v 1.8 2013/11/08 00:39:15 djm Exp $ */ 1/* $OpenBSD: umac.c,v 1.11 2014/07/22 07:13:42 guenther Exp $ */
2/* ----------------------------------------------------------------------- 2/* -----------------------------------------------------------------------
3 * 3 *
4 * umac.c -- C Implementation UMAC Message Authentication 4 * umac.c -- C Implementation UMAC Message Authentication
@@ -73,13 +73,15 @@
73 73
74#include "includes.h" 74#include "includes.h"
75#include <sys/types.h> 75#include <sys/types.h>
76
77#include "xmalloc.h"
78#include "umac.h"
79#include <string.h> 76#include <string.h>
77#include <stdio.h>
80#include <stdlib.h> 78#include <stdlib.h>
81#include <stddef.h> 79#include <stddef.h>
82 80
81#include "xmalloc.h"
82#include "umac.h"
83#include "misc.h"
84
83/* ---------------------------------------------------------------------- */ 85/* ---------------------------------------------------------------------- */
84/* --- Primitive Data Types --- */ 86/* --- Primitive Data Types --- */
85/* ---------------------------------------------------------------------- */ 87/* ---------------------------------------------------------------------- */
@@ -131,41 +133,17 @@ typedef unsigned int UWORD; /* Register */
131/* --- Endian Conversion --- Forcing assembly on some platforms */ 133/* --- Endian Conversion --- Forcing assembly on some platforms */
132/* ---------------------------------------------------------------------- */ 134/* ---------------------------------------------------------------------- */
133 135
134#if HAVE_SWAP32
135#define LOAD_UINT32_REVERSED(p) (swap32(*(const UINT32 *)(p)))
136#define STORE_UINT32_REVERSED(p,v) (*(UINT32 *)(p) = swap32(v))
137#else /* HAVE_SWAP32 */
138
139static UINT32 LOAD_UINT32_REVERSED(const void *ptr)
140{
141 UINT32 temp = *(const UINT32 *)ptr;
142 temp = (temp >> 24) | ((temp & 0x00FF0000) >> 8 )
143 | ((temp & 0x0000FF00) << 8 ) | (temp << 24);
144 return (UINT32)temp;
145}
146
147# if (__LITTLE_ENDIAN__)
148static void STORE_UINT32_REVERSED(void *ptr, UINT32 x)
149{
150 UINT32 i = (UINT32)x;
151 *(UINT32 *)ptr = (i >> 24) | ((i & 0x00FF0000) >> 8 )
152 | ((i & 0x0000FF00) << 8 ) | (i << 24);
153}
154# endif /* __LITTLE_ENDIAN */
155#endif /* HAVE_SWAP32 */
156
157/* The following definitions use the above reversal-primitives to do the right
158 * thing on endian specific load and stores.
159 */
160
161#if (__LITTLE_ENDIAN__) 136#if (__LITTLE_ENDIAN__)
162#define LOAD_UINT32_LITTLE(ptr) (*(const UINT32 *)(ptr)) 137#define LOAD_UINT32_REVERSED(p) get_u32(p)
163#define STORE_UINT32_BIG(ptr,x) STORE_UINT32_REVERSED(ptr,x) 138#define STORE_UINT32_REVERSED(p,v) put_u32(p,v)
164#else 139#else
165#define LOAD_UINT32_LITTLE(ptr) LOAD_UINT32_REVERSED(ptr) 140#define LOAD_UINT32_REVERSED(p) get_u32_le(p)
166#define STORE_UINT32_BIG(ptr,x) (*(UINT32 *)(ptr) = (UINT32)(x)) 141#define STORE_UINT32_REVERSED(p,v) put_u32_le(p,v)
167#endif 142#endif
168 143
144#define LOAD_UINT32_LITTLE(p) (get_u32_le(p))
145#define STORE_UINT32_BIG(p,v) put_u32(p, v)
146
169/* ---------------------------------------------------------------------- */ 147/* ---------------------------------------------------------------------- */
170/* ---------------------------------------------------------------------- */ 148/* ---------------------------------------------------------------------- */
171/* ----- Begin KDF & PDF Section ---------------------------------------- */ 149/* ----- Begin KDF & PDF Section ---------------------------------------- */
@@ -176,6 +154,7 @@ static void STORE_UINT32_REVERSED(void *ptr, UINT32 x)
176#define AES_BLOCK_LEN 16 154#define AES_BLOCK_LEN 16
177 155
178/* OpenSSL's AES */ 156/* OpenSSL's AES */
157#ifdef WITH_OPENSSL
179#include "openbsd-compat/openssl-compat.h" 158#include "openbsd-compat/openssl-compat.h"
180#ifndef USE_BUILTIN_RIJNDAEL 159#ifndef USE_BUILTIN_RIJNDAEL
181# include <openssl/aes.h> 160# include <openssl/aes.h>
@@ -185,6 +164,16 @@ typedef AES_KEY aes_int_key[1];
185 AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key) 164 AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key)
186#define aes_key_setup(key,int_key) \ 165#define aes_key_setup(key,int_key) \
187 AES_set_encrypt_key((const u_char *)(key),UMAC_KEY_LEN*8,int_key) 166 AES_set_encrypt_key((const u_char *)(key),UMAC_KEY_LEN*8,int_key)
167#else
168#include "rijndael.h"
169#define AES_ROUNDS ((UMAC_KEY_LEN / 4) + 6)
170typedef UINT8 aes_int_key[AES_ROUNDS+1][4][4]; /* AES internal */
171#define aes_encryption(in,out,int_key) \
172 rijndaelEncrypt((u32 *)(int_key), AES_ROUNDS, (u8 *)(in), (u8 *)(out))
173#define aes_key_setup(key,int_key) \
174 rijndaelKeySetupEnc((u32 *)(int_key), (const unsigned char *)(key), \
175 UMAC_KEY_LEN*8)
176#endif
188 177
189/* The user-supplied UMAC key is stretched using AES in a counter 178/* The user-supplied UMAC key is stretched using AES in a counter
190 * mode to supply all random bits needed by UMAC. The kdf function takes 179 * mode to supply all random bits needed by UMAC. The kdf function takes