]> git.proxmox.com Git - ceph.git/blob - ceph/src/cls_crypto.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / cls_crypto.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 #include <iostream>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <errno.h>
7
8 #include <openssl/md5.h>
9 #include <openssl/sha.h>
10
11 #include "include/types.h"
12 #include "objclass/objclass.h"
13
14 CLS_VER(1,0)
15 CLS_NAME(crypto)
16
17 int md5_method(cls_method_context_t ctx, char *indata, int datalen,
18 char **outdata, int *outdatalen)
19 {
20 MD5_CTX c;
21 unsigned char *md;
22
23 cls_log("md5 method");
24 cls_log("indata=%.*s data_len=%d", datalen, indata, datalen);
25
26 md = (unsigned char *)cls_alloc(MD5_DIGEST_LENGTH);
27 if (!md)
28 return -ENOMEM;
29
30 MD5_Init(&c);
31 MD5_Update(&c, indata, (unsigned long)datalen);
32 MD5_Final(md,&c);
33
34 *outdata = (char *)md;
35 *outdatalen = MD5_DIGEST_LENGTH;
36
37 return 0;
38 }
39
40 int sha1_method(cls_method_context_t ctx, char *indata, int datalen,
41 char **outdata, int *outdatalen)
42 {
43 SHA_CTX c;
44 unsigned char *md;
45
46 cls_log("sha1 method");
47 cls_log("indata=%.*s data_len=%d", datalen, indata, datalen);
48
49 md = (unsigned char *)cls_alloc(SHA_DIGEST_LENGTH);
50 if (!md)
51 return -ENOMEM;
52
53 SHA1_Init(&c);
54 SHA1_Update(&c, indata, (unsigned long)datalen);
55 SHA1_Final(md,&c);
56
57 *outdata = (char *)md;
58 *outdatalen = SHA_DIGEST_LENGTH;
59
60 return 0;
61 }
62
63 CLS_INIT(crypto)
64 {
65 cls_log("Loaded crypto class!");
66
67 cls_handle_t h_class;
68 cls_method_handle_t h_md5;
69 cls_method_handle_t h_sha1;
70
71 cls_register("crypto", &h_class);
72 cls_register_method(h_class, "md5", CLS_METHOD_RD, md5_method, &h_md5);
73 cls_register_method(h_class, "sha1", CLS_METHOD_RD, sha1_method, &h_sha1);
74
75 return;
76 }
77