Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuzzing Secure Message #762

Merged
merged 3 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ _Infrastructure:_
- Build system and tests now respect the `PATH` settings ([#685](https://github.com/cossacklabs/themis/pull/685)).
- Rename embedded BoringSSL symbols by default to avoid conflicts with system OpenSSL ([#702](https://github.com/cossacklabs/themis/pull/702)).
- Started phasing out CircleCI in favour of GitHub Actions ([#709](https://github.com/cossacklabs/themis/pull/709), [#755](https://github.com/cossacklabs/themis/pull/755)).
- Secure Message is now covered with fuzz testing ([#762](https://github.com/cossacklabs/themis/pull/762)).


## [0.13.6](https://github.com/cossacklabs/themis/releases/tag/0.13.6), November 23rd 2020
Expand Down
71 changes: 71 additions & 0 deletions tools/afl/generate/smessage_encrypt_decrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2019 Cossack Labs Limited
ilammy marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"encoding/binary"
"fmt"
"io"
"os"

"github.com/cossacklabs/themis/gothemis/keys"
"github.com/cossacklabs/themis/gothemis/message"
)

func writeByteString(w io.Writer, str []byte) {
binary.Write(w, binary.BigEndian, uint32(len(str)))
w.Write(str)
}

func main() {
args := os.Args

if len(args) != 3 {
fmt.Printf("usage:\n\t%s {EC|RSA} <message>\n", args[0])
os.Exit(1)
}

keyType := -1
switch args[1] {
case "ec", "EC":
keyType = keys.TypeEC
case "rsa", "RSA":
keyType = keys.TypeRSA
default:
fmt.Fprintf(os.Stderr, "unknown key type: %s\nsupported: EC, RSA\n", args[1])
os.Exit(1)
}
keypair, err := keys.New(keyType)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to generate new keypair: %v\n", err)
os.Exit(1)
}

msg := []byte(args[2])

sm := message.New(keypair.Private, keypair.Public)

encrypted, err := sm.Wrap(msg)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to encrypt Secure Message: %v\n", err)
os.Exit(1)
}

writeByteString(os.Stdout, keypair.Private.Value)
writeByteString(os.Stdout, keypair.Public.Value)
writeByteString(os.Stdout, encrypted)
}
71 changes: 71 additions & 0 deletions tools/afl/generate/smessage_sign_verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2019 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"encoding/binary"
"fmt"
"io"
"os"

"github.com/cossacklabs/themis/gothemis/keys"
"github.com/cossacklabs/themis/gothemis/message"
)

func writeByteString(w io.Writer, str []byte) {
binary.Write(w, binary.BigEndian, uint32(len(str)))
w.Write(str)
}

func main() {
args := os.Args

if len(args) != 3 {
fmt.Printf("usage:\n\t%s {EC|RSA} <message>\n", args[0])
os.Exit(1)
}

keyType := -1
switch args[1] {
case "ec", "EC":
keyType = keys.TypeEC
case "rsa", "RSA":
keyType = keys.TypeRSA
default:
fmt.Fprintf(os.Stderr, "unknown key type: %s\nsupported: EC, RSA\n", args[1])
os.Exit(1)
}
keypair, err := keys.New(keyType)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to generate new keypair: %v\n", err)
os.Exit(1)
}

msg := []byte(args[2])

sm := message.New(keypair.Private, keypair.Public)

signed, err := sm.Sign(msg)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to sign Secure Message: %v\n", err)
os.Exit(1)
}

writeByteString(os.Stdout, keypair.Private.Value)
writeByteString(os.Stdout, keypair.Public.Value)
writeByteString(os.Stdout, signed)
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
114 changes: 114 additions & 0 deletions tools/afl/src/smessage_encrypt_decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2019 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <themis/themis.h>

#include "readline.h"

int main(int argc, char** argv)
{
themis_status_t err = THEMIS_SUCCESS;

/*
* Read test data.
*/

if (argc != 2) {
fprintf(stderr, "usage:\n\t%s <input-file>\n", argv[0]);
return 1;
}

FILE* input = fopen(argv[1], "rb");
if (!input) {
fprintf(stderr, "failed to open %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t* private_key_bytes = NULL;
size_t private_key_size = 0;

if (read_line_binary(input, &private_key_bytes, &private_key_size)) {
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t* public_key_bytes = NULL;
size_t public_key_size = 0;

if (read_line_binary(input, &public_key_bytes, &public_key_size)) {
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t* message_bytes = NULL;
size_t message_size = 0;

if (read_line_binary(input, &message_bytes, &message_size)) {
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

fclose(input);

/*
* Try decrypting it.
*/

uint8_t* decrypted_bytes = NULL;
size_t decrypted_size = 0;

err = themis_secure_message_decrypt(private_key_bytes,
private_key_size,
public_key_bytes,
public_key_size,
message_bytes,
message_size,
NULL,
&decrypted_size);

if (err != THEMIS_BUFFER_TOO_SMALL) {
fprintf(stderr, "failed to determine decrypted message size: %d\n", err);
return 2;
}

decrypted_bytes = malloc(decrypted_size);
if (!decrypted_bytes) {
fprintf(stderr, "failed to allocate memory for decrypted message (%zu bytes)\n", decrypted_size);
return 2;
}

err = themis_secure_message_decrypt(private_key_bytes,
private_key_size,
public_key_bytes,
public_key_size,
message_bytes,
message_size,
decrypted_bytes,
&decrypted_size);

if (err != THEMIS_SUCCESS) {
fprintf(stderr, "failed to decrypt message: %d\n", err);
return 2;
}

return 0;
}
Loading