Ruby  1.9.3p547(2014-05-14revision45962)
ossl_pkcs5.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  * Copyright (C) 2007 Technorama Ltd. <oss-ruby@technorama.net>
4  */
5 #include "ossl.h"
6 
9 
10 #ifdef HAVE_PKCS5_PBKDF2_HMAC
11 /*
12  * call-seq:
13  * PKCS5.pbkdf2_hmac(pass, salt, iter, keylen, digest) => string
14  *
15  * === Parameters
16  * * +pass+ - string
17  * * +salt+ - string
18  * * +iter+ - integer - should be greater than 1000. 2000 is better.
19  * * +keylen+ - integer
20  * * +digest+ - a string or OpenSSL::Digest object.
21  *
22  * Available in OpenSSL 0.9.9?.
23  *
24  * Digests other than SHA1 may not be supported by other cryptography libraries.
25  */
26 static VALUE
27 ossl_pkcs5_pbkdf2_hmac(VALUE self, VALUE pass, VALUE salt, VALUE iter, VALUE keylen, VALUE digest)
28 {
29  VALUE str;
30  const EVP_MD *md;
31  int len = NUM2INT(keylen);
32 
33  StringValue(pass);
34  StringValue(salt);
35  md = GetDigestPtr(digest);
36 
37  str = rb_str_new(0, len);
38 
39  if (PKCS5_PBKDF2_HMAC(RSTRING_PTR(pass), RSTRING_LEN(pass),
40  (unsigned char *)RSTRING_PTR(salt), RSTRING_LEN(salt),
41  NUM2INT(iter), md, len,
42  (unsigned char *)RSTRING_PTR(str)) != 1)
43  ossl_raise(ePKCS5, "PKCS5_PBKDF2_HMAC");
44 
45  return str;
46 }
47 #else
48 #define ossl_pkcs5_pbkdf2_hmac rb_f_notimplement
49 #endif
50 
51 
52 #ifdef HAVE_PKCS5_PBKDF2_HMAC_SHA1
53 /*
54  * call-seq:
55  * PKCS5.pbkdf2_hmac_sha1(pass, salt, iter, keylen) => string
56  *
57  * === Parameters
58  * * +pass+ - string
59  * * +salt+ - string
60  * * +iter+ - integer - should be greater than 1000. 2000 is better.
61  * * +keylen+ - integer
62  *
63  * This method is available almost any version OpenSSL.
64  *
65  * Conforms to rfc2898.
66  */
67 static VALUE
68 ossl_pkcs5_pbkdf2_hmac_sha1(VALUE self, VALUE pass, VALUE salt, VALUE iter, VALUE keylen)
69 {
70  VALUE str;
71  int len = NUM2INT(keylen);
72 
73  StringValue(pass);
74  StringValue(salt);
75 
76  str = rb_str_new(0, len);
77 
78  if (PKCS5_PBKDF2_HMAC_SHA1(RSTRING_PTR(pass), RSTRING_LENINT(pass),
79  (const unsigned char *)RSTRING_PTR(salt), RSTRING_LENINT(salt), NUM2INT(iter),
80  len, (unsigned char *)RSTRING_PTR(str)) != 1)
81  ossl_raise(ePKCS5, "PKCS5_PBKDF2_HMAC_SHA1");
82 
83  return str;
84 }
85 #else
86 #define ossl_pkcs5_pbkdf2_hmac_sha1 rb_f_notimplement
87 #endif
88 
89 void
91 {
92  /*
93  * Password-based Encryption
94  *
95  */
98 
101 }
#define RSTRING_LEN(string)
Definition: generator.h:45
VALUE mOSSL
Definition: ossl.c:250
#define NUM2INT(x)
Definition: ruby.h:536
#define RSTRING_PTR(string)
Definition: generator.h:42
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:515
#define ossl_pkcs5_pbkdf2_hmac
Definition: ossl_pkcs5.c:48
void Init_ossl_pkcs5()
Definition: ossl_pkcs5.c:90
const EVP_MD * GetDigestPtr(VALUE obj)
Definition: ossl_digest.c:36
VALUE eOSSLError
Definition: ossl.c:255
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1358
unsigned long VALUE
Definition: ruby.h:88
register unsigned int len
Definition: name2ctype.h:22210
VALUE ePKCS5
Definition: ossl_pkcs5.c:8
VALUE mPKCS5
Definition: ossl_pkcs5.c:7
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:607
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:324
#define RSTRING_LENINT(str)
Definition: ruby.h:684
#define ossl_pkcs5_pbkdf2_hmac_sha1
Definition: ossl_pkcs5.c:86
#define StringValue(v)
Definition: ruby.h:466
VALUE rb_str_new(const char *, long)
Definition: string.c:410