# Home

## Welcome to the Coherence wiki!

```

 _______  _____  _     _ _______  ______ _______ __   _ _______ _______
 |       |     | |_____| |______ |_____/ |______ | \  | |       |______
 |_____  |_____| |     | |______ |    \_ |______ |  \_| |_____  |______
```

Coherence’s objective is to improve security in L7 with modern cryptography.

Coherence (ko.eˈɾen.s) performs and offloads cryptography operations with a focus on interoperability, flexibility and simplicity. Coherence gives an interface for modern cryptographic algorithms which is inspired by Openssl, it is a REST API in order to be used by any language, in other words Coherence minimizes development time and code complexity. Some of the algorithms offered by Coherence are AES and AES candidates, Sosemanuk, SHA\* family, HMAC, DH, RSA, DSA, ECC, NTRU.

There is a couple of good reasons to use Coherence:

* Offload, you have an specialized software dedicated entirely for cryptographic operations. Cryptographic operations like encrypting and decrypting is a very CPU-intensive task for webapps, so offload alleviates CPU-intensive encryption and decryption tasks from your webapp, boosting application performance.
* Interoperability, it doesn’t depend on a language programming, it depends on standards HTTP/GRPC and JSON, so you can integrate with homogeneous and heterogeneous systems.
* Flexibility, it gives you a complete cryptographic toolkit to implement stream ciphers, block ciphers, RSA, Elliptic curves, Post quantum cryptography. So it gives you all that you need to create your own cryptographic protocol.
* Open source, you can inspect, modify, and enhance.
* Free of charges, you don’t have to pay.

### Features

* Hash functions: SHA3, SHA2, SHA1, WHIRLPOOL, Blake2b, SipHash.
* Password-hashing function: Argon2
* Stream ciphers: Sosemanuk, Salsa20/20.
* Block ciphers: AES, RC6, MARS, Twofish, Serpent, CAST-256, Camellia, SPECK, SIMECK.
* Block ciphers modes: CTR, GCM.
* Message authentication codes: HMAC(SHA3, SHA2, SHA1, WHIRLPOOL), CMAC(AES, RC6, MARS, Twofish, Serpent, CAST-256, Camellia), VMAC(AES, RC6, MARS, Twofish, Serpent, CAST-256, Camellia), Poly1305.
* RSA: Key generation, digital signature, encryption.
* DSA: Key generation, digital signature.
* DH: Key generation, key exchange (rfc and custom parameters).
* ECC: Key generation, ECIES, ECDSA, ECDH, Curve25519, ECNR.
* Post-Quantum Cryptography sign: Dilithium, SPHINCS+, Rainbow.
* Post-Quantum Cryptography kem: NTRU, Kyber, Saber.

## Index

This index starts from easy to more complex json and algorithms, you can find some external useful resources on each page.

* Rand
* Hash
* Argon2
* Poly1305
* Stream ciphers (Sosemanuk, Salsa20)
* Block ciphers (AES, RC6, MARS, Twofish, Serpent, CAST-256, Camellia, SPECK, SIMECK)
* HMAC
* CMAC
* VMAC
* DSA
* RSA
* Diffie Hellman
* ECDSA
* ECIES
* ECDH
* Curve25519
* NTRU
* Security
* Openssl
* Json reference


# Rand

