Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# ext/openssl/ossl_pkcs5.c

Go to the documentation of this file.

    00001 /*
    00002  * $Id$
    00003  * Copyright (C) 2007 Technorama Ltd. <oss-ruby@technorama.net>
    00004  */
    00005 #include "ossl.h"
    00006
    00007 VALUE mPKCS5;
    00008 VALUE ePKCS5;
    00009
    00010 #ifdef HAVE_PKCS5_PBKDF2_HMAC
    00011 /*
    00012  * call-seq:
    00013  *    PKCS5.pbkdf2_hmac(pass, salt, iter, keylen, digest) => string
    00014  *
    00015  * === Parameters
    00016  * * +pass+ - string
    00017  * * +salt+ - string
    00018  * * +iter+ - integer - should be greater than 1000.  2000 is better.
    00019  * * +keylen+ - integer
    00020  * * +digest+ - a string or OpenSSL::Digest object.
    00021  *
    00022  * Available in OpenSSL 0.9.9?.
    00023  *
    00024  * Digests other than SHA1 may not be supported by other cryptography libraries.
    00025  */
    00026 static VALUE
    00027 ossl_pkcs5_pbkdf2_hmac(VALUE self, VALUE pass, VALUE salt, VALUE iter, VALUE keylen, VALUE digest)
    00028 {
    00029     VALUE str;
    00030     const EVP_MD *md;
    00031     int len = NUM2INT(keylen);
    00032
    00033     StringValue(pass);
    00034     StringValue(salt);
    00035     md = GetDigestPtr(digest);
    00036
    00037     str = rb_str_new(0, len);
    00038
    00039     if (PKCS5_PBKDF2_HMAC(RSTRING_PTR(pass), RSTRING_LEN(pass), RSTRING_PTR(salt), RSTRING_LEN(salt), NUM2INT(iter), md, len, RSTRING_PTR(str)) != 1)
    00040         ossl_raise(ePKCS5, "PKCS5_PBKDF2_HMAC");
    00041
    00042     return str;
    00043 }
    00044 #else
    00045 #define ossl_pkcs5_pbkdf2_hmac rb_f_notimplement
    00046 #endif
    00047
    00048
    00049 #ifdef HAVE_PKCS5_PBKDF2_HMAC_SHA1
    00050 /*
    00051  * call-seq:
    00052  *    PKCS5.pbkdf2_hmac_sha1(pass, salt, iter, keylen) => string
    00053  *
    00054  * === Parameters
    00055  * * +pass+ - string
    00056  * * +salt+ - string
    00057  * * +iter+ - integer - should be greater than 1000.  2000 is better.
    00058  * * +keylen+ - integer
    00059  *
    00060  * This method is available almost any version OpenSSL.
    00061  *
    00062  * Conforms to rfc2898.
    00063  */
    00064 static VALUE
    00065 ossl_pkcs5_pbkdf2_hmac_sha1(VALUE self, VALUE pass, VALUE salt, VALUE iter, VALUE keylen)
    00066 {
    00067     VALUE str;
    00068     int len = NUM2INT(keylen);
    00069
    00070     StringValue(pass);
    00071     StringValue(salt);
    00072
    00073     str = rb_str_new(0, len);
    00074
    00075     if (PKCS5_PBKDF2_HMAC_SHA1(RSTRING_PTR(pass), RSTRING_LEN(pass),
    00076                                (const unsigned char *)RSTRING_PTR(salt), RSTRING_LEN(salt), NUM2INT(iter),
    00077                                len, (unsigned char *)RSTRING_PTR(str)) != 1)
    00078         ossl_raise(ePKCS5, "PKCS5_PBKDF2_HMAC_SHA1");
    00079
    00080     return str;
    00081 }
    00082 #else
    00083 #define ossl_pkcs5_pbkdf2_hmac_sha1 rb_f_notimplement
    00084 #endif
    00085
    00086 void
    00087 Init_ossl_pkcs5()
    00088 {
    00089     /*
    00090      * Password-based Encryption
    00091      *
    00092      */
    00093     mPKCS5 = rb_define_module_under(mOSSL, "PKCS5");
    00094     ePKCS5 = rb_define_class_under(mPKCS5, "PKCS5Error", eOSSLError);
    00095
    00096     rb_define_module_function(mPKCS5, "pbkdf2_hmac", ossl_pkcs5_pbkdf2_hmac, 5);
    00097     rb_define_module_function(mPKCS5, "pbkdf2_hmac_sha1", ossl_pkcs5_pbkdf2_hmac_sha1, 4);
    00098 }
    00099