/
session.nim
105 lines (89 loc) · 3.27 KB
/
session.nim
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
# -*- nim -*-
#
# $Id$
#
# Copyright (c) 2017 Jordan Hrycaj <jordan@teddy-net.com>
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted.
#
# The author or authors of this code dedicate any and all copyright interest
# in this code to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and successors.
# We intend this dedication to be an overt act of relinquishment in
# perpetuity of all present and future rights to this code under copyright
# law.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Provide C interface => libsession.a
#
# Compile:
# crylib=..
# nim c -p:$crylib --app:staticLib --noMain --header session.nim
#
import
xcrypt
# ----------------------------------------------------------------------------
# Private functions
# ----------------------------------------------------------------------------
proc doB64Encrypt(s: string; pub: ptr EccPubKey): string =
var
ctx: XCryptCtx
keys = [pub, nil, nil]
chl = getXVerfier()
result = ctx.getXB64Encrypt(addr keys, addr chl) &
ctx.xB64Encrypt(s)
ctx.clearXCrypt
proc doB64Decrypt(s: string; prv: ptr EccPrvKey): string =
var
ctx: XCryptCtx
chl = getXVerfier()
pre = ctx.getXB64Decrypt(s, prv, addr chl)
doAssert 0 < pre
result = ctx.xB64Decrypt(s[pre..<s.len])
ctx.clearXCrypt
# ----------------------------------------------------------------------------
# Public functions
# ----------------------------------------------------------------------------
proc b64_encrypt*(s: cstring; pub: pointer): cstring {.exportc.} =
doB64Encrypt($s, cast[ptr EccPubKey](pub))
proc b64_decrypt*(s: cstring; prv: pointer): cstring {.exportc.} =
doB64Decrypt($s, cast[ptr EccPrvKey](prv))
proc prvkey*: pointer {.exportc.} =
var prvKey: EccPrvKey
prvKey.getEccPrvKey
result = alloc(sizeof EccPrvKey)
result.copyMem(addr prvKey, sizeof EccPrvKey)
proc pubkey*(prvKey: pointer): pointer {.exportc.} =
var
pubKey: EccPubKey
prvKeyPtr = cast[ptr EccPrvKey](prvKey)
pubKey.getEccPubKey(prvKeyPtr)
result = alloc(sizeof EccPubKey)
result.copyMem(addr pubKey, sizeof EccPubKey)
proc freekey*(key: pointer) {.exportc.} =
key.dealloc
# ----------------------------------------------------------------------------
# Tests
# ----------------------------------------------------------------------------
when isMainModule:
const
txt = "Hi There"
let
prv = prvkey
pub = prvKey.pubkey
b64 = txt.b64_encrypt(pub)
doAssert txt == b64.b64_decrypt(prv)
when not defined(check_run):
echo "*** compiles OK"
# ----------------------------------------------------------------------------
# End
# ----------------------------------------------------------------------------