[spec](https://www.cryptopp.com/wiki/RandomNumberGenerator)

## How to ???

```python
#!/usr/bin/env python

import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response=requests.post(url, data=message)
    print response.content

data_js='{"version":1,"algorithm":"RAND_RP","length":12}'
sending(data_js)
```

In this example we generate 12 random bytes with RAND\_RP algorithm without entropy.

* On *data\_js\["algorithm"]* can be one of *{RAND\_RP, RAND\_AUTO, RAND\_RDRAND}*

To add entropy we need to the parameter *"entropy": INT*

* 0 means with entropy using /dev/urandom
* 1 means with entropy using /dev/random
* 2 means without entropy

Json to rand

```javascript
{"version":1,"algorithm":"rand flavor","length":INT ,"entropy":INT}
```


# Hash

## Hash

[HASH functions video](https://www.youtube.com/watch?v=tLkHk__-M6Q\&index=20\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

#### Security

```
Symmetric  |   ECC   |  DH/DSA/RSA  | HASH
-----------+---------+--------------+-----
    80     |   163   |     1024     | 160 
   112     |   233   |     2048     | 224
   128     |   283   |     3072     | 256
   192     |   409   |     7680     | 384
   256     |   571   |    15360     | 512

            Comparable Key Sizes (in bits)
```

### SHA3

[spec](https://csrc.nist.gov/projects/hash-functions/sha-3-project)

Descriptopn: It is the latest member of the Secure Hash Algorithm family of standards, released by NIST.

Outputs: 512, 384, 256, 224.

Uses: General propuse, 3GPP TS 35.231, FIPS 202 , SP 800-185. TUAK, NIST, FIPS.

### SHA2 & SHA1

[spec](https://csrc.nist.gov/publications/detail/fips/180/4/final)

[SHA1 video](https://www.youtube.com/watch?v=JIhZWgJA-9o\&index=21\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

Description: NSA cryptographic hash functions.

Outputs: SHA2: 512, 384, 256, 224. SHA1: 160

Uses: General propuse, backward compatibility. NIST, FIPS.

### Whirlpool

\[spec]\(<https://en.wikipedia.org/wiki/Whirlpool_(cryptography>))

Description: Hash based on AES.

Outputs: 512

Uses: Performance is not impportant, ISO/IEC 10118-3, NESSIE, ISO ,IEC.

### Blake2b

[spec](https://blake2.net/)

Description: Faster hash than SHA-3, SHA2, SHA1, MD5 and at least as secure as the latest standard SHA-3.

Outputs: 512

Uses: Performance is impportant, RFC 7693,

### SipHash

[spec](https://131002.net/siphash/siphash.pdf)

Description: SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions) optimized for speed on short messages.Target applications include network traffic authentication and defense against hash-flooding DoS attacks.

Outputs: 128

## How to ???

```python
#!/usr/bin/env python

import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response=requests.post(url, data=message)
    print response.content

data_js='{"version":1,"algorithm":"SHA3_512","type":"string","plaintext":"Hello world!"}'
sending(data_js)
```

In this example we get SHA3*512 for \_Hello world!* string

* On *data\_js\["algorithm"]* can be one of *{SHA3\_512, SHA3\_384, SHA3\_256, SHA3\_224, SHA\_512, SHA\_384, SHA\_256, SHA\_224, SHA\_1, WHIRLPOOL, BLAKE2B, SIPHASH}*

```python
data_js='{"version":1,"algorithm":"WHIRLPOOL","type":"string","plaintext":"48656c6c6f20776f726c64210d0a0d0a","hex":1}'
```

In this example We get WHIRLPOOL for *48656c6c6f20776f726c64210d0a0d0* string.

* *"hex":1* indicates the string is a hex string
* *"hex":0* indicates the string is a ascii string
* When hex doesn't appear indicates the same as *"hex":0*
* *hex* parameter only applies for when *"type":"string"*
* *hex* parameter indicates if the string is hex or ascii.

```python
data_js='{"version":1,"algorithm":"SHA_1","type":"file","file":"mayhem.txt"}'
```

In this example we get SHA*1 for \_mayhem.txt* file

* On *"type":"file"* the parameter *"hex"* isn't needed
* On *"file":"route to file"* indicates where is the file. It depends on where Coherence is running.
* On *data\_js\["type"]* can be *file* or *string*&#x20;
* *type* indicates if you are going to apply the algorithm on a file or on a string.

Json to hash string

```javascript
{"version":1,"algorithm":" hash flavor","type":"string","plaintext":"your string", "hex":BOOL}
```

Json to hash file

```javascript
{"version":1,"algorithm":"hash flavor","type":"file","file":"your file"}
```


# Argon2

## PHC

[spec](https://password-hashing.net/)

In order to understand *hex* and *type* parameters read [HASH](https://github.com/liesware/coherence/wiki/Hash)

### Argon2

[spec](https://github.com/P-H-C/phc-winner-argon2)

Description: Password hashing winner of PHC.

Outputs: Variable

Uses: Highest resistance against GPU cracking attacks, safest against side-channel attacks.

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response=requests.post(url, data=message)
    print response.content
        return response.content


def argon(data_js):
    req=json.loads(data_js)
    print "Hash passwd \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived Argon2 hash: \n" + (json.dumps(answ)) +"\n\n\n"
    verify= '{ "version": 1 , "algorithm":"ARGON2" ,"family":"argon2i","plaintext": "Hello world!","hex":0,"pwd":"", "operation":"verify"}';
    req=json.loads(verify)
    req["pwd"]=answ["hash"]
    print "Verify passwd \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_1=json.loads(data_js_n)
    print "Recived Argon verification: \n" + (json.dumps(answ_1)) +"\n\n\n"


argon2_js='{ "version": 1 , "algorithm":"ARGON2" ,"family":"argon2i","plaintext": "Hello world!","t_cost":10,"m_cost":16,"parallelism":4,\
"salt":"ABABABABABABABABABABABABABABABAB","hashlen":32, "hex":0, "operation":"hash"}'

argon(argon2_js)
```

In this example We generate hash and validate password with argon2i from string *Hello world!* with t\_cost, m\_cost, parallelism and salt parameters given.

In order to understand t\_cost, m\_cost, parallelism and salt parameters, please read [spec](https://github.com/P-H-C/phc-winner-argon2).

* On *argon2\_js\["family"]* can be one of *{argon2i, argon2d, argon2id}*
* On *argon2\_js\["operation"]* can be *{hash, verify}*
* ARGON2 only supports *"type":"string"*

Json to hash

```javascript
{ "version": 1 , "algorithm":"ARGON2" ,"family":"argon2 flavor",
"plaintext": "your password","t_cost":INT,"m_cost":INT,"parallelism":INT,
"salt":"hex string","hashlen":INT, "hex": BOOL, "operation":"hash"}
```

Json to verify

```javascript
{ "version": 1 , "algorithm":"ARGON2" ,"family":"argon2 flavor",
"plaintext": "your password","hex":BOOL,"pwd":"Hex (hash arong2 string) ", 
"operation":"verify"}
```

In this tutorial We are protecting user's credentials with TLS and Argon2. It means the information is protected in motion, at rest, in use. [End to end user credentials protection](https://scotch.io/@liesware/end-to-end-user-credentials-protection)


# Poly1305

[Message Authentication Codes video](https://www.youtube.com/watch?v=DiLPn_ldAAQ\&index=22\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

In order to understand *hex* and *type* parameters read [HASH](https://github.com/liesware/coherence/wiki/Hash)

## Poly1305

[spec](https://tools.ietf.org/html/rfc7539)

Description: Message authentication code based on block cipher.

Uses: Performance is important

Key size: 256 bits

Nonce size: 128 bits

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

data_js='{"version":1,"algorithm":"POLY1305","type":"string","plaintext":"Hello world!","hex":0,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","nonce":"0123456789ABCDEF0123456789ABCDEF"}'
sending(data_js)
```

In this example we generate POLY1305 to hex string *Hello world!* with the key and nonce given.

Json to poly1035 string

```javascript
{"version":1,"algorithm":"POLY1305","type":"string","plaintext":"your string ",
"hex": BOOL,"key":"Hex string size=32,48,64","nonce":"Hex string size=32"}
```

Json to poly1035 file

```javascript
{"version":1,"algorithm":"POLY1305","type":"file","file":"your file","hex": BOOL,
"key":"Hex stringsize=32,48,64","nonce":"Hex string size=32"}
```


# Stream-ciphers

## eStream

[eStream project](http://www.ecrypt.eu.org/stream/index.html)

[Stream ciphers video - 1 ](https://www.youtube.com/watch?v=AELVJL0axRs\&index=3\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

[Stream ciphers video - 2 ](https://www.youtube.com/watch?v=sKUhFpVxNWc\&index=4\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

In order to understand *hex* and *type* parameters read [HASH](https://github.com/liesware/coherence/wiki/Hash)

### Sosemanuk

[spec](http://www.ecrypt.eu.org/stream/sosemanukpf.html)

Description: Stream cipher and eStream winner.

Uses: Performance is very impportant.

Key size: from 128 to 256 bits, guaranteed security is only 128 bits.

Iv size: 128 bits

### Salsa20

[spec](http://www.ecrypt.eu.org/stream/salsa20pf.html)

Description: Stream cipher and eStream winner.

Uses: Performance is very impportant.

Key size: varibale

Iv size: 64 bits

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def stream(data_js):
    req=json.loads(data_js)
    print "Sosemanuk enc \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived enc: \n" + (json.dumps(answ)) +"\n\n\n"
    dec='{"algorithm":"SOSEMANUK","plaintext":"","iv":"b05691ef92cb9c9bf77e5613819fc4ea",\
    "version":1,"key":"7f685ba92789f0d8d421038f2b1b4fcd73be586d81795ec3ab7939975b7b896e","operation":"dec","type":"string"}'
    req=json.loads(dec)
    req["plaintext"]=answ["result"]
    print "Sosemanuk dec \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ2=json.loads(data_js_n)
    print "Recived dec: \n" + (json.dumps(answ2)) +"\n\n\n"

data_js='{"algorithm":"SOSEMANUK","plaintext":"Hello world!","hex":0,"iv":"b05691ef92cb9c9bf77e5613819fc4ea",\
"version":1,"key":"7f685ba92789f0d8d421038f2b1b4fcd73be586d81795ec3ab7939975b7b896e","operation":"enc","type":"string"}'
stream(data_js)
```

In this example we encrypt and decrypt *Hello world!* string with the key and iv given

On *data\_js\["algorithm"]* can be *SOSEMANUK* or *SALSA20*

Json to enc string (key and iv depends on algorithm you chose)

```javascript
{"algorithm":"estream flavor","plaintext":"your string","hex": BOOL,"iv":"Hex stringsize=16,32",
"version":1,"key":"Hex stringsize=32-64","operation":"enc","type":"string"}
```

Json to dec string (key and iv depends on algorithm you chose)

```javascript
{"algorithm":"estream flavor","plaintext":"your hex enc string ","iv":"Hex stringsize=16,32",
"version":1,"key":"Hex stringsize=32-64","operation":"dec","type":"string"}
```

Json to enc file (key and iv depends on algorithm you chose)

```javascript
{"algorithm":"estream flavor","file":"your file","iv":"Hex stringsize=16,32",
"version":1,"key":"Hex stringsize=32-64","operation":"enc","type":"file"}
```

Json to dec file (key and iv depends on algorithm you chose)

```javascript
{"algorithm":"estream flavor","file":"your file","iv":"Hex stringsize=16,32",
"version":1,"key":"Hex stringsize=32-64","operation":"dec","type":"file"}
```


# Block-ciphers

## Block ciphers

[DES algorithm video - 1](https://www.youtube.com/watch?v=kPBJIhpcZgE\&index=5\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

[DES algorithm video - 2](https://www.youtube.com/watch?v=l-7YW06BFNs\&index=6\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

In order to understand *hex* and *type* parameters read [Hash](https://github.com/liesware/coherence/wiki/Hash)

In order to understand encryption and decryption read [Stream](https://github.com/liesware/coherence/wiki/Stream)

### AES

[spec](http://www.quadibloc.com/crypto/co040401.htm)

[AES algorithm video - 1](https://www.youtube.com/watch?v=x1v2tX4_dkQ\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=7)

[AES algorithm video - 2](https://www.youtube.com/watch?v=NHuibtoL_qk\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=8)

Description: Block cipher and AES winner.

Key:128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, Performance is impportant, FIPS 197, NIST.

### RC6

[spec](https://en.wikipedia.org/wiki/RC6)

Description: Block cipher and AES finalist.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### MARS

[spec](http://www.quadibloc.com/crypto/co040406.htm)

Description: Block cipher and AES finalist.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### Twofish

[spec](https://www.schneier.com/academic/twofish/)

Description: Block cipher and AES finalist.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### Serpent

[spec](http://www.cl.cam.ac.uk/~rja14/serpent.html)

Description: Block cipher and AES finalist.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### CAST-256

[spec](http://www.quadibloc.com/crypto/co040410.htm)

Description: Block cipher and AES finalist.

Key: 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### Camellia

[spec](https://info.isl.ntt.co.jp/crypt/eng/camellia/)

Description: Block cipher, The cipher has been approved for use by the ISO/IEC, the European Union's NESSIE project, the Japanese CRYPTREC.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### Speck128

[spec](https://eprint.iacr.org/2013/404.pdf)

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: IoT

### Simeck64

[spec](https://eprint.iacr.org/2015/612.pdf)

Key: 128 bits.

Iv: 64 bits.

Uses: IoT

## Modes of Operation

[Modes of Operation video](https://www.youtube.com/watch?v=4FBgb2uobWI\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=9)

### CTR

[spec](ps://en.wikipedia.org/wiki/Block_cipher_mode_of_operation)

Description: Block cipher mode

Uses: Performance is important.

### GCM

[spec](https://en.wikipedia.org/wiki/Galois/Counter_Mode)

Description: Block cipher mode

Uses: Authenticated encryption.

* It only support *"type":"string"*
* It doesn't support *"algorithm":"SIMECK64"*

## How to ???

```python
#!/usr/bin/env python

import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def stream(data_js):
    req=json.loads(data_js)
    print "Enc \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived enc: \n" + (json.dumps(answ)) +"\n\n\n"
    dec='{"algorithm":"AES","plaintext":"","iv":"b05691ef92cb9c9bb05691ef92cb9c9b",\
    "version":1,"key":"b05691ef92cb9c9bb05691ef92cb9c9b","operation":"dec","type":"string", "mode":"ctr"}'
    req=json.loads(dec)
    req["plaintext"]=answ["result"]
    print "Dec \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ2=json.loads(data_js_n)
    print "Recived dec: \n" + (json.dumps(answ2)) +"\n\n\n"


data_js='{ "version": 1 , "algorithm":"AES" , "type":"string", "plaintext": "Hello world!", "hex": 0,"operation":"enc",\
"key":"b05691ef92cb9c9bb05691ef92cb9c9b","iv":"b05691ef92cb9c9bb05691ef92cb9c9b", "mode":"ctr" }'
stream(data_js)
```

In this example we encrypt and decrypt Hello world! string using AES with the key and iv given in CTR mode.

On data*js\["algorithm"] can be one of* {AES, RC6, MARS, Twofish, SERPENT, CAST256, CAMELLIA, SPECK128, SIMECK64 }\_

```python
#!/usr/bin/env python

import socket
import json
import os,binascii

def sending(message):
    ip = '127.0.0.1'
    port = 6613
    BUFFER_SIZE = 65536
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, port))
    s.send(message)
    data = s.recv(BUFFER_SIZE)
    s.close()
    return data

def stream(data_js):
    req=json.loads(data_js)
    print "Enc \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived enc: \n" + (json.dumps(answ)) +"\n\n\n"
    dec='{"algorithm":"AES","plaintext":"","iv":"b05691ef92cb9c9bb05691ef92cb9c9b",\
    "version":1,"key":"b05691ef92cb9c9bb05691ef92cb9c9b","operation":"dec","type":"string", "mode":"gcm" ,"adata":"ABCD"}'
    req=json.loads(dec)
    req["plaintext"]=answ["result"]
    print "Dec \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ2=json.loads(data_js_n)
    print "Recived dec: \n" + (json.dumps(answ2)) +"\n\n\n"


data_js='{ "version": 1 , "algorithm":"AES" , "type":"string", "plaintext": "Hello world!", "hex": 0,"operation":"enc",\
"key":"b05691ef92cb9c9bb05691ef92cb9c9b","iv":"b05691ef92cb9c9bb05691ef92cb9c9b", "mode":"gcm" ,"adata":"ABCD"}'
stream(data_js)
```

In this example we encrypt and decrypt Hello world! string using AES with the key,iv,adata given in GCM mode.

On data*js\["algorithm"] can be one of* {AES, RC6, MARS, Twofish, SERPENT, CAST256, CAMELLIA, SPECK128}\_

Json to enc string (key and iv depends on algorithm you chose) in CTR mode

```javascript
{ "version": 1 , "algorithm":"block flavor" , "type":"string", "plaintext": "your string", "hex":BOOL,
"operation":"enc", "key":"Hex stringsize=32-64","iv":"Hex stringsize=16-32", "mode":"ctr"}
```

Json to dec string (key and iv depends on algorithm you chose) in CTR mode

```javascript
{ "version": 1 , "algorithm":"block flavor" , "type":"string", "plaintext": "your hex enc string ", 
"operation":"dec", "key":"Hex stringsize=32-64","iv":"Hex stringsize=16-32", "mode":"ctr"}
```

* To enc/dec a file you need to change *"type":"string" -> "type":"file" , "plaintext": "your hex enc string " -> "file":"your file"*
* To use enc/dec GCM mode you need to change *"mode":"ctr"->"mode":"gcm"* , add *"adata":"your string"* and *"hex"* parameters
* *"hex"* parameter apply for *"string"* and *"adata"* parameter


# HMAC

## MAC

[HMAC video](https://www.youtube.com/watch?v=DiLPn_ldAAQ\&index=22\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

In order to understand *hex* and *type* parameters read [Hash](https://github.com/liesware/coherence/wiki/Hash)

### HMAC

[spec](https://tools.ietf.org/html/rfc2104)

Description: Message authentication code based on hash.

Uses: General porpuse

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

data_js='{"version":1,"algorithm":"HMAC","type":"string","plaintext":"Hello world!","hex":0,\
"key":"0123456789ABCDEF0123456789ABCDEF","family":"sha3_512"}'
sending(data_js)
```

Calculate HMAC-SHA3*512 to string* "Hello world!"\_ with the key given.

On *data\_js\["family"]* can be one of *{sha3\_512, sha3\_384, sha3\_256, sha3\_224, sha\_512, sha\_384, sha\_256, sha\_224, sha\_1 ,whirlpool}*

Json to HMAC string

```javascript
{"version":1,"algorithm":"HMAC","type":"string","plaintext":"your string","hex":BOOL,
"key":"hex string","family":"hash flavor"}
```

Json to HMAC file

```javascript
{"version":1,"algorithm":"HMAC","type":"file","file":"your file",
"key":"hex string","family":"hash flavor"}
```


# CMAC

## MAC

In order to understand *hex* and *type* parameters read [Hash](https://github.com/liesware/coherence/wiki/Hash)

### CMAC

[spec](https://csrc.nist.gov/publications/detail/sp/800-38b/archive/2005-05-01)

Description: Message authentication code based on block cipher.

Uses: When a block cipher is more readily available than a hash function

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

data_js='{"version":1,"algorithm":"CMAC","type":"string","plaintext":"Hello world!",\
"hex":0,"key":"0123456789ABCDEF0123456789ABCDEF","family":"aes"}'
sending(data_js)
```

Calculate CMAC-AES to string *"Hello world!"* with the key given.

On *data\_js\["family"]* can be one of *{"aes", "rc6", "mars","serpent","twofish', "cast256"}*

Json to HMAC string

```javascript
{"version":1,"algorithm":"CMAC","type":"string","plaintext":"your string","hex":BOOL,
"key":"Hex stringsize=32-64","family":"block flavor"}
```

Json to HMAC file

```javascript
{"version":1,"algorithm":"CMAC","type":"file","file":"your file",
"key":"Hex stringsize=32-64","family":"block flavor"}
```


# VMAC

## MAC

In order to understand *hex* and *type* parameters read [Hash](https://github.com/liesware/coherence/wiki/Hash)

### VMAC

[spec](http://www.fastcrypto.org/vmac/)

Description: Message authentication code based on universal hash.

Uses: High performance on 64-bit machines

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

data_js='{"version":1,"algorithm":"VMAC","type":"string","plaintext":"Hello world!",\
"hex":0,"key":"0123456789ABCDEF0123456789ABCDEF", "iv":"0123456789ABCDEF0123456789ABCDEF",\
"family":"aes"}'
sending(data_js)
```

Calculate VMAC-AES to string *"Hello world!"* with the key and iv given.

On *data\_js\["family"]* can be one of *{"aes", "rc6", "mars","serpent","twofish', "cast256"}*

Json to HMAC string

```javascript
{"version":1,"algorithm":"VMAC","type":"string","plaintext":"your string","hex":BOOL,
"key":"Hex stringsize=32-64","iv":"Hex stringsize=32","family":"block flavor"}
```

Json to HMAC file

```javascript
{"version":1,"algorithm":"CMAC","type":"file","file":"your file",
"key":"Hex stringsize=32-64","iv":"Hex stringsize=32","family":"block flavor"}
```


# DSA

[Digital signatures video](https://www.youtube.com/watch?v=jbBe4AS5pk0\&index=18\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

In order to understand *hex* and *type* parameters read [Hash](https://github.com/liesware/coherence/wiki/Hash)

### DSA

[spec](https://www.nist.gov/publications/digital-signature-standard-dss-2)

Description: Public-key cryptosystem

Problem: Discrete logarithm problem

Uses: Digital signatures

* It only supports "type":"string"

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def dsa(data_js, bits):
    req=json.loads(data_js)
    req["length"]=bits
    print "Send gen parameters : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived  dsa gen: \n"+(json.dumps(answ)) +"\n\n\n"
    json_s=json_v='{ "version": 1 , "algorithm":"DSA", "type":"string","plaintext": "Hello world!", "hex":0,"privkey": "" ,"operation":"sign"}'
    req=json.loads(json_s)
    req["privkey"]=answ["privkey"]
    print "Send sign : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_1=json.loads(data_js_n)
    print "Recived  sign done: \n"+(json.dumps(answ_1)) +"\n\n\n"
    json_v='{ "version": 1 , "algorithm":"DSA", "type":"string","plaintext": "Hello world!", "hex":0,"pubkey": "" ,"sign":"","operation":"verify"}'
    req=json.loads(json_v)
    req["pubkey"]=answ["pubkey"]
    req["sign"]=answ_1["sign"]
    print "Send verify : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_2=json.loads(data_js_n)
    print "Recived  verify done: \n"+(json.dumps(answ_2)) +"\n\n\n"


dsa_gen='{ "version": 1 , "algorithm":"DSA", "operation":"gen", "length": 0 }'
dsa(dsa_gen,1024)
```

In this example we generate a DSA key (1024 bits), we sign *"Hello world!"* and validate it.

Json to generate

```javascript
{"operation": "gen", "version": 1, "length": INT, "algorithm": "DSA"}
```

* Length can be 1024, 2048 and 3072

Json to sign string

```javascript
{"algorithm": "DSA", "plaintext": "your string", "hex": BOOL, "version": 1, "operation": "sign", "type": "string", "privkey": "your private key"}
```

Json to validate string

```javascript
{"algorithm": "DSA", "plaintext": "Hello world", "pubkey": "your hex pubkey", "hex": 0, "sign": "your hex signature", "version": 1, "operation": "verify", "type": "string"}
```


# RSA

[RSA intro video](https://www.youtube.com/watch?v=fq6SXByItUI\&index=11\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

[RSA video](https://www.youtube.com/watch?v=QSlWzKNbKrU\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=12)

[RSA sign](https://www.youtube.com/watch?v=jbBe4AS5pk0\&index=18\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

In order to understand *hex* and *type* parameters read [Hash](https://github.com/liesware/coherence/wiki/Hash)

### RSA

[spec](https://es.wikipedia.org/wiki/RSA)

Description: Public-key cryptosystem

Problem: Integer Factoriation

Uses: Encrypt, digital signatures

* It only supports "type":"string"

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def rsa(data_js, bits):
    req=json.loads(data_js)
    req["length"]=bits
    print "Send gen parameters : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived  rsa gen: \n"+(json.dumps(answ)) +"\n\n\n"
    json_s=json_v='{ "version": 1 , "algorithm":"RSA", "type":"string","plaintext": "Hello world!", "hex":0,"privkey": "" ,"operation":"sign"}'
    req=json.loads(json_s)
    req["privkey"]=answ["privkey"]
    print "Send sign : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_1=json.loads(data_js_n)
    print "Recived  sign done: \n"+(json.dumps(answ_1)) +"\n\n\n"
    json_v='{ "version": 1 , "algorithm":"RSA", "type":"string","plaintext": "Hello world!", "hex":0,"pubkey": "" ,"sign":"","operation":"verify"}'
    req=json.loads(json_v)
    req["pubkey"]=answ["pubkey"]
    req["sign"]=answ_1["sign"]
    print "Send verify : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_2=json.loads(data_js_n)
    print "Recived  verify done: \n"+(json.dumps(answ_2)) +"\n\n\n"
    json_enc='{ "version": 1 , "algorithm":"RSA", "type":"string","pubkey": "" ,"operation":"enc", "plaintext":"Hello world!","hex":0 }'
    req=json.loads(json_enc)
    req["pubkey"]=answ["pubkey"]
    print "Send enc : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_3=json.loads(data_js_n)
    print "Recived  enc done: \n"+(json.dumps(answ_3)) +"\n\n\n"
    json_dec='{ "version": 1 , "algorithm":"RSA", "type":"string","privkey": "" ,"operation":"dec", "plaintext":"" }'
    req=json.loads(json_dec)
    req["privkey"]=answ["privkey"]
    req["plaintext"]=answ_3["result"]
    print "Send dec : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_3=json.loads(data_js_n)
    print "Recived  dec done: \n"+(json.dumps(answ_3)) +"\n\n\n"



rsa_gen='{ "version": 1 , "algorithm":"RSA", "operation":"gen" , "length": 0 }'
rsa(rsa_gen,1024)
```

In this example we generate a RSA key (1024 bits), sign and validate *"Hello world!"* string, we enc and dec *"Hello world!"* string.

The default hash function to sign is sha3*256, you can change by adding* "hash*sign"* and can be one of *{"sha3\_512","sha3\_384","sha3\_256","sha3\_224","sha\_512","sha\_384","sha\_256","sha\_224","sha\_1","whirlpool"}*

Json to gen

```javascript
{ "version": 1 , "algorithm":"RSA", "operation":"gen" , "length": INT }
```

Json to sign

```javascript
{ "version": 1 , "algorithm":"RSA", "type":"string","plaintext": "your string", "hex":BOOL,"privkey": "your hex privkey" ,"operation":"sign"}
```

Json to verify

```javascript
{ "version": 1 , "algorithm":"RSA", "type":"string","plaintext": "your string", "hex":BOOL,"pubkey": "your hex pubkey" ,"sign":"your hex signature","operation":"verify"}
```

Json to enc

```javascript
{ "version": 1 , "algorithm":"RSA", "type":"string","pubkey": "your hex pubkey" ,"operation":"enc", "plaintext":"your string","hex":BOOL }
```

Json to dec

```javascript
{ "version": 1 , "algorithm":"RSA", "type":"string","privkey": "your hex privkey" ,"operation":"dec", "plaintext":"your enc hex string" }
```


# DH

[Diffie-Hellman video](https://www.youtube.com/watch?v=aeOzBCbwxUo\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=13)

### DH

[spec](https://tools.ietf.org/html/rfc2631)

Description: Public-key cryptosystem

Problem: Discrete logarithm problem

Uses: Key exchange

## How to ???

```python
#!/usr/bin/env python

import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def dh(data_js):
    req=json.loads(data_js)
    print "Send gen parameters (A): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived gen parameters donde (A): \n" + (json.dumps(answ)) +"\n\n\n"
    data_js_n=sending(json.dumps(req))
    print "Send gen parameters (B): \n " + json.dumps(req) +"\n"
    answ_1=json.loads(data_js_n)
    print "Recived gen parameters donde (B): \n" + (json.dumps(answ_1)) +"\n\n\n"
    gen='{ "version":1 , "algorithm":"DH","family": "", "privkey":"","sharedpub":"", "operation":"a_rfc"}'
    req=json.loads(gen)
    req["family"]=answ["family"]
    req["privkey"]=answ["privkey"]
    req["sharedpub"]=answ_1["pubkey"]
    print "Send agreetment (A): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_2=json.loads(data_js_n)
    print "Recived agreetment donde (A): \n" + (json.dumps(answ_2)) +"\n\n\n"
    req["privkey"]=answ_1["privkey"]
    req["sharedpub"]=answ["pubkey"]
    print "Send agreetment (B): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_3=json.loads(data_js_n)
    print "Recived agreetment donde (B): \n" + (json.dumps(answ_3)) +"\n\n\n"


dh_gen='{ "version": 1 , "algorithm":"DH", "family": "modp256", "operation":"gen_rfc"}'
dh(dh_gen)
```

In this example we generate keys for A and B, and we make a key exchange between A and B.

On *"family"* parameneter can be *{"modp256","modp160","modp224"}*

Json to gen

```javascript
{ "version": 1 , "algorithm":"DH", "family": "mod flavor", "operation":"gen_rfc"}
```

Json to key agreetment

```javascript
{ "version":1 , "algorithm":"DH","family": "mod flavor", "privkey":" your hex privkey",
"sharedpub":"your partnert hex pubkey", "operation":"a_rfc"}
```


# ECC

## ECC

[ECC intro video](https://www.youtube.com/watch?v=IGqrbM52wtg\&index=14\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

[ECC video](https://www.youtube.com/watch?v=vnpZXJL6QCQ\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=16)

In order to understand *hex* and *type* parameters read [HASH](https://github.com/liesware/coherence/wiki/Hash)

### ECIES

[spec](https://www.cryptopp.com/wiki/Elliptic_Curve_Integrated_Encryption_Scheme)

Description: Public-key cryptosystem

Problem: Discrete logarithm problem

Uses: Encrypt

* It only supports "type":"string"

### ECDSA

[spec](https://www.nist.gov/publications/digital-signature-standard-dss-2)

Description: Public-key cryptosystem

Problem: Discrete logarithm problem

Uses: Digital signatures

* It only supports "type":"string"

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def ecc_pb(data_js):
    req=json.loads(data_js)
    curve=req["curve"]
    print "Send gen parameters : \n" + data_js +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print 'Recived  ecc gen: \n'+ json.dumps(answ) +"\n\n\n"
    json_enc='{ "version": 1 , "algorithm":"ECIES", "type":"string","pubkey": "" ,"operation":"enc", "plaintext":"Hello world!" ,"curve":""}'
    req=json.loads(json_enc)
    req["pubkey"]=answ["pubkey"]
    req["curve"]=curve
    print "Send  enc: \n"+(json.dumps(req))+"\n"
    data_js_n=sending(json.dumps(req))
    answ_1=json.loads(data_js_n)
    print "Recived  enc : \n"+(json.dumps(answ_1)) +"\n"
    req["privkey"]=answ["privkey"]
    req["plaintext"]=answ_1["result"]
    req["pubkey"]=""
    req["operation"]="dec"
    data_js_n=sending(json.dumps(req))
    answ_2=json.loads(data_js_n)
    print "Recived  dec done: \n"+(json.dumps(answ_2)) +"\n\n\n"
    json_sign='{ "version": 1 , "algorithm":"ECDSA", "type":"string","plaintext": "Hello world", "hex":0,"privkey": "" ,"operation":"sign"}'
    req=json.loads(json_sign)
    req["privkey"]=answ["privkey"]
    req["curve"]=curve
    data_js_n=sending(json.dumps(req))
    answ_3=json.loads(data_js_n)
    print "Recived  sign done: \n"+(json.dumps(answ_3)) +"\n"
    json_verify='{ "version": 1 , "algorithm":"ECDSA", "type":"string","plaintext": "Hello world", "hex":0,"pubkey": "", "operation":"verify","sign":""}'
    req=json.loads(json_verify)
    req["pubkey"]=answ["pubkey"]
    req["sign"]=answ_3["sign"]
    req["curve"]=curve
    data_js_n=sending(json.dumps(req))
    answ_4=json.loads(data_js_n)
    print "Recived  verify done: \n"+(json.dumps(answ_4)) +"\n\n\n"


ecc_gen='{ "version": 1 , "algorithm":"ECC_GEN", "curve":"secp256k1"}'
ecc_pb(ecc_gen)
```

In this example we generate a ECC key (secp256k1), sign and validate "Hello world!" string, we enc and dec "Hello world!" string.

On *"curve"* can be one of *{"brainpoolP512r1","secp521r1","brainpoolP384r1","secp384r1","brainpoolP320r1","brainpoolP256r1", "secp256k1","sect571r1","sect571k1","sect409r1","sect409k1","sect283r1","sect283k1"}*

The default hash function to sign is sha3*256, you can change by adding* "hash*sign"* and can be one of *{"sha3\_512","sha3\_384","sha3\_256","sha3\_224","sha\_512","sha\_384","sha\_256","sha\_224","sha\_1","whirlpool"}*

Json to gen

```javascript
{ "version": 1 , "algorithm":"ECC_GEN", "curve":"curve flavor"}
```

Json to enc

```javascript
{ "version": 1 , "algorithm":"ECIES", "type":"string", "hex":BOOL,"pubkey": "your hex pubkey",
"operation":"enc", "plaintext":"your string" ,"curve":"curve flavor"}
```

Json to dec

```javascript
{ "version": 1 , "algorithm":"ECIES", "type":"string","privkey": "your hex privkey" ,
"operation":"dec", "plaintext":"your hex enc string" ,"curve":"curve flavor"}
```

Json to sign

```javascript
{ "version": 1 , "algorithm":"ECDSA", "type":"string","plaintext": "your string", "hex":BOOL,
"privkey": " your hex pirvkey" ,"operation":"sign","curve":"curve flavor"}
```

Json to verify

```javascript
{ "version": 1 , "algorithm":"ECDSA", "type":"string","plaintext": "your string", "hex":BOOL,
"pubkey": "your hex pubkey" ,"sign":"your hex signature","operation":"verify","curve":"curve flavor"}
```


# ECDH

In order to understand *hex* and *type* parameters read [HASH](https://github.com/liesware/coherence/wiki/Hash)

### ECDH

[spec](https://csrc.nist.gov/publications/detail/sp/800-56a/rev-2/final)

Description: Public-key cryptosystem

Problem: Discrete logarithm problem

Uses: Key exchange

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def ecdh(data_js):
    req=json.loads(data_js)
    curve=req["curve"]
    print "Send gen parameters (A): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived gen parameters donde (A): \n" + (json.dumps(answ)) +"\n\n\n"
    data_js_n=sending(json.dumps(req))
    print "Send gen parameters (B): \n " + json.dumps(req) +"\n"
    answ_1=json.loads(data_js_n)
    print "Recived gen parameters donde (B): \n" + (json.dumps(answ_1)) +"\n\n\n"
    gen='{ "version":1 , "algorithm":"ECDH","family": "", "privkey":"","sharedpub":"", "operation":"agree"}'
    req=json.loads(gen)
    req["privkey"]=answ["privkey"]
    req["sharedpub"]=answ_1["pubkey"]
    req["curve"]=curve
    print "Send agreetment (A): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_2=json.loads(data_js_n)
    print "Recived agreetment donde (A): \n" + (json.dumps(answ_2)) +"\n\n\n"
    req["privkey"]=answ_1["privkey"]
    req["sharedpub"]=answ["pubkey"]
    print "Send agreetment (B): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_3=json.loads(data_js_n)
    print "Recived agreetment donde (B): \n" + (json.dumps(answ_3)) +"\n\n\n"


ecdh_gen='{ "version": 1 , "algorithm":"ECDH", "curve":"secp256k1", "operation":"gen"}'
ecdh(ecdh_gen)
```

In this example we generate keys for A and B, and we make a key exchange between A and B.

On *"curve"* can be one of *{"brainpoolP512r1","secp521r1","brainpoolP384r1","secp384r1","brainpoolP320r1","brainpoolP256r1", "secp256k1","sect571r1","sect571k1","sect409r1","sect409k1","sect283r1","sect283k1"}*

Json to gen

```javascript
{ "version": 1 , "algorithm":"ECDH", "curve":"curve flavor", "operation":"gen"}
```

Json to key agreetment

```javascript
{ "version":1 , "algorithm":"ECDH","family": "", "privkey":" your hex privkey",
"sharedpub":"your partnert hex pubkey", "operation":"agree","curve":"curve flavor"}
```


# Curve-25519

[Why?](https://safecurves.cr.yp.to/)

[spec](https://cr.yp.to/ecdh/curve25519-20060209.pdf)

Description: Public-key cryptosystem

Problem: Discrete logarithm problem

Uses: Digital signatures and Key exchange

* It only supports "type":"string"

## How to Ed25519?

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def ed25519(data_js):
    req=json.loads(data_js)
    print "Send gen parameters : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived  ed25519 gen: \n"+(json.dumps(answ)) +"\n\n\n"
    json_s=json_v='{ "version": 1 , "algorithm":"ED25519", "type":"string","plaintext": "Hello world!", "hex":0,"privkey": "" ,"operation":"sign"}'
    req=json.loads(json_s)
    req["privkey"]=answ["privkey"]
    print "Send sign : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_1=json.loads(data_js_n)
    print "Recived  sign done: \n"+(json.dumps(answ_1)) +"\n\n\n"
    json_v='{ "version": 1 , "algorithm":"ED25519", "type":"string","plaintext": "Hello world!", "hex":0,"pubkey": "" ,"sign":"","operation":"verify"}'
    req=json.loads(json_v)
    req["pubkey"]=answ["pubkey"]
    req["sign"]=answ_1["sign"]
    print "Send verify : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_2=json.loads(data_js_n)
    print "Recived  verify done: \n"+(json.dumps(answ_2)) +"\n\n\n"

ed25519_gen='{ "version": 1 , "algorithm":"ED25519", "operation":"gen"}'
ed25519(ed25519_gen)
```

Json to gen

```javascript
{"operation": "gen", "version": 1, "algorithm": "ED25519"}
```

Json to sign

```javascript
{"algorithm": "ED25519", "plaintext": "your string", "hex": BOOL, "version": 1, "operation": "sign", 
"type": "string", "privkey": "your hex privkey"}
```

Json to verify

```javascript
{"algorithm": "ED25519", "plaintext": "Hello world!", "pubkey": "your hex pubkey", "hex": BOOL, 
"sign": "your hex signature", "version": 1, "operation": "verify", "type": "string"}
```

## How to X25519?

```python
import socket
import json
import os,binascii

def sending(message):
    ip = '127.0.0.1'
    port = 6613
    BUFFER_SIZE = 65536
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, port))
    s.send(message)
    data = s.recv(BUFFER_SIZE)
    s.close()
    return data

def x25519():
    x25519_gen='{ "version": 1 , "algorithm":"X25519", "operation":"gen"}'
    req=json.loads(x25519_gen)
    print "Send gen parameters (A): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived gen parameters donde (A): \n" + (json.dumps(answ)) +"\n\n\n"
    data_js_n=sending(json.dumps(req))
    print "Send gen parameters (B): \n " + json.dumps(req) +"\n"
    answ_1=json.loads(data_js_n)
    print "Recived gen parameters donde (B): \n" + (json.dumps(answ_1)) +"\n\n\n"
    agree='{ "version":1 , "algorithm":"X25519", "privkey":"","sharedpub":"", "operation":"agree"}'
    req=json.loads(agree)
    req["privkey"]=answ["privkey"]
    req["sharedpub"]=answ_1["pubkey"]
    print "Send agreetment (A): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_2=json.loads(data_js_n)
    print "Recived agreetment donde (A): \n" + (json.dumps(answ_2)) +"\n\n\n"
    req["privkey"]=answ_1["privkey"]
    req["sharedpub"]=answ["pubkey"]
    print "Send agreetment (B): \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_3=json.loads(data_js_n)
    print "Recived agreetment donde (B): \n" + (json.dumps(answ_3)) +"\n\n\n"

x25519()
```

Json to gen

```javascript
{"operation": "gen", "version": 1, "algorithm": "X25519"}
```

Json to agree

```javascript
{"operation": "agree", "version": 1, "sharedpub": "your partnert hex pubkey", 
"algorithm": "X25519", "privkey": "your hex privkey"}
```


# NTRU

In order to understand *hex* and *type* parameters read [Hash](https://github.com/liesware/coherence/wiki/Hash)

### NTRU

Description: Post-quantum public-key cryptosystem

Problem: The shortest vector problem in a lattice

Uses: Encryption

* It only supports "type":"string"

## How to ???

```python
import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

ntru_gen=json.loads('{ "version": 1 , "algorithm":"NTRU", "parameter": "EES1499EP1", "operation":"gen"}')

data_js_n=sending(json.dumps(ntru_gen))
answ=json.loads(data_js_n)
print "Recived gen NTRU: \n" + (json.dumps(answ)) +"\n"
json_enc=json.loads('{ "version": 1 , "algorithm":"NTRU", "type":"string","parameter": "EES1499EP1","pubkey": "" ,"operation":"enc", "plaintext":""}')
json_enc["pubkey"]=answ["pubkey"]
json_enc["plaintext"]="Hello wolrd!"
data_js_n=sending(json.dumps(json_enc))
answ_1=json.loads(data_js_n)
print "Recived enc NTRU: \n" + (json.dumps(answ_1)) +"\n\n\n"
json_dec=json.loads('{ "version": 1 , "algorithm":"NTRU", "type":"string","parameter": "EES1499EP1","privkey": "" ,"operation":"dec", "plaintext":""}')
json_dec["privkey"]=answ["privkey"]
json_dec["pubkey"]=answ["pubkey"]
json_dec["plaintext"]=answ_1["result"]
data_js_n=sending(json.dumps(json_dec))
answ_2=json.loads(data_js_n)
print "Recived dec NTRU: \n" + (json.dumps(answ_2)) +"\n"
```

In this example we generate a NTRU key (EES1499EP1), we enc and dec *"Hello world!"* string.

On *"parameter"* can be one o&#x66;*{"EES449EP1", "EES613EP1","EES761EP1","EES677EP1", "EES887EP1","EES1087EP1","EES1087EP2","EES1171EP1","EES1499EP1"}*

Json to gen

```javascript
{ "version": 1 , "algorithm":"NTRU", "parameter": "ntru flavor", "operation":"gen"}
```

Json to enc

```javascript
{ "version": 1 , "algorithm":"NTRU", "type":"string","parameter": "ntru flavor",
"pubkey": "your ntru hex pubkey" ,"operation":"enc", "plaintext":"your string","hex":BOOL}
```

Json to dec

```javascript
{ "version": 1 , "algorithm":"NTRU", "type":"string","parameter": "ntru flavor",
"privkey": "your ntru hex privkey","pubkey":"your ntru hex pubkey" ,"operation":"dec", 
"plaintext":"your enc hex string"}
```


# Security

Coherence is designed to be accessed by trusted clients inside trusted environments, there is not protocol to protect data between server and client, so that It is no a good idea to expose Coherence to an environment where untrusted clients can directly access it. For the most cases an application mediates access between Coherence and untrusted clients (It is completely feasible to create a proxy with a high level language between HTTPS and HTTP, for example a https proxy server for Coherence was created on Node.js with 58 code lines). In resume untrusted access to Coherence should always be mediated by a layer implementing access control.

The main point are:

1. DoS  because an attacker can exhaust the CPU: when a user makes requests, for example:  a rsa key it consume CPU, if many unauthorized are done. [THC TLS DoS](https://github.com/azet/thc-tls-dos)

Countermeasures:

1. Never expose Coherence directly to internet.
2. Never expose Coherence directly to internet.
3. If you expose it, create a proxy with acl or something like that at least.
4. Configure your firewall.&#x20;


# Openssl

## RSA

### Generate RSA keys

On Openssl:

* openssl genrsa -out rsa.pem 1024
* openssl rsa -in rsa.pem -pubout -outform pem -out rsa-pub.out

On Coherence:

```javascript
{ "version": 1 , "algorithm":"RSA", "operation":"gen" , "length": 1024 }
```

### From Openssl to Coherence priv key

* openssl pkcs8 -nocrypt -in rsa.pem -inform PEM -topk8 -outform DER -out rsa.der
* cat rsa.der | xxd -ps | paste -s -d '' > rsa.der.hex

### From Openssl to Coherence pub key

* openssl rsa -in rsa.pem -pubout -outform DER -out rsa-pub.der
* cat rsa-pub.der | xxd -ps | paste -s -d '' > rsa-pub.der.hex

### From Coherence to Openssl priv key

* cat crsa.der.hex | xxd -r -p - > crsa.der
* openssl rsa -inform der -outform pem -in crsa.der -out crsa.pem

### From Coherence to Openssl pub key

* cat crsa-pub.der.hex | xxd -r -p - > crsa-pub.der
* openssl rsa -inform der -outform pem -pubin -in crsa-pub.der -out crsa-pub.pem

## DSA

### Generate DSA keys

On Openssl:

* openssl dsaparam -out dsa-param.pem 1024
* openssl gendsa -out dsa.pem dsa-param.pem
* openssl dsa -in dsa.pem -pubout -outform PEM -out dsa-pub.pem

On Coherence:

```javascript
{"operation": "gen", "version": 1, "length": 1024, "algorithm": "DSA"}
```

### From Openssl to Coherence priv key

* openssl pkcs8 -nocrypt -in dsa.pem -inform PEM -topk8 -outform DER -out dsa.der
* cat dsa.der | xxd -ps | paste -s -d '' > dsa.der.hex

### From Openssl to Coherence pub key

* openssl dsa -in dsa.pem -pubout -outform DER -out dsa-pub.der
* cat dsa-pub.der | xxd -ps | paste -s -d '' > dsa-pub.der.hex

### From Coherence to Openssl priv key

* cat cdsa.der.hex | xxd -r -p - > cdsa.der
* openssl dsa -inform der -outform pem -in cdsa.der -out cdsa.pem

### From Coherence to Openssl pub key

* cat cdsa-pub.der.hex | xxd -r -p - > cdsa-pub.der
* openssl dsa -inform der -outform pem -pubin -in cdsa-pub.der -out cdsa-pub.pem


# Json-reference

## Basic Rules

When you send a json to Coherence, it only accepts (something like base64):

> alphanum + "!\\"#$%&\\'()\*+,-./:;<=>?@\[\\]^\_\`{|}\~ "

```
#!/usr/bin/env python

import socket
import json
import os,binascii

def sending(message):
    ip = '127.0.0.1'
    port = 6613
    BUFFER_SIZE = 65536
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, port))
    s.send(message)
    data = s.recv(BUFFER_SIZE)
    s.close()
    return data

data_js='{"version":1,"algorithm":"SHA3_512","type":"string","plaintext":"616263","hex":1}'
sending(data_js)
```

Substitute data\_js with your own json

## Hash

Apply hash sha3\_512 to hex string "616263"

> {"version":1,"algorithm":"SHA3\_512","type":"string","plaintext":"616263","hex":1}

Apply hash sha3\_512 to string "mayhem"

> {"version":1,"algorithm":"SHA3\_512","type":"string","plaintext":"mayhem","hex":0}

Apply hash sha3\_512 to string "mayhem"

> {"version":1,"algorithm":"SHA3\_512","type":"string","plaintext":"mayhem"}

Apply hash sha3\_512 to file "mayhem.txt"

> {"version":1,"algorithm":"SHA3\_512","type":"file","file":"mayhem.txt"}

* algorithm: (SHA3\_512, SHA3\_384, SHA3\_256, SHA3\_224, SHA\_512, SHA\_384, SHA\_256, SHA\_224, SHA\_1 ,WHIRLPOOL , BLAKE2B)
* plaintext hex strings , first are decode and then applies algorithm

## Rand numbers

Generate 12 bytes with RAND\_RP without entropy

> {"version":1,"algorithm":"RAND\_RP","length":12}

Generate 12 bytes with RAND\_RP with entropy using /dev/urandom

> {"version":1,"algorithm":"RAND\_RP","length":12,"entropy":0}

Generate 12 bytes with RAND\_RP with entropy using /dev/random

> {"version":1,"algorithm":"RAND\_RP","length":12,"entropy":1}

Generate 12 bytes with RAND\_RP without entropy

> {"version":1,"algorithm":"RAND\_RP","length":12,"entropy":2}

* algorithm: (RAND\_RP, RAND\_AUTO, RAND\_RDRAND)
* Note: RAND\_RDRAND does not need entropy

## Argon2

Generate hash password with argon2i from hex string "0123456789ABCDEF" with t\_cost, m\_cost, parallelism and salt parameters given.

> {"algorithm": "ARGON2", "family": "argon2i", "plaintext": "0123456789ABCDEF", "hashlen": 32, "hex": 1, "t\_cost": 10, "version": 1, "parallelism": 4, "m\_cost": 16, "salt": "ABABABABABABABABABABABABABABABAB", "operation": "hash"}

Generate hash password with argon2i from string "mayhem" with t\_cost, m\_cost, parallelism and salt parameters given.

> {"algorithm": "ARGON2", "family": "argon2i", "plaintext": "mayhem", "hashlen": 32, "hex": 0, "t\_cost": 10, "version": 1, "parallelism": 4, "m\_cost": 16, "salt": "ABABABABABABABABABABABABABABABAB", "operation": "hash"}

Generate hash password with argon2i from string "mayhem" with t\_cost, m\_cost, parallelism and salt parameters given.

> {"algorithm": "ARGON2", "family": "argon2i", "plaintext": "mayhem", "hashlen": 32, "t\_cost": 10, "version": 1, "parallelism": 4, "m\_cost": 16, "salt": "ABABABABABABABABABABABABABABABAB", "operation": "hash"}

* <https://github.com/P-H-C/phc-winner-argon2>
* family: (argon2i, argon2d, argon2id)
* plaintext hex strings , first are decode and then applies algorithm
* Note 1: salt is a hex string

Verify the hex password "0123456789ABCDEF" with argon2i from pwd given.

> {"algorithm": "ARGON2V", "family": "argon2i", "plaintext": "0123456789ABCDEF", "hex": 1, "pwd": "246172676F6E326924763D3139246D3D36353533362C743D31302C703D342451554A42516B464351554A42516B464351554A42516B464351554A42516B464351554A42516B464351554924363276556D62556E2B446D5253385539662F4855656545485262672B4844755631674B4C723956357A3730", "version": 1, "operation": "verify"}

Verify the password "0123456789ABCDEF" with argon2i from pwd given.

> {"algorithm": "ARGON2V", "family": "argon2i", "plaintext": "0123456789ABCDEF", "hex": 0, "pwd": "246172676F6E326924763D3139246D3D36353533362C743D31302C703D342451554A42516B464351554A42516B464351554A42516B464351554A42516B464351554A42516B464351554924363276556D62556E2B446D5253385539662F4855656545485262672B4844755631674B4C723956357A3730", "version": 1, "operation": "verify"}

Verify the password "0123456789ABCDEF" with argon2i from pwd given.

> {"algorithm": "ARGON2V", "family": "argon2i", "plaintext": "0123456789ABCDEF", "pwd": "246172676F6E326924763D3139246D3D36353533362C743D31302C703D342451554A42516B464351554A42516B464351554A42516B464351554A42516B464351554A42516B464351554924363276556D62556E2B446D5253385539662F4855656545485262672B4844755631674B4C723956357A3730", "version": 1, "operation": "verify"}

* family: (argon2i, argon2d, argon2id)
* Note 1: plaintext hex strings , first are decode and then applies algorithm

## POLY1305

Calculate POLY1305 to hex string "616263" with the key and nonce given.

> {"version":1,"algorithm":"POLY1305","type":"string","plaintext":"616263","hex":1,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","nonce":"0123456789ABCDEF0123456789ABCDEF"}

Calculate POLY1305 to string "abc" with the key and nonce given.

> {"version":1,"algorithm":"POLY1305","type":"string","plaintext":"abc","hex":0,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","nonce":"0123456789ABCDEF0123456789ABCDEF"}

Calculate POLY1305 to string "abc" with the key and nonce given.

> {"version":1,"algorithm":"POLY1305","type":"string","plaintext":"abc","key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","nonce":"0123456789ABCDEF0123456789ABCDEF"}

Calculate POLY1305 to file "file\_test/AB.mayhem" with the key and nonce given.

> {"version":1,"algorithm":"POLY1305","type":"file","file":"file\_test/AB.mayhem","key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","nonce":"0123456789ABCDEF0123456789ABCDEF"}

* Note 1: plaintext hex strings , first are decode and then applies algorithm
* Note 2: key, nonce are hex strings

## HMAC

Calculate HMAC-SHA3\_512 to hex string "616263" with the key given.

> {"version":1,"algorithm":"HMAC","type":"string","plaintext":"616263","hex":1,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","family":"sha3\_512"}

Calculate HMAC-SHA3\_512 to string "abc" with the key given

> {"version":1,"algorithm":"HMAC","type":"string","plaintext":"abc","hex":0,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","family":"sha3\_512"}

Calculate HMAC-SHA3\_512 to string "abc" with the key given

> {"version":1,"algorithm":"HMAC","type":"string","plaintext":"abc","key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","family":"sha3\_512"}

Calculate HMAC-SHA3\_512 to file "file\_test/AB.mayhem" with the key given

> {"version":1,"algorithm":"HMAC","type":"file","file":"file\_test/AB.mayhem","key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","family":"sha3\_512"}

* family:  (SHA3\_512, SHA3\_384, SHA3\_256, SHA3\_224, SHA\_512, SHA\_384, SHA\_256, SHA\_224, SHA\_1 ,WHIRLPOOL)
* Note 1: plaintext hex strings , first are decode and then applies algorithm
* Note 2: key is hex string

## CMAC

Calculate CMAC-AES to hex string "616263" with the key given

> {"version":1,"algorithm":"CMAC","type":"string","plaintext":"616263","hex":1,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" ,"family":"aes"}

Calculate CMAC-AES to string "abc" with the key given

> {"version":1,"algorithm":"CMAC","type":"string","plaintext":"abc","hex":0,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","family":"aes"}

Calculate CMAC-AES to string "abc" with the key given

> {"version":1,"algorithm":"CMAC","type":"string","plaintext":"abc","key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","family":"aes"}

Calculate CMAC-AES to file "file\_test/AB.mayhem" with the key given

> {"version":1,"algorithm":"CMAC","type":"file","file":"file\_test/AB.mayhem","key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","family":"aes"}

* family: ("aes", "rc6", "mars","serpent","twofish', "cast256")
* Note 1: plaintext hex strings , first are decode and then applies algorithm
* Note 2: key is hex string

## VMAC

Calculate CMAC-AES to hex string "616263" with the key and iv given

> {"version":1,"algorithm":"VMAC","type":"string","plaintext":"616263","hex":1,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","iv":"0123456789ABCDEF0123456789ABCDEF","family":"aes"}

Calculate CMAC-AES to string "abc" with the key and iv given

> {"version":1,"algorithm":"VMAC","type":"string","plaintext":"abc","hex":0,"key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","iv":"0123456789ABCDEF0123456789ABCDEF","family":"aes"}

Calculate CMAC-AES to string "abc" with the key and iv given

> {"version":1,"algorithm":"VMAC","type":"string","plaintext":"abc","key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","iv":"0123456789ABCDEF0123456789ABCDEF","family":"aes"}

Calculate CMAC-AES to file "file\_test/AB.mayhem" with the key and iv given

> {"version":1,"algorithm":"VMAC","type":"file","file":"file\_test/AB.mayhem","key":"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF","iv":"0123456789ABCDEF0123456789ABCDEF","family":"aes"}

* family: ("aes", "rc6", "mars","serpent","twofish', "cast256")
* Note 1: plaintext hex strings , first are decode and then applies algorithm
* Note 2: key,iv are hex strings

## Stream

Encrypt with sosemanuk the hex string "616263" with the key and iv given.

> {"algorithm":"SOSEMANUK","plaintext":"616263","hex":1,"iv":"b05691ef92cb9c9bf77e5613819fc4ea","version":1,"key":"7f685ba92789f0d8d421038f2b1b4fcd73be586d81795ec3ab7939975b7b896e","operation":"enc","type":"string"}

Decrypt with sosemanuk the hex string "2F99C7" with the key and iv given.

> {"algorithm":"SOSEMANUK","plaintext":"2F99C7","hex":1,"iv":"b05691ef92cb9c9bf77e5613819fc4ea","version":1,"key":"7f685ba92789f0d8d421038f2b1b4fcd73be586d81795ec3ab7939975b7b896e","operation":"dec","type":"string"}

Encrypt with sosemanuk the string "Hello world" with the key and iv given.

> {"algorithm": "SOSEMANUK", "plaintext": "Hello world", "hex": 0, "iv": "fdd48f4c3e7b38d8340d843af4ee4105", "version": 1, "key":"5845b90c901e1e23155fee366aad083c843d86d3bbff5203821bdc1fc44870b6", "operation": "enc", "type": "string"}

Decrypt with sosemanuk the hex string "0B38620DDAE1B83B036DC9" with the key and iv given.

> {"algorithm": "SOSEMANUK", "plaintext": "0B38620DDAE1B83B036DC9", "hex": 0, "iv": "fdd48f4c3e7b38d8340d843af4ee4105", "version": 1, "key":"5845b90c901e1e23155fee366aad083c843d86d3bbff5203821bdc1fc44870b6", "operation": "dec", "type": "string"}

Encrypt with sosemanuk the string "Hello world" with the key and iv given.

> {"algorithm": "SOSEMANUK", "plaintext": "Hello world", "iv": "cf29562a250732c6c126d28e903f1ab2", "version": 1, "key": "bcd3db7c5c17bfa7d937748f97dd11e7f84247c6c62c27afeca2ee9c19d79703", "operation": "enc", "type": "string"}

Decrypt with sosemanuk the hex string "0B38620DDAE1B83B036DC9" with the key and iv given.

> {"algorithm": "SOSEMANUK", "plaintext": "A27EA0F6A1CA0649938D38", "iv": "cf29562a250732c6c126d28e903f1ab2", "version": 1, "key": "bcd3db7c5c17bfa7d937748f97dd11e7f84247c6c62c27afeca2ee9c19d79703", "operation": "dec", "type": "string"}

Encrypt with sosemanuk the file "file\_test/AB.mayhem" with the key and iv given.

> {"file": "file\_test/AB.mayhem", "algorithm": "SOSEMANUK", "iv": "b7b6c3b588d06bc8f3d0eb48918dfd36", "version": 1,"key": "3ff44eca6261be2276ff25a54039eac8418f26750233018c2692f9c98e4ef53b", "operation": "enc", "type": "file"}

Decrypt with sosemanuk the file "file\_test/AB.mayhem.enc" with the key and iv given.

> {"file": "file\_test/AB.mayhem.enc", "algorithm": "SOSEMANUK", "iv": "b7b6c3b588d06bc8f3d0eb48918dfd36", "version": 1, "key": "3ff44eca6261be2276ff25a54039eac8418f26750233018c2692f9c98e4ef53b", "operation": "dec", "type": "file"}

* algorithm: ("SOSEMANUK", "SALSA20")
* Note 1: plaintext hex strings , first are decode and then applies algorithm
* Note 2: key,iv are hex strings
* Note 3: For string decryption the plaintext is hex
* Note 4: For file encryption output is the name file + ".enc"
* Note 5: For file decryption output is the name file + ".dec"
* Note 6: Salsa20 uses 8 bit iv

## Block

Encrypt with AES the hex string "616263" with the key and iv given in mode ctr.

> {"algorithm": "AES", "plaintext": "616263", "hex": 1, "iv": "d58b541118f4aef2c942eb79e57228ee", "version": 1, "mode": "ctr", "key": "1f24df90ae90e8e79eca3f25c120c6c1c68be4c5efced419441c042f6324c683", "operation": "enc", "type": "string"}

Decrypt with AES the hex string "001901" with the key and iv given in mode ctr.

> {"algorithm": "AES", "plaintext": "001901", "hex": 1, "iv": "d58b541118f4aef2c942eb79e57228ee", "version": 1, "mode": "ctr", "key": "1f24df90ae90e8e79eca3f25c120c6c1c68be4c5efced419441c042f6324c683", "operation": "dec", "type": "string"}

Encrypt with AES the hex string "616263" with the key,iv and adata given in mode gcm.

> {"algorithm": "AES", "plaintext": "616263", "type": "string", "hex": 1, "iv": "1f8478e7e528c1d6e2792d7a40e7d47b", "version": 1, "mode": "gcm", "key": "df2fa695c6c61097a1add5324de12fb4780c6597aefd9d69db46324bf856dea0", "operation": "enc", "adata": "ea52426dc92300c85378457ee39354e6b3410181b18a2247"}

Decrypt with AES the hex string "7516B097C5C7E423FEA2EE3BC3D7C042C93B62" with the key,iv and adata given in mode gcm.

> {"algorithm": "AES", "plaintext": "7516B097C5C7E423FEA2EE3BC3D7C042C93B62", "type": "string", "hex": 1, "iv": "1f8478e7e528c1d6e2792d7a40e7d47b", "version": 1, "mode": "gcm", "key": "df2fa695c6c61097a1add5324de12fb4780c6597aefd9d69db46324bf856dea0", "operation": "dec", "adata": "ea52426dc92300c85378457ee39354e6b3410181b18a2247"}

Encrypt with AES the string "Hello world" with the key and iv given in mode ctr.

> {"algorithm": "AES", "plaintext": "Hello world", "hex": 0, "iv": "fe016a5cd69fe3d65e04b16dc3b9a57b", "version": 1, "mode": "ctr", "key": "b47d58c182e1ea2049846465840a7b608b2bcee645f544544fda8adf79d701da", "operation": "enc", "type": "string"}

Decrypt with AES the hex string "47B1604AB5C410AEA759C8" with the key and iv given in mode ctr.

> {"algorithm": "AES", "plaintext": "47B1604AB5C410AEA759C8", "hex": 0, "iv": "fe016a5cd69fe3d65e04b16dc3b9a57b", "version": 1, "mode": "ctr", "key": "b47d58c182e1ea2049846465840a7b608b2bcee645f544544fda8adf79d701da", "operation": "dec", "type": "string"}

Encrypt with AES the string "Hello world" with the key,iv and adata given in mode gcm.

> {"algorithm": "AES", "plaintext": "Hello world", "type": "string", "hex": 0, "iv": "38f0f10ed74f1a3aec2e871ee7d55bb7", "version": 1, "mode": "gcm", "key": "806b622117af9774439dd13744b227dcd3ca0caaed5374baba6da1f6c61cf85c", "operation": "enc", "adata": "dc300f92ed1d255e329c84f782ef87a410fdd74176389366"}

Decrypt with AES the hex string "9A54E2B0885CE5A3FED43FA8014DB36404AE3875BB6FD8F2C557B1" with the key,iv and adata given in mode gcm.

> {"algorithm": "AES", "plaintext": "9A54E2B0885CE5A3FED43FA8014DB36404AE3875BB6FD8F2C557B1", "type": "string", "hex": 0, "iv": "38f0f10ed74f1a3aec2e871ee7d55bb7", "version": 1, "mode": "gcm", "key": "806b622117af9774439dd13744b227dcd3ca0caaed5374baba6da1f6c61cf85c", "operation": "dec", "adata": "dc300f92ed1d255e329c84f782ef87a410fdd74176389366"}

Encrypt with AES the string "Hello world" with the key and iv given in mode ctr.

> {"algorithm": "AES", "plaintext": "Hello world", "iv": "3315aa2791b1967616e3bbf366043678", "version": 1, "mode": "ctr", "key": "69bf227b55e69ae3bf1f35570f6e8899a279b0b26c8c808f573ab15cbe66dd5b", "operation": "enc", "type": "string"}

Decrypt with AES the hex string "307A69CDFA5C630F7D8AF4" with the key and iv given in mode ctr.

> {"algorithm": "AES", "plaintext": "307A69CDFA5C630F7D8AF4", "iv": "3315aa2791b1967616e3bbf366043678", "version": 1, "mode": "ctr", "key": "69bf227b55e69ae3bf1f35570f6e8899a279b0b26c8c808f573ab15cbe66dd5b", "operation": "dec", "type": "string"}

Encrypt with AES the string "Hello world" with the key,iv and adata given in mode gcm.

> {"algorithm": "AES", "plaintext": "Hello world", "type": "string", "iv": "e685a62a303585f11fbd2b30a37fe6c7", "version": 1, "mode": "gcm", "key": "d07f8a8d4f6269b99cdfa8951a5d1714ca3cfead3ae9deeccde3ecdcac27ed2e", "operation": "enc", "adata": "cd18dbb63a27adaaf45d00bf3fe5d0c656a77c2b00d8d211"}

Decrypt with AES the hex string "151BF8439C7F544B641851DF99F39C2545FB3DF4A98122ECBF0660" with the key,iv and adata given in mode gcm.

> {"algorithm": "AES", "plaintext": "151BF8439C7F544B641851DF99F39C2545FB3DF4A98122ECBF0660", "type": "string", "iv": "e685a62a303585f11fbd2b30a37fe6c7", "version": 1, "mode": "gcm", "key": "d07f8a8d4f6269b99cdfa8951a5d1714ca3cfead3ae9deeccde3ecdcac27ed2e", "operation": "dec", "adata": "cd18dbb63a27adaaf45d00bf3fe5d0c656a77c2b00d8d211"}

Encrypt with AES the file "file\_test/AB.mayhem" with the key and iv given in mode ctr.

> {"file": "file\_test/AB.mayhem", "algorithm": "AES", "iv": "82a8b44118d54f924b3b680b432b972e", "version": 1, "mode": "ctr", "key": "40475638b970df800b61a67308a9d3a1c3b7793987ea6399a1319334aa66042a", "operation": "enc", "type": "file"}

Decrypt with AES the file "file\_test/AB.mayhem.enc" with the key and iv given in mode ctr.

> {"file": "file\_test/AB.mayhem.enc", "algorithm": "AES", "plaintext": "file\_test/AB.mayhem.enc", "iv": "82a8b44118d54f924b3b680b432b972e", "version": 1, "mode": "ctr", "key": "40475638b970df800b61a67308a9d3a1c3b7793987ea6399a1319334aa66042a", "operation": "dec", "type": "file"}

* algorithm: ("AES","RC6","MARS","SERPENT","TWOFISH","CAST256")
* Note 1: plaintext hex strings , first are decode and then applies algorithm
* Note 2: key,iv are hex strings
* Note 3: For string decryption the plaintext is hex
* Note 4: For file encryption output is the name file + ".enc"
* Note 5: For file decryption output is the name file + ".dec"
* Note 6: GCM mode does not support file encryption

## RSA

Generate 1024 RSA key pair

> {"operation": "gen", "version": 1, "length": 1024, "algorithm": "RSA"}

Sign the string "Hello world" with privkey given

> {"algorithm": "RSA", "plaintext": "Hello world", "hex": 0, "version": 1, "operation": "sign", "type": "string", "privkey": "30820274020100300D06092A864886F70D01010105000482025E3082025A0201000281810094A9246782C6986E8AC545B98D4D4888FF0B222F67E5F933A1093512ED8CB933DD7C54D35FE544A22DAFF880F8F1D104443CDC68631374C7A3766A6D2F66635F21C884C5ABAAA2BB5B00FAB4E97DB85C87D862A9FD739967F6772B68BDF7AEF54DC0B2A602559ECCBEA59B49596A423FC2284E37558E1D4678C65ADEEFFE0F7302011102818002EA37EDF381625C853B157211D456C16E69A14C3940BE9C9EC3F1FB59FDBD5B5EB21FC7E8C84299C4A918F378550E23388DBE0C160566AE94C6162A4C393929FE081D0103CE32DE61308047B4269E5443B97C535D82E5C71499F70B701C4B093891E5C52E417ED1B0E8C96E91BC915EBB002EDE956238E0F9EADD0BA0122D01024100C26F438EE0ABC2C6CEF15FF471065910E49537E09B81BAFC5DD62E7F57CE8A07CC17334F4287E9B7D960893195E1E9E3A6558C9BDC39042CE34FD1F7A08A16B9024100C3BB7A0308EABDA72F660C7794C5D482254F662DC0DE17C27DF4C5A2108632173C98B90E88C1714EA6E4F110BAF7627CDAC96B43B6C3C447CBAC7F966BD5018B024100893F7AFB716A2F22EC6E259D7CF56C0BECA590DAC81F56D0423CD58710CE0714901060742EF686DC20F8D9502D9068DCEDE208E67D556C5BEBBFDF819E7F979102405C1BFD2E9AC8D1B8164E23FC09C6821F209DD5BB2D95B0D3FF09C66A6221088367CF6624F50FBCD9B7F344441BBFB5E066F55FA76510D4D67DF6D2A123AF8841024022A68F1978CB85DBF4EDEDE030339CE9A995425D0B89FEB57E65BB694DD7304EC83883C4F7228E664501A4966EF5EA66B468277FE16E7E1C8C20B4B39AEB2C6F"}

Verify the digital signature of string "Hello world" with pubkey given

> {"algorithm": "RSA", "plaintext": "Hello world", "pubkey": "30819D300D06092A864886F70D010101050003818B003081870281810094A9246782C6986E8AC545B98D4D4888FF0B222F67E5F933A1093512ED8CB933DD7C54D35FE544A22DAFF880F8F1D104443CDC68631374C7A3766A6D2F66635F21C884C5ABAAA2BB5B00FAB4E97DB85C87D862A9FD739967F6772B68BDF7AEF54DC0B2A602559ECCBEA59B49596A423FC2284E37558E1D4678C65ADEEFFE0F73020111", "hex": 0, "sign": "1A400315E7132A3E15FCE6814D17454FB3F9E6A2035296C7D57BB63E5A39BE8F5B1FCCD8653D038B46B32265658B31289C205620B75E88EBD95CA1E0E30FD78B60271878BE279ED3C56FE855DA8FB327B453FEF5136BDDAF36932DAC5C0C08E678B49D518060F862D2A1960B100B94B5AF81E787004F7CDB4D6BCF63246904F5", "version": 1, "operation": "verify", "type": "string"}

Encrypt the string "hello world!!!" with pubkey given

> {"algorithm": "RSA", "plaintext": "hello world!!!", "type": "string", "version": 1, "operation": "enc", "pubkey": "30819D300D06092A864886F70D010101050003818B003081870281810094A9246782C6986E8AC545B98D4D4888FF0B222F67E5F933A1093512ED8CB933DD7C54D35FE544A22DAFF880F8F1D104443CDC68631374C7A3766A6D2F66635F21C884C5ABAAA2BB5B00FAB4E97DB85C87D862A9FD739967F6772B68BDF7AEF54DC0B2A602559ECCBEA59B49596A423FC2284E37558E1D4678C65ADEEFFE0F73020111"}

Decrypt the plaintext given with the privkey given

> {"algorithm": "RSA", "plaintext": "82E215088FC28DD580B35906367BFF7992A9F13034DAEEA14D2CDE9C2A539758AFD63A2B160E5C7D9700D81EE630A5D574DC1802152FE7405CD86D7A58FF0E3353C32B0DF48DAF9BCF721EF438C5405E941AE05ACA5DA732B06FFE11903158B66E5333DA57CCBCF2015B0E1C64AF5E006355B9F8D89901829B88962B2C2C7EE6", "type": "string", "version": 1, "operation": "dec", "privkey": "30820274020100300D06092A864886F70D01010105000482025E3082025A0201000281810094A9246782C6986E8AC545B98D4D4888FF0B222F67E5F933A1093512ED8CB933DD7C54D35FE544A22DAFF880F8F1D104443CDC68631374C7A3766A6D2F66635F21C884C5ABAAA2BB5B00FAB4E97DB85C87D862A9FD739967F6772B68BDF7AEF54DC0B2A602559ECCBEA59B49596A423FC2284E37558E1D4678C65ADEEFFE0F7302011102818002EA37EDF381625C853B157211D456C16E69A14C3940BE9C9EC3F1FB59FDBD5B5EB21FC7E8C84299C4A918F378550E23388DBE0C160566AE94C6162A4C393929FE081D0103CE32DE61308047B4269E5443B97C535D82E5C71499F70B701C4B093891E5C52E417ED1B0E8C96E91BC915EBB002EDE956238E0F9EADD0BA0122D01024100C26F438EE0ABC2C6CEF15FF471065910E49537E09B81BAFC5DD62E7F57CE8A07CC17334F4287E9B7D960893195E1E9E3A6558C9BDC39042CE34FD1F7A08A16B9024100C3BB7A0308EABDA72F660C7794C5D482254F662DC0DE17C27DF4C5A2108632173C98B90E88C1714EA6E4F110BAF7627CDAC96B43B6C3C447CBAC7F966BD5018B024100893F7AFB716A2F22EC6E259D7CF56C0BECA590DAC81F56D0423CD58710CE0714901060742EF686DC20F8D9502D9068DCEDE208E67D556C5BEBBFDF819E7F979102405C1BFD2E9AC8D1B8164E23FC09C6821F209DD5BB2D95B0D3FF09C66A6221088367CF6624F50FBCD9B7F344441BBFB5E066F55FA76510D4D67DF6D2A123AF8841024022A68F1978CB85DBF4EDEDE030339CE9A995425D0B89FEB57E65BB694DD7304EC83883C4F7228E664501A4966EF5EA66B468277FE16E7E1C8C20B4B39AEB2C6F"}

Sign the file "file\_test/AB.mayhem" with privkey given

> {"algorithm": "RSA", "file": "file\_test/AB.mayhem", "version": 1, "operation": "sign", "type": "file", "privkey": "30820274020100300D06092A864886F70D01010105000482025E3082025A0201000281810094A9246782C6986E8AC545B98D4D4888FF0B222F67E5F933A1093512ED8CB933DD7C54D35FE544A22DAFF880F8F1D104443CDC68631374C7A3766A6D2F66635F21C884C5ABAAA2BB5B00FAB4E97DB85C87D862A9FD739967F6772B68BDF7AEF54DC0B2A602559ECCBEA59B49596A423FC2284E37558E1D4678C65ADEEFFE0F7302011102818002EA37EDF381625C853B157211D456C16E69A14C3940BE9C9EC3F1FB59FDBD5B5EB21FC7E8C84299C4A918F378550E23388DBE0C160566AE94C6162A4C393929FE081D0103CE32DE61308047B4269E5443B97C535D82E5C71499F70B701C4B093891E5C52E417ED1B0E8C96E91BC915EBB002EDE956238E0F9EADD0BA0122D01024100C26F438EE0ABC2C6CEF15FF471065910E49537E09B81BAFC5DD62E7F57CE8A07CC17334F4287E9B7D960893195E1E9E3A6558C9BDC39042CE34FD1F7A08A16B9024100C3BB7A0308EABDA72F660C7794C5D482254F662DC0DE17C27DF4C5A2108632173C98B90E88C1714EA6E4F110BAF7627CDAC96B43B6C3C447CBAC7F966BD5018B024100893F7AFB716A2F22EC6E259D7CF56C0BECA590DAC81F56D0423CD58710CE0714901060742EF686DC20F8D9502D9068DCEDE208E67D556C5BEBBFDF819E7F979102405C1BFD2E9AC8D1B8164E23FC09C6821F209DD5BB2D95B0D3FF09C66A6221088367CF6624F50FBCD9B7F344441BBFB5E066F55FA76510D4D67DF6D2A123AF8841024022A68F1978CB85DBF4EDEDE030339CE9A995425D0B89FEB57E65BB694DD7304EC83883C4F7228E664501A4966EF5EA66B468277FE16E7E1C8C20B4B39AEB2C6F"}

Verify the digital signature of file "file\_test/AB.mayhem.signed" with pubkey given

> {"algorithm": "RSA", "file": "file\_test/AB.mayhem.signed", "pubkey": "30819D300D06092A864886F70D010101050003818B003081870281810094A9246782C6986E8AC545B98D4D4888FF0B222F67E5F933A1093512ED8CB933DD7C54D35FE544A22DAFF880F8F1D104443CDC68631374C7A3766A6D2F66635F21C884C5ABAAA2BB5B00FAB4E97DB85C87D862A9FD739967F6772B68BDF7AEF54DC0B2A602559ECCBEA59B49596A423FC2284E37558E1D4678C65ADEEFFE0F73020111", "version": 1, "operation": "verify", "type": "string"}

Be careful, when you sign a document the output is file name + "sign", in this case file\_test/AB.mayhem.sign, to verify you need to concatenate.

```
cat file_test/AB.mayhem file_test/AB.mayhem.sign > file_test/AB.mayhem.signed
```

* Note 1: plaintext hex strings , first are decode and then applies algorithm
* Note 2: keys are hex strings
* Note 3: For string decryption the plaintext is hex
* Note 4: hex parameter is optional 0 indicate string is not hex and 1 indicate string is hex, default 0.
* Note 5: RSA does not support file encryption

## DSA

Generate 1024 DSA key pair

> {"operation": "gen", "version": 1, "length": 1024, "algorithm": "DSA"}

Sign the string "Hello world" with privkey given

> {"algorithm": "DSA", "plaintext": "Hello world", "hex": 0, "length": 1024, "version": 1, "operation": "sign", "type": "string", "privkey": "3082014B0201003082012C06072A8648CE3804013082011F02818100F132CDA3C884F877C25556188AFE6BEF185B9EA13ED451D406F5CFAC2AAE04C3F003F500268FF64C8A73607E30BF8125451A7DF36041503EC6EBE643924573BFF7F45080E1DC13D9676648F91EF6FBFD504ABF91C5E3E5A0C5A9044290FA605748B87358E8E80FD489603A85E96D2F37C6AEE2C3BA9AE22DBDEB6F8DC5BA4A070215008AF380C12622501A0EFD7B95D20ADB1FA9EA1D1702818100EFF2D0B9972CE675CA8E0F6C87C75B18EB5888EC830CDCD32AFDAF23724757D0F22EF2653AFBEFB4B61C8773A52C687804268C303BAF2DB672A3F489AFAADC6D3BEF15E0F06E872211FA84FF5D13BBD96CD57387E5AE217FE1070EADB99ECE0DC7462872E9B7DCBDE054A653B9F64D9D02D80153EDD374624EADC4B20C1F1025041602146CF7EB67929A6616F193DB23A1D1AE8C8FF69D3B"}

Verify the digital signature of string "Hello world" with pubkey given

> {"algorithm": "DSA", "plaintext": "Hello world", "pubkey": "308201B83082012C06072A8648CE3804013082011F02818100F132CDA3C884F877C25556188AFE6BEF185B9EA13ED451D406F5CFAC2AAE04C3F003F500268FF64C8A73607E30BF8125451A7DF36041503EC6EBE643924573BFF7F45080E1DC13D9676648F91EF6FBFD504ABF91C5E3E5A0C5A9044290FA605748B87358E8E80FD489603A85E96D2F37C6AEE2C3BA9AE22DBDEB6F8DC5BA4A070215008AF380C12622501A0EFD7B95D20ADB1FA9EA1D1702818100EFF2D0B9972CE675CA8E0F6C87C75B18EB5888EC830CDCD32AFDAF23724757D0F22EF2653AFBEFB4B61C8773A52C687804268C303BAF2DB672A3F489AFAADC6D3BEF15E0F06E872211FA84FF5D13BBD96CD57387E5AE217FE1070EADB99ECE0DC7462872E9B7DCBDE054A653B9F64D9D02D80153EDD374624EADC4B20C1F102503818500028181009ED34DF0E1734BE7E2105A283CA6059995BA05207E51DD0EEA68B8E71933419EEBD35ABD6EC9B3C18D3B74707E0C1B644ABBDFE76631E3770AF5823640528CE8935127227E6F7789BBAA1A9727C63186A618BB7634C9052C6F4A61336F6D72AB5B59FBE7CA75ECA7C262DF19E34B40F7841138FE213A81F0A1FA827C5844C2A0", "hex": 0, "sign": "442870692F838F579A906197DF147D2BE8CB42B2131B264407DC992905328AD442D21BAB2C4355A8", "version": 1, "operation": "verify", "type": "string"}

* Note 1: plaintext hex strings , first are decode and then applies algorithm
* Note 2: keys are hex strings
* Note 3: For string decryption the plaintext is hex
* Note 4: hex parameter is optional 0 indicate string is not hex and 1 indicate string is hex, default 0.
* Note 5: to sign file is the same like RSA
* Note 6: key length are 1024, 2048 and 3096

## DH

Generate a 1024 custom DH key

> {"length": 1024, "version": 1, "operation": "gen\_n\_rfc", "algorithm": "DH"}

Generate custom DH key with g,q,p and a shared-key with g,q,p,sharedpub given

> {"algorithm": "DH", "g": "0x2h", "q": "0x4c43a35765a7da1fba0703df93501de469f791c840d741ace94cae79e2207c17c683b61355abab933a4d1deaabe2809e64a77d85c5654c4962379ce516f7d19283d9b000c200549a2bf06e83252a911614e9a5e794c103b06cf0f41db28dc69527ed3054fce6ebc892bd7473c3a1a421e35d4295cd793593803e625042881847h", "p": "0x988746aecb4fb43f740e07bf26a03bc8d3ef239081ae8359d2995cf3c440f82f8d076c26ab575726749a3bd557c5013cc94efb0b8aca9892c46f39ca2defa32507b360018400a93457e0dd064a55222c29d34bcf29820760d9e1e83b651b8d2a4fda60a9f9cdd791257ae8e787434843c6ba852b9af26b27007cc4a08510308fh", "version": 1, "operation": "a\_n\_rfc\_gen", "sharedpub": "42010BCFA20D232268A967B006C852A2ADAB05AA27CE1EFEB2807E1FB37343B9993EE18B47A5D50B897954F995BE5B24DBC95C28050BDB916DCBF90FE46DCD5D802CF51BB68F736204642614D6FA7D1E6F98A07FD295929F65A0B2F8777491DCFA616DFE012C4C7CBBF6219715BE2BD98DD0BDFC2C5D9EC9750713B5806A59E6"}

Generate a shared-key with g,q,p,sharedpub given

> {"algorithm": "DH", "g": "0x2h", "q": "0x4c43a35765a7da1fba0703df93501de469f791c840d741ace94cae79e2207c17c683b61355abab933a4d1deaabe2809e64a77d85c5654c4962379ce516f7d19283d9b000c200549a2bf06e83252a911614e9a5e794c103b06cf0f41db28dc69527ed3054fce6ebc892bd7473c3a1a421e35d4295cd793593803e625042881847h", "p": "0x988746aecb4fb43f740e07bf26a03bc8d3ef239081ae8359d2995cf3c440f82f8d076c26ab575726749a3bd557c5013cc94efb0b8aca9892c46f39ca2defa32507b360018400a93457e0dd064a55222c29d34bcf29820760d9e1e83b651b8d2a4fda60a9f9cdd791257ae8e787434843c6ba852b9af26b27007cc4a08510308fh", "version": 1, "operation": "a\_n\_rfc", "sharedpub": "5348996122CA5076857506844E2CFD58DC8CB9393478B237D78A2AF3E2CCAF9C6DA8536A133DF050EC95916645D3CBE3AC9EC3805E223B412CC4E8BCFA6ACA5BCCF9AF1D59AC8403BD79C74C586266AC16ABB89027F584C4F9074E5A549E6F6E2F4CC88FA9E3F1752D33FDC1599F71ECB6159F914AC2FC4449076048DDE84D98", "privkey": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000BA849157DD9B15F703A27537F20B62E3C84D8C19"}

Generate a modp256 DH key

> {"operation": "gen\_rfc", "version": 1, "algorithm": "DH", "family": "modp256"}

Generate a modp256 shared-key with the sharedpub and privkey given

> {"algorithm": "DH", "family": "modp256", "version": 1, "operation": "a\_rfc", "sharedpub": "6E05A9AC40DD30BB348AC1B7F702E50705273AD8B9EBA41EE74090D76580D72DB1C114501FF11D9B231B3A0253CA95503D12CE74198FCF5014997DE3CBDFCBB95F5364FC89047E4787804D6D375162D69862AD5751BC9BF0CC2799271F54806891D3756E4EAAF8981BFD07767A9B5844672254E1B7D3B46A8901E68F242B0D221287EAC839185B6E36D8FA8618F329840436726A4116D15EC03E4DEACE3017C269E92CD1B1A2ADBE8C1AC517777DA48143043E2A58C7DDF24BACA42804DE2AFD41ECE5029CC1C0CAD78DCEBA4E82ACF2EF8ABF819158A01DD00B6590C20AF799B5D48C1B2399DBC3AA98C17302B49D6508F8D1146C5C78CA2C1BEE6BA4447738", "privkey": "000000011B878B369478626B964DAC6F48FBFE8A1FDAAAD0F2B04B2C76DCDE56"}

## ECC

> {"version": 1, "curve": "brainpoolP512r1", "algorithm": "ECC\_GEN"}
>
> {"algorithm": "ECIES", "plaintext": "hello world!!!", "type": "string", "curve": "secp256k1", "version": 1, "operation": "enc", "pubkey": "3056301006072A8648CE3D020106052B8104000A034200046834B695857EF396C514B4DAFE852DB8C8B5011D64C0B591470127242DFD7FB6A475AF07A02FAC70D36F8B7CE40075FE5E6B5CD0B64FE54255852A311FA7A132"}
>
> {"algorithm": "ECIES", "plaintext": "0474AA7F7642EC769D79B78ACC8F55DF3C8CC2DCA2F175852487FD7D62357A1D743EC0087F26C0DEF109D8E095945FF0F668CD9B55EBCE6E77DF2015445E649D531616E7224D8253703A48810ED9FD9FA00F3E780B4DA295476A4F749B663543B56C54", "type": "string", "curve": "secp256k1", "version": 1, "operation": "dec", "pubkey": "", "privkey": "303E020100301006072A8648CE3D020106052B8104000A042730250201010420C29E12E92DD3125112C62105D3CBF66D403F6950D4BB0900BEC6A284750C5B00"}
>
> {"algorithm": "ECDSA", "plaintext": "Hello world", "curve": "secp256k1", "hex": 0, "version": 1, "operation": "sign", "type": "string", "privkey": "303E020100301006072A8648CE3D020106052B8104000A042730250201010420C29E12E92DD3125112C62105D3CBF66D403F6950D4BB0900BEC6A284750C5B00"}
>
> {"algorithm": "ECDSA", "plaintext": "Hello world", "pubkey": "3056301006072A8648CE3D020106052B8104000A034200046834B695857EF396C514B4DAFE852DB8C8B5011D64C0B591470127242DFD7FB6A475AF07A02FAC70D36F8B7CE40075FE5E6B5CD0B64FE54255852A311FA7A132", "hex": 0, "curve": "secp256k1", "sign": "8B8C833F4712F4939E4AE1339881CE92BA535A39AB1C719A6E383E7163B377DF2DC390DF97362B07E1AC24D31DA98D84F512ED480BABD1D8CCC2D3EC3C371D10", "version": 1, "operation": "verify", "type": "string"}
>
> {"operation": "gen", "version": 1, "curve": "brainpoolP512r1", "algorithm": "ECDH"}
>
> {"algorithm": "ECDH", "family": "", "curve": "brainpoolP512r1", "version": 1, "operation": "agree", "sharedpub": "048A8A4617B3424FC29889E577E6DCDA1472872D97337644EA73D7EB074784FC3A19731A66D5225DFE6F8174E28C79289B863685153C140D5DAABECE8B32D2F2544ADEE9379FA4CB2C5AC21395C3B48045A52BDEFD2971C3B839880EC0154644A213F62D7FCA31DF74BAA2FB9B2A04E302644D137D19DD67E39A1E4A0AFA162F3C", "privkey": "9482034136882455A51772B79E7C13648EB34614F3B09926AA77B5C4342122FA00124D41C57836526EF06153E61E6BBBD06F5B0B8CCFE6CD8F712429CA811B3F"}

* curve: ("brainpoolP512r1","secp521r1","brainpoolP384r1","secp384r1","brainpoolP320r1","brainpoolP256r1","secp256k1",

  "sect571r1","sect571k1","sect409r1","sect409k1","sect283r1","sect283k1")
* Note 1: to sign file is the same like RSA
* Note 2: ECIES does not support file encryption
* Note 3: ECIES and ECDSA can use the same key, ECDH use its own  key


# Links

Useful links

## Cryptography

<http://www.crypto-textbook.com/>

<https://www.youtube.com/watch?v=2aHkqB2-46k&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy>

<http://www.cryptovirology.com/>

<https://csrc.nist.gov/Projects/Cryptographic-Research>

<http://www.ecrypt.eu.org/stream/>

<https://password-hashing.net/>

## Cypherpunk

<https://www.activism.net/>

<https://www.youtube.com/watch?v=i85fX9-sKYo>

<https://www.youtube.com/watch?v=ektKtroaR4o>

## Linux

<http://advancedlinuxprogramming.com/>

<http://man7.org/tlpi/>

## Linux Admin

<https://access.redhat.com/documentation/en/red-hat-enterprise-linux/>

## C

<http://www.tldp.org/HOWTO/Program-Library-HOWTO/>

## Security

<https://www.cs.fsu.edu/~redwood/OffensiveComputerSecurity/lectures.html>


# Understanding-the-code

## Overview

In this page we are going to explain/describe the coherence's code, this code is built with the help of:

* [argon2](https://github.com/P-H-C/phc-winner-argon2)
* [cryptopp](https://github.com/P-H-C/phc-winner-argon2)
* [libntru](https://github.com/tbuktu/libntru)
* [liboqs](https://github.com/open-quantum-safe/liboqs/)
* [libuv](https://github.com/libuv/libuv)
* [rapidjson](https://github.com/Tencent/rapidjson)

## params.h

* It defines (with preprocessor) which algorithms are going to supported
* It defines info\_log structure to store the log data
* It defines params structure to help to parse json data and share it with the functions&#x20;

## coherence.c

In this file is main function. We create a tcp server with libuv, validate the input, create the logs output and send the client's input to be parsed.

* *main()* displays the banner, create the tcp server, starts the event loop and call *on\_new\_connection()*
* *on\_new\_connection()* accepts the clients, calls *alloc\_buffer()* and *on\_read()*
* *alloc\_buffer()* creates the buffer to store client's input
* *on\_read()* reads data from socket, starts t
  * *if (nread < 0)* an error on  reading socket data
  * *if (nread >= 0)* data is read, creates log info, validate the buffer with *ok\_buff()*,if the buffer isn't ok close the connection, if the buffer is ok sends the input to *PARSING()* , writes the answer, *parse\_log* cleans the log structure and prints the log.

### Further reading

* [libuv - uv\_stream](http://docs.libuv.org/en/v1.x/stream.html)
* [libuv sample](https://github.com/trevnorris/libuv-examples)
* [libuv-dox-examples](https://github.com/thlorenz/libuv-dox/tree/master/examples)

## parsing.h

* *PARSING()* parse the json input with *Parsingjson()* ,and address the algorithm to its functions
* *parse\_log* parse the log, basically delete sensitive information like private keys with *Clear2json()*

### Further reading

* [rapidjson intro](http://rapidjson.org/md_doc_tutorial.html)

## parse-func.h

In this file we can find functions to validate inputs from json client input.

## crypt-argon2.h

Functions to process argon2 requests

### Further reading

* [example](https://github.com/P-H-C/phc-winner-argon2/blob/master/src/argon2.c)&#x20;

## crypt-block.h

Functions to process block cipher requests

### Further reading

* [example](https://www.cryptopp.com/wiki/Block_Cipher)
* [aes example](https://www.cryptopp.com/wiki/Advanced_encryption_standard)

## crypt-dh.h

Functions to process Diffie-Hellman requests

### Further reading

* [example](https://www.cryptopp.com/wiki/Diffie-Hellman)

## crypt-dsa.h

Functions to process DSA requests

### Further reading

* [example](https://www.cryptopp.com/wiki/Digital_Signature_Algorithm)

## crypt-ecc.h

Functions to process Elliptic Curves requests

### Further reading

* [example](https://www.cryptopp.com/wiki/Elliptic_Curve_Cryptography)

## crypt-hash.h

Functions to process HASh requests

### Further reading

* [example](https://www.cryptopp.com/wiki/Hash_Functions)

## crypt-mac.h

Functions to process MAC requests

### Further reading

* [hmac example](https://www.cryptopp.com/wiki/HMAC)
* \[cmac example] (<https://www.cryptopp.com/wiki/CMAC>)

## crypt-ntru.h

Functions to process NTRU requests

### Further reading

* [example](https://github.com/tbuktu/libntru)

## crypt-oqs.h

Functions to process Post Quantum Algorithms requests

### Further reading

* [example](https://github.com/open-quantum-safe/liboqs/blob/master/tests/example_sig.c)

## crypt-rand.h

Functions to process Rand numbers generation requests

### Further reading

* [example](https://www.cryptopp.com/wiki/RandomNumberGenerator)

## crypt-rsa.h

Functions to process RSA requests

### Further reading

* [example](https://www.cryptopp.com/wiki/RSA_Cryptography)

## crypt-stream.h

Functions to process Stream Ciphers requests

### Further reading

* [example](https://www.cryptopp.com/wiki/Stream_Cipher)


