~ubuntu-branches/ubuntu/vivid/munge/vivid

« back to all changes in this revision

Viewing changes to src/munged/cipher.c

  • Committer: Bazaar Package Importer
  • Author(s): Gennaro Oliva
  • Date: 2011-02-28 20:41:12 UTC
  • mfrom: (6.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20110228204112-2lc8ss9geeusv5uo
Tags: 0.5.10-1
* New upstream release 
* Updated copyright, homepage, watch thanks to Chris Dunlap
* Standards version upgraded to 3.9.1.0 (no changes) 
* Switch to dpkg-source 3.0 (quilt) format
* Added explicit dependency by the same version of libmunge for munge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
 
 *  $Id: cipher.c 771 2010-03-02 23:14:07Z dun $
 
2
 *  $Id: cipher.c 890 2011-01-20 01:54:21Z chris.m.dunlap $
3
3
 *****************************************************************************
4
4
 *  Written by Chris Dunlap <cdunlap@llnl.gov>.
5
 
 *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
 
5
 *  Copyright (C) 2007-2011 Lawrence Livermore National Security, LLC.
6
6
 *  Copyright (C) 2002-2007 The Regents of the University of California.
7
7
 *  UCRL-CODE-155910.
8
8
 *
9
9
 *  This file is part of the MUNGE Uid 'N' Gid Emporium (MUNGE).
10
 
 *  For details, see <http://home.gna.org/munge/>.
 
10
 *  For details, see <http://munge.googlecode.com/>.
11
11
 *
12
12
 *  MUNGE is free software: you can redistribute it and/or modify it under
13
13
 *  the terms of the GNU General Public License as published by the Free
46
46
 
47
47
 
48
48
/*****************************************************************************
 
49
 *  Private Data
 
50
 *****************************************************************************/
 
51
 
 
52
static int _cipher_is_initialized = 0;
 
53
 
 
54
 
 
55
/*****************************************************************************
49
56
 *  Private Prototypes
50
57
 *****************************************************************************/
51
58
 
 
59
static void _cipher_init_subsystem (void);
52
60
static int _cipher_init (cipher_ctx *x, munge_cipher_t cipher,
53
61
    unsigned char *key, unsigned char *iv, int enc);
54
62
static int _cipher_update (cipher_ctx *x, void *dst, int *dstlen,
65
73
 *  Public Functions
66
74
 *****************************************************************************/
67
75
 
 
76
void
 
77
cipher_init_subsystem (void)
 
78
{
 
79
/*  Note that this call is *NOT* thread-safe.
 
80
 */
 
81
    if (! _cipher_is_initialized) {
 
82
        _cipher_init_subsystem ();
 
83
        _cipher_is_initialized++;
 
84
    }
 
85
    return;
 
86
}
 
87
 
 
88
 
68
89
int
69
90
cipher_init (cipher_ctx *x, munge_cipher_t cipher,
70
91
             unsigned char *key, unsigned char *iv, int enc)
71
92
{
72
93
    int rc;
73
94
 
 
95
    assert (_cipher_is_initialized);
74
96
    assert (x != NULL);
75
97
    assert (key != NULL);
76
98
    assert (iv != NULL);
91
113
{
92
114
    int rc;
93
115
 
 
116
    assert (_cipher_is_initialized);
94
117
    assert (x != NULL);
95
118
    assert (x->magic == CIPHER_MAGIC);
96
119
    assert (x->finalized != 1);
114
137
{
115
138
    int rc;
116
139
 
 
140
    assert (_cipher_is_initialized);
117
141
    assert (x != NULL);
118
142
    assert (x->magic == CIPHER_MAGIC);
119
143
    assert (x->finalized != 1);
134
158
{
135
159
    int rc;
136
160
 
 
161
    assert (_cipher_is_initialized);
137
162
    assert (x != NULL);
138
163
    assert (x->magic == CIPHER_MAGIC);
139
164
 
147
172
int
148
173
cipher_block_size (munge_cipher_t cipher)
149
174
{
 
175
    assert (_cipher_is_initialized);
150
176
    return (_cipher_block_size (cipher));
151
177
}
152
178
 
154
180
int
155
181
cipher_iv_size (munge_cipher_t cipher)
156
182
{
 
183
    assert (_cipher_is_initialized);
157
184
    return (_cipher_iv_size (cipher));
158
185
}
159
186
 
161
188
int
162
189
cipher_key_size (munge_cipher_t cipher)
163
190
{
 
191
    assert (_cipher_is_initialized);
164
192
    return (_cipher_key_size (cipher));
165
193
}
166
194
 
168
196
int
169
197
cipher_map_enum (munge_cipher_t cipher, void *dst)
170
198
{
 
199
    assert (_cipher_is_initialized);
171
200
    return (_cipher_map_enum (cipher, dst));
172
201
}
173
202
 
183
212
#include "common.h"
184
213
#include "log.h"
185
214
 
 
215
static int _cipher_map [MUNGE_CIPHER_LAST_ITEM];
 
216
 
186
217
static int _cipher_update_aux (cipher_ctx *x, void *dst, int *dstlen,
187
218
    const void *src, int srclen);
188
219
 
 
220
 
 
221
void
 
222
_cipher_init_subsystem (void)
 
223
{
 
224
    int i;
 
225
 
 
226
    for (i = 0; i < MUNGE_CIPHER_LAST_ITEM; i++) {
 
227
        _cipher_map [i] = -1;
 
228
    }
 
229
    _cipher_map [MUNGE_CIPHER_BLOWFISH] = GCRY_CIPHER_BLOWFISH;
 
230
    _cipher_map [MUNGE_CIPHER_CAST5] = GCRY_CIPHER_CAST5;
 
231
    _cipher_map [MUNGE_CIPHER_AES128] = GCRY_CIPHER_AES128;
 
232
    _cipher_map [MUNGE_CIPHER_AES256] = GCRY_CIPHER_AES256;
 
233
    return;
 
234
}
 
235
 
 
236
 
189
237
static int
190
238
_cipher_init (cipher_ctx *x, munge_cipher_t cipher,
191
239
              unsigned char *key, unsigned char *iv, int enc)
484
532
static int
485
533
_cipher_map_enum (munge_cipher_t cipher, void *dst)
486
534
{
487
 
    int algo;
488
 
    int rc = 0;
 
535
    int algo = -1;
489
536
 
490
 
    switch (cipher) {
491
 
        case MUNGE_CIPHER_BLOWFISH:
492
 
            algo = GCRY_CIPHER_BLOWFISH;
493
 
            break;
494
 
        case MUNGE_CIPHER_CAST5:
495
 
            algo = GCRY_CIPHER_CAST5;
496
 
            break;
497
 
        case MUNGE_CIPHER_AES128:
498
 
            algo = GCRY_CIPHER_AES128;
499
 
            break;
500
 
        case MUNGE_CIPHER_AES256:
501
 
            algo = GCRY_CIPHER_AES256;
502
 
            break;
503
 
        default:
504
 
            rc = -1;
505
 
            break;
506
 
    }
507
 
    if ((dst != NULL) && (rc == 0)) {
 
537
    if ((cipher > MUNGE_CIPHER_DEFAULT) && (cipher < MUNGE_CIPHER_LAST_ITEM)) {
 
538
        algo = _cipher_map [cipher];
 
539
    }
 
540
    if (algo < 0) {
 
541
        return (-1);
 
542
    }
 
543
    if (dst != NULL) {
508
544
        * (int *) dst = algo;
509
545
    }
510
 
    return (rc);
 
546
    return (0);
511
547
}
512
548
 
513
549
#endif /* HAVE_LIBGCRYPT */
535
571
 
536
572
#include <openssl/evp.h>
537
573
 
 
574
static const EVP_CIPHER *_cipher_map [MUNGE_CIPHER_LAST_ITEM];
 
575
 
 
576
 
 
577
void
 
578
_cipher_init_subsystem (void)
 
579
{
 
580
    int i;
 
581
 
 
582
    for (i = 0; i < MUNGE_CIPHER_LAST_ITEM; i++) {
 
583
        _cipher_map [i] = NULL;
 
584
    }
 
585
    _cipher_map [MUNGE_CIPHER_BLOWFISH] = EVP_bf_cbc ();
 
586
    _cipher_map [MUNGE_CIPHER_CAST5] = EVP_cast5_cbc ();
 
587
 
 
588
#if HAVE_EVP_AES_128_CBC
 
589
    _cipher_map [MUNGE_CIPHER_AES128] = EVP_aes_128_cbc ();
 
590
#endif /* HAVE_EVP_AES_128_CBC */
 
591
 
 
592
#if HAVE_EVP_AES_256_CBC && HAVE_EVP_SHA256
 
593
    _cipher_map [MUNGE_CIPHER_AES256] = EVP_aes_256_cbc ();
 
594
#endif /* HAVE_EVP_AES_256_CBC && HAVE_EVP_SHA256 */
 
595
 
 
596
    return;
 
597
}
 
598
 
 
599
 
538
600
static int
539
601
_cipher_init (cipher_ctx *x, munge_cipher_t cipher,
540
602
              unsigned char *key, unsigned char *iv, int enc)
640
702
static int
641
703
_cipher_map_enum (munge_cipher_t cipher, void *dst)
642
704
{
643
 
    const EVP_CIPHER *algo;
644
 
    int               rc = 0;
 
705
    const EVP_CIPHER *algo = NULL;
645
706
 
646
 
    switch (cipher) {
647
 
        case MUNGE_CIPHER_BLOWFISH:
648
 
            algo = EVP_bf_cbc ();
649
 
            break;
650
 
        case MUNGE_CIPHER_CAST5:
651
 
            algo = EVP_cast5_cbc ();
652
 
            break;
653
 
#if HAVE_EVP_AES_128_CBC
654
 
        case MUNGE_CIPHER_AES128:
655
 
            algo = EVP_aes_128_cbc ();
656
 
            break;
657
 
#endif /* HAVE_EVP_AES_128_CBC */
658
 
#if HAVE_EVP_AES_256_CBC && HAVE_EVP_SHA256
659
 
        case MUNGE_CIPHER_AES256:
660
 
            algo = EVP_aes_256_cbc ();
661
 
            break;
662
 
#endif /* HAVE_EVP_AES_256_CBC && HAVE_EVP_SHA256 */
663
 
        default:
664
 
            rc = -1;
665
 
            break;
666
 
    }
667
 
    if ((dst != NULL) && (rc == 0)) {
 
707
    if ((cipher > MUNGE_CIPHER_DEFAULT) && (cipher < MUNGE_CIPHER_LAST_ITEM)) {
 
708
        algo = _cipher_map [cipher];
 
709
    }
 
710
    if (algo == NULL) {
 
711
        return (-1);
 
712
    }
 
713
    if (dst != NULL) {
668
714
        * (const EVP_CIPHER **) dst = algo;
669
715
    }
670
 
    return (rc);
 
716
    return (0);
671
717
}
672
718
 
673
719
#endif /* HAVE_OPENSSL */