~ubuntu-branches/ubuntu/utopic/moonshot-gss-eap/utopic-backports

« back to all changes in this revision

Viewing changes to libeap/src/crypto/aes-wrap.c

  • Committer: Package Import Robot
  • Author(s): Sam Hartman
  • Date: 2014-09-16 08:38:39 UTC
  • Revision ID: package-import@ubuntu.com-20140916083839-ipqco3thb1wcwvs0
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * AES Key Wrap Algorithm (128-bit KEK) (RFC3394)
 
3
 *
 
4
 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License version 2 as
 
8
 * published by the Free Software Foundation.
 
9
 *
 
10
 * Alternatively, this software may be distributed under the terms of BSD
 
11
 * license.
 
12
 *
 
13
 * See README and COPYING for more details.
 
14
 */
 
15
 
 
16
#include "includes.h"
 
17
 
 
18
#include "common.h"
 
19
#include "aes.h"
 
20
#include "aes_wrap.h"
 
21
 
 
22
/**
 
23
 * aes_wrap - Wrap keys with AES Key Wrap Algorithm (128-bit KEK) (RFC3394)
 
24
 * @kek: 16-octet Key encryption key (KEK)
 
25
 * @n: Length of the plaintext key in 64-bit units; e.g., 2 = 128-bit = 16
 
26
 * bytes
 
27
 * @plain: Plaintext key to be wrapped, n * 64 bits
 
28
 * @cipher: Wrapped key, (n + 1) * 64 bits
 
29
 * Returns: 0 on success, -1 on failure
 
30
 */
 
31
int aes_wrap(const u8 *kek, int n, const u8 *plain, u8 *cipher)
 
32
{
 
33
        u8 *a, *r, b[16];
 
34
        int i, j;
 
35
        void *ctx;
 
36
 
 
37
        a = cipher;
 
38
        r = cipher + 8;
 
39
 
 
40
        /* 1) Initialize variables. */
 
41
        os_memset(a, 0xa6, 8);
 
42
        os_memcpy(r, plain, 8 * n);
 
43
 
 
44
        ctx = aes_encrypt_init(kek, 16);
 
45
        if (ctx == NULL)
 
46
                return -1;
 
47
 
 
48
        /* 2) Calculate intermediate values.
 
49
         * For j = 0 to 5
 
50
         *     For i=1 to n
 
51
         *         B = AES(K, A | R[i])
 
52
         *         A = MSB(64, B) ^ t where t = (n*j)+i
 
53
         *         R[i] = LSB(64, B)
 
54
         */
 
55
        for (j = 0; j <= 5; j++) {
 
56
                r = cipher + 8;
 
57
                for (i = 1; i <= n; i++) {
 
58
                        os_memcpy(b, a, 8);
 
59
                        os_memcpy(b + 8, r, 8);
 
60
                        aes_encrypt(ctx, b, b);
 
61
                        os_memcpy(a, b, 8);
 
62
                        a[7] ^= n * j + i;
 
63
                        os_memcpy(r, b + 8, 8);
 
64
                        r += 8;
 
65
                }
 
66
        }
 
67
        aes_encrypt_deinit(ctx);
 
68
 
 
69
        /* 3) Output the results.
 
70
         *
 
71
         * These are already in @cipher due to the location of temporary
 
72
         * variables.
 
73
         */
 
74
 
 
75
        return 0;
 
76
}