Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sha_drv.c « tls « src - github.com/processone/ejabberd.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8dcdb90e39c596ad85692387cc7e68804847b20b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * ejabberd, Copyright (C) 2002-2012   ProcessOne
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 */

#include <erl_driver.h>
#include <openssl/sha.h>
#ifdef HAVE_MD2
#include <openssl/md2.h>
#endif

/*
 * R15B changed several driver callbacks to use ErlDrvSizeT and
 * ErlDrvSSizeT typedefs instead of int.
 * This provides missing typedefs on older OTP versions.
 */
#if ERL_DRV_EXTENDED_MAJOR_VERSION < 2
typedef int ErlDrvSizeT;
typedef int ErlDrvSSizeT;
#endif

static ErlDrvData sha_drv_start(ErlDrvPort port, char *buf)
{
  set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
  return NULL;
}

static ErlDrvSSizeT sha_drv_control(ErlDrvData handle,
			   unsigned int command,
			   char *buf, ErlDrvSizeT len,
			   char **rbuf, ErlDrvSizeT rlen)
{
  ErlDrvBinary *b = NULL;

  switch (command) {
#ifdef HAVE_MD2
  case 2:
    rlen = MD2_DIGEST_LENGTH;
    b = driver_alloc_binary(rlen);
    if (b) MD2((unsigned char*)buf, len, (unsigned char*)b->orig_bytes);
    break;
#endif
  case 224:
    rlen = SHA224_DIGEST_LENGTH;
    b = driver_alloc_binary(rlen);
    if (b) SHA224((unsigned char*)buf, len, (unsigned char*)b->orig_bytes);
    break;
  case 256:
    rlen = SHA256_DIGEST_LENGTH;
    b = driver_alloc_binary(rlen);
    if (b) SHA256((unsigned char*)buf, len, (unsigned char*)b->orig_bytes);
    break;
  case 384:
    rlen = SHA384_DIGEST_LENGTH;
    b = driver_alloc_binary(rlen);
    if (b) SHA384((unsigned char*)buf, len, (unsigned char*)b->orig_bytes);
    break;
  case 512:
    rlen = SHA512_DIGEST_LENGTH;
    b = driver_alloc_binary(rlen);
    if (b) SHA512((unsigned char*)buf, len, (unsigned char*)b->orig_bytes);
    break;
  };
  
  if (b) {
    *rbuf = (char *)b;
  } else {
    *rbuf = NULL;
    rlen = 0;
  };

  return rlen;
}

ErlDrvEntry sha_driver_entry = {
  NULL,			/* F_PTR init, N/A */
  sha_drv_start,        /* L_PTR start, called when port is opened */
  NULL,                 /* F_PTR stop, called when port is closed */
  NULL,			/* F_PTR output, called when erlang has sent */
  NULL,			/* F_PTR ready_input, called when input descriptor ready */
  NULL,			/* F_PTR ready_output, called when output descriptor ready */
  "sha_drv",		/* char *driver_name, the argument to open_port */
  NULL,                 /* F_PTR finish, called when unloaded */
  NULL,			/* handle */
  sha_drv_control,	/* F_PTR control, port_command callback */
  NULL,			/* F_PTR timeout, reserved */
  NULL,			/* F_PTR outputv, reserved */
  /* Added in Erlang/OTP R15B: */
  NULL,                 /* ready_async */
  NULL,                 /* flush */
  NULL,                 /* call */
  NULL,                 /* event */
  ERL_DRV_EXTENDED_MARKER,        /* extended_marker */
  ERL_DRV_EXTENDED_MAJOR_VERSION, /* major_version */
  ERL_DRV_EXTENDED_MINOR_VERSION, /* minor_version */
  0,                    /* driver_flags */
  NULL,                 /* handle2 */
  NULL,                 /* process_exit */
  NULL                  /* stop_select */
};

DRIVER_INIT(sha_drv) /* must match name in driver_entry */
{
  return &sha_driver_entry;
}